Updating GUI with Block Break Event

Discussion in 'Plugin Development' started by InsertNameHere, Jul 24, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    InsertNameHere

    I'm making a counter for the number of diamonds, iron, gold, emeralds, etc. mined below a certain Y-level. I have the Inventory working to display a count of diamond ore, gold ore, iron ore, etc. broken. However, how do you get the inventory to update each time the block is broken?

    As of right now, closing and then opening the inventory is the only way to update the inventory. I'm wondering if there's a way to use player.updateInventory() to update the inventory while the player has it open.

    I understand that something has to go in a BlockBreakEvent that updates the player, but how do I get a list of players viewing the inventory from that event alone?
     
  2. Offline

    KarimAKL

    @InsertNameHere How do you display it? If it's by items then just add/set the items in the inventory.
    EDIT: I just noticed that "by items" sounds pretty dumb when working with an inventory, what i meant was with the amount of items in a stack.
     
    Last edited by a moderator: Jul 24, 2019
  3. Offline

    InsertNameHere

    @KarimAKL No, I have a player head with the display name set as the player's name. I have the count set a the lore for each head. I doubt the lores update themselves right?
     
  4. Offline

    KarimAKL

    @InsertNameHere Just get the ItemStack, edit the lore, then set the edited version back in the inventory.
     
  5. Offline

    InsertNameHere

    @KarimAKL Is there an elegant way of doing this? The way I'm thinking of involves a for loop that loops through each ItemStack in the Inventory, getting the Lore, then getting the proper index using a switch statement or if statement, and then incrementing the lore by one.

    Is there a shorter way to do this? It seems kind of tedious to have multiple for loops and if statements...
     
  6. Offline

    Machine Maker

    I mean if the counter for a specific block is always in the same spot in your custom inventory, you can just get the item in that slot and then change the lore.
     
    KarimAKL likes this.
  7. Offline

    InsertNameHere

    @X1machinemaker1X It isn't the same. The list the inventory loops through contains a list of players below a certain Y-level. So each ItemStack may change depending on where the player is.

    I created a new class for Inventory stuff, but I'm having a few NRE issues. This is my Inventory class

    Code:
    private ModerationLite p;
    private Inventory miningGUI = Bukkit.createInventory(null, 45, ChatColor.BLUE + "Players Mining");
    private Inventory confirmGUI = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Confirm Teleport");
    
    public InventoryManager(ModerationLite p) {
        this.p = p;
    }
    
    public boolean invContainsPlayer(UUID uniqueID) {
        if (miningGUI.getContents().length == 0)
            return false;
        else {
            for (ItemStack item: miningGUI.getContents()) {
                System.out.println(item.getItemMeta().getDisplayName());
            }
        }
    
        return false;
    }
    
    public void openGUI(Player player) {
        for (User u: p.getMiners().keySet()) {
            System.out.println(invContainsPlayer(u.getUniqueID()));
            ItemStack head = new ItemStack(Material.PLAYER_HEAD);
            ItemMeta meta = head.getItemMeta();
            meta.setDisplayName(Bukkit.getPlayer(u.getUniqueID()).getDisplayName());
            meta.setLore(Arrays.asList(
                                     ChatColor.GREEN + "Y-Level: " + p.getMiners().get(u),
                                     ChatColor.GOLD + "Stone: " + u.getStone(),
                                     ChatColor.GOLD + "Emeralds: " + u.getEmerald(),
                                     ChatColor.GOLD + "Diamonds: " + u.getDiamond(),
                                     ChatColor.GOLD + "Gold: " + u.getGold(),
                                     ChatColor.GOLD + "Iron: " + u.getIron(),
                                     ChatColor.GOLD + "Lapis: " + u.getLapis(),
                                     ChatColor.GOLD + "Redstone: " + u.getRedstone(),
                                     ChatColor.GOLD + "Coal: " + u.getCoal()));
            head.setItemMeta(meta);
            miningGUI.addItem(head);
        }
    
        ItemStack exit = new ItemStack(Material.RED_WOOL);
        ItemMeta exitMeta = exit.getItemMeta();
        exitMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Exit");
        exit.setItemMeta(exitMeta);
        miningGUI.setItem(44, exit);
        player.openInventory(miningGUI);
    }
    
    Basically, I have a list of Users (player UUID and count of all the ores in-game) and whenever I run the command /miners, it brings up players that are below a certain Y-level. Without the invContainsPlayer() method, whenever I open the inventory, it adds all the players mining, regardless if their head is already in the inventory. The method simply checks if the player is already in the inventory or not. However, invContainsPlayer() seems to be producing null.

    I currently have the System.out.println() statements to test to see if the code is working. I get a null pointer exception error whenever I run the command to open the inventory. Any reason why it's returning null?
     
    Last edited: Jul 25, 2019
  8. Offline

    KarimAKL

    @InsertNameHere If you don't know where the item is located then you need to loop the inventory and check it.
    You are just telling us that you get a NullPointerException, but not which line is causing it; i'm guessing it's 'item.getItemMeta().getDisplayName()' that's causing it, because you aren't checking if it has a display name.
     
  9. Offline

    InsertNameHere

    @KarimAKL It should though... in the actual openGUI method, I have a line inside the list of Users that sets the Display name of the head: meta.setDisplayName(Bukkit.getPlayer(u.getUniqueID()).getDisplayName());

    I don't understand, why do I have to check if the item has a display name when I set it in openGUI?
     
  10. Offline

    Machine Maker

    DisplayName is the name given to the item if it is renamed. If it has its original name, DisplayName is null, (if I recall correctly)
     
  11. Offline

    InsertNameHere

    @KarimAKL @X1machinemaker1X Ooohh I get it... calling .getContents() returns the whole inventory. Since not all of the inventory is filled up, the ItemStack received is null... That's why it's getting the NPE
     
  12. Offline

    Machine Maker

    Or that yeah. :)
     
  13. Offline

    KarimAKL

  14. Offline

    InsertNameHere

    @KarimAKL No, not yet. I believe I have the update-inventory part working, but whenever the player returns home (hypothetically speaking), the player head doesn't get removed.

    And if I use the .remove() method, it only removes the player head, but doesn't shift all the ItemStacks to the left. Honestly, this would've been much easier if I were able to get a list of players viewing the inventory and then just have it loop through the list running player.updateInventory().
     
  15. Offline

    KarimAKL

    @InsertNameHere I think something like this should work:
    Code:Java
    1. boolean removedItem = false;
    2. for (int i = 0; i < inventory.getSize(); i++) {
    3. ItemStack item = inventory.getItem(i);
    4. if (item == null) continue;
    5. if (item.equals(/*the item to remove*/)) {
    6. inventory.removeItem(item);
    7. removedItem = true;
    8. }
    9. if (!removedItem) continue;
    10. inventory.removeItem(item);
    11. inventory.setItem(i-1, item);
    12. }

    I just came up with it on the spot so i haven't thought much about it.
    Let me know if there's anything you don't understand.
     
  16. Offline

    darthteddy1

    make sure to update the inventory afterwards ;)
     
    KarimAKL likes this.
  17. Offline

    InsertNameHere

    @darthteddy1 @KarimAKL I can't update the inventory though... Maybe you guys misunderstood my question, so let me re-explain.

    I have a class, User, that holds a player's UUID and eight integers of the number of ores mined as well as stone. If the player is below a certain Y-level (let's say 32), then the plugin automatically adds the player's UUID into a HashMap<User, Integer>. The list of Users can be checked by running a command that pulls up an inventory of the players mining. I want to have it so that when an ore is mined, say diamond, the plugin automatically increments the diamond counter by one and updates the inventory too.

    Currently, I have it so that the lore automatically updates when the BlockBreakEvent is called. The only problem is that in order to update the inventory, the player will have to run the command again. Is there a way to have it so that the GUI automatically updates when the player has it opened?

    I'm trying to find a way to get the list of Players viewing the inventory and then looping through each player and running player.updateInventory(). But how do I do that?

    Sorry if I sound a bit aggressive about this, but I've spent three days trying to resolve this problem and it's kind of irritating me at this point.
     
  18. Offline

    Machine Maker

    So the inventory has a getViewers() method that returns a List<HumanEntity> of all people viewing the inventory.
     
  19. Offline

    InsertNameHere

    @X1machinemaker1X But isn't that only an available method in InventoryClickEvent? If so, doesn't InventoryClickEvent only trigger when the player clicks an inventory slot? How would I have the inventory update without clicking anything?
     
  20. Offline

    Machine Maker

  21. Offline

    InsertNameHere

    @KarimAKL No, it doesn't work. I'm getting a NPE on the method invContainsPlayer. Here's the code in my manager class

    Code:
    public class InventoryManager {
    
        private InventoryManager() {}
    
        private static InventoryManager instance = new InventoryManager();
        private UserList users = UserList.getInstance();
    
        private Inventory miningGUI = Bukkit.createInventory(null, 45, ChatColor.DARK_BLUE + "Players Mining");
        private Inventory confirmGUI = Bukkit.createInventory(null, 9, ChatColor.DARK_BLUE + "Confirm Teleportation");
    
        public static InventoryManager getInstance() {
            return instance;
        }
    
        public void openGUI(Player sender) {
            for (User u: users.getUsers()) {
                if (!invContainsUser(u)) {
                    ItemStack head = new ItemStack(Material.PLAYER_HEAD);
                    SkullMeta headMeta = (SkullMeta) head.getItemMeta();
    
                    headMeta.setOwningPlayer(Bukkit.getOfflinePlayer(u.getUniqueID()));
                    headMeta.setDisplayName(Bukkit.getPlayer(u.getUniqueID()).getDisplayName());
                    headMeta.setLore(Arrays.asList(
                        ChatColor.GREEN + "Y-Level: " + u.getyLevel(),
                        ChatColor.GOLD + "Total: " + u.getTotal(),
                        ChatColor.GOLD + "Emeralds: " + u.getEmerald(),
                        ChatColor.GOLD + "Diamonds: " + u.getDiamond(),
                        ChatColor.GOLD + "Gold: " + u.getGold(),
                        ChatColor.GOLD + "Iron: " + u.getIron(),
                        ChatColor.GOLD + "Lapis: " + u.getLapis(),
                        ChatColor.GOLD + "Redstone: " + u.getRedstone(),
                        ChatColor.GOLD + "Coal: " + u.getCoal()
                    ));
                    head.setItemMeta(headMeta);miningGUI.addItem(head);
                }
            }
    
            ItemStack exit = new ItemStack(Material.RED_WOOL);ItemMeta exitMeta = exit.getItemMeta();
            exitMeta.setDisplayName(ChatColor.RED.toString() + ChatColor.BOLD + "Exit");
            exit.setItemMeta(exitMeta);miningGUI.setItem(44, exit);
    
            sender.openInventory(miningGUI);
        }
    
        public void openConfirmGUI(Player sender, Player target) {
            ItemStack confirm = new ItemStack(Material.GREEN_WOOL);
            ItemMeta confirmMeta = confirm.getItemMeta();
            confirmMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Confirm");
            confirm.setItemMeta(confirmMeta);
            confirmGUI.setItem(0, confirm);
    
            ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD);
            SkullMeta playerMeta = (SkullMeta) playerHead.getItemMeta();
            playerMeta.setOwningPlayer(Bukkit.getOfflinePlayer(target.getUniqueId()));
            playerMeta.setDisplayName(target.getDisplayName());
            playerHead.setItemMeta(playerMeta);confirmGUI.setItem(4, playerHead);
    
            ItemStack cancel = new ItemStack(Material.RED_WOOL);
            ItemMeta cancelMeta = cancel.getItemMeta();
            cancelMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Go Back");
            cancel.setItemMeta(cancelMeta);
            confirmGUI.setItem(8, cancel);
    
            sender.openInventory(confirmGUI);
        }
    
        private boolean invContainsUser(User u) {
            for (ItemStack item: miningGUI.getContents()) {
                if (item != null && item.getItemMeta().getDisplayName().equals(Bukkit.getPlayer(u.getUniqueID()).getDisplayName()))
                    return true;
            }
    
            return false;
        }
    
        public void updateGUI(UUID uniqueID) {
            User u = users.getUser(uniqueID);
            ItemStack head = getItemStack(uniqueID);
            if (head != null) {
                ItemMeta meta = head.getItemMeta();
                meta.getLore().clear();
                meta.setLore(Arrays.asList(
                    ChatColor.GREEN + "Y-Level: " + u.getyLevel(),
                    ChatColor.GOLD + "Total: " + u.getTotal(),
                    ChatColor.GOLD + "Emeralds: " + u.getEmerald(),
                    ChatColor.GOLD + "Diamonds: " + u.getDiamond(),
                    ChatColor.GOLD + "Gold: " + u.getGold(),
                    ChatColor.GOLD + "Iron: " + u.getIron(),
                    ChatColor.GOLD + "Lapis: " + u.getLapis(),
                    ChatColor.GOLD + "Redstone: " + u.getRedstone(),
                    ChatColor.GOLD + "Coal: " + u.getCoal()
                ));
                head.setItemMeta(meta);
    
                for (HumanEntity viewer: miningGUI.getViewers()) {
                    if (viewer instanceof Player) {
                        ((Player) viewer).updateInventory();
                    }
                }
            }
        }
    
        public void removeItem(UUID uniqueID) {
            ItemStack head = getItemStack(uniqueID);
            if (head != null) {
                int nullCount = 0;
                for (int i = 0; i < miningGUI.getSize() - 1; i++) {
                    if (miningGUI.getItem(i) == null) {
                        nullCount++;
                     } else if (miningGUI.getItem(i).equals(head)) {
                        miningGUI.remove(miningGUI.getItem(i));
                        nullCount++;
                     } else {
                        miningGUI.setItem(i - nullCount, miningGUI.getItem(i));}
                 }
            }
        }
    
        public ItemStack getItemStack(UUID uniqueID) {
            for (ItemStack item: miningGUI.getContents()) {
                if (item != null && item.getItemMeta().getDisplayName().equals(Bukkit.getPlayer(uniqueID).getDisplayName()))
                    return item;
            }
            return null;
        }
    }
    
    Console says there's an error in the method invContainsPlayer():

    Code:
    28.07 23:18:50 [Server] INFO RecordTime issued server command: /miners
    28.07 23:18:50 [Server] ERROR null
    28.07 23:18:50 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'miners' in plugin Moderate v1.0
    28.07 23:18:50 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:48) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:704) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1621) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1461) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:9) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_212]
    28.07 23:18:50 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_212]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:896) [spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    28.07 23:18:50 [Server] INFO Caused by: java.lang.NullPointerException
    28.07 23:18:50 [Server] INFO at me.recordtime.moderate.managers.InventoryManager.invContainsUser(InventoryManager.java:92) ~[?:?]
    28.07 23:18:50 [Server] INFO at me.recordtime.moderate.managers.InventoryManager.openGUI(InventoryManager.java:35) ~[?:?]
    28.07 23:18:50 [Server] INFO at me.recordtime.moderate.commands.Miners.onCommand(Miners.java:39) ~[?:?]
    28.07 23:18:50 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_1.13.2.jar:git-Spigot-1a3504a-84f3da3]
    28.07 23:18:50 [Server] INFO ... 15 more
    
    More specifically, this is the line that's giving the null error

    Code:
    if (item != null && item.getItemMeta().getDisplayName().equals(Bukkit.getPlayer(u.getUniqueID()).getDisplayName()))
    
    Any ideas why?
     
  22. Offline

    KarimAKL

    @InsertNameHere You are only checking if the item isn't null, you are still trying to compare the displayname without checking if the item has one.
    Use the following:
    Code:Java
    1. if (item != null && /*I'm not sure 'hasItemMeta()' is needed, but it's just in case*/item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getDisplayName().equals(/*Your string*/))
     
  23. Offline

    Machine Maker

    @KarimAKL This seems to suggest hasItemMeta is a good idea but it isn’t 100% convulsive.
     
    KarimAKL likes this.
  24. Offline

    InsertNameHere

    @KarimAKL I ended up trying a different way altogether, by removing the for-each loop in openGUI() and having the plugin automatically add the player on a move event. i.e when a player moves below a certain Y-level, it automatically adds the player to the inventory.

    I tested it on a server and had two problems with the inventory listener. First, whenever a player clicks outside of the inventory when the GUI is open, it brings up a null error.
    Code:
    [02:03:26 ERROR]: Could not pass event InventoryClickEvent to Moderate v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:66) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:507) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:492) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:2108) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.PacketPlayInWindowClick.a(SourceFile:33) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:9) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_212]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_212]
            at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:896) [spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot.jar:git-Spigot-1a3504a-dfa7583]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot.jar:git-Spigot-1a3504a-dfa7583]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_212]
    Caused by: java.lang.NullPointerException
            at me.recordtime.moderate.events.InventoryListener.onInventoryClickEvent(InventoryListener.java:28) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_212]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_212]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_212]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:312) ~[spigot.jar:git-Spigot-1a3504a-dfa7583]
            ... 15 more
    
    This is the code in my listener class
    Code:
    public class InventoryListener implements Listener {
    
        private Moderate p;
        private InventoryManager manager = InventoryManager.getInstance();
    
        public InventoryListener(Moderate p) {
            this.p = p;
        }
    
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e) {
            if (e.getWhoClicked() instanceof Player) {
                Player player = (Player) e.getWhoClicked();
                if (e.getView().getTitle().equals(ChatColor.DARK_BLUE + "Players Mining")) {
                    Material item = e.getCurrentItem().getType();
                    if (item != null && item.equals(Material.PLAYER_HEAD)) {
                        Player target = Bukkit.getPlayer(e.getCurrentItem().getItemMeta().getDisplayName());
                        manager.openConfirmGUI(player, target);
                    }
    
                    if (e.getCurrentItem().getType().equals(Material.RED_WOOL))
                        player.closeInventory();e.setCancelled(true);
                } else if (e.getView().getTitle().equals(ChatColor.DARK_BLUE + "Confirm Teleportation")) {
                    switch (e.getCurrentItem().getType()) {
                        case GREEN_WOOL:
                            Player miner = Bukkit.getPlayer(e.getClickedInventory().getItem(4).getItemMeta().getDisplayName());
                            player.setGameMode(GameMode.SPECTATOR);
                            player.teleport(miner.getLocation());
                            player.sendMessage(ChatColor.GREEN + "You have been teleported.");
                            break;
                        case RED_WOOL:
                            manager.openGUI(player);
                            break;
                        }
                    e.setCancelled(true);
                }
            }
        }
    }
    
    It's clear the check I have at the beginning with "item != null" does not work. Any reason why?

    The second problem I have is that whenever someone attempts to click on the head to teleport, they end up picking the head up and can't drop it. When they close the inventory, it drops the head on the ground with the lore, display name, etc. Here's a video illustrating what happens:

    .

    It seems to be a problem with the event not being canceled, but the code works as intended on the test server.
     
  25. Offline

    timtower Administrator Administrator Moderator

    @InsertNameHere You are using getType, Material item = e.getCurrentItem().getType();
    Your null check should check e.getCurrentItem()
     
  26. Offline

    KarimAKL

    @InsertNameHere As timtower said, 'item' is a Material, and you are getting that material by using ItemStack#getType() while the ItemStack is null, so you get a NullPointerException.
    As for your second problem, you are not cancelling the event when you click the head. Try printing some messages to check why the confirmation GUI isn't opening.
     
Thread Status:
Not open for further replies.

Share This Page