Riding mob

Discussion in 'Spigot Plugin Development' started by Aurelian6, May 5, 2015.

  1. Hello,

    I have a problem with my custom mob, the bat doesn't spawn.

    RideAbleBat class:
    Code (Text):

    public class RideAbleBat extends EntityBat
    {
    public RideAbleBat(World world, Player player)
    {
       super(world);
       this.rider = player;
       this.setSize(10, 10);
    }

      Player rider = null;

      @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.5F;
        forMot = ((EntityLiving)this.passenger).ba;
       if (forMot <= 0.0F) {
          forMot *= 0.25F;
       }
        sideMot *= 0.75F;
     
       float speed = 0.35F;
     

        f(speed);
       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))
           {
             double jumpHeight = 0.5D;
             this.motY = jumpHeight;
           }
         }
       }
       catch (Exception e)
       {
          e.printStackTrace();
       }
    }
    }
     

    Register class:
    Code (Text):
    public enum Register {
        BAT("CustomBat", 4574852, EntityType.BAT, EntityBat.class, RideAbleBat.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();
               }
           }
       }
       /*
        public static void registerEntities()
          {
            for (Register entity : values()) {
              a(entity.getCustomClass(), entity.getName(), entity.getID());
            }
            try
            {
             BiomeBase[]  biomes = (BiomeBase[])getPrivateStatic(BiomeBase.class, "biomes");
            }
            catch (Exception exc)
            {
              BiomeBase[] biomes;
              return;
            }
            BiomeBase[] biomes;
            BiomeBase[] arrayOfBiomeBase1 = biomes;int k = biomes.length;
            for (k = 0; biomes.length < k; k++)
            {
              BiomeBase biomeBase = arrayOfBiomeBase1[k];
              if (biomeBase == null) {
                break;
              }
              for (String field : new String[] { "as", "at", "au", "av" }) {
                try
                {
                  Field list = BiomeBase.class.getDeclaredField(field);
                  list.setAccessible(true);
             
                  List<BiomeMeta> mobList = (List)list
                    .get(biomeBase);
                  int i2;
                  int i1;
                  for (Iterator localIterator = mobList.iterator(); localIterator.hasNext(); i1 > i2)
                  {
                    BiomeMeta meta = (BiomeMeta)localIterator.next();
                    Register[] arrayOfRideAbleEntityType2;
                    i2 = (arrayOfRideAbleEntityType2 = values()).length;i1 = 0; continue;Register entity = arrayOfRideAbleEntityType2[i1];
                    if (entity.getNMSClass().equals(meta.b)) {
                      meta.b = entity.getCustomClass();
                    }
                    i1++;
                  }
                }
                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.2D;
       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 RideAbleBat(nmsWorld, player), player);
       }
     
    }
    Code for spawning :
    Code (Text):
    Register.rideBat(player);
    Can you help me ?
    Good day.
     
  2. Try using the bat mob id instead 4574852
     
  3. Okay, it's working.
    But i can't control the mob and and he sometimes moves very quickly. ()
    Also, how can the entity dies when there is no passage above?
     
  4. I have added this:

    this.passenger = (Entity) ((CraftPlayer)player).getHandle();

    But when I move it works but everything is reversed, as I make my key to move, it recoils and the speed is very fast, how to solve this?
     
  5. Have you got it already? I've the same problem with sheeps and any other mob...
     
  6. What is the issue? Here's how I make entites ride players:
    Code (Text):
    entity.setPassenger(player);
     
  7. From what I can see, some of those methods are wrong. For example, in 1.8 you set speed with this.j(float speed) and in the latest 1.8 it's this.k(float speed). This could explain why you're getting unexpected outcomes. I'd recommend to refer to @DSH105 's EchoPet https://github.com/DSH105/EchoPet/b...at/nms/v1_8_R3/entity/EntityPet.java#L349-426 since it looks like you're trying to use what he originally wrote a little while back on the Bukkit forums.
     
    • Like Like x 1