Make mobs stationary

Discussion in 'Plugin Development' started by glasseater, Jan 16, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Antybarrel

    @glasseater Ok good, Do you still have any errors in that class?
     
  2. Offline

    glasseater

  3. Offline

    Antybarrel

  4. Offline

    glasseater

    @Antybarrel
    Code:
    The constructor CustomEntityType(String, int, EntityType, Class<EntityVillager>, Class<CustomEntityVillager>) is undefined
    PS. Thanks for being patient with me :p
     
  5. Offline

    Antybarrel

    @glasseater Yes don't worry about that, that is erroring because the CustomEntityVillager.class isn't working yet. Can you post the errors you are getting in there.

    P.S: No worries :p
     
  6. Offline

    glasseater

    @Antybarrel Sure thing. For goal selector: goalSelector cannot be resolved to a variable

    For target selector: targetSelector cannot be resolved to a variable

    and for: super(world); The constructor Object(World) is undefined
     
  7. Offline

    Antybarrel

    @glasseater This sounds like you have just got the imports wrong. Can you put all the imports you have got in that class here?
     
  8. Offline

    glasseater

    @Antybarrel

    Code:
    import java.lang.reflect.Field;
    
    import org.bukkit.craftbukkit.v1_7_R1.util.UnsafeList;
    import net.minecraft.server.v1_7_R1.EntityVillager;
    import net.minecraft.server.v1_7_R1.PathfinderGoalSelector;
    import net.minecraft.server.v1_7_R1.World;
     
  9. Offline

    Antybarrel

    @glasseater

    Edit: Nevermind can you screenshot your whole class file
     
  10. Offline

    glasseater

  11. Offline

    Antybarrel

    @glasseater Ah,

    Where you have:
    Code:
    public class CustomEntityVillager
    //It needs to be
    public class CustomEntityVillager extends EntityVillager {
     
  12. Offline

    glasseater

    @Antybarrel Got rid of the error, but it doesn't work :/ I am spawning it with a spawn egg
     
  13. Offline

    Antybarrel

    @glasseater Have you registered in your onEnable and deregistered in your onDisbale
    Code:
        public void onEnable(){
      
             CustomEntityType.registerEntities();
           
        }
       
        public void onDisable(){
            CustomEntityType.unregisterEntities();
           
       
        }
    
     
  14. Offline

    glasseater

  15. Offline

    Antybarrel

    @glasseater Strange I just quickly did this and its working for me can you post all your class files?
     
  16. Offline

    glasseater

    @Antybarrel
    Sure thing.

    CustomEntityType:

    Code:
    import java.lang.reflect.Field;
    import java.util.List;
    import java.util.Map;
    
    import net.minecraft.server.v1_7_R1.BiomeBase;
    import net.minecraft.server.v1_7_R1.BiomeMeta;
    import net.minecraft.server.v1_7_R1.EntityInsentient;
    import net.minecraft.server.v1_7_R1.EntityTypes;
    import net.minecraft.server.v1_7_R1.EntityVillager;
    
    import org.bukkit.entity.EntityType;
    public enum CustomEntityType {
        VILLAGER("Villager", 120, EntityType.VILLAGER, EntityVillager.class, CustomEntityVillager.class);
        
    private String name;
    private int id;
    private EntityType entityType;
    private Class<? extends EntityInsentient> nmsClass;
    private Class<? extends EntityInsentient> customClass;
    private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass,
    Class<? extends EntityInsentient> customClass) {
    this.name = name;
    this.id = id;
    this.entityType = entityType;
    this.nmsClass = nmsClass;
    this.customClass = customClass;
    }
    public String getName() {
    return name;
    }
    public int getID() {
    return id;
    }
    public EntityType getEntityType() {
    return entityType;
    }
    public Class<? extends EntityInsentient> getNMSClass() {
    return nmsClass;
    }
    public Class<? extends EntityInsentient> getCustomClass() {
    return customClass;
    }
    /**
    * Register our entities.
    */
    public static void registerEntities() {
    for (CustomEntityType entity : values())
    a(entity.getCustomClass(), entity.getName(), entity.getID());
    // BiomeBase#biomes became private.
    BiomeBase[] biomes;
    try {
    biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    } catch (Exception exc) {
    // Unable to fetch.
    return;
    }
    for (BiomeBase biomeBase : biomes) {
    if (biomeBase == null)
    break;
    // This changed names from J, K, L and M.
    for (String field : new String[] { "as", "at", "au", "av" })
    try {
    Field list = BiomeBase.class.getDeclaredField(field);
    list.setAccessible(true);
    @SuppressWarnings("unchecked")
    List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    // Write in our custom class.
    for (BiomeMeta meta : mobList)
    for (CustomEntityType entity : values())
    if (entity.getNMSClass().equals(meta.b))
    meta.b = entity.getCustomClass();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    /**
    * Unregister our entities to prevent memory leaks. Call on disable.
    */
    public static void unregisterEntities() {
    for (CustomEntityType entity : values()) {
    // Remove our class references.
    try {
    ((Map) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
    } catch (Exception e) {
    e.printStackTrace();
    }
    try {
    ((Map) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    for (CustomEntityType entity : values())
    try {
    // Unregister each entity by writing the NMS back in place of the custom class.
    a(entity.getNMSClass(), entity.getName(), entity.getID());
    } catch (Exception e) {
    e.printStackTrace();
    }
    // Biomes#biomes was made private so use reflection to get it.
    BiomeBase[] biomes;
    try {
    biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    } catch (Exception exc) {
    // Unable to fetch.
    return;
    }
    for (BiomeBase biomeBase : biomes) {
    if (biomeBase == null)
    break;
    // The list fields changed names but update the meta regardless.
    for (String field : new String[] { "as", "at", "au", "av" })
    try {
    Field list = BiomeBase.class.getDeclaredField(field);
    list.setAccessible(true);
    @SuppressWarnings("unchecked")
    List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    // Make sure the NMS class is written back over our custom class.
    for (BiomeMeta meta : mobList)
    for (CustomEntityType entity : values())
    if (entity.getCustomClass().equals(meta.b))
    meta.b = entity.getNMSClass();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    /**
    * A convenience method.
    * @param clazz The class.
    * @param f The string representation of the private static field.
    * @return The object found
    * @throws Exception if unable to get the object.
    */
    private static Object getPrivateStatic(@SuppressWarnings("rawtypes") Class clazz, String f) throws Exception {
    Field field = clazz.getDeclaredField(f);
    field.setAccessible(true);
    return field.get(null);
    }
    /*
    * Since 1.7.2 added a check in their entity registration, simply bypass it and write to the maps ourself.
    */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static void a(Class paramClass, String paramString, int paramInt) {
    try {
    ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    ((Map) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
    ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    } catch (Exception exc) {
    // Unable to register the new class.
    }
    }
    }
    CustomEntityVillager:

    Code:
    import java.lang.reflect.Field;
    
    import org.bukkit.craftbukkit.v1_7_R1.util.UnsafeList;
    import net.minecraft.server.v1_7_R1.EntityVillager;
    import net.minecraft.server.v1_7_R1.PathfinderGoalSelector;
    import net.minecraft.server.v1_7_R1.World;
    
    public class CustomEntityVillager extends EntityVillager {
    
    
    public CustomEntityVillager(World world) {
    super(world);
    
    try {
        Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
        bField.setAccessible(true);
        Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
        cField.setAccessible(true);
        bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
        bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
        cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
        cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
        } catch (Exception exc) {
        exc.printStackTrace();
        }
    
    
    }
    
    }
    GUI:

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    
    public class GUI extends JavaPlugin implements Listener {
       
        public void onEnable(){
              
            CustomEntityType.registerEntities();
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
         
       }
     
       public void onDisable(){
           CustomEntityType.unregisterEntities();
           // Very important make sure you unregister
       }
    
        @EventHandler(priority = EventPriority.NORMAL)
        public void checkDamage(EntityDamageEvent e) {
            if (e.isCancelled() || !e.getEntityType().equals(EntityType.VILLAGER)) return;
            e.setCancelled(true);
        }
    
        public void teleportInWord(Player player, int x, int y, int z) {
            player.teleport(new Location(player.getWorld(), x, y, z));
        }
        public void openGUI(Player player){
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Shop");
            ItemStack soupkit = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta soupkitMeta = pvpkit.getItemMeta();
            ItemStack kit = new ItemStack(Material.ENDER_PEARL);
            ItemMeta kitMeta = archerkit.getItemMeta();
            soupkitMeta.setDisplayName(ChatColor.RED + "Soup Kit");
            soupkit.setItemMeta(pvpkitMeta);
            kitMeta.setDisplayName(ChatColor.YELLOW + "Kit");
          kit.setItemMeta(archerkitMeta);
            inv.setItem(0, soupkit);
            inv.setItem(4, kit);
            player.openInventory(inv);
        }
        @EventHandler
        public void onPlayerInteractEntity(PlayerInteractEntityEvent e){
            if (!e.isCancelled()) { e.setCancelled(true); }
            if(e.getRightClicked().getType() == EntityType.VILLAGER){
                openGUI(e.getPlayer());
          
              
    
               
            }
        }
    }
     
  17. Offline

    Antybarrel

    @glasseater I can't see anything wrong here, I just copied all that code into my server and it works! I'm not sure why it's not working for you. Are you sure you have exported it and restarted the server?
     
  18. Offline

    glasseater

    @Antybarrel Positive, did you get any warnings on yours? What version are you using? Also, are you using a spawn egg to spawn it in?
     
  19. Offline

    Antybarrel

    @glasseater Im using 1.7.9 and a slightly later craftbukkit verison than you, I get no warnings and using a spawn egg yes.
     
  20. Offline

    glasseater

Thread Status:
Not open for further replies.

Share This Page