2016-08-05 14 views
1

私は、胸の場所が自分のconfig.yml内のどの場所とも一致しない場合、誰かが胸を開いたときにチェックするプラグインを作成しようとしています。今、私はこのコードを持っている :Bukkitプラグインがconfig.ymlから場所を取得する

public class Listeners implements Listener { 

    public Core plugin; 
    public Listeners(Core core) { 
     this.plugin = core; 
    } 

    @EventHandler 
    private void onPlayerInteract(PlayerInteractEvent e) { 
     if(e.getAction() == Action.RIGHT_CLICK_BLOCK) { 
      if(plugin.createMode) { 
       if (e.getClickedBlock().getType() == Material.CHEST) { 
        e.setCancelled(true); 
        Location loc = e.getClickedBlock().getLocation(); 
        String name = plugin.name; 
        plugin.getConfig().set(name + ".world", loc.getWorld().getName()); 
        plugin.getConfig().set(name + ".x", loc.getBlockX()); 
        plugin.getConfig().set(name + ".y", loc.getBlockY()); 
        plugin.getConfig().set(name + ".z", loc.getBlockZ()); 
        plugin.saveConfig(); 
        e.getPlayer().sendMessage(ChatColor.GOLD + "[ChestTreasure] " + ChatColor.RESET + "Treasury chest successfully created!"); 
        plugin.createMode = false; 
       } 
      } else { 
       if(e.getClickedBlock().getType() == Material.CHEST) { 
        plugin.chests.clear(); 
        for (String key : plugin.getConfig().getKeys(false)){ 
         //We are getting every key from our config.yml file 
         ConfigurationSection location = plugin.getConfig().getConfigurationSection(key); 
         String world = location.getString(key + ".world"); 
         int x = location.getInt(key + ".x"); 
         int y = location.getInt(key + ".y"); 
         int z = location.getInt(key + ".z"); 
         e.getPlayer().sendMessage("Hondnota x je " + String.valueOf(x)); 
         Location l = new Location(Bukkit.getWorld(world), x, y, z); 
         plugin.chests.add(l); 
        } 
        for(Location l : plugin.chests) { 
         e.getPlayer().sendMessage(String.valueOf(e.getClickedBlock().getLocation().getX())); 
         if(l == e.getClickedBlock().getLocation()) { 
          e.getPlayer().sendMessage("Jeej"); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

をしかし、私は胸を右クリックすると、メッセージjeej doesntのが表示され、何が表示され、すべてがメッセージHodnotaある

ArratList<Location> list = new ArrayList(); 
foreach(get world, x, y, z from each key in section Locations in my config) { 
    list.add(new Location(world, x, y, z)); 
    foreach(Location l : list) { 
    if(l == p.location... 
} 
} 

編集:だから私はこのような何かをしたいですx je 0しかし、私は自分の設定にいくつかのキーがあり、xは0ではありません。コンソールでは、このエラーが表示されます:あなたが何ができるか

[12:25:13 ERROR]: Could not pass event PlayerInteractEvent to ChestTreasure v1.0 
org.bukkit.event.EventException 
     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:492) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:890) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:55) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:11) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91] 
     at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91] 
     at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at java.lang.Thread.run(Unknown Source) [?:1.8.0_91] 
Caused by: java.lang.IllegalArgumentException: Name cannot be null 
     at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getWorld(CraftServer.java:1023) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.Bukkit.getWorld(Bukkit.java:500) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at me.sudoman281.chestTreasure.Listeners.onPlayerInteract(Listeners.java:47) ~[?:?] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91] 
     at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91] 
     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     ... 17 more 
+0

"あなたはこのようなもの"をあなたは知っているようです。どうやらそれでは不十分で、より具体的なものがほしいと思っています。正確に何を望んでいるのかを具体的に説明しなければなりません。 – Julian

+0

より明記してください。 – manetsus

+0

リスナー47はどのラインですか? – Kerooker

答えて

1

は以下の通りです: 次のようにあなたのconfigsであなたの場所を保存:私たちは行くすべてのconfigsを取得するには、

# CONFIG.YML # 
location1: 
x: 0 
y: 0 
z: 0 
world: "world" 
location2: 
x: .... 
. 
. 

そして:

ArrayList<Location> listLocations = new ArrayList<Location>(); 
for (String key : getConfig().getKeys(false)){ 
    //We are getting every key from our config.yml file 
    ConfigurationSection location = getConfig().getConfigurationSection(key); 
    int x = location.getInt(key + ".x"); 
    int y = location.getInt(key + ".y"); 
    int z = location.getInt(key + ".z"); 
    String world = location.getString(key + ".world"); 
    Location l = new Location(Bukkit.getWorld(world), x, y, z); 
    listLocations.add(l); 
} 

あなたは今のArrayList listLocationsにその設定ファイル上にあるすべての場所を持っている必要があり、そこからあなたのチェックを行うことができます!

+0

しかし、まだ問題がある、私は私の質問を編集しました。 – xtrontross

+0

原因:java.lang.IllegalArgumentException:名前はNULLであってはなりません。例の47行目。私が47行目を見つけようとしたときにコードを渡すことができますか? "}" – Lightspeed360

0

あなたはので、設定から、個々の値(x、y、z、世界)を取得する必要はありません。あなただけ使用できることを意味し、

Location implements ConfigurationSerializableconfig#set("path.to.location", yourLocation);yourLocation = (Location) config#get("path.to.location");


今、あなたの例外:

私はあなたにはlearn how to read stacktracesを提案します。これは、将来あなたにとって非常に役立つでしょう。これらの行から

 java.lang.IllegalArgumentException: Name cannot be null 
     at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.craftbukkit.v1_10_R1.CraftServer.getWorld(CraftServer.java:1023) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at org.bukkit.Bukkit.getWorld(Bukkit.java:500) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7] 
     at me.sudoman281.chestTreasure.Listeners.onPlayerInteract(Listeners.java:47) ~[?:?] 

世界は(ただし、私は上記のように、あなただけの場所を使用することをお勧めし、String world = location.getString(key + ".world"); があなたのconfigパスを確認してくださいここでnullであるので、私は、あなたが(間接的に)Bukkit.getWorld(null);と呼ばれることを理解し)

関連する問題