简体   繁体   中英

Why am I getting an error for opening a player inventory?

Error:

[09:47:20 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'store' in plugin AtlantisCommands v1.0-SNAPSHOT
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_281]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_281]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_281]
Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_8_R3.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:176) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at me.publicy.atlantis.commands.storecommand.onCommand(storecommand.java:27) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more

Code:

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class storecommand implements CommandExecutor , Listener {

    public static Inventory inv;

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("acommand")) {
            Player player = (Player) sender;
            if (player.hasPermission("a.permission")) {
                if (player.getUniqueId().toString().equalsIgnoreCase("uuid")) {
                    player.openInventory(inv);
                }
                player.sendMessage("message")

            } else {
                player.sendMessage(ChatColor.DARK_RED + "You do not have permission to execute this command");
            }
        }
        return false;
    }

    @EventHandler
    public void onClick(InventoryClickEvent event) {
        if (!event.getWhoClicked().getInventory().equals(inv))
            return;
        if (!event.getWhoClicked().getUniqueId().toString().equalsIgnoreCase("uuid"))
            return;
        if (event.getCurrentItem() == null)
            return;
        if (event.getCurrentItem().getItemMeta() == null)
            return;
        if (event.getCurrentItem().getItemMeta().getDisplayName() == null)
            return;
        if (event.getInventory().equals(inv)) {
            event.setCancelled(true);
        }

        if (event.getSlot() == 2 && event.getCurrentItem().getType() == Material.CARROT) {

        }
        if (event.getSlot() == 3 && event.getCurrentItem().getType() == Material.POTATO) {

        }
        if (event.getSlot() == 4 && event.getCurrentItem().getType() == Material.BREAD) {

        }
        if (event.getSlot() == 5 && event.getCurrentItem().getType() == Material.RAW_BEEF) {

        }
        if (event.getSlot() == 6 && event.getCurrentItem().getType() == Material.BARRIER) {
            event.getWhoClicked().closeInventory();
        }
        event.setCancelled(true);
    }

    public static void createInv(Player player) {
        inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "");

        ItemStack item = new ItemStack(Material.CARROT);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("");
        item.setItemMeta(meta);
        inv.setItem(2, item);

        ItemStack item1 = new ItemStack(Material.POTATO);
        ItemMeta meta1 = item1.getItemMeta();
        meta1.setDisplayName("");
        item1.setItemMeta(meta1);
        inv.setItem(3, item1);

        ItemStack item2 = new ItemStack(Material.BREAD);
        ItemMeta meta2 = item2.getItemMeta();
        meta2.setDisplayName("");
        item2.setItemMeta(meta2);
        inv.setItem(4, item2);

        ItemStack item3 = new ItemStack(Material.RAW_BEEF);
        ItemMeta meta3 = item3.getItemMeta();
        meta3.setDisplayName("");
        item3.setItemMeta(meta3);
        inv.setItem(5, item3);

        ItemStack item4 = new ItemStack(Material.BARRIER);
        ItemMeta meta4 = item4.getItemMeta();
        meta4.setDisplayName("");
        item4.setItemMeta(meta4);
        inv.setItem(6, item4);

        if (player.getInventory().equals(inv)) {
            player.sendMessage(ChatColor.DARK_RED + "Done!");
        }
    }
}

Shows an error when trying to open an inventory, this method has worked before on 1.16 and I'm confused on why it does not work on 1.8, would be great if someone could help me here. I am also trying to bind it to a uuid which may be the problem but any suggestions on how to keep the fact that it only happens when one person does this would be great.

You have to make sure that your Inventory has been created before opening it...

It seems like you are never calling the method createInv() which leads the inv variable to be null and that would throw your NullPointerException .

There are other better ways to achieve what you are trying to do. The best one according me would be to make a single instance of the inventory (and not create one for every Player ) since it looks like the items in the inventory do not change depending on the player.

You just have to make a simple Singleton pattern for your inventory declaration. This could look like this:

// This is your inv variable, but it is now private to prevent accessibility while it is
// not declared.
private static Inventory inv;

public static getInventoryFoo(/* You can pass parameters here if needed */){
    if (inv == null){
        inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "");
        // add the ItemStack here like you did in your createInv() method.
        //...
    }
    return inv;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM