1v1 System

Discussion in 'Plugin Development' started by Etched, May 24, 2014.

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

    Etched

    Hi all,
    I've been coding a 1v1 system, but I'm stuck on one part. I'm trying to figure out how to get which player is fighting which.
    Here is my code.
    Code:java
    1. package me.Etched.Main;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.World;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.entity.PlayerDeathEvent;
    18. import org.bukkit.event.player.PlayerInteractEntityEvent;
    19. import org.bukkit.event.player.PlayerMoveEvent;
    20.  
    21. public class Request implements Listener{
    22.  
    23. public static Map<Player, Player> request = new HashMap<Player, Player>();
    24. public static List<List<Player>> fighting = new ArrayList<List<Player>>();
    25. public static List<Player> pregame = new ArrayList<Player>();
    26.  
    27. public Main plugin;
    28. public Request(Main ins){
    29. this.plugin = ins;
    30. }
    31.  
    32. World w = Bukkit.getWorld(plugin.getConfig().getString("arena1.world"));
    33. int x = Integer.parseInt(plugin.getConfig().getString("arena1.x"));
    34. int y = Integer.parseInt(plugin.getConfig().getString("arena1.y"));
    35. int z = Integer.parseInt(plugin.getConfig().getString("arena1.z"));
    36. float yaw = Float.parseFloat(plugin.getConfig().getString("arena1.yaw"));
    37. float pitch = Float.parseFloat(plugin.getConfig().getString("arena1.pitch"));
    38. Location arena1 = new Location(w, x, y, z, yaw, pitch);
    39.  
    40. World w1 = Bukkit.getWorld(plugin.getConfig().getString("arena2.world"));
    41. int x1 = Integer.parseInt(plugin.getConfig().getString("arena2.x"));
    42. int y1 = Integer.parseInt(plugin.getConfig().getString("arena2.y"));
    43. int z1 = Integer.parseInt(plugin.getConfig().getString("arena2.z"));
    44. float yaw1 = Float.parseFloat(plugin.getConfig().getString("arena2.yaw"));
    45. float pitch1 = Float.parseFloat(plugin.getConfig().getString("arena2.pitch"));
    46. Location arena2 = new Location(w1, x1, y1, z1, yaw1, pitch1);
    47.  
    48.  
    49. @EventHandler
    50. public void onAccept(PlayerInteractEntityEvent e){
    51. final Player p = e.getPlayer();
    52. final Player victim = (Player) e.getRightClicked();
    53. if(p.getItemInHand().getType() == Material.SIGN && p.getItemInHand().getItemMeta().getDisplayName().contains("Request 1v1")){
    54. if(victim instanceof Player){
    55. if(request.containsKey(p.getName())){
    56. p.sendMessage(ChatColor.RED + "Request with " + request.get(p).getName() + " cancelled. Now requesting to 1v1 with " + victim.getName());
    57. request.put(p, victim);
    58. victim.sendMessage(ChatColor.GOLD + p.getName() + " just send you a request to 1v1. Right click him to accept.");
    59. }else{
    60. p.sendMessage(ChatColor.GOLD + "Requesting to 1v1 with " + victim.getName());
    61. request.put(p, victim);
    62. victim.sendMessage(ChatColor.GOLD + p.getName() + " just send you a request to 1v1. Right click him to accept.");
    63. }
    64. if(request.get(victim).equals(p)){
    65. p.sendMessage("Accepted the duel... Teleporting");
    66. victim.sendMessage("Your request was accepted... Teleporting");
    67. request.remove(p);
    68. request.remove(victim);
    69. p.teleport(arena1);
    70. victim.teleport(arena2);
    71. pregame.add(p);
    72. pregame.add(victim);
    73. for(Player other : Bukkit.getServer().getOnlinePlayers()){
    74. if(!other.equals(p)){
    75. p.hidePlayer(other);
    76. }
    77. }
    78.  
    79. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    80. public void run(){
    81. pregame.remove(p);
    82. pregame.remove(victim);
    83. ArrayList<Player> players = new ArrayList<Player>();
    84. players.add(p);
    85. players.add(victim);
    86. fighting.add(players);
    87. }
    88. }, 20*5);
    89. }
    90. }
    91. }
    92. }
    93.  
    94. @EventHandler
    95. public void onMove(PlayerMoveEvent e){
    96. Player p = e.getPlayer();
    97. if(pregame.contains(p)){
    98. e.setCancelled(true);
    99. }
    100. }
    101.  
    102. @EventHandler
    103. public void onDeath(PlayerDeathEvent e){
    104. Player p = e.getEntity();
    105. for(List<Player> fight : fighting){
    106. if(fight.contains(p)){
    107. p.sendMessage(ChatColor.RED + ChatColor.BOLD.toString() + "You lost the 1v1!");
    108. Player winner =
    109. }else{
    110. return;
    111. }
    112. }
    113. }
    114. }


    Sorry about the weird unfinished thing in the death event, that's where I'm trying to get the other player. Sorry if the List seems dodgy, its me trying to figure out a way (and I couldn't be bothered to remove it for this :p)
     
  2. Offline

    jthort

    Etched Why are you parsing every value you are getting from the config, why not save the actual number to the .yml file using
    Code:
    <ConfigurationFile>.set(Key, Value(As an Int not a String) );
     
  3. Offline

    Etched

    I don't know :p

    jthort Actually don't worry, I figured it out. I'll use a constructor and a custom 1v1 class :)
    Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page