Solved Player join to arena or CreateArena

Discussion in 'Plugin Development' started by Invokers, Aug 19, 2022.

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

    Invokers

    hi, I was doing the method of joining the arena, but stopped at the moment of adding a player to the arena, that is, the arena is being checked for the status and the maximum number of players. and then I stopped, I don't understand what to do next. looked at more popular plugins, but I didn't understand there either.

    Code join (open)

    Code:
        public  static void JoinGame(Player player, String name) {
            if (Utils.ArenaDatas.get(name).getState().equals(GameState.Game)|| Utils.ArenaDatas.get(name).getState().equals(GameState.Reset))
            {
                Utils.Message("Арена запускается", player);
            } else {
                if (Utils.ArenaDatas.get(name).getMaxPlayer() == size) {
                    Utils.Message("Арена полная", player);
                } else {
                    System.out.print(3);
                  //  ScoreBoards scoreboardManager = new ScoreBoards(player);
                  //  scoreboardManager.ScroreBoardLobby();
                }
            }
        }


    Maybe you should add a player hash card to add a player there. More precisely, like this
    Code:
    HashMap <Player String> players = new HashMap<>();
    Then check the key for the value. at the start of the arena, teleport to locations

    At the expense of the teams, I think to do so
    Team (open)

    Code:
    public class Teams {
        private List<String> members = new ArrayList<>();
    
        private ArenaManager arena;
    
        private Location spawn;
    
        private Location leashSpawn;
    
        private String name;
    
        private int size;
    
        public Teams(String paramString, ArenaManager paramArena, Location paramLocation1, Location paramLocation2, int paramInt) {
            this.name = paramString;
            this.arena = paramArena;
            this.spawn = paramLocation1;
            this.size = paramInt;
            this.leashSpawn = paramLocation2;
            ColorControl.getInstance().getTeamManager().addTeam(this);
        }
    
        public List<String> getMembers() {
            return this.members;
        }
    
        public void setMembers(List<String> paramList) {
            this.members = paramList;
        }
    
        public void addMembers(String paramString) {
            this.members.add(paramString);
        }
    
        public void cleanMembers() {
            this.members.clear();
        }
    
        public ArenaManager getArena() {
            return this.arena;
        }
    
        public void setArena(ArenaManager paramArena) {
            this.arena = paramArena;
        }
    
        public Location getSpawn() {
            return this.spawn;
        }
    
        public void setSpawn(Location paramLocation) {
            this.spawn = paramLocation;
        }
    
        public Location getBalloonSpawn() {
            return this.leashSpawn;
        }
    
        public void setBalloonSpawn(Location paramLocation) {
            this.leashSpawn = paramLocation;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String paramString) {
            this.name = paramString;
        }
    
        public int getSize() {
            return this.size;
        }
    
        public void setSize(int paramInt) {
            this.size = paramInt;
        }
    
    
    
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Invokers The "Solved" tag means that you don't need help anymore.
    Don't add it when you make a thread.
     
  3. Offline

    Invokers

  4. Offline

    xpaintall

    use UUID instead of Player
     
  5. Offline

    Invokers

    An internal error occurred while attempting to perform this command

    Game (open)

    Code:
    public class Game {
    
        private String name;
        private GameState state;
    
        public Game(String name) {
            this.setName(name);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public boolean playerJoins(Player p) {
            Player player = p.getPlayer();
            if (this.state == GameState.Game || this.state == GameState.Reset) {
                Utils.Message("No bro", player);
            } else {
                ColorControl.getGameManager().addGamePlayer(player, this);
                Utils.Message("Yes bro", player);
            }
            return true;
        }
    
    
    }

    GameManager (open)

    Code:
    public class GameManager {
    
        private Map<Player, Game> gamePlayer;
        private ArrayList<Game> games;
    
        public GameManager() {
            this.games = new ArrayList<>();
            this.gamePlayer = new HashMap<>();
        }
    
        public Game addGame(String name) {
            Game existing = getGame(name);
            if (existing != null)
                return null;
            Game newGame = new Game(name);
            this.games.add(newGame);
            return newGame;
        }
    
        public void addGamePlayer(Player player, Game game) {
            if (this.gamePlayer.containsKey(player))
                this.gamePlayer.remove(player);
            this.gamePlayer.put(player, game);
        }
    
        public Game getGame(String name) {
            for (Game game : this.games) {
                if (game.getName().equals(name))
                    return game;
            }
            return null;
        }
    
        public int getGamePlayerAmount() {
            return this.gamePlayer.size();
        }
    
        public ArrayList<Game> getGames() {
            return this.games;
        }
    
    
    
    
    }
    

    cCommand 94 line (open)

    Code:
                   if (args[0].equalsIgnoreCase("joinArena")) {
                        String str = args[1];
                        if (ColorControl.getInstance() == null) {
                            Utils.Message("Error", player);
                        } else {
                           Game game = ColorControl.getGameManager().addGame(str);
                            game.playerJoins(player);
                        }
    
    
                    }
     

    Attached Files:

  6. Offline

    bennie3211

    Can you please give us the full class, becuase 'cCommand 94 line' spoiler won't tell us what line is line 94 exactly. For so far it can be that getGameManager() returns null or the variable game is null if the 'str' is not known as an arena. And for further knowledge, its handy to learn to read the stack trace yourself to trace the error back, helps with developing which is explained here https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/.
     
  7. Offline

    Invokers

    Code:
    public class CcCommands implements CommandExecutor {
    
        private ColorControl plugin;
    
        public CcCommands(ColorControl plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = null;
            if (sender instanceof Player)
            {
                player = (Player) sender;
            }
    
            if (command.getName().equalsIgnoreCase("colorcontrol")) {
                if (args.length == 0) {
                    Utils.Message("/colorcontrol createArena [name] [min - player] [max - player] [name lobby] [power game]", player);
                }
                if (args.length >= 1) {
                    if (args[0].equalsIgnoreCase("help")) {
                        Utils.Message("/colorcontrol createArena [name] [min - player] [max - player] [name lobby] [power game]", player);
                    }
                    if (args[0].equalsIgnoreCase("setTeamSpawn")) {
                        if (Utils.ArenaDatas.containsKey(String.valueOf(args[1]))) {
                            Utils.Message("Создайте Арену для начала", player);
                            return true;
                        }
                        if (args.length < 2) {
                            Utils.Message(" /cc setTeamSpawn <ArenaName> <teamName>", player);
                            return true;
                        }
                        String str = args[2];
                        if (!TeamColor.containsTeam(str)) {
                            Utils.Message("&aТакой команды " + str + "не существует", player);
                            return true;
                        }
    
                    }
                    if(args[0].equalsIgnoreCase("CreateArena")) {
                        if (player.hasPermission("*")||player.isOp()) {
                            if (EventLocation.poss1 == null|| EventLocation.poss2 == null) {
                                Utils.Message("Выделите регион /colorcontrol ItemArena", player);
                            } else {
                                int minPlayer = Integer.parseInt(args[2]);
                                int maxPlayer = Integer.parseInt(args[3]);
                                int teamamount = Integer.parseInt(args[5]);
                                String namepower = "";
                                if (args[4].equalsIgnoreCase("solo")) {
                                    namepower = String.valueOf(GameState.Solo);
                                } else if (args[4].equalsIgnoreCase("team")){
                                    namepower = String.valueOf(GameState.Team);
                                }
    
                                Utils.ArenaDatas.put(args[1], new ArenaManager(args[1], EventLocation.poss1, EventLocation.poss2, minPlayer, maxPlayer, GameState.Lobby, GameState.valueOf(namepower), teamamount, player.getWorld()));
                                Utils.Message("Арена успешно создана", player);
                            }
                        } else {
                            Utils.Message("У вас нет прав", player);
                        }
                    }
                    if (args[0].equalsIgnoreCase("ItemArena")) {
                        player.getInventory().addItem(ColorControlItem.ItemArena());
                    }
                    if (args[0].equalsIgnoreCase("Start")) {
                    }
                    if (args[0].equalsIgnoreCase("rtp")) {
                        Game game = ColorControl.getInstance().getGameManager().addGame("Arena1");
                    }
                    if (args[0].equalsIgnoreCase("joinArena")) {
                        String str = args[1];
                        if (ColorControl.getInstance() == null) {
                            Utils.Message("Error", player);
                        } else {
                           Game game = ColorControl.getGameManager().addGame(str); // line 94
                            game.playerJoins(player);
                        }
    
    
                    }
                    if (args[0].equalsIgnoreCase("leave")) {
                        String name =  new GameManager().getGame(args[1]).getName();
                        Utils.Message(name, player);
    
                    }
                    if (args[0].equalsIgnoreCase("profile")) {
                        Utils.Message("Money "+Utils.PlayerDatas.get(player.getUniqueId()).getMoney(), player);
                        Utils.PlayerDatas.get(player.getUniqueId()).addMoney(1);
                    }
                }
            }
            return false;
        }
    }
     
    Last edited: Aug 22, 2022
  8. Offline

    timtower Administrator Administrator Moderator

    @Invokers And what is line 94? As you removed the includes.
     
  9. Offline

    Invokers

    Update
     
  10. Offline

    bennie3211

    This class has only 95 lines, so I think there is something wrong, since line 94 can't throw an NPE. And I suggest to change your player check in the beginning of the command, check if it isn't a player, and send a simple message to the command sender and return, now it will give NPE's if the command comes from console!
     
  11. Offline

    Invokers

    Sorry I threw it off without imports

    It didn't help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 22, 2022
  12. Offline

    timtower Administrator Administrator Moderator

    Then show what line is line 94
     
  13. Offline

    Invokers

    Cccomand.java (open)

    Code:
    package ColorControl.command;
    
    
    import ColorControl.Arena.*;
    import ColorControl.ColorControl;
    import ColorControl.GameState;
    import ColorControl.Item.ColorControlItem;
    import ColorControl.Utils;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.io.IOException;
    import java.util.Set;
    
    
    public class CcCommands implements CommandExecutor {
    
        private ColorControl plugin;
    
        public CcCommands(ColorControl plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = null;
            if (sender instanceof Player)
            {
                player = (Player) sender;
            }
    
            if (command.getName().equalsIgnoreCase("colorcontrol")) {
                if (args.length == 0) {
                    Utils.Message("/colorcontrol createArena [name] [min - player] [max - player] [name lobby] [power game]", player);
                }
                if (args.length >= 1) {
                    if (args[0].equalsIgnoreCase("help")) {
                        Utils.Message("/colorcontrol createArena [name] [min - player] [max - player] [name lobby] [power game]", player);
                    }
                    if (args[0].equalsIgnoreCase("setTeamSpawn")) {
                        if (!Utils.ArenaDatas.containsKey(args[1])) {
                            Utils.Message("Создайте Арену для начала"+Utils.ArenaDatas.containsKey(args[1]), player);
                            return true;
                        }
                        if (args.length < 2) {
                            Utils.Message(" /cc setTeamSpawn <ArenaName> <teamName>", player);
                            return true;
                        }
                        String str = args[2];
                        if (!TeamColor.containsTeam(str)) {
                            Utils.Message("&aТакой команды " + str + " не существует", player);
                            return true;
                        } else {
                            if (Utils.Arena.get(args[1]+".team") == null) {
                                Utils.Arena.set(args[1]+".team.", " ");
                            } else {
                                Set<String> strs = Utils.Arena.getConfigurationSection(args[1]+".team").getKeys(false);
                                if (strs.size() >= Utils.ArenaDatas.get(args[1]).getTeamsAmount()) {
                                    Utils.Message("Не возможно создать точку спавна для данной арены", player);
                                } else {
                                    double x = player.getLocation().getX();
                                    double y = player.getLocation().getY();
                                    double z = player.getLocation().getZ();
                                    Utils.Arena.set(args[1]+".team."+str+".x", x);
                                    Utils.Arena.set(args[1]+".team."+str+".y", y);
                                    Utils.Arena.set(args[1]+".team."+str+".z", z);
    
                                    Utils.Message("Спавн команды "+str+" установлен!", player);
                                }
                            }
    
    
                            try {
                                Utils.Arena.save(Utils.File_Arena);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                    }
                    if(args[0].equalsIgnoreCase("CreateArena")) {
                        if (player.hasPermission("*")||player.isOp()) {
                            if (EventLocation.poss1 == null|| EventLocation.poss2 == null) {
                                Utils.Message("Выделите регион /colorcontrol ItemArena", player);
                            } else {
                                int minPlayer = Integer.parseInt(args[2]);
                                int maxPlayer = Integer.parseInt(args[3]);
                                int teamamount = Integer.parseInt(args[5]);
                                String namepower = "";
                                if (args[4].equalsIgnoreCase("solo")) {
                                    namepower = String.valueOf(GameState.Solo);
                                } else if (args[4].equalsIgnoreCase("team")){
                                    namepower = String.valueOf(GameState.Team);
                                }
    
                                Utils.ArenaDatas.put(args[1], new ArenaManager(args[1], EventLocation.poss1, EventLocation.poss2, minPlayer, maxPlayer, GameState.Lobby, GameState.valueOf(namepower), teamamount, player.getWorld()));
                                Utils.Message("Арена успешно создана", player);
                            }
                        } else {
                            Utils.Message("У вас нет прав", player);
                        }
                    }
                    if (args[0].equalsIgnoreCase("ItemArena")) {
                        player.getInventory().addItem(ColorControlItem.ItemArena());
                    }
                    if (args[0].equalsIgnoreCase("Start")) {
                    }
                    if (args[0].equalsIgnoreCase("rtp")) {
                        Game game = ColorControl.getInstance().getGameManager().addGame("Arena1");
                    }
                    if (args[0].equalsIgnoreCase("joinArena")) {
                        String str = args[1];
                        Game gm = ColorControl.getGameManager().addGame(args[1]); // line 116 was 94 (Slightly tweaked 1 of the commands)
                    }
                    if (args[0].equalsIgnoreCase("leave")) {
                        String name =  new GameManager().getGame(args[1]).getName();
                        Utils.Message(name, player);
    
                    }
                    if (args[0].equalsIgnoreCase("profile")) {
                        Utils.Message("Money "+Utils.PlayerDatas.get(player.getUniqueId()).getMoney(), player);
                        Utils.PlayerDatas.get(player.getUniqueId()).addMoney(1);
                    }
                }
            }
            return false;
        }
    }
    

    GameManager (open)

    Code:
    package ColorControl.Arena;
    
    import ColorControl.ColorControl;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    public class GameManager {
    
        private Map<Player, Game> gamePlayer;
        private ArrayList<Game> games;
    
        public void GameManager() {
            this.games = new ArrayList<>();
            this.gamePlayer = new HashMap<>();
        }
    
        public Game addGame(String name) {
            Game names = getGame(name);
            if (names == null ) {
                Bukkit.broadcastMessage("N "+names);
            }
            Game game = new Game(name);
            this.games.add(game);
            return game;
        }
    
        public Game getGame(String name) {
            for (Game game : this.games) {
                if (game.getName().equals(name))
                    return game;
            }
            return null;
        }
    
    
    
    }
    

    Game (open)

    Code:
    package ColorControl.Arena;
    
    import ColorControl.GameState;
    
    public class Game {
    
        private String name;
        private GameState state;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Game(String name) {
            this.name = name;
            this.state = GameState.Game;
        }
    
    
    
    }
    


    Do you need the Main class code?
     
  14. Offline

    CraftCreeper6

    @Invokers
    Have you changed the code since the error? If so we'll need an updated stack trace.

    Also, you can convert an enum to a string just by simply calling enum#toString() on it.
     
  15. Offline

    timtower Administrator Administrator Moderator

    @CraftCreeper6 Enum#name() might be better. Not sure how toString deals with arguments.
     
    CraftCreeper6 likes this.
  16. Offline

    Invokers

  17. Offline

    timtower Administrator Administrator Moderator

    Then post a new stacktrace
     
  18. Offline

    Invokers

    Deleted the last Command file by mistake. Made a new one


    Code:
    package ColorControl.command;
    
    import ColorControl.Arena.ArenaManager;
    import ColorControl.Arena.EventLocation;
    import ColorControl.Arena.Game;
    import ColorControl.ColorControl;
    import ColorControl.Item.ColorControlItem;
    import ColorControl.Utils;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import ColorControl.GameState;
    
    public class ColorControlCommands implements CommandExecutor {
        private ColorControl plugin;
    
        public ColorControlCommands(ColorControl plugin) {
            this.plugin = plugin;
        }
    
        protected ColorControl getPlugin() {
            return this.plugin;
        }
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = null;
            if (sender instanceof  org.bukkit.entity.Player) {
                player = (Player) sender;
            } else {
                sender.sendMessage("Данную команду можно выполнять только из игры");
            }
            if (args.length <1 ) {
                Utils.Message("======[ColorControl Help]======", player);
                Utils.Message("/cc CreateArena", player);
            }
            if (command.getName().equalsIgnoreCase("ColorControl")) {
                if (args[0].equalsIgnoreCase("Wand")) {
                    player.getInventory().addItem(ColorControlItem.ItemArena());
                    Utils.Message("Вам дали топор для выделения точек арены", player);
                    return true;
                }
                if (args[0].equalsIgnoreCase("CreateArena")) {
                    if (EventLocation.poss1 == null || EventLocation.poss2 == null) {
                        Utils.Message("Вы не установили точки арены //cc Wand", player);
                        return true;
                    }
                    if (args.length < 5) {
                        Utils.Message("/cc CreateArena Name minPlayer MaxPlayer GameType maxTeamSize", player);
                        return true;
                    }
                    int min = Integer.parseInt(args[2]);
                    int max = Integer.parseInt(args[3]);
                    String power = "";
                    int teamsize = Integer.parseInt(args[5]);
                    if (min == 0 || min < 1) {
                        Utils.Message("Минимально количество не может быть 0 и меньше 1", player);
                    }
                    if (max == min || max < 1) {
                        Utils.Message("Максимальное количество количество не может быть 0 и меньше 1", player);
                    }
                    if (args[4].equalsIgnoreCase("Solo")) {
                        power = GameState.Solo.name();
                    } else {
                        power = GameState.Team.name();
                    }
                    Game game = ColorControl.getInstance().getGameManager().addGame(String.valueOf(args[1])); //Line 69
                    Utils.ArenaDatas.put(args[1], new ArenaManager(args[1], EventLocation.poss1, EventLocation.poss2, min, max, GameState.Lobby, GameState.valueOf(power), teamsize, player.getWorld()));
                    Utils.Message("Арена создана", player);
                }
            }
    
            return false;
        }
    }
    new code GameManager
    Code:
    public class GameManager {
    
        private Map<Player, Game> gamePlayer;
        private ArrayList<Game> games;
    
        public GameManager() {
            this.games = new ArrayList<>();
            this.gamePlayer = new HashMap<>();
        }
    
        public Game addGame(String name) {
            Game existing = getGame(name);
            if (existing != null) {
                return null;
            }
            Game newGame = new Game(name);
            this.games.add(newGame);
            return newGame;
        }
    
        public void addGamePlayer(Player player, Game game) {
            if (this.gamePlayer.containsKey(player))
                this.gamePlayer.remove(player);
            this.gamePlayer.put(player, game);
        }
    
        public Game getGame(String name) {
            for (Game game : this.games) {
                if (game.getName().equals(name))
                    return game;
            }
            return null;
        }
    
    
    
    }
     

    Attached Files:

    Last edited: Aug 23, 2022
  19. Offline

    CraftCreeper6

    Any of the following:
    Your instance of ColorControl is null
    Your getGameManager call is returning null

    Also, args[1] is already a string so you don't have to do String#valueOf on it.
     
  20. Offline

    Invokers

    Thank you all for your help, but the Russians have such a saying "The one who is looking for will always find" I found a solution on the forum, but I would like to ask if I will create similar classes as Gamemanager, then prescribe for everyone like this?

    I came across a solution to the problem like this, I checked getGameManager by output to the console, it was there that I discovered that the hetero is null and actually started Googling further

    Code:
        private static GameManager gameManager = new GameManager();
    https://bukkit.org/threads/not-sure-why-my-getinstance-returns-null.431230/
     
  21. Offline

    CraftCreeper6

    Don't quite understand what you're asking. Is this solved?
     
  22. Offline

    Invokers

Thread Status:
Not open for further replies.

Share This Page