mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-25 12:15:53 +01:00
Further micro-optimisation
This commit is contained in:
parent
b933246a95
commit
85cf761933
@ -49,6 +49,10 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
name = registryName;
|
||||
}
|
||||
|
||||
private CitizensNPC create(EntityType type, UUID uuid, int id, String name) {
|
||||
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC createNPC(EntityType type, String name) {
|
||||
return createNPC(type, UUID.randomUUID(), generateUniqueId(), name);
|
||||
@ -65,7 +69,7 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
public NPC createNPC(EntityType type, UUID uuid, int id, String name) {
|
||||
Preconditions.checkNotNull(name, "name cannot be null");
|
||||
Preconditions.checkNotNull(type, "type cannot be null");
|
||||
CitizensNPC npc = getByType(type, uuid, id, name);
|
||||
CitizensNPC npc = create(type, uuid, id, name);
|
||||
|
||||
if (npc == null)
|
||||
throw new IllegalStateException("Could not create NPC.");
|
||||
@ -148,10 +152,6 @@ public class CitizensNPCRegistry implements NPCRegistry {
|
||||
return npcs.get(id);
|
||||
}
|
||||
|
||||
private CitizensNPC getByType(EntityType type, UUID uuid, int id, String name) {
|
||||
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC getByUniqueId(UUID uuid) {
|
||||
return uniqueNPCs.get(uuid);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.citizensnpcs.npc;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.entity.EntityType;
|
||||
@ -13,11 +14,11 @@ public class EntityControllers {
|
||||
}
|
||||
|
||||
public static EntityController createForType(EntityType type) {
|
||||
Class<? extends EntityController> controllerClass = TYPES.get(type);
|
||||
if (controllerClass == null)
|
||||
Constructor<? extends EntityController> constructor = TYPES.get(type);
|
||||
if (constructor == null)
|
||||
throw new IllegalArgumentException("Unknown EntityType: " + type);
|
||||
try {
|
||||
return controllerClass.newInstance();
|
||||
return constructor.newInstance();
|
||||
} catch (Throwable ex) {
|
||||
Throwables.getRootCause(ex).printStackTrace();
|
||||
return null;
|
||||
@ -25,8 +26,17 @@ public class EntityControllers {
|
||||
}
|
||||
|
||||
public static void setEntityControllerForType(EntityType type, Class<? extends EntityController> controller) {
|
||||
TYPES.put(type, controller);
|
||||
try {
|
||||
Constructor<? extends EntityController> constructor = controller.getConstructor();
|
||||
constructor.setAccessible(true);
|
||||
TYPES.put(type, constructor);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static final Map<EntityType, Class<? extends EntityController>> TYPES = Maps.newEnumMap(EntityType.class);
|
||||
private static final Map<EntityType, Constructor<? extends EntityController>> TYPES = Maps
|
||||
.newEnumMap(EntityType.class);
|
||||
}
|
||||
|
@ -19,11 +19,9 @@ import net.minecraft.server.v1_14_R1.World;
|
||||
|
||||
public abstract class MobEntityController extends AbstractEntityController {
|
||||
private final Class<?> clazz;
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
protected MobEntityController(Class<?> clazz) {
|
||||
super(clazz);
|
||||
this.constructor = getConstructor(clazz);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@ -50,7 +48,7 @@ public abstract class MobEntityController extends AbstractEntityController {
|
||||
|
||||
private net.minecraft.server.v1_14_R1.Entity createEntityFromClass(Object... args) {
|
||||
try {
|
||||
return (net.minecraft.server.v1_14_R1.Entity) constructor.newInstance(args);
|
||||
return (net.minecraft.server.v1_14_R1.Entity) getConstructor(clazz).newInstance(args);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
@ -19,11 +19,9 @@ import net.minecraft.server.v1_15_R1.World;
|
||||
|
||||
public abstract class MobEntityController extends AbstractEntityController {
|
||||
private final Class<?> clazz;
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
protected MobEntityController(Class<?> clazz) {
|
||||
super(clazz);
|
||||
this.constructor = getConstructor(clazz);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@ -50,7 +48,7 @@ public abstract class MobEntityController extends AbstractEntityController {
|
||||
|
||||
private net.minecraft.server.v1_15_R1.Entity createEntityFromClass(Object... args) {
|
||||
try {
|
||||
return (net.minecraft.server.v1_15_R1.Entity) constructor.newInstance(args);
|
||||
return (net.minecraft.server.v1_15_R1.Entity) getConstructor(clazz).newInstance(args);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
@ -19,11 +19,9 @@ import net.minecraft.server.v1_16_R3.World;
|
||||
|
||||
public abstract class MobEntityController extends AbstractEntityController {
|
||||
private final Class<?> clazz;
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
protected MobEntityController(Class<?> clazz) {
|
||||
super(clazz);
|
||||
this.constructor = getConstructor(clazz);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@ -50,7 +48,7 @@ public abstract class MobEntityController extends AbstractEntityController {
|
||||
|
||||
private net.minecraft.server.v1_16_R3.Entity createEntityFromClass(Object... args) {
|
||||
try {
|
||||
return (net.minecraft.server.v1_16_R3.Entity) constructor.newInstance(args);
|
||||
return (net.minecraft.server.v1_16_R3.Entity) getConstructor(clazz).newInstance(args);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
@ -17,11 +17,9 @@ import net.minecraft.world.level.Level;
|
||||
|
||||
public abstract class MobEntityController extends AbstractEntityController {
|
||||
private final Class<?> clazz;
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
protected MobEntityController(Class<?> clazz) {
|
||||
super(clazz);
|
||||
this.constructor = getConstructor(clazz);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@ -44,7 +42,7 @@ public abstract class MobEntityController extends AbstractEntityController {
|
||||
|
||||
private net.minecraft.world.entity.Entity createEntityFromClass(Object... args) {
|
||||
try {
|
||||
return (net.minecraft.world.entity.Entity) constructor.newInstance(args);
|
||||
return (net.minecraft.world.entity.Entity) getConstructor(clazz).newInstance(args);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
@ -17,11 +17,9 @@ import net.minecraft.world.level.Level;
|
||||
|
||||
public abstract class MobEntityController extends AbstractEntityController {
|
||||
private final Class<?> clazz;
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
protected MobEntityController(Class<?> clazz) {
|
||||
super(clazz);
|
||||
this.constructor = getConstructor(clazz);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@ -44,7 +42,7 @@ public abstract class MobEntityController extends AbstractEntityController {
|
||||
|
||||
private net.minecraft.world.entity.Entity createEntityFromClass(Object... args) {
|
||||
try {
|
||||
return (net.minecraft.world.entity.Entity) constructor.newInstance(args);
|
||||
return (net.minecraft.world.entity.Entity) getConstructor(clazz).newInstance(args);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user