Solved Cancelling dragging Items out of a Gui dosn't work

Discussion in 'Spigot Plugin Development' started by LittleProgrammer, Jun 8, 2021.

  1. Hi,
    i have a problem with my Gui. I want to it that you can't drag the Item out but id dosen't work I don't know why but I hope you know.

    There are no errors and I'm using Java 1.8 and Spigot 1.8.8.
    This is the Code:

    Code (Java):
    package de.littleprogrammer.yt.commands;

    import org.bukkit.Bukkit;
    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.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;


    public class LobbyTeleporterCommand implements CommandExecutor {

        private static Inventory inv;

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            inv = Bukkit.createInventory(null, 9*5, "§3Teleportieren");

            ItemStack itemStack = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14);
            ItemMeta itemMeta = itemStack.getItemMeta();
            itemMeta.setDisplayName("§4§lTest");

            itemStack.setItemMeta(itemMeta);

            inv.setItem(22, itemStack);

            p.openInventory(inv);

            return false;
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            if (event.getInventory().getTitle().equals("§3Teleportieren")){
                    event.setCancelled(true);
            }
        }

    }
     
     
  2. you forgot to implement Listener
     
    • Informative Informative x 1
  3. Oh true but how can I make it?
    In my IDE is it red underlinedif I type

    Code (Java):
    public class LobbyTeleporterCommand implements CommandExecutor, implements Listener {
     
  4. You should implement Listener and register the event. You can do it in the constructor. You can use something like this:


    Code (Text):
    public LobbyTeleporterCommand(JavaPlugin plugin){
        plugin.getCommand("chatbot").setExecutor(this);
        Bukkit.getPluginManager().registerEvents(this, plugin);
    }
    Then in your main you can do something like
    Code (Text):
    new LobbyTeleporterCommand(this);
     
    • Like Like x 1
  5. Code (Java):
    public class LobbyTeleporterCommand implements CommandExecutor, Listener {
     
    • Agree Agree x 1
    • Informative Informative x 1
  6. Oh thanks it's working :D

    I forgot to register the listener in the main I thought that it would be fine if I just register the Command xD
     
    • Funny Funny x 2
  7. No problem! If it's working you can set it as solved!
     
  8. Strahan

    Benefactor

    Also
    Code (Text):
    private static Inventory inv;
    No reason for that to be static.

    Code (Text):
    Player p = (Player)sender;
    Don't blindly cast, check first if sender is a Player.

    Code (Text):
    inv = Bukkit.createInventory(null, 9*5, "§3Teleportieren");
    Don't embed the color character. ChatColor exists for a reason; you'll hurt its feelings if you keep ignoring it :)
     
    • Agree Agree x 1
  9. Agreed! Specifically on the last one - I always make an utility for any of my projects, that translates the color codes. Feel free to use.
    Code (Java):

    public static String color(String msg) {
        return ChatColor.translateeAlternateColorCodes('&', msg);
    }
     
     
    • Useful Useful x 1