Solved Please delete

Discussion in 'Spigot Plugin Development' started by sistem21, Jun 12, 2016.

  1. Hi guys, today i was working on a plugin which gives players the ability of ride PolarBears of minecraft 1.10.
    I've worked with nms to make players control the bears, overriding the method g(float f, float f1) of the class EntityLiving.
    The problem is that when i right click a bear, the server crashes.
    Here is the code:
    Code (mainclass (Unknown Language)):
    package me.sistem21.potatobears;

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.Map;

    import net.minecraft.server.v1_10_R1.EntityTypes;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.PolarBear;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class PotatoBears extends JavaPlugin implements Listener {

       public void onEnable() {
         Bukkit.getServer().getPluginManager().registerEvents(this, this);
         registerBear();
       }

       @EventHandler
       public void on(PlayerInteractEntityEvent e) {
         Player p = e.getPlayer();
         if (e.getRightClicked() instanceof PolarBear) {
           PolarBear bear = (PolarBear) e.getRightClicked();
           bear.setPassenger(p);
         }
       }
     
       @SuppressWarnings("unchecked")
       public static void registerBear(){
      try {
      final Field idMapField = EntityTypes.class.getDeclaredField("e");
      idMapField.setAccessible(true);
      Map<Integer, Class<?>> idMap = (Map<Integer, Class<?>>) idMapField.get(null);
      final Field nameMapField = EntityTypes.class.getDeclaredField("c");
      nameMapField.setAccessible(true);
      Map<String, Class<?>> nameMap = (Map<String, Class<?>>) nameMapField.get(null);
      nameMap.remove("PolarBear");
      idMap.remove(102);
      final Method a = EntityTypes.class.getDeclaredMethod("a", Class.class, String.class, Integer.TYPE);
      a.setAccessible(true);
      a.invoke(null, CustomEntityPolarBear.class, "PolarBear", 102);
      } catch (Throwable t) {
      t.printStackTrace();
      }
       }

    }
    Code (custombearclass (Unknown Language)):

    package me.sistem21.potatobears;

    import java.lang.reflect.Field;

    import net.minecraft.server.v1_10_R1.Entity;
    import net.minecraft.server.v1_10_R1.EntityHuman;
    import net.minecraft.server.v1_10_R1.EntityLiving;
    import net.minecraft.server.v1_10_R1.EntityPolarBear;
    import net.minecraft.server.v1_10_R1.GenericAttributes;
    import net.minecraft.server.v1_10_R1.World;

    public class CustomEntityPolarBear extends EntityPolarBear {

       public CustomEntityPolarBear(World world) {
         super(world);
       }

     
    @Override
       public void g(float f, float f1) {
         EntityLiving entityLiving = (EntityLiving) this.bw();
         if (entityLiving == null) {
           for (Entity e : this.passengers) {
             if (e instanceof EntityHuman) {
               entityLiving = (EntityLiving) e;
               break;
             }
           }

           if (entityLiving == null) {
             this.P = 0.5F;
             super.g(f, f1);
             return;
           }
         }

         float yaw = entityLiving.yaw;
         this.yaw = yaw;
         this.lastYaw = yaw;
         this.pitch = entityLiving.pitch * 0.5F;
         this.setYawPitch(this.yaw, this.pitch);
         float yaw2 = this.yaw;
         this.aO = yaw2;
         this.aQ = yaw2;

         f = entityLiving.bf * 0.5F;
         f1 = entityLiving.bg;
         if (f1 <= 0.0F) {
           f1 *= 0.25F;
         }

         this.l(Double.valueOf(this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue()).floatValue());
         this.g(f, f1);

         Field jump = null;
      try {
      jump = EntityLiving.class.getDeclaredField("aW");
      } catch (NoSuchFieldException e1) {
      e1.printStackTrace();
      } catch (SecurityException e1) {
      e1.printStackTrace();
      }
      jump.setAccessible(true);
     
      if (jump != null && this.onGround) {
      try {
      if (jump.getBoolean(entityLiving)) {
      double jumpHeight = 0.5D;
      this.motY = jumpHeight;
      }
      } catch (IllegalAccessException e) {
      e.printStackTrace();
      }
      }
       }
    }

     
    And here is the crash report:
    ---- Minecraft Crash Report ----
    // You're mean.

    Time: 12/06/16 11.41
    Description: Ticking entity

    java.lang.StackOverflowError: Ticking entity
    at java.util.HashMap.hash(Unknown Source)
    at java.util.HashMap.get(Unknown Source)
    at net.minecraft.server.v1_10_R1.AttributeMapBase.a(SourceFile:17)
    at net.minecraft.server.v1_10_R1.AttributeMapServer.e(SourceFile:16)
    at net.minecraft.server.v1_10_R1.AttributeMapServer.a(SourceFile:10)
    at net.minecraft.server.v1_10_R1.EntityLiving.getAttributeInstance(EntityLiving.java:1351)
    at me.sistem21.potatobears.CustomEntityPolarBear.g(CustomEntityPolarBear.java:51)
    at me.sistem21.potatobears.CustomEntityPolarBear.g(CustomEntityPolarBear.java:52)
    at me.sistem21.potatobears.CustomEntityPolarBear.g(CustomEntityPolarBear.java:52)
    [...]

    Entity's Vehicle: ~~ERROR~~ NullPointerException: null
    Stacktrace:
    at net.minecraft.server.v1_10_R1.World.tickEntities(World.java:1434)
    at net.minecraft.server.v1_10_R1.WorldServer.tickEntities(WorldServer.java:632)
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:804)
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399)
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672)
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571)
    at java.lang.Thread.run(Unknown Source)



    This is the line 52:
    Code (Text):
    this.g(f, f1);

    Sorry for my shoddy english, i'm italian ;)
     
    #1 sistem21, Jun 12, 2016
    Last edited: Jun 12, 2016
  2. you really shouldn't do that if you have a problem even if you solve it by yourself you should paste here how you fixed it so you can help people who might have your problem. And also don't change the title the thread won't be deleted
     
    • Like Like x 1
  3. i9hdkill

    i9hdkill Retired Moderator
    Retired Supporter

    Please post your solution, so others can profit from your solution aswell.
     
    • Agree Agree x 2