-3

I've been using this code to read private fields in Java to later pass them into Maps for bukkit.

Field FieldF = net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("F");
    FieldF.setAccessible(true);
    Object valueF = FieldF.get("valueF");

However, when I do this try or more times I get Errors.

Unhandled exception type NoSuchFieldException 

enter image description here

Once I add throws declaration throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException I get another error: enter image description here

After I add throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException

enter image description here

How can I use the first code without any errors? I can only use it twice before I get the error.

Full 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 net.minecraft.server.v1_9_R2.Entity;

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).
    Field FieldF = net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("F");
    FieldF.setAccessible(true);
    Object valueF = FieldF.get("valueF");
    Field FieldC = net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("D");
    FieldC.setAccessible(true);
    Object valueC = FieldC.get("valueC");
    Field FieldD = net.minecraft.server.v1_9_R2.EntityTypes.class.getDeclaredField("C");
    FieldD.setAccessible(true);
    Object valueD = FieldD.get("valueD");



    ((Map) valueC).put(name, clazz);
    ((Map) valueD).put(clazz, name);
    ((Map) valueF).put(clazz, Integer.valueOf(id));
    // ((Map)getPrivateField("e",
    // net.minecraft.server.v1_7_R4.EntityTypes.class,
    // null)).put(Integer.valueOf(id), clazz);
    // ((Map)getPrivateField("g",
    // net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name,
    // Integer.valueOf(id));
}

} 
ItzBenteThePig
  • 696
  • 1
  • 9
  • 19
Roke
  • 300
  • 1
  • 6
  • 27

2 Answers2

1

First, learn a bit of Java. https://docs.oracle.com/javase/tutorial/essential/exceptions/

Use try/catch blocks instead of throws declarations in addToMaps()

An example:

 Class clazz = null;
 try{
  clazz = Class.forName("YourClass");
 } catch (ClassNotFoundException e){
  e.printStackTrace();
 }
RoccoDev
  • 518
  • 4
  • 14
1

Firstly your reflection usage isn't exactly correct. This is how it should be set up:

// field.get(Object reference) = gets the field value of the given instance
Object ref = MyApplication.getExample();
Field f = Example.class.getField("exampleField");
f.setAccessible(true);
Object val = f.get(ref);

// If the field is static you don't need a reference
Field staticField = Example.class.getField("staticField");
staticField.setAccessible(true);
Object val = staticField.get(null);

As for syntax, you are calling methods that throw exceptions. You will need to handle them with a try-catch. If your constructor throws exceptions you won't be able to create those enum constants.

Unrelated to errors, capitalized variable names aren't smiled upon. Nor are capitalized package names.

Display Name
  • 942
  • 1
  • 10
  • 20