Solved Transfer arguments to other class

Discussion in 'Plugin Development' started by DiamondDaniel, Jun 25, 2020.

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

    DiamondDaniel

    Hello! I am developing a plugin that gives the player a gui when they are punishing a player in my server and I have 3 different classes. I need to move the argument from the GUICommand class to the ClickEvent Class. Does anyone know how?
    GUIPlugin.java (Main script) (open)

    Code:
    package me.itsmedan.bangui;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import me.itsmedan.bangui.commands.GUICommand;
    import me.itsmedan.bangui.events.ClickEvent;
    
    public final class GUIPlugin extends JavaPlugin {
        @Override
        public void onEnable() {
            getCommand("punish").setExecutor(new GUICommand());
           
            getServer().getPluginManager().registerEvents(new ClickEvent(), this);
        }
    }
    


    GUICommand.java (open)

    Code:
    package me.itsmedan.bangui.commands;
    
    import org.bukkit.command.CommandExecutor;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class GUICommand implements CommandExecutor {
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (player.hasPermission("punish.gui")) {
                    if(args.length == 1) {
                        Inventory punish = Bukkit.createInventory(player, 54, ChatColor.AQUA + "Punish: " + args[0]);
                       
                        ItemStack malicioushacks = new ItemStack(Material.CHEST);
                        ItemStack nonmalicioushacks = new ItemStack(Material.CHEST);
                       
                        ItemMeta malicioushacks_meta =  malicioushacks.getItemMeta();
                        malicioushacks_meta.setDisplayName(ChatColor.DARK_RED + "Malicious Hacks - 1st Offence");
                        ArrayList<String> malicioushacks_lore = new ArrayList<>();
                        malicioushacks_lore.add(ChatColor.GOLD + "Kill Aura");
                        malicioushacks_lore.add(ChatColor.GOLD + "Auto Armour");
                        malicioushacks_lore.add(ChatColor.GOLD + "Fly");
                        malicioushacks_lore.add(ChatColor.GOLD + "Speed");
                        malicioushacks_lore.add(ChatColor.GOLD + "Others (Listed on website)");
                        malicioushacks_meta.setLore(malicioushacks_lore);
                        malicioushacks.setItemMeta(malicioushacks_meta);
                       
                        ItemMeta nonmalicioushacks_meta =  nonmalicioushacks.getItemMeta();
                        nonmalicioushacks_meta.setDisplayName(ChatColor.DARK_RED + "Non Malicious Hacks - 1st Offence");
                        ArrayList<String> nonmalicioushacks_lore = new ArrayList<>();
                        nonmalicioushacks_lore.add(ChatColor.GOLD + "Any hacks NOT listed as Malicious Hacks");
                        nonmalicioushacks_meta.setLore(nonmalicioushacks_lore);
                        nonmalicioushacks.setItemMeta(nonmalicioushacks_meta);
                       
                        ItemStack[] menu_items = {malicioushacks, nonmalicioushacks};
                        punish.setContents(menu_items);
                       
                        player.openInventory(punish);
                       
                    }
                    else {
                        player.sendMessage(ChatColor.RED + "You need to provide the players name!");
                        return true;
                    }
               
                }
                else {
                    player.sendMessage(ChatColor.RED + "No permission!");
                    return true;
                }
               
            }
           
            return true;
        }
       
       
    }
    


    ClickEvent.java (open)

    Code:
    package me.itsmedan.bangui.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    public class ClickEvent implements Listener {
       
        @EventHandler
        public void clickEvent(InventoryClickEvent e) {
           
            Player player = (Player) e.getWhoClicked();
           
            if (e.getClickedInventory().getTitle().equalsIgnoreCase(ChatColor.AQUA + "Punishment GUI")) {
               
                switch(e.getCurrentItem().getType()) {
                    case CHEST:
                        player.closeInventory();
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "sudo " + player.getName() + " ban 1s test -s");
                }
               
                e.setCancelled(true);
            }
           
        }
       
    }
     
  2. Offline

    Machine Maker

    I assume you mean the player name when you saw "the argument". You could go about this several ways. You could include the player name (preferable the UUID) somewhere in the inventory, like the title, or in the lore of one of the items in that inventory. Then in the listener, you just check that item to see who it is you are trying to punish

    Another way would be to have a hash map that stores the command users UUID, and the person who is being punished's uuid when the player executes the command to open the GUI. Then in the listener, you just get the UUID from that map based on who opened the gui.
     
  3. Offline

    DiamondDaniel

    The title has the argument in it but how would I check to see what it is on the listener end?​
     
  4. Offline

    Machine Maker

    Well get the inventory from the click event, get the InventoryView, then get the title of that view.
     
  5. Offline

    DiamondDaniel

    I imported the inventory and inventoryview but i dont know how to add the code to get the title in the clickevent

    Nevermind, I solved it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 25, 2020
Thread Status:
Not open for further replies.

Share This Page