Solved Disallow players to move items in their inventory

Discussion in 'Spigot Plugin Help' started by PartyMC, Feb 19, 2020.

  1. I am currently working on my own plugin, and want to make it so that players can't move the items I give them on join in their inventory. Now they can move them all around, which results in them also being able to dump them in GUIs. This is how far I got:

    Code (Text):
    package me.PartyProNL.PartyParks.Listeners;

    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.inventory.InventoryInteractEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.EventHandler;

    public class Prevention implements Listener {

        @EventHandler
        public void onDrop(PlayerDropItemEvent event) {
            event.setCancelled(true);
        }
        public void onInventoryClick(InventoryInteractEvent event) {
            event.setCancelled(true);
        }
        public void onBlockPlace(BlockPlaceEvent event) {
            if(!(event.getPlayer().hasPermission("partyparks.place"))) {
                event.setCancelled(true);
            }
        }
        public void onBlockBreak(BlockBreakEvent event) {
            if(!(event.getPlayer().hasPermission("partyparks.break"))) {
                event.setCancelled(true);
            }
        }
    }
    This is the complete code. The anti-drop thing does work, but the one about InventoryInteractEvent does not work. They can still move the items. How can I fix this?
     
  2. drives_a_ford

    Moderator

    • Agree Agree x 1
  3. Furthermore you forgot to add @EventHandler in the other methods.
     
  4. If you only want to deactivate the move in a certain inventory, you can use the InventoryClickEvent to query the name of the inventory and then cancel it. If it should be deactivated in every inventory, you can cancel the entire InventoryClickEvent
     
    • Agree Agree x 1
  5. Okay, here's what I've got now:

    Code (Text):
    package me.PartyProNL.PartyParks.Listeners;

    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.EventHandler;

    public class AntiInventoryMove implements Listener {

        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
                event.setCancelled(true);
                event.getWhoClicked().sendMessage("Event recorded!");
        }
    }
    But I am having other problems, which I think are stopping this from working. I have another file that has PlayerInteractEvent, and I get lots of errors from it saying it couldn't pass the event to my plugin. Here's the code for that file:

    Code (Text):
    package me.PartyProNL.PartyParks.Listeners;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;

    import me.PartyProNL.PartyParks.Menus.PartyParksMenu;
    import me.PartyProNL.PartyParks.Menus.ProfileMenu;
    import me.PartyProNL.PartyParks.Util.Color;

    public class MenuOpener implements Listener{
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
           
            Player p = event.getPlayer();
            if(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(Color.color("&e&lPartyParks &8>> &7Right click"))) {
                PartyParksMenu.partyparksMenu(event.getPlayer());
            }
            if(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals(Color.color("&e" + p.getName() + " &8>> &7Right click"))) {
                ProfileMenu.profileMenu(event.getPlayer());
            }
        }
    }
    Does any of you see anything wrong with this? I can't seem to figure it out, and I think this is what's stopping the other thing from working.
     
  6. Okay, I managed to fix the error in the second file. I got an error due to me checking for a thing that had a possibility of not being there. Now the anti move thing does work!