Custom Entity spawning

Discussion in 'Plugin Development' started by Block34, Jul 15, 2014.

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

    Block34

    I've got the following classes for spawning a custom entity zombie(that attacks iron golems in villages). My question is: How do I spawn this custom entity?

    Main Class:
    Code:java
    1. package com.evileye350.mobai;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class MobAI extends JavaPlugin{
    10.  
    11. @Override
    12. public void onEnable(){
    13. getLogger().info("MobAI v1.0 has been activated");
    14. }
    15.  
    16. @Override
    17. public void onDisable(){
    18. getLogger().info("MobAI v1.0 has been disabled");
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    22. if(cmd.getName().equalsIgnoreCase("spawnzombie")){
    23. Player player = (Player) sender;
    24. Location loc = player.getLocation();
    25. CustomZombie z = new CustomZombie(player.getWorld());
    26. EntityTypes.spawnEntity(z, loc);
    27. }
    28. return false;
    29. }
    30.  
    31. }


    EntityTypes enumeration(I plan on adding more custom entities in the future):
    Code:java
    1. package com.evileye350.mobai;
    2.  
    3. import java.util.Map;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
    7.  
    8. import net.minecraft.server.v1_7_R3.Entity;
    9.  
    10. public enum EntityTypes {
    11.  
    12. //NAME("Entity name", Entity ID, yourcustomclass.class);
    13. CUSTOM_ZOMBIE("Zombie", 54, CustomZombie.class);
    14.  
    15. private EntityTypes(String name, int id, Class<? extends Entity> custom){
    16. addToMaps(custom, name, id);
    17. }
    18.  
    19. public static void spawnEntity(Entity entity, Location loc){
    20.  
    21. ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity);
    22. entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
    23.  
    24. }
    25.  
    26. private static void addToMaps(Class clazz, String name, int id){
    27.  
    28. //remove maps with // in front if you dont want to override default entities
    29. //((Map)getPrivateField("c", EntityTypes.class, null)).put(name, clazz);
    30. ((Map)CustomZombie.getPrivateField("d", EntityTypes.class, null)).put(clazz, name);
    31. //((Map)getPrivateField("e", EntityTypes.class, null)).put(Integer.valueOf(id), clazz);
    32. ((Map)CustomZombie.getPrivateField("f", EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
    33. //((Map)getPrivateField("g", EntityTypes.class, null)).put(name, Integer.valueOf(id));
    34. }
    35. }
    36.  


    The Custom Zombie entity class:
    Code:java
    1. package com.evileye350.mobai;
    2.  
    3. import java.util.List;
    4. import java.util.Map;
    5. import java.lang.reflect.Field;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
    9.  
    10. import net.minecraft.server.v1_7_R3.Entity;
    11. import net.minecraft.server.v1_7_R3.EntityHuman;
    12. import net.minecraft.server.v1_7_R3.EntityIronGolem;
    13. import net.minecraft.server.v1_7_R3.EntityVillager;
    14. import net.minecraft.server.v1_7_R3.EntityZombie;
    15. import net.minecraft.server.v1_7_R3.PathfinderGoalFloat;
    16. import net.minecraft.server.v1_7_R3.PathfinderGoalHurtByTarget;
    17. import net.minecraft.server.v1_7_R3.PathfinderGoalLookAtPlayer;
    18. import net.minecraft.server.v1_7_R3.PathfinderGoalMeleeAttack;
    19. import net.minecraft.server.v1_7_R3.PathfinderGoalMoveThroughVillage;
    20. import net.minecraft.server.v1_7_R3.PathfinderGoalMoveTowardsRestriction;
    21. import net.minecraft.server.v1_7_R3.PathfinderGoalNearestAttackableTarget;
    22. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomLookaround;
    23. import net.minecraft.server.v1_7_R3.PathfinderGoalRandomStroll;
    24. import net.minecraft.server.v1_7_R3.PathfinderGoalSelector;
    25.  
    26. public class CustomZombie extends EntityZombie{
    27. public CustomZombie(org.bukkit.World world){
    28. super(((CraftWorld)world).getHandle());
    29. //List is also java.awt unsure and test
    30. List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector);
    31. goalB.clear();
    32. List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector );
    33. goalC.clear();
    34. List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector);
    35. targetB.clear();
    36. List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector);
    37. targetC.clear();
    38.  
    39. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    40. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.0D, false));
    41. this.goalSelector.a(3, new PathfinderGoalMeleeAttack(this, EntityIronGolem.class, 1.0D, false));//custom goal: attack iron golems
    42. this.goalSelector.a(4, new PathfinderGoalMeleeAttack(this, EntityVillager.class, 1.0D, true));
    43. this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 1.0D));
    44. this.goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 1.0D, false));
    45. this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0D));
    46. this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    47. this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    48. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    49. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, true));
    50. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class, 0, false));
    51. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityIronGolem.class, 0, false));
    52. }
    53.  
    54. public static Object getPrivateField(String fieldName,Class clazz, Object object){
    55. Field field;
    56. Object o = null;
    57.  
    58. try{
    59. field = clazz.getDeclaredField(fieldName);
    60.  
    61. field.setAccessible(true);
    62.  
    63. o = field.get(object);
    64. }
    65. e.printStackTrace();
    66. }
    67. e.printStackTrace();
    68. }
    69.  
    70. return o;
    71. }
    72. }
    73.  


    I've looked through a few threads(and tutorial by jacek) but couldn't determine how to spawn it. Is the spawnentity() method in EntityTypes correct? Did I call spawnentity() correctly in the Main class's /spawnzombie command?
     
  2. Offline

    THEREDBARON24

    You use World.spawnEntity(args); to spawn an entity or just World.spawn(). I guess I am not sure what you are trying to accomplish with the other classes. My apologies.
     
  3. Offline

    ResultStatic

    Block34 you have to register it, register ur custom entity class in onEnable, also spawn it using the nms world method addentity,

    @param name, Entityname ex." Zombie"
    @param id, entities id, i think the zombie one is 54, creeper is 50, you can look it up
    @param customClass: your custom class
    Code:
     public static void registerEntity(String name, int id, Class<? extends EntityInsentient> customClass) {
          try {
          List<Map<?, ?>> dataMaps = new ArrayList<Map<?, ?>>();
          for (Field f : EntityTypes.class.getDeclaredFields()) {
          if (f.getType().getSimpleName().equals(Map.class.getSimpleName())) {
          f.setAccessible(true);
          dataMaps.add((Map<?, ?>) f.get(null));
                }
            }
            ((Map<Class<? extends EntityInsentient>, String>) dataMaps.get(1)).put(customClass, name);
            ((Map<Class<? extends EntityInsentient>, Integer>) dataMaps.get(3)).put(customClass, id);
     
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
     
  4. Offline

    Block34

    THEREDBARON24 spawnEntity is the method in EntityTypes that I plan to use to spawn the entity.
    ResultStatic
    So do I just create that method and then in onEnable put registerEntity(args)?
    Also, what do you mean nms world method addEntity, don't you have to create an object/instance of it before spawning it into the world?
     
  5. Offline

    ResultStatic

    Block34 that goes in ur on enable, and you spawn the Entity using this. i use a method that converts Bukkit world to nms world, just ask if u need that, or u can simply cast to craftworld

    Code:
    CustomMonster melee = new CustomMonster(nms.getNMSWorld(l.getWorld()), l, name);
    melee.setLocation(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());// make sure u set the location with yaw and pitch or clients will crash
    nms.getNMSWorld(l.getWorld()).addEntity(melee);
     
Thread Status:
Not open for further replies.

Share This Page