2017-12-30 08:36:36 +01:00
|
|
|
package net.citizensnpcs.nms.v1_10_R1.entity;
|
|
|
|
|
2020-10-18 12:32:33 +02:00
|
|
|
import java.lang.invoke.MethodHandle;
|
2017-12-30 08:36:36 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.util.Map;
|
2021-12-28 17:30:53 +01:00
|
|
|
import java.util.WeakHashMap;
|
2023-02-22 17:06:13 +01:00
|
|
|
|
2017-12-30 08:36:36 +01:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
|
|
|
|
import org.bukkit.entity.Entity;
|
2023-02-22 17:06:13 +01:00
|
|
|
|
2022-02-20 03:04:09 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
2022-07-22 10:40:18 +02:00
|
|
|
import net.citizensnpcs.nms.v1_10_R1.util.NMSImpl;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.citizensnpcs.npc.AbstractEntityController;
|
2022-07-25 17:14:00 +02:00
|
|
|
import net.citizensnpcs.trait.ScoreboardTrait;
|
2020-10-18 12:32:33 +02:00
|
|
|
import net.citizensnpcs.util.NMS;
|
2022-07-22 10:40:18 +02:00
|
|
|
import net.minecraft.server.v1_10_R1.EntityInsentient;
|
2017-12-30 08:36:36 +01:00
|
|
|
import net.minecraft.server.v1_10_R1.World;
|
|
|
|
|
|
|
|
public abstract class MobEntityController extends AbstractEntityController {
|
|
|
|
private final Constructor<?> constructor;
|
|
|
|
|
|
|
|
protected MobEntityController(Class<?> clazz) {
|
|
|
|
super(clazz);
|
|
|
|
this.constructor = getConstructor(clazz);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Entity createEntity(Location at, NPC npc) {
|
|
|
|
net.minecraft.server.v1_10_R1.Entity entity = createEntityFromClass(((CraftWorld) at.getWorld()).getHandle(),
|
|
|
|
npc);
|
2022-07-22 10:40:18 +02:00
|
|
|
if (entity instanceof EntityInsentient) {
|
|
|
|
NMSImpl.clearGoals(((EntityInsentient) entity).goalSelector, ((EntityInsentient) entity).targetSelector);
|
|
|
|
}
|
2023-03-05 17:28:44 +01:00
|
|
|
entity.setPositionRotation(at.getX(), at.getY(), at.getZ(), at.getYaw(), at.getPitch());
|
|
|
|
if (npc != null) {
|
|
|
|
// entity.onGround isn't updated right away - we approximate here so
|
|
|
|
// that things like pathfinding still work *immediately* after spawn.
|
|
|
|
org.bukkit.Material beneath = at.getBlock().getRelative(BlockFace.DOWN).getType();
|
|
|
|
if (beneath.isSolid()) {
|
|
|
|
entity.onGround = true;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
UUID_FIELD.invoke(entity, npc.getUniqueId());
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
|
|
|
|
npc.getOrAddTrait(ScoreboardTrait.class).createTeam(npc.getUniqueId().toString());
|
|
|
|
}
|
2022-02-20 03:04:09 +01:00
|
|
|
}
|
2017-12-30 08:36:36 +01:00
|
|
|
return entity.getBukkitEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
private net.minecraft.server.v1_10_R1.Entity createEntityFromClass(Object... args) {
|
|
|
|
try {
|
|
|
|
return (net.minecraft.server.v1_10_R1.Entity) constructor.newInstance(args);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Constructor<?> getConstructor(Class<?> clazz) {
|
|
|
|
Constructor<?> constructor = CONSTRUCTOR_CACHE.get(clazz);
|
|
|
|
if (constructor != null)
|
|
|
|
return constructor;
|
|
|
|
try {
|
2021-12-28 17:30:53 +01:00
|
|
|
CONSTRUCTOR_CACHE.put(clazz, constructor = clazz.getConstructor(World.class, NPC.class));
|
|
|
|
return constructor;
|
2017-12-30 08:36:36 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new IllegalStateException("unable to find an entity constructor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 17:30:53 +01:00
|
|
|
private static final Map<Class<?>, Constructor<?>> CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
|
2020-10-18 12:32:33 +02:00
|
|
|
private static final MethodHandle UUID_FIELD = NMS.getSetter(net.minecraft.server.v1_10_R1.Entity.class,
|
|
|
|
"uniqueID");
|
2012-02-04 09:13:20 +01:00
|
|
|
}
|