Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

java - Get a Player from a Iterator Bukkit 1.16.5

I have a problem that today is wearing me down deeply, I created a plugin that adds player to an arraylist when clicking in an inventory (I did so unfortunately because I'm not very good), the game is only one and contains 2 player, I can't make sure that if a player exits, through a method, he can give victory to the other Player, I tried a lot of methods, this is the last one (In this code below I try to put a method where if I remotely understood it should send a victory message to the player, which obviously it does not and then execute LobbyJoin, which is a method that takes you to the lobby):

private void makeWinSupport(Player winner, Player loser) {
    if (playerInGame.contains(winner) && playerInGame.contains(loser)) {
        winner.sendMessage(plugin.cc("&6(!) Hai vinto!"));
        LobbyJoin(loser);
        LobbyJoin(winner);
        playerInGame.remove(winner);
        playerInGame.remove(loser);
    }
}

public void makePlayerWin(Player loser) {
    Iterator<Player> i = playerInGame.iterator();
        System.out.println(1);
        if(i.hasNext() && i == loser) {
            makeWinSupport(i.next(), loser);

        } else {
            makeWinSupport(playerInGame.get(0), loser);
        }

    @EventHandler
public void onQuit(PlayerQuitEvent e) {
    e.setQuitMessage("");
    Player p = e.getPlayer();
    if(playerInGame.contains(p)) {
        makePlayerWin(p);
        }
    }
}
question from:https://stackoverflow.com/questions/65835529/get-a-player-from-a-iterator-bukkit-1-16-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use PlayerQuitEvent to remove players from your plugin when they leave. I made this example assuming you have a list called yourPlayerList which stores the 2 competing players, but you can modify it to work with more players.

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
    if(yourPlayerList.remove(event.getPlayer())){//only true if the player was removed from the queue, which only happens if it was in the queue originally
        Player loser=event.getPlayer();//the loser
        Player winner=yourPlayerList.get(0);//the other player is still in the queue
        makeWinSupport(winner,loser);
    }
}

Don't forget to register the event in the event manager.

EDIT: If you want to remove the player from the list and only end the game if there is only one player left, you can do something like this:

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
    if(yourPlayerList.remove(event.getPlayer())&&yourPlayerList.size()==1){//removes the player and checks the amount of active players
        Player winner=yourPlayerList.get(0);
        //here you can call a method which accepts only the winning player. If you have 4 players, there is no reason to include a 'loser' player.
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...