Need Help with Custom Entity

Discussion in 'Plugin Development' started by Saith1998, Sep 4, 2015.

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

    Saith1998

    Hi guys I have an some issues with Custom Entities. For some reason this code doesnt work. I would really appreciate if you could help me.
    Spawning the customEntiy and set a Player as Passenger
    Code:
    @EventHandler
    public void onInventoryInteract(InventoryClickEvent e) throws IOException{
        if(e.getInventory() != null){
            if(e.getCurrentItem() != null){
               
                    if(e.getInventory().getName()== "§4Dein Drache"){
                        e.setCancelled(true);
                        if(e.getCurrentItem().getType() == Material.NAME_TAG){
    return;
                    }else if(e.getCurrentItem().getType() == Material.SADDLE){
                        Player p = (Player) e.getWhoClicked();
                        p.sendMessage(m.prefix+"§aDu reitest deinen Drachen");
                        for(Entity ent : p.getNearbyEntities(5, 5, 5)){
                            if(Zähmer.create(p).ishisDrache(ent)){
                                ent.remove();
                                m.drachen.remove(ent.getUniqueId().toString());
                            }
                        }
                       
                          Location location = p.getLocation().add(0, 5, 0);
                          EntityPlayer cp = ((CraftPlayer) p).getHandle();
                        
                            World world = location.getWorld();
    
                            net.minecraft.server.v1_7_R4.World mcWorld = ((CraftWorld) world).getHandle();
                         Drache drache = new Drache(mcWorld);
                         drache.setPosition(location.getX(), location.getY(), location.getZ());
    cp.setPassengerOf(drache);
                         mcWorld.addEntity(drache, SpawnReason.CUSTOM);
                        
                        m.drachen.add(drache.getUniqueID().toString());
                    File    f = new File("plugins/Dragonwar/Spieler/"+p.getUniqueId()+".yml");
                        YamlConfiguration cfg = YamlConfiguration.loadConfiguration(f);
                        cfg.set("Drache.UUID", drache.getUniqueID().toString());
                        cfg.save(f);
                        e.getWhoClicked().getOpenInventory().close();
                    }
       
       
       
                }
           
            }
        }
        }
    CustomEntiy class
    Code:
    package me.saith.dragonwar.Entity;
    
    import java.lang.reflect.Method;
    
    import net.minecraft.server.v1_7_R4.BiomeBase;
    import net.minecraft.server.v1_7_R4.EntityEnderDragon;
    import net.minecraft.server.v1_7_R4.EntityInsentient;
    import net.minecraft.server.v1_7_R4.EntityTypes;
    
    import org.bukkit.entity.EntityType;
    
    public enum CustomEntityType {
        ENDERDRAGON("Enderdrache", 510000, EntityType.ENDER_DRAGON, EntityEnderDragon.class, Drache.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 this.name;
        }
        public int getID(){
            return this.id;
        }
        public EntityType getEntityType(){
            return this.entityType;
        }
        public Class<? extends EntityInsentient> getNMSClass(){
            return this.nmsClass;
        }
        public Class<? extends EntityInsentient> getCustomClass(){
            return this.customClass;
       
    }
        public static void registerEntities(){
            for (CustomEntityType entity : values()){
                try{
                    Method a = EntityTypes.class.getDeclaredMethod("a", new Class<?>[]{Class.class, String.class, int.class});
                    a.setAccessible(true);
                    a.invoke(null, entity.getCustomClass(), entity.getName(), entity.getID());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        
            for (BiomeBase biomeBase : BiomeBase.getBiomes()){
                if (biomeBase == null){
                    break;
                }
            
            }
        }
    }
    
    2. Entity Class
    Code:
    package me.saith.dragonwar.Entity;
    
    import net.minecraft.server.v1_7_R4.EntityEnderDragon;
    import net.minecraft.server.v1_7_R4.EntityHuman;
    import net.minecraft.server.v1_7_R4.EntityLiving;
    import net.minecraft.server.v1_7_R4.World;
    
    public class Drache extends EntityEnderDragon{
    
        public Drache(World world) {
            super(world);
        }
         @Override
         public void e(float sideMot, float forMot) {
                if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
                    super.e(sideMot, forMot);
                    this.W = 0.5F;    // Make sure the entity can walk over half slabs, instead of jumping
                    return;
                }
            
              
            
                this.lastYaw = this.yaw = this.passenger.yaw;
                this.pitch = this.passenger.pitch * 0.5F;
            
                // Set the entity's pitch, yaw, head rotation etc.
                this.b(this.yaw, this.pitch); //[url]https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/Entity.java#L163-L166[/url]
                this.aO = this.aM = this.yaw;
            
                this.W = 1.0F;    // The custom entity will now automatically climb up 1 high blocks
            
                sideMot = ((EntityLiving) this.passenger).bd * 0.5F;
                forMot = ((EntityLiving) this.passenger).be;
            
                if (forMot <= 0.0F) {
                    forMot *= 0.25F;    // Make backwards slower
                }
                sideMot *= 0.75F;    // Also make sideways slower
            
                float speed = 0.35F;    // 0.2 is the default entity speed. I made it slightly faster so that riding is better than walking
                this.i(speed);    // Apply the speed
                super.e(sideMot, forMot);    // Apply the motion to the entity
            
            }
            
    }
    
     
  2. Offline

    CoolDude53

    Are you listening for a spawn event anywhere in your other code?
     
  3. Offline

    Saith1998

  4. Offline

    ShadowLAX

    @Saith1998 What does not work? Does the entity not function properly? Does it not spawn in when you try to spawn it?
     
  5. Offline

    Saith1998

    @ShadowLAX it kind of spawns but noone can see it. It just attacks. And if I try to sit on it nothing happens :/
     
  6. @Saith1998
    If you use the method "a" from the EntityTypes class and there is already an entity with the info, errors are thrown. You need to manually put the info in the private static maps:
    c: String -> Entity class
    d: Entity class -> String
    e: Entity id (int) -> Entity class
    f: Entity class -> Entity id (int)
     
  7. Offline

    Saith1998

    @megamichiel Thx I already asked myself why there was a big error when I restarted the server 2 Times ^^
     
  8. @Saith1998
    Also, just a note,
    Code:
    if(e.getInventory().getName()=="§4Dein Drache"){
    Will never return true. You need to use String#equals(Object) to make it work properly.
     
  9. Offline

    ShadowLAX

    @Saith1998 You can't see the entity because you register your entity with an unknown Entity ID, more specifically "510000". The 'f' field in EntityTypes maps your class to an Integer (an Entity ID), which is used to determine how the Entity will be displayed. Change it an Ender dragon's EID (63) and it should work.
     
    CoolDude53 likes this.
  10. Offline

    Saith1998

  11. @Saith1998
    I think it might be due to EnderDragon entities working slightly different and maybe the method not being called or the dragon moving differently. If this is the case, or if it's not, idk how to fix it :/
     
  12. Offline

    ShadowLAX

    @Saith1998 You're overriding the wrong method. In 1.8, entity movement is controlled by the method 'g', not 'e'.

    EDIT: Noticed that you're in 1.7, you need to make sure that you are using the correct version of code for what server you are running since you haven't specified.
     
Thread Status:
Not open for further replies.

Share This Page