Merge pull request #1971 from krzys-h/fix-entity-follow-forge

Fix "#entity follow <name>" under Forge
This commit is contained in:
Leijurv 2020-10-23 18:54:11 -07:00 committed by GitHub
commit 6d51e10090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,7 +32,19 @@ public enum EntityClassById implements IDatatypeFor<Class<? extends Entity>> {
public Class<? extends Entity> get(IDatatypeContext ctx) throws CommandException { public Class<? extends Entity> get(IDatatypeContext ctx) throws CommandException {
ResourceLocation id = new ResourceLocation(ctx.getConsumer().getString()); ResourceLocation id = new ResourceLocation(ctx.getConsumer().getString());
Class<? extends Entity> entity; Class<? extends Entity> entity;
if ((entity = EntityList.REGISTRY.getObject(id)) == null) { try {
entity = EntityList.REGISTRY.getObject(id);
} catch(NoSuchFieldError e) {
// Forge removes EntityList.REGISTRY field and provides the getClass method as a replacement
// See https://github.com/MinecraftForge/MinecraftForge/blob/1.12.x/patches/minecraft/net/minecraft/entity/EntityList.java.patch
try {
entity = (Class<? extends Entity>) EntityList.class.getMethod("getClass", ResourceLocation.class).invoke(null, id);
} catch (Exception ex) {
throw new RuntimeException("EntityList.REGISTRY does not exist and failed to call the Forge-replacement method", ex);
}
}
if (entity == null) {
throw new IllegalArgumentException("no entity found by that id"); throw new IllegalArgumentException("no entity found by that id");
} }
return entity; return entity;