Skip to content

Instantly share code, notes, and snippets.

@noobmobile
Created January 7, 2020 17:08
Show Gist options
  • Save noobmobile/330a541d7e88e915731932b2e5d1bee8 to your computer and use it in GitHub Desktop.
Save noobmobile/330a541d7e88e915731932b2e5d1bee8 to your computer and use it in GitHub Desktop.
Métodos para alterar a velocidade e a distância que um mob te enxerga.
package com.dont.theend.utils;
import net.minecraft.server.v1_8_R3.EntityZombie;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.Entity;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Reflections {
public static void setMobFollowRange(Entity entity, double range) {
try {
Object craftLivingEntity = CRAFT_LIVING_ENTITY.cast(entity);
Object entityLiving = GET_HANDLE.invoke(craftLivingEntity);
Object attributeInstance = GET_ATTRIBUTE_INSTANCE.invoke(entityLiving, FOLLOW_RANGE);
SET_VALUE.invoke(attributeInstance, range);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setMobFollowSpeed(Entity entity, double speed) {
try {
Object craftLivingEntity = CRAFT_LIVING_ENTITY.cast(entity);
Object entityLiving = GET_HANDLE.invoke(craftLivingEntity);
Object attributeInstance = GET_ATTRIBUTE_INSTANCE.invoke(entityLiving, SPEED);
SET_VALUE.invoke(attributeInstance, speed);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void disableNoclip(Entity ent){
try {
Object craftLivingEntity = CRAFT_LIVING_ENTITY.cast(ent);
Object entityLiving = GET_HANDLE.invoke(craftLivingEntity);
Object entity = ENTITY.cast(entityLiving);
NO_CLIP.set(entity,false);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Method GET_HANDLE, GET_ATTRIBUTE_INSTANCE, SET_VALUE;
private static Class CRAFT_LIVING_ENTITY, ENTITY;
private static Object FOLLOW_RANGE, SPEED;
private static Field NO_CLIP;
private static String version = Bukkit.getServer().getClass().getPackage().getName().replace('.', ',').split(",")[3];
private static Class getClass(String clazz) throws ClassNotFoundException {
return Class.forName(clazz.replace("#x", version));
}
static {
try {
Class ATTRIBUTES = getClass("net.minecraft.server.#x.GenericAttributes");
ENTITY = getClass("net.minecraft.server.#x.Entity");
NO_CLIP = ENTITY.getField("noclip");
CRAFT_LIVING_ENTITY = getClass("org.bukkit.craftbukkit.#x.entity.CraftLivingEntity");
GET_HANDLE = CRAFT_LIVING_ENTITY.getMethod("getHandle");
GET_ATTRIBUTE_INSTANCE = getClass("net.minecraft.server.#x.EntityLiving").getMethod("getAttributeInstance", getClass("net.minecraft.server.#x.IAttribute"));
FOLLOW_RANGE = ATTRIBUTES.getField("FOLLOW_RANGE").get(null);
SPEED = ATTRIBUTES.getField("MOVEMENT_SPEED").get(null);
SET_VALUE = getClass("net.minecraft.server.#x.AttributeInstance").getMethod("setValue", double.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment