Minecraft Crashes (Custom Entity)

Discussion in 'Plugin Development' started by Chintzi, Jul 14, 2016.

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

    Chintzi

    So i have literally been looking around on google the whole day and i looked at every Custom entities creation tutorial and they don't seem to work for me they all are outdated and theres always something that needs to be changed but im not sure what to change, anyways i found someones code they posted on github and it crashes my server and minecraft when i attept to spawn it:


    AncientGladiator.java (Custom entity class) (open)

    Code:
    package craftextra.entities;
    
    import net.minecraft.server.v1_10_R1.EntitySkeleton;
    import net.minecraft.server.v1_10_R1.GenericAttributes;
    import net.minecraft.server.v1_10_R1.World;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
    import org.bukkit.craftbukkit.v1_10_R1.entity.CraftLivingEntity;
    import org.bukkit.craftbukkit.v1_10_R1.entity.CraftSkeleton;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    
    public class AncientGladiator extends EntitySkeleton {
    
        public AncientGladiator(World world) {
            super(world);
        }
    
        protected void initAttributes() {
            super.initAttributes();
    
            this.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).setValue(10.0D);
            this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(0.3D);
            this.getAttributeInstance(GenericAttributes.maxHealth).setValue(200.0D);
        }
    
        public static Skeleton spawn(Location location) {
            World mcWorld = (World) ((CraftWorld) location.getWorld()).getHandle();
            final AncientGladiator customEntity = new AncientGladiator(mcWorld);
            customEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
            ((CraftLivingEntity) customEntity.getBukkitEntity()).setRemoveWhenFarAway(false);
            mcWorld.addEntity(customEntity, SpawnReason.CUSTOM);
            return (CraftSkeleton) customEntity.getBukkitEntity();
    
        }
    }



    NMSUtils.java (open)

    Code:
    package craftextra.entities;
    
    import net.minecraft.server.v1_10_R1.EntityInsentient;
    import net.minecraft.server.v1_10_R1.EntityTypes;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    public class NMSUtils {
    
        public void registerEntity(String name, int id, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass) {
            try {
    
                /*
                * First, we make a list of all HashMap's in the EntityTypes class
                * by looping through all fields. I am using reflection here so we
                * have no problems later when minecraft changes the field's name.
                * By creating a list of these maps we can easily modify them later
                * on.
                 */
                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));
                    }
                }
    
                /*
                * since minecraft checks if an id has already been registered, we
                * have to remove the old entity class before we can register our
                * custom one
                *
                * map 0 is the map with names and map 2 is the map with ids
                 */
                if (dataMaps.get(2).containsKey(id)) {
                    dataMaps.get(0).remove(name);
                    dataMaps.get(2).remove(id);
                }
    
                /*
                * now we call the method which adds the entity to the lists in the
                * EntityTypes class, now we are actually 'registering' our entity
                 */
                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();
            }
        }
    
    }
    



    Spawn method (open)
    Code:
    Skeleton ancientGladiator = AncientGladiator.spawn(player.getLocation());




    Registering it (open)
    Code:
    new NMSUtils().registerEntity("AncientGladiator", 65, EntityBat.class, AncientGladiator.class);
     
  2. Offline

    Lordloss

    It crashes because you use the entity ID of Bats, but your custom class extends Skeleton.
     
Thread Status:
Not open for further replies.

Share This Page