2016-06-24 3 views
-3

私はこのコードを使用してJavaでプライベートフィールドを読み込んだ後、bukkit用のマップに渡しました。エラーを取得する前にスニペットコードを2回だけ使用することができます

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

しかし、これを試してみると、エラーが発生します。私は最初のコードを使用することができますどのようにNoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException

enter image description here

スロー追加した後 enter image description here

enter image description here

私は追加すると

Unhandled exception type NoSuchFieldException 
は、私は別のエラーを取得する宣言 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessExceptionをスローします何もせずにエラー?私はエラーが出る前に2回しか使用できません。

全コード:

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

} 

答えて

1

まず、ジャワのビットを学びます。 https://docs.oracle.com/javase/tutorial/essential/exceptions/

使用して/ catchブロックを試してみてください代わりに(addToMapsに宣言をスロー)

例:

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

まず、あなたの反射の使用量は正確に正しくありません。これを設定する方法です:

構文に関しては、例外をスローするメソッドを呼び出しています。あなたはtry-catchでそれらを処理する必要があります。コンストラクタが例外をスローすると、それらの列挙定数を作成できなくなります。

エラーとは無関係に、大文字の変数名は微笑んでいません。大文字のパッケージ名もありません。

関連する問題