在出现错误之前只能使用代码片段两次

Can only use snippet code twice before getting errors

我一直在使用此代码读取 Java 中的私有字段,以便稍后将它们传递到 Maps for bukkit。

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

但是,当我尝试或多次执行此操作时,出现错误。

Unhandled exception type NoSuchFieldException 

添加 throws 声明后 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException 我收到另一个错误:

在我添加 throws 之后 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException

如何使用第一个代码而不会出现任何错误?我只能用两次才报错。

完整代码:

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

} 

首先,学习一点Java。 https://docs.oracle.com/javase/tutorial/essential/exceptions/

在 addToMaps()

中使用 try/catch 块而不是抛出声明

一个例子:

 Class clazz = null;
 try{
  clazz = Class.forName("YourClass");
 } catch (ClassNotFoundException e){
  e.printStackTrace();
 }

首先,您的反射用法不完全正确。应该这样设置:

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

至于语法,你正在调用抛出异常的方法。您将需要使用 try-catch 来处理它们。如果您的构造函数抛出异常,您将无法创建这些枚举常量。

与错误无关,大写的变量名不会被嘲笑。也不是大写的包名称。