Solved Remove a specific item from player's Inventory

Discussion in 'Spigot Plugin Development' started by MLGesus, Aug 13, 2019.

  1. Coding an RPG plugin, currently made an item called Scroll of retreat, upon right click while the item is in a player's inventory, the player is returned to their bed or world spawn when there is not bed.

    However it is supposed to be consumed on use. Currently with my code it instantly crashes the server after it teleports me. What other way is there for me to safely remove items?
    Code (Java):
    player.getInventory().remove(player.getInventory().getItemInMainHand());
    Fixed:
    The fix:
    event.getItem().setAmount(0)
     
    #1 MLGesus, Aug 13, 2019
    Last edited: Aug 14, 2019
  2. You should verify if the ItemStack is not null or is not AIR, something it makes a crash
     
  3. Code (Java):
    if (p.getItemInHand() == null) return;
            if (p.getItemInHand().getType() == Material.AIR) return;
           
            //code
     
  4. Let me just show the whole code

    Code (Java):
    @EventHandler
        public void onRightClick(PlayerInteractEvent event) {


            Player player = event.getPlayer();
            if (player.getInventory().getItemInMainHand().getType().equals(Material.PAPER)) {
                World world = getServer().getWorld("world");
                ItemStack item = player.getInventory().getItemInMainHand();
                PersistentDataContainer xd = item.getItemMeta().getPersistentDataContainer();
                if (xd.isEmpty()) {
                    System.out.println("The player" + player.getName() + "is trying to use a teleportation scroll but its just a normal paper");
                } else {
                    System.out.println("The player " + player.getName() + " is using a teleport scroll, teleporting!");
                    if (player.getBedSpawnLocation() != null) {
                        player.sendMessage("Woosh! You've returned to the last place you rested!");


                        player.teleport(player.getBedSpawnLocation());
                        player.getInventory().remove(player.getInventory().getItemInMainHand());
                    } else {
                        player.teleport(world.getSpawnLocation());
                        player.sendMessage("You either do not have a bed or your bed was destroyed, teleporting to spawn instead!");
                        player.getInventory().remove(player.getInventory().getItemInMainHand());
                    }

                }
            }
        }
     
  5. Strahan

    Benefactor

    You shouldn't assume the ItemStack you get has meta. That said, that's not the problem. I took your code and implemented it in my test plugin to see if I could replicate, and yea, it crashes for me.

    Code:
    Code (Text):
    @EventHandler
    public void onRightClick(PlayerInteractEvent event) {
      Player player = event.getPlayer();
      if (!player.getInventory().getItemInMainHand().getType().equals(Material.PAPER)) return;

      ItemStack item = player.getInventory().getItemInMainHand();
      if (!item.hasItemMeta()) return;
     
      World world = getServer().getWorld("world");
      PersistentDataContainer xd = item.getItemMeta().getPersistentDataContainer();
     
      if (xd.isEmpty()) {
        System.out.println("The player" + player.getName() + "is trying to use a teleportation scroll but its just a normal paper");
      } else {
        System.out.println("The player " + player.getName() + " is using a teleport scroll, teleporting!");
        if (player.getBedSpawnLocation() != null) {
          player.sendMessage("Woosh! You've returned to the last place you rested!");
          player.teleport(player.getBedSpawnLocation());
          player.getInventory().remove(player.getInventory().getItemInMainHand());
        } else {
          player.sendMessage("You either do not have a bed or your bed was destroyed, teleporting to spawn instead!");
          player.teleport(world.getSpawnLocation());
          player.getInventory().remove(player.getInventory().getItemInMainHand()); //  <-- crashes here
        }
      }
    As you said, it runs fine until it gets to the inv.remove() line then it dies:

    Code (Text):
    [21:23:42] [Server thread/ERROR]: Encountered an unexpected exception
    java.lang.AssertionError: TRAP
            at net.minecraft.server.v1_14_R1.ItemStack.checkEmpty(ItemStack.java:82) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.ItemStack.setCount(ItemStack.java:851) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.PlayerInteractManager.a(PlayerInteractManager.java:394) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1293) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:28) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) ~[spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819) [spigot-1.14.4.jar:git-Spigot-ea7e48b-368f4e9]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_181
    Tried doing .remove(item) and it crashes the same way. The only way I got it to work without shitting itself was to do event.getItem().setAmount(0). Strange.
     
  6. Hmm that is odd
    but I'll try what you have suggested.