From 50bf03b82b72866bd54e796a37707d45bcbf3d80 Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 17 Jan 2017 13:07:22 -0800 Subject: [PATCH] Cache entity constructors for a small performance optimization --- jsons/1.11.2.json | 12 +++--- .../minecraft/entity/EntityList.java.patch | 21 +++++++--- .../world/WorldEntitySpawner.java.patch | 18 +++++++++ .../minecraft/world/biome/Biome.java.patch | 37 ++++++++++++++++++ .../fml/common/registry/EntityEntry.java | 38 +++++++++++++++++++ .../minecraftforge/test/CustomSpriteTest.java | 4 +- 6 files changed, 116 insertions(+), 14 deletions(-) diff --git a/jsons/1.11.2.json b/jsons/1.11.2.json index 5ff1eda9e..d77a170c8 100644 --- a/jsons/1.11.2.json +++ b/jsons/1.11.2.json @@ -242,13 +242,13 @@ } }, { - "name": "com.mojang:realms:1.10.8", + "name": "com.mojang:realms:1.10.10", "downloads": { "artifact": { - "size": 3025164, - "sha1": "67560bbd6a83d48ae8b956f1feac09010bde879c", - "path": "com/mojang/realms/1.10.8/realms-1.10.8.jar", - "url": "https://libraries.minecraft.net/com/mojang/realms/1.10.8/realms-1.10.8.jar" + "size": 3031435, + "sha1": "92d9ba93e1e99b75f2776062a5515f6aa88d9f10", + "path": "com/mojang/realms/1.10.10/realms-1.10.10.jar", + "url": "https://libraries.minecraft.net/com/mojang/realms/1.10.10/realms-1.10.10.jar" } } }, @@ -558,6 +558,6 @@ "minecraftArguments": "--username ${auth_player_name} --version ${version_name} --gameDir ${game_directory} --assetsDir ${assets_root} --assetIndex ${assets_index_name} --uuid ${auth_uuid} --accessToken ${auth_access_token} --userType ${user_type} --versionType ${version_type}", "minimumLauncherVersion": 18, "releaseTime": "2016-12-21T09:29:12+00:00", - "time": "2016-12-21T09:42:21+00:00", + "time": "2017-01-12T10:44:50+00:00", "type": "release" } diff --git a/patches/minecraft/net/minecraft/entity/EntityList.java.patch b/patches/minecraft/net/minecraft/entity/EntityList.java.patch index 869bb9f9c..124d3f13d 100644 --- a/patches/minecraft/net/minecraft/entity/EntityList.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityList.java.patch @@ -63,16 +63,25 @@ @Nullable public static Entity func_191304_a(@Nullable Class p_191304_0_, World p_191304_1_) { -@@ -174,7 +186,7 @@ +@@ -168,13 +180,15 @@ + @SideOnly(Side.CLIENT) + public static Entity func_75616_a(int p_75616_0_, World p_75616_1_) + { +- return func_191304_a(func_90035_a(p_75616_0_), p_75616_1_); ++ net.minecraftforge.fml.common.registry.EntityEntry entry = net.minecraftforge.fml.common.registry.GameData.getEntityRegistry().func_148754_a(p_75616_0_); ++ return entry == null ? null : entry.newInstance(p_75616_1_); + } + @Nullable public static Entity func_188429_b(ResourceLocation p_188429_0_, World p_188429_1_) { - return func_191304_a((Class)field_191308_b.func_82594_a(p_188429_0_), p_188429_1_); -+ return func_191304_a(getClass(p_188429_0_), p_188429_1_); ++ net.minecraftforge.fml.common.registry.EntityEntry entry = net.minecraftforge.fml.common.registry.ForgeRegistries.ENTITIES.getValue(p_188429_0_); ++ return entry == null ? null : entry.newInstance(p_188429_1_); } @Nullable -@@ -189,7 +201,17 @@ +@@ -189,7 +203,17 @@ } else { @@ -90,7 +99,7 @@ } return entity; -@@ -197,7 +219,7 @@ +@@ -197,7 +221,7 @@ public static Set func_180124_b() { @@ -99,7 +108,7 @@ } public static boolean func_180123_a(Entity p_180123_0_, ResourceLocation p_180123_1_) -@@ -336,7 +358,7 @@ +@@ -336,7 +360,7 @@ func_191305_a("zombie_horse", 3232308, 9945732); func_191305_a("zombie_pigman", 15373203, 5009705); func_191305_a("zombie_villager", 5651507, 7969893); @@ -108,7 +117,7 @@ } private static void func_191303_a(int p_191303_0_, String p_191303_1_, Class p_191303_2_, String p_191303_3_) -@@ -357,22 +379,19 @@ +@@ -357,22 +381,19 @@ else { ResourceLocation resourcelocation = new ResourceLocation(p_191303_1_); diff --git a/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch b/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch index 525af1f21..fbeb2bff9 100644 --- a/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/WorldEntitySpawner.java.patch @@ -20,6 +20,15 @@ { BlockPos blockpos = func_180621_a(p_77192_1_, chunkpos1.field_77276_a, chunkpos1.field_77275_b); int k1 = blockpos.func_177958_n(); +@@ -136,7 +138,7 @@ + + try + { +- entityliving = (EntityLiving)biome$spawnlistentry.field_76300_b.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_77192_1_}); ++ entityliving = (EntityLiving)biome$spawnlistentry.newInstance(p_77192_1_); + } + catch (Exception exception) + { @@ -146,8 +148,10 @@ entityliving.func_70012_b((double)f, (double)i3, (double)f1, p_77192_1_.field_73012_v.nextFloat() * 360.0F, 0.0F); @@ -52,3 +61,12 @@ { return false; } +@@ -258,7 +263,7 @@ + + try + { +- entityliving = (EntityLiving)biome$spawnlistentry.field_76300_b.getConstructor(new Class[] {World.class}).newInstance(new Object[] {p_77191_0_}); ++ entityliving = (EntityLiving)biome$spawnlistentry.newInstance(p_77191_0_); + } + catch (Exception exception) + { diff --git a/patches/minecraft/net/minecraft/world/biome/Biome.java.patch b/patches/minecraft/net/minecraft/world/biome/Biome.java.patch index b852758c5..0b7a84f4b 100644 --- a/patches/minecraft/net/minecraft/world/biome/Biome.java.patch +++ b/patches/minecraft/net/minecraft/world/biome/Biome.java.patch @@ -154,3 +154,40 @@ public static void func_185358_q() { func_185354_a(0, "ocean", new BiomeOcean((new Biome.BiomeProperties("Ocean")).func_185398_c(-1.0F).func_185400_d(0.1F))); +@@ -549,6 +631,7 @@ + public Class field_76300_b; + public int field_76301_c; + public int field_76299_d; ++ private final java.lang.reflect.Constructor ctr; + + public SpawnListEntry(Class p_i1970_1_, int p_i1970_2_, int p_i1970_3_, int p_i1970_4_) + { +@@ -556,12 +639,28 @@ + this.field_76300_b = p_i1970_1_; + this.field_76301_c = p_i1970_3_; + this.field_76299_d = p_i1970_4_; ++ ++ java.lang.reflect.Constructor tmp = null; ++ try ++ { ++ tmp = p_i1970_1_.getConstructor(World.class); ++ } ++ catch (NoSuchMethodException e) ++ { ++ com.google.common.base.Throwables.propagate(e); ++ } ++ ctr = tmp; + } + + public String toString() + { + return this.field_76300_b.getSimpleName() + "*(" + this.field_76301_c + "-" + this.field_76299_d + "):" + this.field_76292_a; + } ++ ++ public EntityLiving newInstance(World world) throws Exception ++ { ++ return (EntityLiving)ctr.newInstance(world); ++ } + } + + public static enum TempCategory diff --git a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntry.java b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntry.java index 2447cdd49..a454734e2 100644 --- a/src/main/java/net/minecraftforge/fml/common/registry/EntityEntry.java +++ b/src/main/java/net/minecraftforge/fml/common/registry/EntityEntry.java @@ -18,9 +18,15 @@ */ package net.minecraftforge.fml.common.registry; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import com.google.common.base.Throwables; + import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityList.EntityEggInfo; +import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl; public class EntityEntry extends Impl @@ -28,11 +34,30 @@ public class EntityEntry extends Impl private Class cls; private String name; private EntityEggInfo egg; + private Constructor ctr; public EntityEntry(Class cls, String name) { this.cls = cls; this.name = name; + init(); + } + + //Protected method, to make this optional, in case people subclass this to have a better factory. + protected void init() + { + try + { + this.ctr = this.cls.getConstructor(World.class); + } + catch (NoSuchMethodException e) + { + throw new RuntimeException("Invalid class " + this.cls + " no constructor taking " + World.class.getName()); + } + catch (SecurityException e) + { + Throwables.propagate(e); + } } public Class getEntityClass(){ return this.cls; } @@ -45,4 +70,17 @@ public class EntityEntry extends Impl if (this.getRegistryName() != null) EntityList.ENTITY_EGGS.put(this.getRegistryName(), egg); } + + public Entity newInstance(World world) + { + try + { + return (Entity)this.ctr.newInstance(world); + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } + } } diff --git a/src/test/java/net/minecraftforge/test/CustomSpriteTest.java b/src/test/java/net/minecraftforge/test/CustomSpriteTest.java index 29329c390..5841691f2 100644 --- a/src/test/java/net/minecraftforge/test/CustomSpriteTest.java +++ b/src/test/java/net/minecraftforge/test/CustomSpriteTest.java @@ -25,7 +25,7 @@ import java.io.IOException; /** * Test for {@link TextureStitchEvent.Pre}. */ -@Mod(modid = CustomSpriteTest.MOD_ID, name = CustomSpriteTest.NAME, version = "1.0") +@Mod(modid = CustomSpriteTest.MOD_ID, name = CustomSpriteTest.NAME, version = "1.0", clientSideOnly = true) public class CustomSpriteTest { static final String MOD_ID = "custom_sprite_test"; @@ -95,4 +95,4 @@ public class CustomSpriteTest return false; } } -} \ No newline at end of file +}