How to get Private Field? [Java 8]

Discussion in 'Plugin Development' started by TheEnderCrafter9, Jun 22, 2016.

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

    MCMastery

  2. How do I do this multiple times [3] in one class without getting Unhandled exception type NoSuchFieldException and Unhandled exception type IllegalAccessException?

    It works until I attempt to use it the third time;

    Eclipse higlights (per line)
    Code:
    net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("strD"); //or strF or strC
    Object valueD = fieldD.get(D); // Or Object valueC = fieldC.get(C); or Object valueF = fieldF.get(F);
    
    Code:
    package io.github.rookietec9.EnderPlugin.Entities;
    
    import java.lang.reflect.Field;
    import java.util.Map;
    
    import org.bukkit.Location;
    import org.bukkit.World;
    
    import io.github.rookietec9.EnderPlugin.Entities.Others.OtherC;
    import io.github.rookietec9.EnderPlugin.Entities.Others.OtherD;
    import io.github.rookietec9.EnderPlugin.Entities.Others.OtherF;
    import net.minecraft.server.v1_9_R2.Entity;
    import net.minecraft.server.v1_9_R2.EntityTypes;
    class Others {
    
        class OtherC
    {
        public void setStrC(String valueC)
        {
        }
    }
    
    class OtherD
    {
        public void setStrD(String valueD)
        {
        }
    }
    
    class OtherF
    {
        public void setStrF(String valueF)
        {
        }
    }
    class Other
    {
        public void setStr(String value)
        {
        }
    }
    
    }
    
    public enum CustomBase {
    
        // NAME("Entity name", Entity ID, yourcustomclass.class)
    
        CUSTOM_SKELETON("Skeleton", 54, CustomSkeleton.class); // You can add as
                                                                // many as you want.
    
        private CustomBase(String name, int id, Class<? extends Entity> custom) {
            addToMaps(custom, name, id);
        }
    
        public static void spawnEntity(Entity entity, Location loc) {
            Location Loc = new Location((World) entity.getWorld(), loc.getX(),loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            spawnEntity(entity, Loc);
        }
    
        @SuppressWarnings({ "rawtypes", "unchecked"})
        private static void addToMaps(Class clazz, String name, int id) {
            // Remove the lines with // in front of them if you want to override
            // default entities (You'd have to remove the default entity from the
            // map first though).
                //START C
                    // /*
                    Other C = new OtherC();
                    C.setStrC("c");
                    Field fieldC =  net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("StrC");
                    fieldC.setAccessible(true);
                    Object valueC = fieldC.get(C);
                    // */
                //END C
                //START D
                    // /*
                    Other D = new OtherF();
                    D.setStrF("d");
                    Field fieldD =  net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("strD");
                    fieldD.setAccessible(true);
                    Object valueD = fieldD.get(D);
                    // */
                //END D
                //START F
               
                    OtherF F = new OtherF();
                    F.setStrF("f");
                    Field fieldF =  net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("strF");
                    fieldF.setAccessible(true);
                    Object valueF = fieldF.get(F);
                    // */
                //END F
            ((Map) valueC).put(name, clazz);
            ((Map) valueD).put(clazz, name);
            // ((Map)getPrivateField("e",
            // net.minecraft.server.v1_7_R4.EntityTypes.class,
            // null)).put(Integer.valueOf(id), clazz);
            // ((Map) valueF).put(clazz, Integer.valueOf(id));
            // ((Map)getPrivateField("g",
            // net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name,
            // Integer.valueOf(id));
        }
    
        }
     
    Last edited by a moderator: Jun 22, 2016
Thread Status:
Not open for further replies.

Share This Page