Solved How do I use reflection for this?

Discussion in 'Plugin Help/Development/Requests' started by westjet, Mar 27, 2015.

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


    This is my code that I'm trying use:
    ((EntityInsentient) petf).goalSelector.a(4, new PathfinderGoalTempt(running, 1.2D, player.getItemInHand(), false));
    Eclipse is complaining that the PathfinderGoalTempt is undefined.
    Reflection is the answer.
    How would I use it in this case?
    This is the code I am using for reflection, but I don't know where to go after this:
    try {
                            Field f = EntityInsentient.class.getDeclaredField("goalSelector");
                            Object o = f.get(eeee);
                        } catch (SecurityException e) {
                            // Auto-generated catch block
                        } catch (IllegalArgumentException e) {
                            // Auto-generated catch block
                        } catch (NoSuchFieldException e) {
                            // Auto-generated catch block
                        } catch (IllegalAccessException e) {
                            // Auto-generated catch block
  2. Offline


    Reflection is not necessarily the answer- have you tried simply building against CraftBukkit.jar?
  3. Offline


    I include CraftBukkit as a dependence in all my plugins. But I'm not sure how to use it in this case.
  4. Offline


    @westjet You know in the build path put Craftbukkit above bukkit.
    And why don't you create a pathfinder yourself? Just copy the pathfindergoaltempt and edit it yourself if you don't know how to write one yourself.
  5. Offline


    Should be something like

    import net.minecraft.server.v1_7_R3.PathfinderGoalTempt;

    Depending on your version of CraftBukkit.
  6. Offline


    I did import import net.minecraft.server.v1_8_R2.PathfinderGoalTempt;

    This is what i get:
    This is how I try to use it:
    ((EntityInsentient) entity).goalSelector.a(4, new PathfinderGoalTempt(running, 1.2D, player.getItemInHand(), false));
    @mine-care suggested it, and he says it works when you use reflection. That is because the class goalSelector is protected so it is not visible.
    Last edited: Mar 27, 2015
  7. Offline


    Last edited: Mar 27, 2015
  8. Offline


  9. Offline


    Neither the class nor the constructor are private, at least not in the 1.8.3 code I'm looking at. Both public. Probably your signature is wrong- here is what I see:

    public PathfinderGoalTempt(EntityCreature entitycreature, double d0, Item item, boolean flag)

    There is basically no way an NMS class is going to take a BukkitTask as a parameter :)

    Your IDE should be auto-suggesting constructor parameters and such, by the way- another good reason not to use reflection unless you absolutely have to!

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
    Last edited by a moderator: Mar 27, 2015
  10. Offline


    @NathanWolf He just said he changed and still is getting the same error
  11. Offline


    I would really want to get this working! The whole thing started with some piece of code in MC that is not allowing me to rotate mobs while their being ridden.
  12. Offline

    bwfcwalshy Retired Staff

    Moved to Bukkit Alternates.
  13. Offline


    Wow. I was playing around, and I found out that eclipse won't complain about this:
    ((EntityInsentient) eeee).goalSelector.a(4, new PathfinderGoalTempt(((EntityCreature) eeee), 1.2D, Item.getById(5), false));
    The only thing is, how to I make it so that ignores what item the player uses?

    Using the following NMS code:
    ((EntityInsentient) ((CraftSheep) eeee).getHandle()).goalSelector.a(4, new PathfinderGoalTempt(((EntityCreature) ((CraftSheep) eeee).getHandle()), 1.2D, Item.getById(0), false));
    But what really fixed my original probem with entities not rotating while player is riding them is the following NMS code:

    ((CraftSheep) eeee).getHandle().yaw = player.getLocation().getYaw();
    I'm requesting a thread lock.​
    Last edited: Mar 27, 2015
  14. Offline

    mrCookieSlime Retired Staff

    Locked on request.
Thread Status:
Not open for further replies.

Share This Page