Skip to content

Instantly share code, notes, and snippets.

@GrimEpp
Created April 5, 2020 13:20
Show Gist options
  • Select an option

  • Save GrimEpp/acf305bb4eeccb12f0e23da413499b0d to your computer and use it in GitHub Desktop.

Select an option

Save GrimEpp/acf305bb4eeccb12f0e23da413499b0d to your computer and use it in GitHub Desktop.
package net.bylivet.kitpvp.utils;
import net.minecraft.server.v1_8_R3.*;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_8_R3.util.UnsafeList;
import org.bukkit.entity.Villager;
import org.bukkit.event.entity.CreatureSpawnEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class NPC extends EntityVillager {
public NPC(World server) {
super(server);
try {
setField(goalSelector, PathfinderGoalSelector.class, new UnsafeList<PathfinderGoalSelector>(), "b");
setField(targetSelector, PathfinderGoalSelector.class, new UnsafeList<PathfinderGoalSelector>(), "b");
setField(goalSelector, PathfinderGoalSelector.class, new UnsafeList<PathfinderGoalSelector>(), "c");
setField(targetSelector, PathfinderGoalSelector.class, new UnsafeList<PathfinderGoalSelector>(), "c");
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
this.goalSelector.a(0, new PathfinderGoalFloat(this));
this.goalSelector.a(1, new PathfinderGoalLookAtTradingPlayer(this));
this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 0.6D));
this.goalSelector.a(9, new PathfinderGoalInteract(this, EntityHuman.class, 3.0F, 1.0F));
this.goalSelector.a(9, new PathfinderGoalInteract(this, EntityVillager.class, 5.0F, 0.02F));
this.goalSelector.a(9, new PathfinderGoalRandomStroll(this, 0.6D));
this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
}
private static void setField(Object object, Class clas, Object value, String fieldValue) throws IllegalAccessException, NoSuchFieldException {
Field field = clas.getDeclaredField(fieldValue);
field.setAccessible(true);
field.set(object, value);
}
@Override
public void move(double d0, double d1, double d2) {
}
@Override
public void collide(Entity entity) {
}
@Override
public boolean damageEntity(DamageSource damagesource, float f) {
return false;
}
@Override
public void g(double d0, double d1, double d2) {
}
public void registerEntity(String name, int id, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass){
try {
List<Map<?, ?>> dataMap = new ArrayList<>();
for (Field f : EntityTypes.class.getDeclaredFields()){
if (f.getType().getSimpleName().equals(Map.class.getSimpleName())){
f.setAccessible(true);
dataMap.add((Map<?, ?>) f.get(null));
}
}
if (dataMap.get(2).containsKey(id)){
dataMap.get(0).remove(name);
dataMap.get(2).remove(id);
}
Method method = EntityTypes.class.getDeclaredMethod("a", Class.class, String.class, int.class);
method.setAccessible(true);
method.invoke(null, customClass, name, id);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Location loc;
public static Villager villager;
public static void spawn(){
if (loc == null)
return;
World mcWorld = ((CraftWorld) loc.getWorld()).getHandle();
final NPC customEnt = new NPC(mcWorld);
customEnt.registerEntity("Villager", 120, EntityVillager.class, NPC.class);
customEnt.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
((CraftLivingEntity) customEnt.getBukkitEntity()).setRemoveWhenFarAway(false);
mcWorld.addEntity(customEnt, CreatureSpawnEvent.SpawnReason.CUSTOM);
villager = (Villager) customEnt.getBukkitEntity();
villager.setCustomName(ChatColor.GREEN + "Velg kit");
}
public static void setloc(Location location) {
loc = location;
spawn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment