Plugin work only for 1 player at time

Discussion in 'Plugin Development' started by dayactor69, Jun 29, 2021.

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

    dayactor69

    I have developed a plugin that have a gui and when i use it, it can be used only for 1 player at the same time and i dont know why. Can someone help me?

    That's the main class
    Code:
    package com.dayactor69.plugin;
    
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerCommandSendEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.event.server.ServerCommandEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.dayactor69.plugin.ControlCommand.chat;
    import com.dayactor69.plugin.ControlCommand.controlloyesno;
    import com.dayactor69.plugin.ControlCommand.obbiettivo2;
    import com.dayactor69.plugin.ControlCommand.obbiettivo3;
    import com.dayactor69.plugin.ControlCommand.obbiettivo4;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
           
            separateMessageOpen();
            System.out.println(" Il plugin e' stato abilitato correttamente");
            separateMessageClose();    
           
            this.getConfig().options().copyDefaults();
            saveDefaultConfig();
           
            Bukkit.getPluginManager().registerEvents(this, this);
           
            this.getCommand("freeze").setExecutor(new ControlCommand());
            this.getCommand("unfreeze").setExecutor(new EndControlCommand(this));
        }
           
    
        public void guiControllo(Player player)     {
       
           
           
            // BEGGINNING
            Inventory gui = Bukkit.createInventory(null, 45, ChatColor.GREEN + "Menu Controllo");
       
            // LORES
            List<String> nohackLore = new ArrayList<>();
            nohackLore.add(ChatColor.GRAY + "Clicca per concludere il controllo");
            nohackLore.add(ChatColor.GRAY + "No Hack trovate/Pulito");
           
            List<String> ammissioneLore = new ArrayList<>();
            ammissioneLore.add(ChatColor.GRAY + "Clicca per concludere il controllo");
            ammissioneLore.add(ChatColor.GRAY + "Ammissione Controllo Hack");
           
            List<String> trovateLore = new ArrayList<>();
            trovateLore.add(ChatColor.GRAY + "Clicca per concludere il controllo");
            trovateLore.add(ChatColor.GRAY + "Hack trovate in controllo");
           
            List<String> impossibileLore = new ArrayList<>();
            impossibileLore.add(ChatColor.GRAY + "Clicca per concludere il controllo");
            impossibileLore.add(ChatColor.GRAY + "Impossibilita' Controllo");
    
            List<String> evidentiLore = new ArrayList<>();
            evidentiLore.add(ChatColor.GRAY + "Clicca per concludere il controllo");
            evidentiLore.add(ChatColor.GRAY + "Hack Evidenti");
           
            List<String> clearinvLore = new ArrayList<>();
            clearinvLore.add(ChatColor.GRAY + "Clicca per cancellare l'inventario");
            clearinvLore.add(ChatColor.GRAY + "Del Player sospetto");
           
            List<String> contornoLore = new ArrayList<>();
           
           
            // ITEMSTACKS
            ItemStack  noHack = new ItemStack(Material.LIME_WOOL);
            ItemMeta noHackMeta = noHack.getItemMeta();
            noHackMeta.setDisplayName(ChatColor.GREEN + "No Hack");
            noHackMeta.setLore(nohackLore);
            noHack.setItemMeta(noHackMeta);
           
            ItemStack  ammissione = new ItemStack(Material.YELLOW_WOOL);
            ItemMeta ammissioneMeta = ammissione.getItemMeta();
            ammissioneMeta.setDisplayName(ChatColor.YELLOW + "Ammissione Hack");
            ammissioneMeta.setLore(ammissioneLore);
            ammissione.setItemMeta(ammissioneMeta);
           
            ItemStack  trovate = new ItemStack(Material.RED_WOOL);
            ItemMeta trovateMeta = trovate.getItemMeta();
            trovateMeta.setDisplayName(ChatColor.RED + "Hack Trovate");
            trovateMeta.setLore(trovateLore);
            trovate.setItemMeta(trovateMeta);
           
            ItemStack  impossibile = new ItemStack(Material.BLUE_WOOL);
            ItemMeta impossibileMeta = impossibile.getItemMeta();
            impossibileMeta.setDisplayName(ChatColor.BLUE + "Impossibilita Controllo");
            impossibileMeta.setLore(impossibileLore);
            impossibile.setItemMeta(impossibileMeta);
           
            ItemStack  evidenti = new ItemStack(Material.PURPLE_WOOL);
            ItemMeta evidentiMeta = evidenti.getItemMeta();
            evidentiMeta.setDisplayName(ChatColor.DARK_PURPLE + "Hack Evidenti");
            evidentiMeta.setLore(evidentiLore);
            evidenti.setItemMeta(evidentiMeta);
           
            ItemStack  clearinv = new ItemStack(Material.BARRIER);
            ItemMeta clearinvMeta = clearinv.getItemMeta();
            clearinvMeta.setDisplayName(ChatColor.RED + "CLEAR INVENTORY");
            clearinvMeta.setLore(clearinvLore);
            clearinv.setItemMeta(clearinvMeta);
           
            ItemStack contorno = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
            ItemMeta contornoMeta = impossibile.getItemMeta();
           
           
           
            // ITEM SETTING
       
            gui.setItem(10, noHack);
            gui.setItem(13, ammissione);
            gui.setItem(16, impossibile);
            gui.setItem(28, trovate);
            gui.setItem(31, evidenti);
            gui.setItem(34, clearinv);
            gui.setItem(0, contorno);
            gui.setItem(1, contorno);
            gui.setItem(2, contorno);
            gui.setItem(3, contorno);
            gui.setItem(4, contorno);
            gui.setItem(5, contorno);
            gui.setItem(6, contorno);
            gui.setItem(7, contorno);
            gui.setItem(8, contorno);
            gui.setItem(9, contorno);
            gui.setItem(11, contorno);
            gui.setItem(12, contorno);
            gui.setItem(14, contorno);
            gui.setItem(15, contorno);
            gui.setItem(17, contorno);
            gui.setItem(18, contorno);
            gui.setItem(19, contorno);
            gui.setItem(20, contorno);
            gui.setItem(21, contorno);
            gui.setItem(22, contorno);
            gui.setItem(23, contorno);
            gui.setItem(24, contorno);
            gui.setItem(25, contorno);
            gui.setItem(26, contorno);
            gui.setItem(27, contorno);
            gui.setItem(29, contorno);
            gui.setItem(30, contorno);
            gui.setItem(32, contorno);
            gui.setItem(33, contorno);
            gui.setItem(35, contorno);
            gui.setItem(36, contorno);
            gui.setItem(37, contorno);
            gui.setItem(38, contorno);
            gui.setItem(39, contorno);
            gui.setItem(40, contorno);
            gui.setItem(41, contorno);
            gui.setItem(42, contorno);
            gui.setItem(43, contorno);
            gui.setItem(44, contorno);
           
           
           
            // FINAL
           
            player.openInventory(gui);
           
        }
       
        @EventHandler
        public void onClick(InventoryClickEvent e) {
           
            Player player = (Player) e.getWhoClicked();
           
            Location spawn = new Location(Bukkit.getWorld("vanilla"), 231, 70, -172, 0, 0);
           
            if (e.getView().getTitle().contains("Menu Controllo")) {
                if (e.getCurrentItem() != null) {
                e.setCancelled(true);
                   
               
               
                   
                    switch (e.getCurrentItem().getType()) {
                    case LIME_WOOL:
                        player.sendMessage(ChatColor.RED + "Player Rilasciato con successo!");
                        player.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.sendMessage(ChatColor.GREEN + "Controllo completato! Grazie della collaborazione. Buon Game!");
                        controlloyesno.siono = 0;
                        chat.nochat.remove(obbiettivo4.sospettostring);
                        break;
                    case YELLOW_WOOL:
                        player.sendMessage(ChatColor.RED + "Player Bannato con successo!");
                        player.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.teleport(spawn);
                        String command = "tempban " + EndControlCommand.obbiettivo.sospettostring + " 4d Amissione Hack";
                        controlloyesno.siono = 0;
                        chat.nochat.remove(obbiettivo4.sospettostring);
                        player.performCommand(command);
                       
                        break;
                    case BLUE_WOOL:
                        player.sendMessage(ChatColor.RED + "Player Bannato con successo!");
                        player.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.teleport(spawn);
                        String command2 = "tempban " + EndControlCommand.obbiettivo.sospettostring + " 6d Impossibilita' Controllo";
                        controlloyesno.siono = 0;
                        chat.nochat.remove(obbiettivo4.sospettostring);
                        player.performCommand(command2);
                       
                        break;
                    case RED_WOOL:
                        player.sendMessage(ChatColor.RED + "Player Bannato con successo!");
                        player.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.teleport(spawn);
                        String command1 = "tempban " + EndControlCommand.obbiettivo.sospettostring + " 7d Cheats";
                        controlloyesno.siono = 0;
                        chat.nochat.remove(obbiettivo4.sospettostring);
                        player.performCommand(command1);
                       
                       
                        break;
                    case PURPLE_WOOL:
                        player.sendMessage(ChatColor.RED + "Player Bannato con successo!");
                        player.teleport(spawn);
                        EndControlCommand.obbiettivo1.sospettoplayer.teleport(spawn);
                        String command3 = "tempban " + EndControlCommand.obbiettivo.sospettostring + " 6d Hack Evidenti";
                        controlloyesno.siono = 0;
                        chat.nochat.remove(obbiettivo4.sospettostring);
                        player.performCommand(command3);
                       
                        break;   
                       
                    case BARRIER:
                        if (player.hasPermission("staff.clearinv")) {
                        player.sendMessage(ChatColor.RED + "Inventato Resettato con successo!");
                        String command4 = "clear " + EndControlCommand.obbiettivo.sospettostring;
                        player.performCommand(command4);
                        }
                        break;   
                       
                    default:
                        return;
                    }
                }
                player.closeInventory();
            }
           
           
        }
    
       
        @EventHandler
        public void onCommand(PlayerCommandPreprocessEvent event) {
        String event1 = event.getMessage();
        Player player1 = event.getPlayer();
       
        if (controlloyesno.siono == 1) {
            if (ControlCommand.obbiettivo4.sospettostring == player1) {
                event.setCancelled(true);
                obbiettivo4.sospettostring.sendMessage(ChatColor.RED + "Non puoi eseguire comandi durante un controllo!!");
            }
            }
        }
       
       
        @EventHandler
        public void actOnQuitEvent(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            String playerName = player.getName();
            Location spawn = new Location(Bukkit.getWorld("vanilla"), 231, 70, -172, 0, 0);
            if (controlloyesno.siono == 1) {
            if (player == ControlCommand.obbiettivo4.sospettostring) {
                obbiettivo2.staffstring.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Il player " + playerName + " ha leavvato durante il controllo!!");
            controlloyesno.siono = 0;
            chat.nochat.remove(obbiettivo4.sospettostring);
            player.teleport(spawn);
           
            }
        }
    }
       
        @EventHandler
        public void chatEvent(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            String playerName = player.getName();
            if (controlloyesno.siono == 1) {
            if (player == ControlCommand.obbiettivo4.sospettostring) {
                String messaggio1 = e.getMessage();
                    obbiettivo2.staffstring.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "SOSPETTO » " + ChatColor.GRAY + messaggio1);
                    player.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "SOSPETTO » " + ChatColor.GRAY + messaggio1);
                        e.setCancelled(true);
                }
            }
           
            if (controlloyesno.siono == 1) {
                if (player == ControlCommand.obbiettivo2.staffstring) {
                    String messaggio1 = e.getMessage();
                        obbiettivo4.sospettostring.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "STAFFER » " + ChatColor.GRAY + messaggio1);
                        player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "STAFFER » " + ChatColor.GRAY + messaggio1);
                            e.setCancelled(true);
                    }
                }
           
           
        }
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
        for (Player player : chat.nochat) {
        event.getRecipients().remove(player);
            }
        }
       
       
    
       
         @Override
         public void onDisable() {
             System.out.println("Il plugin e' stato abilitato correttamente");
         }
        
        
         public void separateMessageOpen() {
            System.out.println(" -------------------------------------------");
            System.out.println("|                                           |");
         }
        
         public void separateMessageClose() {
                System.out.println("|                                           |");
                System.out.println(" -------------------------------------------");
             }
    }
    
    And that's the command class:


    Code:
    package com.dayactor69.plugin;
    
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffectType;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import java.awt.Event;
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.HashMap;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.command.CommandExecutor;
    
    
    
    public class ControlCommand implements CommandExecutor {
       
        public static class controlloyesno {
            public static double siono = 0;
        }
       
        public static class obbiettivo2 {
            public static String staffplayer;
            public static Player staffstring;
        }
        public static class obbiettivo3 {
            public static String sospettoplayer;
        }
        public static class obbiettivo4 {
            public static Player sospettostring;
        }
       
        public static class chat {
            static ArrayList<Player> nochat = new ArrayList<Player>();
        }
       
           
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
           
           
            Player staff = (Player) sender;
            Player sospetto = Bukkit.getPlayer(args[0]);
            String staff1 = staff.getName();
            String sospetto1 = sospetto.getName();
            obbiettivo2.staffplayer = staff1;
            obbiettivo2.staffstring = staff;
            obbiettivo3.sospettoplayer = sospetto1;
            obbiettivo4.sospettostring = sospetto;
           
            if(staff.hasPermission("staff.controllo")) {
           
            TextComponent message = new TextComponent("[APRI LA GUI DEL CONTROLLO]");
            message.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
            message.setBold(true);
            message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/finecontrollo " + sospetto1) );
           
            if (args.length > 0) {
                if (args[0].equals("") == true) {
                    staff.sendMessage("[Errore] Inserisci il nome di un player dopo il comando!");
                } else {
                   
               
               
                if (controlloyesno.siono == 1) {
                   
                }else {
                    controlloyesno.siono++;
            }
               
               
               
    
                chat.nochat.add(obbiettivo4.sospettostring);
                Location loc = new Location(Bukkit.getWorld("vanilla"), 256, 30, -96, 0, 0);
                staff.teleport(loc);
                staff.sendMessage(" ");
                staff.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "» CONTROLLO " + ChatColor.RED + obbiettivo2.staffplayer + ChatColor.GRAY + " sta controllando " + ChatColor.RED + obbiettivo3.sospettoplayer);
                staff.sendMessage(" ");
                staff.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "» CONTROLLO " + ChatColor.GRAY + "Caricamento...");
                staff.sendMessage("");
                staff.sendMessage("");
                staff.spigot().sendMessage(message);
               
                sospetto.teleport(loc);
                sospetto.sendMessage(ChatColor.RED + "Dai il tuo codice anydesk allo staffer che ti sta controllando! Non sloggare o verrai bannato!");
               
                }
    
                } else
                    staff.sendMessage("[Errore] Inserisci il nome di un player dopo il comando!");
            }
       
               
           
            return false;
           
           
        }
    
    }
    
    Can someone tell me how can i make this work for more players at same time?
     
    Last edited by a moderator: Jun 29, 2021
  2. Offline

    c7dev

    Can you describe the issue in more detail? Does it send the wrong message when someone else has used the command? How much of the code in the command class is executed if the second player tries to run it?

    If you're still not sure what is causing it, formatting the code might help. For example, you don't need to have each variable in its own class, and you shouldn't use static classes for that purpose. Properly indenting the code can also help improve readability, which can make it easier to find an error. This might show an "if" statement that could be preventing the gui code from running because of a variable that is only set back to normal once the first player closes the gui.
     
  3. Offline

    dayactor69

    When the command is executed, some variables change like from 0 to 1. For a player to execute the command again need that variable to be 0. It's not a console error but i think that some variables like the name of the players need to be stored in a array, cause when the first player run it, is all executed with that name
     
  4. Offline

    c7dev

    It seems that the "controlloyesno.siono" variable starts at 0 and then goes to 1 and stays there as soon as the command is run. It would be a good idea to store it in an array -- an easy way of doing this is to have an ArrayList of UUIDs, and to get a 0 or a 1, you would do List<UUID>#contains(Player#getUniqueId()), meaning if a player is in the list, controlloyesno.siono = 1, and if not, it is 0.

    If that's still not working, can you repost the code with the proper identation, it's hard to see where one set of brackets starts and ends, which might be the reason the bottom part of the code doesn't execute when siono equals 1.
     
Thread Status:
Not open for further replies.

Share This Page