Solved Players can take items out from GUI.

Discussion in 'Spigot Plugin Development' started by wiktor_k, Apr 18, 2019.

  1. Hello, I have problem with my plugin/code. Players can remove items from the GUI.

    Code: https://pastebin.com/Eu6DGBB8


    1. case STONE:
    2. p.closeInventory();
    3. p.performCommand("drop"); // Command /drop - open the drop list in GUI.
    4. break;
    5. case BEACON:
    6. p.closeInventory();
    7. p.performCommand("efekty"); // Command /efekty - open the effects shop in GUI.
    8. break;
     
  2. Just check your inventory names.
     
  3. It's so sad to see people trying to make plugins before they know Java..

    To your problem: The inventory's name is different than the one your InventoryClickEvent checks.
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  4. Strahan

    Benefactor

    Also it's much easier when doing the check to use the ChatColor.stripColor method so you don't have to worry about colors. As it is, you're mixing ChatColor enum plus a & color code in the string you check, when the inv you set on the (for some inexplicable reason static) inventory object has a different color setup.

    Also, you may want to learn about loops. When you find yourself typing the same code over and over and over with minor changes it's a sign you may want to rethink your approach. Those forty-five lines of setItem could be changed to just a couple with a loop.

    Also also, rather than return when you detect a player inv clicking in your GUI just set it cancelled. Otherwise they'll be confused as to why the menu suddenly vanished if they miss-click.

    Also also also (lol), why are you doing p.closeInventory() in every case of your switch? If you do it in every case, there is no need to do it at all in the switch block. Also why switch anyway on stained glass and book when it does the exact same thing as your default? Just don't switch on them and they'll fall to default and perform the same action. You also forgot a break in the first two cases so stained glass, book and stone will perform command "drop". I'd just rework it thus:
    Code (Text):

    p.closeInventory();
    switch (e.getCurrentItem().getType()) {
    case STONE:
        p.performCommand("drop");
        break;
    case BEACON:
        p.performCommand("efekty");
        break;
    }
    PS why are you returning false on your command? Return true.
     
    #4 Strahan, Apr 18, 2019
    Last edited: Apr 18, 2019
  5. Danke, nochmal. Mein fehler.
     
  6. Yes, it sad too for me. Thank you for help.
     
  7. I don't mixing ChatColor and String FixColor from my another utils class. Yes I did but I wanted to see if the problem would be solved in this way.
    The problem was the name of Inventory. It was the same but the color not.

    Yes, that's right also I know I could use loops but I didn't learn Java/Spigot-API with the documentation rather Youtube Tutorials and rewriting another plugins. Before I started with write plugins I writed scripts (Skript Plugin) but I was on same level writing.

    Sorry, I don't understand what you mean.

    As I said I chose the bad way to learn Java. Youtube :(
    I have read your post and I did what you says. Code looks better then before.

    First step of my fix

    Code (Text):

            for(int i = 0; i < 45; ++i) inv.setItem(i, puste);
           
            inv.setItem(11, informacje);
            inv.setItem(13, drop);
            inv.setItem(15, ekipa);
            inv.setItem(28, rangi);
            inv.setItem(30, sklep);
            inv.setItem(32, kity);
            inv.setItem(34, craftingi);
           
            player.openInventory(inv);
    }
    Second step of my fix

    Code (Text):

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatUtil.fixColor("&8ยป &4&oBlad: &cNie mozesz uzyc tej komendy!"));
                return false;
            }
            Player p = (Player) sender;
            openGUI(p);
            return true;
        }
    }
    Third step of my fix

    Code (Text):

            switch (e.getCurrentItem().getType()) {
            case STONE:
                p.performCommand("drop");
                break;
            case SKULL_ITEM:
                p.performCommand("ekipa");
                break;
            case GOLD_SWORD:
                p.performCommand("rangi");
                break;
            case GOLD_INGOT:
                p.performCommand("sklep");
                break;
            case DIAMOND_PICKAXE:
                p.performCommand("kit");
                break;
            case WORKBENCH:
                p.performCommand("craftingi");
                break;
            default:
                break;
            }
    }
     
    • Like Like x 1
  8. Strahan

    Benefactor

    Looks better.

    Yep. That's why I said:
    Which is why stripping color prior to a check makes those issues non existent.

    Disregard that, I misread the code.

    Well, it's never too late to take the time to learn Java properly :)
     

Share This Page