Implement a surprisingly missed optimisation

This commit is contained in:
fullwall 2021-12-29 00:30:53 +08:00
parent 19ee0e2c19
commit b933246a95
10 changed files with 40 additions and 45 deletions

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_10_R1.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.util.NMS;
@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_10_R1.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_11_R1.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.util.NMS;
@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_11_R1.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_12_R1.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.util.NMS;
@ -59,13 +58,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_12_R1.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_13_R2.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.util.NMS;
@ -58,14 +57,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_13_R2.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_14_R1.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
@ -63,14 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(EntityTypes.class, World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_14_R1.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_15_R1.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_15_R1.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
@ -63,14 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(EntityTypes.class, World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_15_R1.Entity.class,
"uniqueID");
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_16_R3.entity;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_16_R3.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
@ -63,13 +62,14 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(EntityTypes.class, World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityTypes.class, World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_16_R3.Entity.class,
"uniqueID");
}

View File

@ -2,14 +2,13 @@ package net.citizensnpcs.nms.v1_17_R1.entity;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_17_R1.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
@ -57,11 +56,12 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(EntityType.class, Level.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityType.class, Level.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
}

View File

@ -2,14 +2,13 @@ package net.citizensnpcs.nms.v1_18_R1.entity;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_18_R1.util.NMSImpl;
import net.citizensnpcs.npc.AbstractEntityController;
@ -57,11 +56,12 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(EntityType.class, Level.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(EntityType.class, Level.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
}

View File

@ -3,14 +3,13 @@ package net.citizensnpcs.nms.v1_8_R3.entity;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.entity.Entity;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.util.NMS;
@ -58,12 +57,13 @@ public abstract class MobEntityController extends AbstractEntityController {
if (constructor != null)
return constructor;
try {
return clazz.getConstructor(World.class, NPC.class);
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
return constructor;
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
}
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = Maps.newHashMap();
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static final Field UUID_FIELD = NMS.getField(net.minecraft.server.v1_8_R3.Entity.class, "uniqueID");
}