Plugin Help [1.8] Custom Entity Control [Help needed]

Discussion in 'Plugin Help/Development/Requests' started by jasperdekiller, Aug 20, 2015.

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

    jasperdekiller

    Hello,

    For my test server I am trying to make a Plugin, like whenever you type /test a Blaze will spawn and you will be set as a passenger of the Blaze.

    What I have working so far:
    - Adding the CustomEntity
    - Spawning the CustomEntity
    - And set the player as passenger

    I found a peace of code on the intenet to control a Custom Entity by using the WASD Keys on your keyboard.
    But like the Speed of the Entity is too high! And like the controls are not working properly like when I try to go forward (By pressing W) it will go backwards and same for the side movements A is Right and D is Left. And it isn't going up aswell.

    Custom Entity (Blaze):
    Code:
    public class RideAbleBlaze extends EntityBat {
       
        Player rider = null;
       
        public RideAbleBlaze(net.minecraft.server.v1_8_R3.World world, Player player) {
            super(world);
            this.rider = player;
            this.setSize(10, 10);
        }
    
       
        @SuppressWarnings("unused")
       
       
        @Override
        public void g(float sideMot, float forMot)
        {
          if ((this.passenger == null) || (!(this.passenger instanceof EntityHuman)))
          {
            super.g(sideMot, forMot);
            this.S = 0.5F;
            return;
          }
          EntityHuman human = (EntityHuman)this.passenger;
          this.lastYaw = (this.yaw = this.passenger.yaw);
          this.pitch = (this.passenger.pitch * 0.5F);
          this.setYawPitch(this.yaw, this.pitch);
          this.aK = (this.aM = this.yaw);
          this.S = 1.0F;
       
          sideMot = ((EntityLiving)this.passenger).aZ - 0.00001F; //backwards
          forMot = ((EntityLiving)this.passenger).ba;
          if (forMot <= 0.0F) {
              forMot *= 0.25F;    // Make backwards slower
          }
          sideMot *= 0.35F;    // 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.f(speed);    // Apply the speed
          super.e(sideMot, forMot);    // Apply the motion to the entity
         
    
          super.g(sideMot, forMot);
          try
          {
            Field jump = null;
            jump = EntityLiving.class.getDeclaredField("aY");
            jump.setAccessible(true);
            if ((jump != null) && (this.onGround)) {
              if (jump.getBoolean(this.passenger))
              {
                Logger log = Logger.getLogger("minecraft");
                log.info("Nu");
                double jumpHeight = 0.5D;
                this.motY = motY + jumpHeight;
              }
            }
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }
    }
    
    
    Register Class (Register.class):
    Code:
    public enum Register {
        BLAZE("CustomBat", 4574852, EntityType.BLAZE, EntityBat.class, RideAbleBlaze.class);
        private String name;
        private int id;
        private EntityType entityType;
        private Class<? extends EntityInsentient> nmsClass;
        private Class<? extends EntityInsentient> customClass;
        private Register(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 regissterEntities() {
            for (Register 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()); //error here, ID already registered
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        private static Object getPrivateStatic(
                @SuppressWarnings("rawtypes") Class clazz, String f)
                throws Exception {
            Field field = clazz.getDeclaredField(f);
            field.setAccessible(true);
            return field.get(null);
        }
        @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
        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 localException) {
            }
        }
       
        // MAKING
        public static double mountSpeed = 0.04D;
        private static double maxHealth = 2.0D;
        private static void make(EntityLiving nmsEntity, Player player) {
            if (!canSummonMount(player.getLocation())) {
                player.sendMessage("§cVous ne pouvez pas utiliser cette monture ici.");
                return;
            }
            LivingEntity mount = (LivingEntity) nmsEntity.getBukkitEntity();
            // Adulte
            if (mount instanceof EntityAgeable) ((EntityAgeable)mount).setAge(0);
            Location location = player.getLocation();
            World nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
            nmsEntity.setPosition(location.getX(), location.getY() + 0.3, location.getZ());
            nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM);
            // LA VIE
            mount.setMaxHealth(maxHealth);;
            mount.setPassenger(player);
            player.closeInventory();
        }
       
        public static boolean canSummonMount(Location location) {
            return true; // TODO: improve
        }
       
        public static void rideBat(Player player) {
            Location loc = player.getLocation();
            World nmsWorld = ((CraftWorld) loc.getWorld()).getHandle();
            make(new RideAbleBlaze(nmsWorld, player), player);
        }
       
    }
    
    Main Class (Pets.class):
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName().equalsIgnoreCase("test") && sender.isOp()) {
                Player p = (Player) sender;
                //Location loc = p.getLocation();
    //            spawnCustomEntity(loc, p);
                Register.rideBat(p);
               
            }
            return false;
        }
       
        protected static Field mapStringToClassField, mapClassToStringField, mapClassToIdField, mapStringToIdField;
         //protected static Field mapIdToClassField;
         
         static
         {
             try
             {
                 mapStringToClassField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("c");
                 mapClassToStringField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("d");
                 //mapIdtoClassField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("e");
                 mapClassToIdField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("f");
                 mapStringToIdField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("g");
         
                 mapStringToClassField.setAccessible(true);
                 mapClassToStringField.setAccessible(true);
                 //mapIdToClassField.setAccessible(true);
                 mapClassToIdField.setAccessible(true);
                 mapStringToIdField.setAccessible(true);
             }
             catch(Exception e) {e.printStackTrace();}
         }
         
         @SuppressWarnings({ "rawtypes", "unchecked" })
         protected static void addCustomEntity(Class entityClass, String name, int id)
         {
             if (mapStringToClassField == null || mapStringToIdField == null || mapClassToStringField == null || mapClassToIdField == null)
             {
                 return;
             }
             else
             {
                 try
                 {
                     Map mapStringToClass = (Map) mapStringToClassField.get(null);
                     Map mapStringToId = (Map) mapStringToIdField.get(null);
                     Map mapClasstoString = (Map) mapClassToStringField.get(null);
                     Map mapClassToId = (Map) mapClassToIdField.get(null);
         
                     mapStringToClass.put(name, entityClass);
                     mapStringToId.put(name, Integer.valueOf(id));
                     mapClasstoString.put(entityClass, name);
                     mapClassToId.put(entityClass, Integer.valueOf(id));
         
                     mapStringToClassField.set(null, mapStringToClass);
                     mapStringToIdField.set(null, mapStringToId);
                     mapClassToStringField.set(null, mapClasstoString);
                     mapClassToIdField.set(null, mapClassToId);
                 }
                 catch (Exception e)
                 {
                     e.printStackTrace();
                 }
             }
         }
       
        public void onLoad() {
           getLogger().info("Loading Custom Mobs..   ");
           addCustomEntity(RideAbleBlaze.class, "CustomBat", 61);
           //51 is the network id of a sekeleton so if I were to spawn this, it would appear to be a skeleton instead of a zombie
        }
    
    Hopefully someone could help me understand the code and how to solve the problem.
    If you need any more information just ask me!
     
Thread Status:
Not open for further replies.

Share This Page