RookieTEC9 RookieTEC9 - 5 months ago 10
Java Question

Can only use snippet code twice before getting errors

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));
}

}

Answer

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();
 }
Comments