1v1 System HELP

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

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

    Garris0n

    For one, you really shouldn't use the == operators for that. It will work for now, just because the game returns the same instance every time, but it won't work if you start to save them in a file, for example.

    For another, I hope "dAPI" isn't the name of the class. If it is, read this. Also, "dAPI.getInstance().getUUID(player)" is not in any way shorter than player.getUniqueId().

    Actually, now that I look at it, does that method return a string? If so, that == comparison is probably going to fail. You shouldn't do that, or, if you're going to do that (for no valid reason), you should use .equals(). But you can just store the UUIDs inside the lists.

    And this is why you should also always provide full code, now I'm not really sure if that's the problem or not.
     
  2. Offline

    xCalib0r

    Garris0n I am now using getUniqueId but I tried .equals and that gave me some null pointer exceptionsn and what not.

    Etched like an abstract base class essentially?

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

    Garris0n

    Post your current code.
     
  4. Offline

    xCalib0r

    Garris0n So I got ALOT done and fixed ALOT but one problem is right now how would I check if ONE of the player leaves, then give the win to the other guy, I know how to give the win to the other guy but how do I check if two people are in a fight and one LEAVES? Here's my code so far.

    Code:
    public static HashMap<UUID, UUID> request = new HashMap<UUID, UUID>();
    public static ArrayList<UUID> fighting = new ArrayList<UUID>();
     
    public static ArrayList<UUID> pregame = new ArrayList<UUID>();
    public static ArrayList<UUID> inarena = new ArrayList<UUID>();
     
    public static void setup1v1Player(Player p) {
    inarena.add(p.getUniqueId());
    SpawnProtection.spawnprotected.remove(p.getUniqueId());
    BowDownProtection.bowdownprotection.remove(p.getUniqueId());
    p.teleport(LocationManager.getInstance().get1v1Location());
    dInventory.getInstance().sortoutPlayer(p);
    p.getInventory().setItem(0, dInventory.getInstance().getNormal1v1Rod());
    }
     
    public static void teleportPlayers(Player p1, Player p2) {
    p1.teleport(LocationManager.getInstance().get1v1FirstPostitionLocation());
    p2.teleport(LocationManager.getInstance().get1v1SecondPostitionLocation());
    }
     
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
     
    if (e.getCurrentItem() == null) {
    return;
    }
     
    if (e.getCurrentItem().getType() == dInventory.getInstance().getNormal1v1Rod().getType()) {
    e.setCancelled(true);
    e.getWhoClicked().closeInventory();
    }
    }
     
    @EventHandler
    public void onInteract(PlayerInteractEntityEvent e) {
    final Player p = e.getPlayer();
    if (e.getRightClicked() instanceof Player) {
    final Player target = (Player) e.getRightClicked();
     
    if (!inarena.contains(p.getUniqueId())) {
    return;
    }
     
    if (pregame.contains(target.getUniqueId()) || fighting.contains(target.getUniqueId())) {
    return;
    }
     
    if (p.getItemInHand().getType() == dInventory.getInstance().getNormal1v1Rod().getType()) {
    request.put(p.getUniqueId(), target.getUniqueId());
    if (request.get(target.getUniqueId()) != (p.getUniqueId())) {
    p.sendMessage(ChatColor.GREEN + "You have challenged " + target.getName() + " to a normal 1v1!");
    target.sendMessage(ChatColor.GREEN + p.getName() + " has challenged you to a normal 1v1! Right click him with your rod to accept!");
    } else {
    request.remove(target.getUniqueId());
    teleportPlayers(p, target);
    pregame.add(p.getUniqueId());
    pregame.add(target.getUniqueId());
    dAPI.getInstance().clearPlayerChat(p);
    dAPI.getInstance().clearPlayerChat(target);
    dInventory.getInstance().sortoutPlayer(p);
    dInventory.getInstance().sortoutPlayer(target);
     
    for (Player others : Bukkit.getServer().getOnlinePlayers()) {
    p.hidePlayer(others);
    target.hidePlayer(others);
    p.showPlayer(target);
    target.showPlayer(p);
    }
     
    giveNormalPvP(p);
    giveNormalPvP(target);
     
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(dKits.getInstance(), new Runnable() {
    public void run() {
    pregame.remove(p.getUniqueId());
    pregame.remove(target.getUniqueId());
    fighting.add(p.getUniqueId());
    fighting.add(target.getUniqueId());
    }
    }, 20 * 5);
     
    if (!fighting.contains(p.getUniqueId())) {
    for (Player others : Bukkit.getServer().getOnlinePlayers()) {
    target.showPlayer(others);
    fighting.remove(target.getUniqueId());
    StatsManager.getInstance().add1v1Wins(target, 1);
    target.playSound(target.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    dInventory.getInstance().sortoutPlayer(target);
    target.getInventory().setItem(0, dInventory.getInstance().getNormal1v1Rod());
    }
    }
     
    }
    }
    }
    }
     
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    if (pregame.contains(p.getUniqueId())) {
     
    if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
    return;
    }
     
    e.setTo(e.getFrom());
    }
    }
     
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (fighting.contains(p.getUniqueId())) {
    inarena.remove(p.getUniqueId());
    fighting.remove(p.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    p.showPlayer(others);
    }
    }
    }
    if (e.getEntity().getKiller() instanceof Player) {
    Player killer = (Player) e.getEntity().getKiller();
    if (fighting.contains(killer.getUniqueId())) {
    fighting.remove(killer.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    killer.showPlayer(others);
    }
    StatsManager.getInstance().add1v1Wins(killer, 1);
    killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    dInventory.getInstance().sortoutPlayer(killer);
    killer.getInventory().setItem(0, dInventory.getInstance().getNormal1v1Rod());
    }
    }
    }
     
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e) {
    if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (!inarena.contains(p.getUniqueId())) {
    return;
    }
     
    if (!fighting.contains(e.getDamager().getUniqueId())) {
    e.setCancelled(true);
    }
     
    }
    }
     
    @EventHandler
    public void onCommand(PlayerCommandPreprocessEvent e) {
    Player p = e.getPlayer();
     
    if (pregame.contains(p.getUniqueId()) || fighting.contains(p.getUniqueId())) {
    if (e.getMessage().startsWith("/")) {
    p.sendMessage(ChatColor.RED + "You cannot use commmands while being in a 1v1!");
    e.setCancelled(true);
    }
    }
    }
     
    private void giveNormalPvP(Player p) {
    p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
    dInventory.getInstance().addSoup(p, 8);
    }
    
    bump

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

    xTrollxDudex

    xCalib0r
    4 bumps in a single day!!??? Calm yourself man...

    All you gotta do is add a quit listener, and get the game the player was in.
     
  6. Offline

    xCalib0r

    xTrollxDudex Well no one's answering , and no it's not that easy, look at my code.
     
  7. Offline

    xTrollxDudex

    xCalib0r
    That doesn't mean you can bump all you want...
     
  8. Offline

    xCalib0r

    xTrollxDudex Well If you see each bump was after like 7 hours

    @xTrollxDudex Still sorry about that, can you please help?

    [edit by JaguarJo: merged posts. Don't double post, use the edit option instead. Also, bumping is only allowed once per 24 hours. I have removed some of your spam bumps.]
     
  9. Offline

    xTrollxDudex

    xCalib0r
    The minimum is 24 hours, not 4. And I just did. Read my first post again.
     
  10. Offline

    xCalib0r

    xTrollxDudex See thats the thing, I can't get the game that the player is in if you see my code above. The only way is to make an arraylist and it it to them when they both right click eachother and on QUIT it can only remove the player though because there would be no way of getting the target as well.
     
  11. Offline

    RingOfStorms

    You should store your players in a HashMap<UUID, UUID> and then you can still loop through they keySet and values to go through all players, but you can also get the other player because they are matched together.
     
  12. Offline

    xCalib0r

    You see iff I were to do something like this HOW would it get the target? How would it get the other guy in my hashamap, right now if a player leaves it gives the win to the Player who left, no way I can get the other guy in the hashmap (target) in the onquit event.

    Code:
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
     
    if (pregame.contains(p.getUniqueId()) || fighting.contains(p.getUniqueId())) {
    if (fighting.contains(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    p.showPlayer(others);
    }
    StatsManager.getInstance().add1v1Wins(p, 1);
    p.playSound(p.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(p);
    }
    }
    }
    
     
  13. Offline

    xTrollxDudex

    xCalib0r
    Well, make a hashmap with both of them on there when they start dueling.
     
  14. Offline

    xCalib0r

    xTrollxDudex IK there is it's called fighting BUT HOW would I detect on teh quit even the OTHER guy?
     
  15. Offline

    RingOfStorms



    Use a hashmap. When two players duel you can use the hashmap to track who is dueling who, rather that a bunch of trickery with get last killer and what not. Currently your system isn't the best one as it is. Imagine if something else killed the player, the other guy would not get recognized in the death event and he would be stuck in the arena forever. Using a hashmap is much more reliable in your situation.
     
  16. Offline

    xTrollxDudex

  17. Offline

    xCalib0r

    xTrollxDudex Did you not understand what I said? I know what a playerquit event is I AM USING HASHMAPS the thing is I'm putting the player AND the target in it, when tthe player QUITS how will it detect the TARGET? it would only detect the player.

    RingOfStorms Jesus PLEASE look at my code, I am using hashmap, like everything in my code is hashmap, as you can see when two people are fighting it puts them in the Fighting hashmap refer to the code that I posted.

    RingOfStorms plus if you refer to my code my 1v1 arena is working just fine it DETECTS who won and who didn't.

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

    RingOfStorms

    So then what's the problem? Just use on quit event and get the other player in the hash map?
     
  19. Offline

    xCalib0r

    RingOfStorms How would you get the other player in the hashmap? PlayerQuitEvent can only get the player, how would I get the TARGET?
     
  20. Offline

    xTigerRebornx

    xCalib0r Loop through the HashMap (using an Entry), and then check if either the key/value is the Player's UUID, or using something like a Bidimap
     
  21. Offline

    xCalib0r

    xTigerRebornx see this is what I have now, but still how would I get theo ther guy
    Code:
    for (UUID fighter : fighting) {
    fighting.remove(fighter);
    //blblalbal still how do I get the other player that didnt leave
    }
    
     
  22. Offline

    xTigerRebornx

    xCalib0r You said that you were using a Map, did you not?
    That looks like you are using a List/Set of sorts, if you are using a Map, try what I said and loop through the Map's entry set to check for the Player, checking both the Key and Value for their UUID
     
  23. Offline

    xCalib0r

    @xTigerRebornx here look at my code to see how I'm handling everything

    Code:
    public static HashMap<UUID, UUID> request = new HashMap<UUID, UUID>();
    public static HashMap<UUID, UUID> alreadyrequested = new HashMap<UUID, UUID>();
     
    public static ArrayList<UUID> fighting = new ArrayList<UUID>();
    public static ArrayList<UUID> pregame = new ArrayList<UUID>();
    public static ArrayList<UUID> inarena = new ArrayList<UUID>();
    public static ArrayList<UUID> leftearly = new ArrayList<UUID>();
     
    public static void setup1v1Player(Player p) {
    if (KitManager.getInstance().kitType.containsKey(p.getUniqueId())) {
    KitManager.getInstance().kitType.remove(p.getUniqueId());
    }
    inarena.add(p.getUniqueId());
    dInventory.getInstance().sortoutPlayer(p);
    SpawnProtection.spawnprotected.remove(p.getUniqueId());
    SpawnProtection.spawnjumpprotected.remove(p.getUniqueId());
    BowDownProtection.bowdownprotection.remove(p.getUniqueId());
    p.teleport(LocationManager.getInstance().get1v1Location());
    p.getInventory().setItem(0, dInventory.getInstance().getNormal1v1Rod());
    }
     
    public static void teleportPlayers(Player p1, Player p2) {
    p1.teleport(LocationManager.getInstance().get1v1FirstPostitionLocation());
    p2.teleport(LocationManager.getInstance().get1v1SecondPostitionLocation());
    }
     
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
     
    if (e.getCurrentItem() == null) {
    return;
    }
     
    if (e.getCurrentItem().getType() == dInventory.getInstance().getNormal1v1Rod().getType()) {
    e.setCancelled(true);
    e.getWhoClicked().closeInventory();
    }
    }
     
    @EventHandler
    public void onInteract(PlayerInteractEntityEvent e) {
    final Player p = e.getPlayer();
    if (e.getRightClicked() instanceof Player) {
    final Player target = (Player) e.getRightClicked();
     
    if (!inarena.contains(p.getUniqueId())) {
    return;
    }
     
    if (pregame.contains(target.getUniqueId()) || fighting.contains(target.getUniqueId())) {
    return;
    }
     
    if (p.getItemInHand().getType() == dInventory.getInstance().getNormal1v1Rod().getType()) {
    request.put(p.getUniqueId(), target.getUniqueId());
    if (request.get(target.getUniqueId()) != (p.getUniqueId())) {
     
    if (alreadyrequested.get(p.getUniqueId()) != target.getUniqueId()) {
    p.sendMessage(ChatColor.GREEN + "You have challenged " + target.getName() + " to a normal 1v1!");
    }
     
    if (alreadyrequested.get(p.getUniqueId()) != target.getUniqueId()) {
    target.sendMessage(ChatColor.GREEN + p.getName() + " has challenged you to a normal 1v1! Right click him with your rod to accept!");
    }
     
    if (alreadyrequested.get(p.getUniqueId()) != target.getUniqueId()) {
    alreadyrequested.put(p.getUniqueId(), target.getUniqueId());
    return;
    }
     
    if (alreadyrequested.get(p.getUniqueId()) == target.getUniqueId()) {
    p.sendMessage(ChatColor.RED + "You have already challenged " + target.getName() + " to a normal 1v1!");
    }
     
    } else {
    request.remove(p.getUniqueId());
    request.remove(target.getUniqueId());
    alreadyrequested.remove(p.getUniqueId());
    alreadyrequested.remove(target.getUniqueId());
    teleportPlayers(p, target);
    pregame.add(p.getUniqueId());
    pregame.add(target.getUniqueId());
    dAPI.getInstance().clearPlayerChat(p);
    dAPI.getInstance().clearPlayerChat(target);
    dInventory.getInstance().sortoutPlayer(p);
    dInventory.getInstance().sortoutPlayer(target);
     
    for (Player others : Bukkit.getServer().getOnlinePlayers()) {
    p.hidePlayer(others);
    target.hidePlayer(others);
    p.showPlayer(target);
    target.showPlayer(p);
    }
     
    giveNormalPvP(p);
    giveNormalPvP(target);
     
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(dKits.getInstance(), new Runnable() {
    public void run() {
    pregame.remove(p.getUniqueId());
    pregame.remove(target.getUniqueId());
    fighting.add(p.getUniqueId());
    fighting.add(target.getUniqueId());
    }
    }, 20 * 5);
     
    }
    }
    }
    }
     
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    if (pregame.contains(p.getUniqueId())) {
     
    if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) {
    return;
    }
     
    e.setTo(e.getFrom());
    }
    }
     
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (fighting.contains(p.getUniqueId())) {
    inarena.remove(p.getUniqueId());
    fighting.remove(p.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    p.showPlayer(others);
    }
    StatsManager.getInstance().add1v1Loss(p, 1);
    }
    }
    if (e.getEntity().getKiller() instanceof Player) {
    Player killer = (Player) e.getEntity().getKiller();
    if (fighting.contains(killer.getUniqueId())) {
    fighting.remove(killer.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    killer.showPlayer(others);
    }
    StatsManager.getInstance().add1v1Wins(killer, 1);
    killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(killer);
    }
    }
    }
     
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e) {
    if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (!inarena.contains(p.getUniqueId())) {
    return;
    }
     
    if (!fighting.contains(e.getDamager().getUniqueId())) {
    e.setCancelled(true);
    }
     
    }
    }
     
    @EventHandler
    public void onCommand(PlayerCommandPreprocessEvent e) {
    Player p = e.getPlayer();
     
    if (pregame.contains(p.getUniqueId()) || fighting.contains(p.getUniqueId())) {
    if (e.getMessage().startsWith("/")) {
    p.sendMessage(ChatColor.RED + "You cannot use commmands while being in a 1v1!");
    e.setCancelled(true);
    }
    }
    }
     
    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
    Player p = e.getPlayer();
    /*
    if (pregame.contains(p.getUniqueId()) || fighting.contains(p.getUniqueId())) {
    if (fighting.contains(p.getUniqueId())) {
    fighting.remove(p.getUniqueId());
    for (Player others : Bukkit.getOnlinePlayers()) {
    p.showPlayer(others);
    }
    StatsManager.getInstance().add1v1Wins(p, 1);
    p.playSound(p.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
    setup1v1Player(p);
    }
    }
    */
    for (UUID fighter : fighting) {
    fighting.remove(fighter);
    //blblalbal still how do I get the other player that didnt leave
    }
    }
     
    private void giveNormalPvP(Player p) {
    p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
    p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
    dInventory.getInstance().addSoup(p, 8);
    }
    
     
  24. Offline

    xTigerRebornx

    xCalib0r Okay, so a few comments.
    1. Why so much static.....Learn the proper use of statics, clean up your code.
    2. Do you actually need 4 Lists for handling the 1v1s
    3. In your PlayerCommandPreprocessEvent, there is no need to check if the message starts with a '/', as its already fired for using a command

    I'd recommend restructuring your code so that it isn't such a huge mess of Lists and Maps. I'd recommend that you use an Arena-based system for handling your matches. Have an Arena class that stores the two Players fighting, the status of the match, the setup, and anything that you feel should be handled on a per-match basis (Start method, End method, etc). Then, have a class to manage the instances of your Arena class (You can use a Singleton-like structure to handle this), as well as use it for the Listeners of what should be handled in those Arenas.

    Tl;dr, use OOP to your advantage, drop the unneeded overuse of static and take a cleaner and more manageable setup for handling your 1v1s
     
    TheSpherret likes this.
  25. Offline

    xCalib0r

    xTigerRebornx lol @ the why so much static, the reason it's static is because I kept it that way UNTIL im done and of course it will all be privates.

    xTigerRebornx Can you give me in example how I would setup an arena use for this?

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

    RingOfStorms

    Go learn java and object oriented programming.
     
  27. Offline

    xTigerRebornx

    xCalib0r Some pseudo-code (don't copy paste, will probably not work)
    Code:
    class Arena {
      private UUID player1;
      private UUID player2;
     
      // Other fields for whatever you may need, like the state
      // Methods such as getters for UUID, changing state, starting, etc
      public Arena(Player one, Player two){
        // Get/Set UUIDs here
      }
     
       
    }
    A general skeleton of the Arena, it takes in two Players, which you can store the UUIDs of.
    Its hard to create an "example" for you. Its something you control. You are deciding what will go in your plugins, how your matches will start/stop.
     
  28. Offline

    xCalib0r

    xTigerRebornx I'll start working on arenas as soon as I figure out how to make it work with what I have right now. Any ideas?
     
  29. Offline

    xTigerRebornx

    xCalib0r Making it work with what you have right now would require you to have a way of matching two Players together, so that you can get who is fighting who when you need to do your checks.
    What you have right now though, is messy and eats up more resources then it would need to, it'd be better to do a rewrite based on this, but handled through a cleaner system using something similar to an Arena.
    If you are actually wanting to continue with what you have now, then use a Map to match Players, then use a loop to find the Entry that you've put into the map for the Player that leaves (Loop through the Entry Set and check if either the Key or Value is the Player/Player's UUID)
     
  30. Offline

    xCalib0r

    xTigerRebornx Well once I use an hashmap how would I loop through the it and fight the entry?
     
Thread Status:
Not open for further replies.

Share This Page