Bukkit创建自定义方法。问题

时间:2015-07-29 05:27:55

标签: java minecraft bukkit

我正在尝试创建一种方法,可以让我更轻松地添加项目。创建库存很容易。这是我的代码。 这是问题,每当我右键点击我的发光石灰尘时,它会抛出偶数异常错误,我不知道为什么。这是每当有人右键点击发光石灰尘时它应该做的事情,它应该打开一个绿色的名为“小工具”的库存。库存中有一个名为“HidePlayers”的项目

错误

        [01:25:55 ERROR]: Could not pass event PlayerInteractEvent to KnoxHub v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:310) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
a:62) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
ava:502) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
ava:487) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
ractEvent(CraftEventFactory.java:227) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git
-Spigot-f928e7a-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
ractEvent(CraftEventFactory.java:194) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git
-Spigot-f928e7a-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInte
ractEvent(CraftEventFactory.java:190) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git
-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
:694) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
ckPlace.java:52) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91ae
d8]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlo
ckPlace.java:1) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed
8]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
3) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spig
ot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
18) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
67) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
57) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
:560) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
Caused by: java.lang.IllegalArgumentException: Chests must have a size that is a
 multiple of 9!
        at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[spigot-1
.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.createInventory(CraftServe
r.java:1525) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]

        at org.bukkit.Bukkit.createInventory(Bukkit.java:937) ~[spigot-1.8.7-R0.
1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at crypted.knoxhub.Gadgets.GadgetCore.KInventoryCreate(GadgetCore.java:3
5) ~[?:?]
        at crypted.knoxhub.Gadgets.GadgetCore.AddGadget(GadgetCore.java:42) ~[?:
?]
        at crypted.knoxhub.Gadgets.GadgetCore.Interact(GadgetCore.java:66) ~[?:?
]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
.8.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
va:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        ... 18 more
>

GadgetCore.Java

private Core Plugin;
public GadgetCore(Core p){ this.Plugin = p; }

//Inventories
public Inventory GadgetMenu;

// Simple Inventory Creation
public Inventory KInventoryCreate( int slots, String invname ) throws IOException {

    return Bukkit.createInventory(null, slots, invname);

}

// Simple Gadget Creation
public void AddGadget(Player player,int position ,String permission, ItemStack item, String GadgetItemName, String lore) throws IOException {

     GadgetMenu = KInventoryCreate(10, ChatColor.GREEN + "Gadgets");

    if ( !player.hasPermission(permission) ) {

        player.sendMessage( ChatColor.RED + GadgetItemName.toUpperCase() + ChatColor.YELLOW + " can only be used by specific Ranks");

        return;

    }

    GadgetMenu.setItem(position, Plugin.applyLore(item, GadgetItemName, lore));


}


@EventHandler
public void Interact(PlayerInteractEvent e) throws IOException{
    Player p = e.getPlayer();
    ItemStack item = p.getItemInHand();

    if ( e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK) ) {
        if ( item.getType().equals(Material.GLOWSTONE_DUST)) {
            e.setCancelled(true);
            AddGadget(p, 3, "gadget.hide", new ItemStack(Material.GLOWSTONE_DUST), "HidePlayers", ChatColor.GRAY + "d");
            p.openInventory(GadgetMenu);
            p.sendMessage("Debug");
        }
    }

}

@EventHandler
public void Debug(PlayerInteractEvent e) throws IOException{
    Player p = e.getPlayer();
    ItemStack item = p.getItemInHand();

    if ( e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK) ) {
        if ( item.getType().equals(Material.REDSTONE) ) {
            e.setCancelled(true);
            p.openInventory(GadgetMenu);
            p.sendMessage("Debug");
        }
    }

}

Core.Java

public class Core extends JavaPlugin implements Listener {


public Core plugin;
public GadgetCore gc;

public ItemStack applyLore(ItemStack stack, String name, String lore1){
    ItemMeta meta = stack.getItemMeta();
    meta.setDisplayName(name);
    ArrayList<String> lore = new ArrayList<String>();
    lore.add(lore1);
    meta.setLore(lore);
    stack.setItemMeta(meta);
    return stack;
}


public void onEnable(){

    Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[KnoxHub] created by 2hash");

   // Bukkit.getPluginManager().registerEvents(new );
   getServer().getPluginManager().registerEvents(new GadgetCore(this), this);

}


}

1 个答案:

答案 0 :(得分:1)

在stacktrace中:

Caused by: java.lang.IllegalArgumentException: Chests must have a size that is a multiple of 9!

Stacktraces是你的朋友!如果您想了解如何理解错误消息并自行调试,请阅读此内容:https://stackoverflow.com/a/3988794/3476226

要解决您的具体问题,您应该只是将10方法的这一行中的AddGadget更改为9或其他倍数,因为库存总是有九列所以创建一个大小为9的库存将使一个9列乘1行,如果有18个槽,则为9x2,27将为9x3,依此类推。

GadgetMenu = KInventoryCreate(10, ChatColor.GREEN + "Gadgets");

但更好的是,只需修改您的KInventoryCreate方法并返回类似Bukkit.createInventory(null, 9*rows, invname);的内容而不是原始的插槽号,这样您就可以安全地再次犯同样的错误。