Skip to content

Instantly share code, notes, and snippets.

@vemacs
Last active January 15, 2023 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vemacs/6cd43a50950796458984 to your computer and use it in GitHub Desktop.
Save vemacs/6cd43a50950796458984 to your computer and use it in GitHub Desktop.
private static String serverVersion;
private static Method getHandle;
private static Method getNBTTag;
private static Class<?> nmsEntityClass;
private static Class<?> nbtTagClass;
private static Method c;
private static Method setInt;
private static Method f;
public static void setAiEnabled(Entity entity, boolean enabled) {
try {
if (serverVersion == null) {
String name = Bukkit.getServer().getClass().getName();
String[] parts = name.split("\\.");
serverVersion = parts[3];
}
if (getHandle == null) {
Class<?> craftEntity = Class.forName("org.bukkit.craftbukkit." + serverVersion + ".entity.CraftEntity");
getHandle = craftEntity.getDeclaredMethod("getHandle");
getHandle.setAccessible(true);
}
Object nmsEntity = getHandle.invoke(entity);
if (nmsEntityClass == null) {
nmsEntityClass = Class.forName("net.minecraft.server." + serverVersion + ".Entity");
}
if (getNBTTag == null) {
getNBTTag = nmsEntityClass.getDeclaredMethod("getNBTTag");
getNBTTag.setAccessible(true);
}
Object tag = getNBTTag.invoke(nmsEntity);
if (nbtTagClass == null) {
nbtTagClass = Class.forName("net.minecraft.server." + serverVersion + ".NBTTagCompound");
}
if (tag == null) {
tag = nbtTagClass.newInstance();
}
if (c == null) {
c = nmsEntityClass.getDeclaredMethod("c", nbtTagClass);
c.setAccessible(true);
}
c.invoke(nmsEntity, tag);
if (setInt == null) {
setInt = nbtTagClass.getDeclaredMethod("setInt", String.class, Integer.TYPE);
setInt.setAccessible(true);
}
int value = enabled ? 0 : 1;
setInt.invoke(tag, "NoAI", value);
if (f == null) {
f = nmsEntityClass.getDeclaredMethod("f", nbtTagClass);
f.setAccessible(true);
}
f.invoke(nmsEntity, tag);
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment