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
1 changed files with 13 additions and 1 deletions

View File

@ -32,7 +32,19 @@ public enum EntityClassById implements IDatatypeFor<Class<? extends Entity>> {
public Class<? extends Entity> get(IDatatypeContext ctx) throws CommandException {
ResourceLocation id = new ResourceLocation(ctx.getConsumer().getString());
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");
}
return entity;