Is there a better way?

Discussion in 'Plugin Development' started by FlareLine, Sep 24, 2013.

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

    FlareLine

    I have two classes (neither are my main class) that interact with each other to add players to Arraylists when they perform a command, for example. Using /bb join 2 would add the player to playerlist2, and the same for 1 and 3.
    Here is the BBGame.java

    Code:java
    1. package pack.flareline.blockbash;
    2.  
    3.  
    4.  
    5. import java.util.ArrayList;
    6.  
    7.  
    8.  
    9. import org.bukkit.ChatColor;
    10.  
    11. import org.bukkit.entity.Player;
    12.  
    13. import org.bukkit.event.Listener;
    14.  
    15.  
    16.  
    17. public class BBGame implements Listener {
    18.  
    19.  
    20.  
    21. BBMain plugin;
    22.  
    23.  
    24.  
    25. public BBGame(BBMain instance) {
    26.  
    27. plugin = instance;
    28.  
    29. }
    30.  
    31.  
    32.  
    33. /* Activating Arrays */
    34.  
    35. public static ArrayList<String> playerlist1 = new ArrayList<String>();
    36.  
    37. public static ArrayList<Player> gteamlist1 = new ArrayList<Player>();
    38.  
    39. public static ArrayList<Player> pteamlist1 = new ArrayList<Player>();
    40.  
    41.  
    42.  
    43. public static ArrayList<String> playerlist2 = new ArrayList<String>();
    44.  
    45. public static ArrayList<Player> gteamlist2 = new ArrayList<Player>();
    46.  
    47. public static ArrayList<Player> pteamlist2 = new ArrayList<Player>();
    48.  
    49.  
    50.  
    51. public static ArrayList<String> playerlist3 = new ArrayList<String>();
    52.  
    53. public static ArrayList<Player> gteamlist3 = new ArrayList<Player>();
    54.  
    55. public static ArrayList<Player> pteamlist3 = new ArrayList<Player>();
    56.  
    57.  
    58.  
    59. /* Activating Variables */
    60.  
    61. public static int ArenaID1;
    62.  
    63. public static int players1 = playerlist1.size();
    64.  
    65. public static int teams1 = 2;
    66.  
    67. public static int gteam1 = gteamlist1.size();
    68.  
    69. public static int pteam1 = pteamlist1.size();
    70.  
    71.  
    72.  
    73. public static int ArenaID2;
    74.  
    75. public static int players2 = playerlist2.size();
    76.  
    77. public static int teams2 = 2;
    78.  
    79. public static int gteam2 = gteamlist2.size();
    80.  
    81. public static int pteam2 = pteamlist2.size();
    82.  
    83.  
    84.  
    85. public static int ArenaID3;
    86.  
    87. public static int players3 = playerlist3.size();
    88.  
    89. public static int teams3 = 2;
    90.  
    91. public static int gteam3 = gteamlist3.size();
    92.  
    93. public static int pteam3 = pteamlist3.size();
    94.  
    95.  
    96.  
    97. public static void delplayer(Player player) {
    98.  
    99. String name = player.getName();
    100.  
    101. playerlist1.remove(name);
    102.  
    103. player.sendMessage(ChatColor.YELLOW + "You have left the Game!");
    104.  
    105. }
    106.  
    107.  
    108.  
    109. public static void addplayer(Player player, int arena) {
    110.  
    111. String name = player.getName();
    112.  
    113. if (arena == 1) {
    114.  
    115. if (players1 >= 1) {
    116.  
    117. player.sendMessage(ChatColor.YELLOW + "Sorry, Arena 1 is full!");
    118.  
    119. } else {
    120.  
    121. playerlist1.add(name);
    122.  
    123. player.sendMessage(ChatColor.YELLOW
    124.  
    125. + "You have joined Arena 1!");
    126.  
    127. }
    128.  
    129. }
    130.  
    131. if (arena == 2) {
    132.  
    133. if (players2 >= 1) {
    134.  
    135. player.sendMessage(ChatColor.YELLOW + "Sorry, Arena 2 is full!");
    136.  
    137. } else {
    138.  
    139. playerlist2.add(name);
    140.  
    141. player.sendMessage(ChatColor.YELLOW
    142.  
    143. + "You have joined Arena 2!");
    144.  
    145. }
    146.  
    147. if (arena == 3) {
    148.  
    149. if (players3 >= 1) {
    150.  
    151. player.sendMessage(ChatColor.YELLOW
    152.  
    153. + "Sorry, Arena 3 is full!");
    154.  
    155. } else {
    156.  
    157. }
    158.  
    159. playerlist3.add(name);
    160.  
    161. player.sendMessage(ChatColor.YELLOW
    162.  
    163. + "You have joined Arena 3!");
    164.  
    165. }
    166.  
    167. }
    168.  
    169. }
    170.  
    171.  
    172.  
    173. public static void listplayers(Player playerp, String arena) {
    174.  
    175. if (arena.equalsIgnoreCase("1")){
    176.  
    177.  
    178.  
    179. String list = playerlist1.toString();
    180.  
    181. playerp.sendMessage(ChatColor.YELLOW + "Players in game: " + list);
    182.  
    183. }
    184.  
    185. if (arena.equalsIgnoreCase("2")){
    186.  
    187.  
    188.  
    189. String list = playerlist2.toString();
    190.  
    191. playerp.sendMessage(ChatColor.YELLOW + "Players in game: " + list);
    192.  
    193. }
    194.  
    195. if (arena.equalsIgnoreCase("3")){
    196.  
    197.  
    198.  
    199. String list = playerlist3.toString();
    200.  
    201. playerp.sendMessage(ChatColor.YELLOW + "Players in game: " + list);
    202.  
    203. }
    204.  
    205. }
    206.  
    207. }
    208.  
    209.  

    And the BBCommandExecutor.java
    Code:java
    1. package pack.flareline.blockbash;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class BBCommandExecutor implements CommandExecutor {
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label,
    14. String[] args) {
    15. if (!(sender instanceof Player)) {
    16. sender.sendMessage(ChatColor.RED
    17. + "You must be a Player to use this command.");
    18. return true;
    19. } else {
    20. if (cmd.getName().equalsIgnoreCase("bb") && args.length == 0) {
    21. sender.sendMessage(ChatColor.GOLD
    22. + "BlockBash v"
    23. + Bukkit.getServer().getPluginManager()
    24. .getPlugin("BlockBash").getDescription()
    25. .getVersion() + ".0");
    26. return true;
    27. }
    28. if (cmd.getName().equalsIgnoreCase("bb") && args.length == 2) {
    29. if (args[0].equalsIgnoreCase("join")) {
    30. if (args[1].equalsIgnoreCase("1")) {
    31. String players = sender.getName();
    32. int arena = 1;
    33. Player playerp = Bukkit.getServer().getPlayer(players);
    34. BBGame.addplayer(playerp, arena);
    35. return true;
    36. }
    37. if (args[1].equalsIgnoreCase("2")) {
    38. String players = sender.getName();
    39. int arena = 2;
    40. Player playerp = Bukkit.getServer().getPlayer(players);
    41. BBGame.addplayer(playerp, arena);
    42. return true;
    43. }
    44. if (args[1].equalsIgnoreCase("3")) {
    45. String players = sender.getName();
    46. int arena = 3;
    47. Player playerp = Bukkit.getServer().getPlayer(players);
    48. BBGame.addplayer(playerp, arena);
    49. return true;
    50. }
    51. if (args.length == 1) {
    52. sender.sendMessage(ChatColor.YELLOW
    53. + "Please provide an Arena ID");
    54. }
    55. }
    56. if (args[0].equalsIgnoreCase("leave")) {
    57. String players = sender.getName();
    58. Player playerp = Bukkit.getServer().getPlayer(players);
    59. BBGame.delplayer(playerp);
    60. return true;
    61. }
    62. if (args[0].equalsIgnoreCase("playing")) {
    63. if (args.length == 1) {
    64. sender.sendMessage(ChatColor.YELLOW
    65. + "Please provide a valid Arena ID");
    66. return false;
    67. }
    68. if (args[1].equalsIgnoreCase("1")
    69. || args[1].equalsIgnoreCase("2")
    70. || args[1].equalsIgnoreCase("3")) {
    71. String arena = args[1].toString();
    72. String players = sender.getName();
    73. Player playerp = Bukkit.getServer().getPlayer(players);
    74. BBGame.listplayers(playerp, arena);
    75. return true;
    76. } else {
    77. sender.sendMessage(ChatColor.YELLOW
    78. + "Please provide a valid Arena ID");
    79. return false;
    80. }
    81.  
    82. }
    83.  
    84. }
    85. return false;
    86. }
    87. }
    88.  
    89. }
    90.  

    Some error messages may be missing, but that is another story. I would like an easier, or less code consuming way of getting the 2nd argument, and adding them to the corresponding playerlist.
    Cheers, FlareLine.
     
  2. Offline

    xTrollxDudex

    FlareLine
    Yeah there's a less code consuming way... For your games! Go to the resources section and scroll down to my minigame tutorial, you'll know when you see a comical picture of Nicholas Cage on the left.
     
  3. Offline

    FlareLine

    xTrollxDudex Does this require me to wholly rewrite my plugin or could I implement the Arena class somehow?
     
  4. Offline

    xTrollxDudex

    FlareLine
    Haven't thought about rewriting it, but probably no way to reduce the amount of code checking for the second argument which could easily be reduced with OOP.
     
  5. Offline

    FlareLine

    xTrollxDudex I'll have a go at your version of the Minigame. I'll try to combine the two to obtain my desired plugin. Thanks for your help.
     
Thread Status:
Not open for further replies.

Share This Page