Solved Casting Villager to CraftVillager, but in reflection.

Discussion in 'Spigot Plugin Development' started by TheMasteredPanda, Apr 13, 2017.

  1. Working out how to do this in reflection:

    Code (Text):
     net.minecraft.server.v1_11_R1.EntityVillager villager1 = ((CraftVillager) shopKeeper).getHandle(); // Get our villager
            NBTTagCompound tag = new NBTTagCompound(); // Create our tag
            villager1.c(tag); //Write our tag
            tag.set("PersistenceRequired", new NBTTagByte((byte) 1)); // Set our tag
            villager1.a(tag); // Add it to villager :)
    This should stop the entity from despawning.

    I've gotten this far and I've failed multiple times:

    Code (Text):

    Object villager = ReflectionUtils.callMethod(ReflectionUtils.getDeclaredMethod((Class<?>) ReflectionUtils.getOBClass("CraftVillager").cast(shopKeeper.getClass()), "getHandle",null), null);

    try {
        Object tag = ReflectionUtils.callConstructor(ReflectionUtils.getNMSClass("NBTTagCompound").getConstructor(), null);
        ReflectionUtils.callMethod(ReflectionUtils.getDeclaredMethod(ReflectionUtils.getNMSClass("NBTTagCompound"), "c", ReflectionUtils.getNMSClass("NBTBase")), null, ReflectionUtils.getNMSClass("NBTBase"), villager, tag);
        Object nbtTagByte = ReflectionUtils.callConstructor(ReflectionUtils.getConstructor(ReflectionUtils.getNMSClass("NBTTagByte"), byte.class), (byte) 1);
        Map<String, Object> nbtSettings = Maps.newHashMap();
        nbtSettings.put("PersistenceRequired", nbtTagByte);
        ReflectionUtils.callMethod(ReflectionUtils.getDeclaredMethod(ReflectionUtils.getNMSClass("NBTTagCompound"), "set", ReflectionUtils.getNMSClass("NBTBase")), tag, nbtSettings);
        ReflectionUtils.callMethod(ReflectionUtils.getDeclaredMethod(ReflectionUtils.getNMSClass("NBTTagCompound"), "a", ReflectionUtils.getNMSClass("NBTBase")), villager, tag);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
     
    ReflectionUtils Referance

    At which point have I screwed up and is there an easier way of ensuring an entity will not despawn?

    Ty,
    Panda.
     
    • Agree Agree x 1