1.15.2 Trying to find a way to watch for player deaths from inside command function?

Discussion in 'Spigot Plugin Development' started by BenjaminLamont, Feb 16, 2020.

  1. Hi, I've started trying to write my own simple plugin as a starting project that would randomly swap players' locations every couple of minutes and then watch for environmental deaths for a short period after, logging the death to the score of the player originally at that location (before the random teleport) on a scoreboard object. I've currently got the plugin set up to use a command to start the teleports and the scoreboard, which is all working fine, however, I'm struggling to work out how to integrate a way to track player deaths into this? I think it might be something to do with the fact that I've got everything inside the onCommand function, but I'm not quite sure how else I might go about it? Sorry for the noobish question, I'm generally a python/nodejs programmer and I haven't done Java in a while so I'm a bit rusty.

    Command .java file (excluding imports)
    Code (Java):
    public class StartGame implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
            List<Player> onlinePlayers = new ArrayList<Player>();
            Bukkit.broadcastMessage("Starting a new game of Random Swap PvPvE.");
            long lastSwap = System.currentTimeMillis();

            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective obj = board.registerNewObjective("PlayerKills", "dummy", "Random Swaps PvPvE");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);

            for (Player online : Bukkit.getOnlinePlayers()) {
                Score playerElims = obj.getScore(online.getDisplayName());
                playerElims.setScore(0);
            }

            for (Player online : Bukkit.getOnlinePlayers()) {
                online.setScoreboard(board);
            }



            Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginCommand("startgame").getPlugin() ,new Runnable() {
                @Override
                public void run() {
                    onlinePlayers.clear();
                    long lastSwap = System.currentTimeMillis();
                    //Score playerElims = obj.getScore(online.getDisplayName());
                    //playerElims.setScore(playerElims.getScore()+1);
                    //online.sendMessage(Integer.toString(playerElims.getScore()));
                    onlinePlayers.addAll(Bukkit.getOnlinePlayers());
                    Collections.shuffle(onlinePlayers);
                    Location tempLocation = null;
                    if(onlinePlayers.size() > 1) {
                        Bukkit.broadcastMessage("Shuffling players.");
                        for (int i = 0; i < onlinePlayers.size(); i++) {
                            if (i == 0) {
                                tempLocation = onlinePlayers.get(i).getLocation();
                            }
                            if (i == onlinePlayers.size() - 1) {
                                onlinePlayers.get(i).getPlayer().teleport(tempLocation);
                            }else{
                                onlinePlayers.get(i).getPlayer().teleport(onlinePlayers.get(i+1).getLocation());
                            }
                        }
                    }
                }
            }, 100, 600);



            return true;
        }
    }
     
     
  2. Assuming this is for a minigame a hashmap/list containing the players,wich will be deleted and created by each end of game,from there you grab the instance into your event(the class holding your PlayerDeathEvent) and simply check if the player is in that list/hashmap before doing anything..