1v1 System HELP

Discussion in 'Plugin Development' started by xCalib0r, May 31, 2014.

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

    xTigerRebornx

    xCalib0r Use an Entry to loop through the map, then check if either Entry#getKey() or Entry#getValue() is equal to the UUID that died. If you don't know how to loop through a Map, go look it up. Its not to hard to find out.
     
  2. Offline

    xCalib0r

    xTigerRebornx No I know how to loop but not how to get BOTH of the players like how would I check if player died give it to the other guy inn the hashmap. This is what I have so far
    Code:
    for (Map.Entry<UUID, UUID> fighters :  fighting.entrySet()) {
     
    }
    
     
  3. Offline

    xTigerRebornx

    xCalib0r From your fighters variable, you can get both the Key and Value. Check if either of them are the UUID, and if they are, store it in some sort of temporary storage (Like some variables to hold the Entry and the winning UUID, or a Map with the Entry as the Key and the winner UUID as the value), then after you are looping through, use that temporary storage to remove the Entry from your original Map and then reward the winner.
     
  4. Offline

    xCalib0r

    So I kindna got what you're saying then I got confused, this is kinda what I got
    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
    for (Map.Entry<UUID, UUID> fighters :  fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    // put him in a winner list
    } 
    if (fighters.getValue() == p.getUniqueId()) {
    // put him in a winner list
    }
    }
    }
    
     
  5. Offline

    xTigerRebornx

    xCalib0r Something like that, I'd use a chain of ifs/else-ifs
    Something like:
    Code:
    if(fighters.getKey() == p.getUniqueId(){
      // Loser because they left, Entry#getValue() is winner
    } else if(fighters.getValue() == p.getUniqueId(){
      // Loser because they left, Entry#getKey() would be winner
    } else {
      continue; // Let the for loop continue
    }
     
  6. Offline

    xCalib0r

    xTigerRebornx Wait I didn't get the commenting on that, can you explain it again?
     
  7. Offline

    xTigerRebornx

    xCalib0r Okay, so since you are using the Player who quit's UUID, the winner will be the opposite of what you checked for.
    If the person who quit is the Key, the winner would be the Value since the 'Key' quit the game.
    If the person who quit is the Value, the winner is the Key since the 'Value' quit
    If they are neither, continue the for loop since you still haven't found the Entry for the person who quit.
     
  8. Offline

    xCalib0r

    xTigerRebornx alright so I would do something like this??

    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    looser.add(fighters.getValue());
    } else if (fighters.getValue() == p.getUniqueId()) {
    looser.add(fighters.getKey());
    } else {
    continue; // Let the for loop continue
    }
    }
    if (!looser.contains(p.getUniqueId())) {
    makewinner(p);
    }
    }
    
     
  9. Offline

    xTigerRebornx

    xCalib0r It'd be smarter to just temporary store the winner in a local-variable, then just get the winner's Player object through the stored UUID.
    Something like:
    Code:
    UUID winner;
     
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
      winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
      winner = fighters.getKey();
    } else {
      continue; // Let the for loop continue
    }
    }
    
    Also, you should have a check to see if the Player who quit is actually in a 1v1.
     
  10. Offline

    xCalib0r

    xTigerRebornx ok so once I do that how do I DO SOMETHING to the winner

    like after the for loop I state if (p.getUniqueId().equals(winner)) {do something to winnner}??
     
  11. Offline

    xTigerRebornx

    xCalib0r Well, you have your method that handles them winning, all you need is a simple way to get an online player from a UUID, which is simply Bukkit#getPlayer(UUID uuid), and then you have the winner in the form of a Player object
     
  12. Offline

    xTrollxDudex

    More spoon feeding?
     
  13. Offline

    xTigerRebornx

    xCalib0r Well, you simply do what you want to happen when the Player wins, since you already have the Player that won, its up to you to do what happens when the Player wins

    xTrollxDudex Its his choice if he chooses to copy-paste my pseudo-code, if he doesn't take the time to actually understand what its doing, and he comes back asking for help, he will simply be told to go and learn Java, its his choice on whether or not he wants to actually learn or just sit there with his pile of statics and general non-OOP code that I have told him to scrap and replace with an arena system multiple times.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  14. Offline

    xCalib0r

    xTigerRebornx I said earlier EVERYTHING I did was just a template and I would go back and organize and RE-DO this entire system and make it into an arena system, I stated that I first wanted to UNDERSTAND how to do this before I go ahead and yes I am actually learning and not copy and pasting. Ok well right now it seems to be working but everytime a new player joines the server, the winner keeps getting the win, I tried checking if the player was fighting or not but I guess I'm doing it wrong. This is what I'm doing right now

    Code:
    if (fighting.containsKey(p.getUniqueId())) {
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
    winner = fighters.getKey();
    } else {
    continue;
    }
    }
    }
     
    Player winner = Bukkit.getPlayer(this.winner);
     
    if (winner != null) {
                              // do winning stuff.
    }
    
    Yes I erased my checking if the player was fighting or not code because it wasn't working.
     
  15. Offline

    xTigerRebornx

  16. Offline

    xCalib0r

    xTigerRebornx Sent you the old code , this is the code I meant

    Code:
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
    winner = fighters.getKey();
    } else {
    continue;
    }
    }
     
    Player winner = Bukkit.getPlayer(this.winner);
     
    if (winner != null) {
    //do stuff
    }
    
    I don't see anything wrong with it.
     
  17. Offline

    xTigerRebornx

    xCalib0r When I say the 'winner' variable, I am referring to the one that you've decided to put outside the method (for whatever reason), leaving it outside the method will result in it carrying over to the end of the next match, and can mess up things. Make the UUID a local variable.


    You also said that it gives that Player a win when Players join, can you provide the code that is ran on join?
     
  18. Offline

    xCalib0r

    xTigerRebornx O i meant when a new player joins AND leaves.

    xTigerRebornx ahhhhhhh wow I can't believe I missed That. Ok but what do I initialize the winner as? Not null of course.

    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
     
    UUID winner;
     
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
    winner = fighters.getKey();
    } else {
    continue;
    }
    }
     
    Player wonplayer = Bukkit.getPlayer(winner);
     
    if (wonplayer != null) {
    for (Player others : Bukkit.getOnlinePlayers()) {
    wonplayer.showPlayer(others);
    }
     
    if (fighting.containsKey(wonplayer.getUniqueId())) {
    fighting.remove(wonplayer.getUniqueId());
    }
     
    StatsManager.getInstance().add1v1Wins(wonplayer, 1);
    wonplayer.playSound(wonplayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(wonplayer);
    }
    
    winner needs to be initialized but of course it can't be null.

    xTigerRebornx sooo??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  19. Offline

    xTigerRebornx

    xCalib0r According to earlier posts, you said you understood the pseudo-code I provided. This is clearly not the case.
    Look at what the for loop does, think about it, then if you are still confused, then come and ask.
     
  20. Offline

    xCalib0r

    xTigerRebornx Ok will do.

    xTigerRebornx Well i can't believe i asked something like that, might have been drunk, one thing though after a player wins fighting doesn't get remove because it still says cannot use commands in 1v1.

    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
     
    UUID winner;
     
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
    winner = fighters.getKey();
    } else {
    continue;
    }
     
    Player wonplayer = Bukkit.getPlayer(winner);
     
    if (wonplayer != null) {
     
    for (Player others : Bukkit.getOnlinePlayers()) {
    wonplayer.showPlayer(others);
    }
     
    if (pregame.containsKey(wonplayer.getUniqueId())) {
    pregame.remove(wonplayer.getUniqueId());
    }
     
    if (fighting.containsKey(wonplayer.getUniqueId())) {
    fighting.remove(wonplayer.getUniqueId());
    }
     
    if (pregame.containsKey(p.getUniqueId())) {
    pregame.remove(p.getUniqueId());
    }
     
    if (fighting.containsKey(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    }
     
    StatsManager.getInstance().add1v1Wins(wonplayer, 1);
    wonplayer.playSound(wonplayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(wonplayer);
     
    }
    break;
    }
     
    for (Map.Entry<UUID, UUID> pregamers : pregame.entrySet()) {
    if (pregamers.getKey() == p.getUniqueId()) {
    winner = pregamers.getValue();
    } else if (pregamers.getValue() == p.getUniqueId()) {
    winner = pregamers.getKey();
    } else {
    continue;
    }
     
    Player wonplayer = Bukkit.getPlayer(winner);
     
    if (wonplayer != null) {
    for (Player others : Bukkit.getOnlinePlayers()) {
    wonplayer.showPlayer(others);
    }
     
    if (pregame.containsKey(wonplayer.getUniqueId())) {
    pregame.remove(wonplayer.getUniqueId());
    }
     
    if (fighting.containsKey(wonplayer.getUniqueId())) {
    fighting.remove(wonplayer.getUniqueId());
    }
     
    if (pregame.containsKey(p.getUniqueId())) {
    pregame.remove(p.getUniqueId());
    }
     
    if (fighting.containsKey(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    }
     
    StatsManager.getInstance().add1v1Wins(wonplayer, 1);
    wonplayer.playSound(wonplayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(wonplayer);
     
    }
    break;
    }
     
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  21. Offline

    xCalib0r

  22. Offline

    xCalib0r

  23. Offline

    RingOfStorms

    no
     
  24. Offline

    xCalib0r

    This is the problem, after the player wins fighting doesn't get remove because it still says cannot use commands in 1v1.
    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
     
    UUID winner;
     
    for (Map.Entry<UUID, UUID> fighters : fighting.entrySet()) {
    if (fighters.getKey() == p.getUniqueId()) {
    winner = fighters.getValue();
    } else if (fighters.getValue() == p.getUniqueId()) {
    winner = fighters.getKey();
    } else {
    continue;
    }
     
    Player wonplayer = Bukkit.getPlayer(winner);
     
    if (wonplayer != null) {
     
    for (Player others : Bukkit.getOnlinePlayers()) {
    wonplayer.showPlayer(others);
    }
     
    if (pregame.containsKey(wonplayer.getUniqueId())) {
    pregame.remove(wonplayer.getUniqueId());
    }
     
    if (fighting.containsKey(wonplayer.getUniqueId())) {
    fighting.remove(wonplayer.getUniqueId());
    }
     
    if (pregame.containsKey(p.getUniqueId())) {
    pregame.remove(p.getUniqueId());
    }
     
    if (fighting.containsKey(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    }
     
    StatsManager.getInstance().add1v1Wins(wonplayer, 1);
    wonplayer.playSound(wonplayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(wonplayer);
     
    }
    break;
    }
     
    for (Map.Entry<UUID, UUID> pregamers : pregame.entrySet()) {
    if (pregamers.getKey() == p.getUniqueId()) {
    winner = pregamers.getValue();
    } else if (pregamers.getValue() == p.getUniqueId()) {
    winner = pregamers.getKey();
    } else {
    continue;
    }
     
    Player wonplayer = Bukkit.getPlayer(winner);
     
    if (wonplayer != null) {
    for (Player others : Bukkit.getOnlinePlayers()) {
    wonplayer.showPlayer(others);
    }
     
    if (pregame.containsKey(wonplayer.getUniqueId())) {
    pregame.remove(wonplayer.getUniqueId());
    }
     
    if (fighting.containsKey(wonplayer.getUniqueId())) {
    fighting.remove(wonplayer.getUniqueId());
    }
     
    if (pregame.containsKey(p.getUniqueId())) {
    pregame.remove(p.getUniqueId());
    }
     
    if (fighting.containsKey(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    }
     
    StatsManager.getInstance().add1v1Wins(wonplayer, 1);
    wonplayer.playSound(wonplayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(wonplayer);
     
    }
    break;
    }
     
    
     
  25. Offline

    RingOfStorms

    From a glance I dont see anything about commands or cancelling or a message. And also, why would you require the player to use a command to leave the 1v1, shouldn't it automatically happen?
     
  26. Offline

    xCalib0r

    RingOfStorms wait wtf are you talking about? There is no command to leeave the 1v1. if you look up to previous talks about this, this is when a player leaves the game when they are fighting so it gives the win to the other guy.
     
  27. Offline

    RingOfStorms

    Ok so you answered your own problem? If they don't have any commands then I don't see why "it still says cannot use commands in 1v1" is even a problem. Your question makes no sense. And the code you have posted is incomplete and makes it impossible to help you. All I can think of from a glance at that code is you're calling setup1v1Player(wonplayer). I have no idea what that function does but based on the name I guess it puts it back into all the maps you just removed it from? I really don't know how to help you without all of your code. And use [ syntax=java ] [/syntax] rather than [ code ] [ /code ] tags.
     
Thread Status:
Not open for further replies.

Share This Page