Solved Using ArrayLists for Lives

Discussion in 'Plugin Development' started by mamifsidtect, May 27, 2014.

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

    mamifsidtect

    Alright guys, I'm back with a new problem, I switched from HashMaps, to ArrayLists, but nothing I do seems to get it to work. So here's my new code.

    Arena.java:
    Code:java
    1. package io.github.mamifsidtect.goldengun2.managers;
    2.  
    3. import io.github.mamifsidtect.goldengun2.GoldenGun;
    4. import io.github.mamifsidtect.goldengun2.listeners.GameListener;
    5. import io.github.mamifsidtect.goldengun2.managers.Arena.ArenaState;
    6. import io.github.mamifsidtect.goldengun2.managers.MessageManager.MessageType;
    7.  
    8. import java.util.ArrayList;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.GameMode;
    13. import org.bukkit.Location;
    14. import org.bukkit.Material;
    15. import org.bukkit.configuration.ConfigurationSection;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20.  
    21. public class Arena implements Listener {
    22.  
    23. public enum ArenaState { DISABLED, WAITING, COUNTING_DOWN, STARTED; }
    24.  
    25. public ItemStack classselect = new ItemStack(Material.STICK); {
    26. ItemMeta csmeta = classselect.getItemMeta();
    27. ArrayList<String> cs = new ArrayList<String>();
    28. csmeta.setDisplayName(ChatColor.YELLOW + "Class Selection (Right-Click)");
    29. cs.add("");
    30. cs.add(ChatColor.GOLD + "Right-click to choose your class");
    31. csmeta.setLore(cs);
    32. classselect.setItemMeta(csmeta);
    33. }
    34.  
    35. private int id, taskID, numPlayers, currentPlayers = 0;
    36. protected ArenaState state = ArenaState.DISABLED;
    37. public ArrayList<PlayerData> data;
    38. private Location spawnPoint;
    39.  
    40. public Arena(int id) {
    41. this.id = id;
    42. this.data = new ArrayList<PlayerData>();
    43. this.numPlayers = ConfigManager.getArenas().get("arenas." + id + ".numPlayers");
    44.  
    45. ConfigurationSection s = ConfigManager.getArenas().get("arenas." + id + ".spawn");
    46. spawnPoint = LocationManager.locationFromConfig(s, true);
    47.  
    48. state = ArenaState.WAITING;
    49. }
    50.  
    51. public int getID() {
    52. return id;
    53. }
    54.  
    55. public ArenaState getState() {
    56. return state;
    57. }
    58.  
    59. public int getCurrentPlayers() {
    60. return currentPlayers;
    61. }
    62.  
    63. public void addPlayer(Player p) {
    64. if (currentPlayers >= numPlayers) {
    65. MessageManager.getInstance().msg(p, MessageType.BAD, "There are too many players in that arena already!");
    66. return;
    67. }
    68.  
    69. if (spawnPoint == null) {
    70. MessageManager.getInstance().msg(p, MessageType.BAD, "The spawn point for this arena has not been set yet.");
    71. return;
    72. }
    73.  
    74. data.add(new PlayerData(p));
    75.  
    76. p.getInventory().clear();
    77.  
    78. p.getInventory().setItem(4, new ItemStack(classselect));
    79.  
    80. currentPlayers++;
    81.  
    82. GameListener.getInstance().fiveLivesRemaining.add(p);
    83. System.out.print("This worked");
    84.  
    85. SimpleScoreboard ss = new SimpleScoreboard(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Golden" + ChatColor.YELLOW + "Gun" + ChatColor.DARK_GRAY + "]");
    86. ss.add("===================");
    87. ss.blankLine();
    88.  
    89. ss.add(ChatColor.YELLOW + "Current Players:");
    90. ss.add("" + currentPlayers);
    91. ss.blankLine();
    92.  
    93. ss.add(ChatColor.YELLOW + "Phase:");
    94. ss.add("Pre-Alpha");
    95. ss.blankLine();
    96.  
    97. ss.add("===================");
    98. ss.build();
    99. ss.send(p);
    100.  
    101. p.setGameMode(GameMode.SURVIVAL);
    102.  
    103. p.teleport(spawnPoint);
    104. }
    105.  
    106. public void removePlayer(Player p) {
    107. PlayerData d = getPlayerData(p);
    108. d.restorePlayer();
    109. data.remove(d);
    110.  
    111. currentPlayers--;
    112.  
    113. p.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
    114.  
    115. if (currentPlayers == 1) {
    116. stop(data.get(0).getPlayer());
    117. } else if (currentPlayers == 0) {
    118. stop();
    119. }
    120. }
    121.  
    122. public void start() {
    123. this.state = ArenaState.COUNTING_DOWN;
    124.  
    125. final Countdown c = new Countdown(30, "The game is starting in %t seconds!", this, 30, 20, 10, 5, 4, 3, 2, 1);
    126. this.taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(GoldenGun.getPlugin(), new Runnable() {
    127. public void run() {
    128. if (!c.isDone()) {
    129. c.run();
    130. state = ArenaState.COUNTING_DOWN;
    131. }
    132. else Bukkit.getServer().getScheduler().cancelTask(taskID);
    133. state = ArenaState.STARTED;
    134. }
    135. }, 0, 20);
    136. }
    137.  
    138. public void stop(Player winner) {
    139. if (winner != null) MessageManager.getInstance().broadcast(MessageType.GOOD, winner.getName() + " has won in arena " + id + "!");
    140.  
    141. ArenaManager.getInstance().getArena(winner).removePlayer(winner);
    142.  
    143. for (PlayerData pd : data) pd.restorePlayer();
    144.  
    145. this.state = ArenaState.WAITING;
    146. }
    147.  
    148. public void stop() {
    149. for (PlayerData pd : data) pd.restorePlayer();
    150.  
    151. this.state = ArenaState.WAITING;
    152. }
    153.  
    154. public boolean containsPlayer(Player p) {
    155. return getPlayerData(p) != null;
    156. }
    157.  
    158. private PlayerData getPlayerData(Player p) {
    159. for (PlayerData d : data) {
    160. if (d.isForPlayer(p)) return d;
    161. }
    162. return null;
    163. }
    164.  
    165. protected void sendMessage(MessageType type, String... messages) {
    166. for (PlayerData d : data) MessageManager.getInstance().msg(d.getPlayer(), type, messages);
    167. }
    168. }
    169.  
    170. class PlayerData {
    171.  
    172. private String playerName;
    173. private ItemStack[] contents, armorContents;
    174. private Location location;
    175. private GameMode gm;
    176.  
    177. protected PlayerData(Player p) {
    178. this.playerName = p.getName();
    179. this.contents = p.getInventory().getContents();
    180. this.armorContents = p.getInventory().getArmorContents();
    181. this.location = Bukkit.getWorld("world").getSpawnLocation();
    182. this.gm = p.getGameMode();
    183. }
    184.  
    185. @SuppressWarnings("deprecation")
    186. protected Player getPlayer() {
    187. return Bukkit.getServer().getPlayer(playerName);
    188. }
    189.  
    190. @SuppressWarnings("deprecation")
    191. protected void restorePlayer() {
    192. Player p = Bukkit.getServer().getPlayer(playerName);
    193.  
    194. p.getInventory().setContents(contents);
    195. p.getInventory().setArmorContents(armorContents);
    196. p.teleport(location);
    197. p.setGameMode(gm);
    198. }
    199.  
    200. protected boolean isForPlayer(Player p) {
    201. return playerName.equalsIgnoreCase(p.getName());
    202. }
    203. }
    204.  
    205. class Countdown implements Runnable {
    206.  
    207. private boolean isDone = false;
    208. private int timer;
    209. private String msg;
    210. private Arena a;
    211. private ArrayList<Integer> countingNums;
    212.  
    213. public Countdown(int start, String msg, Arena a, int... countingNums) {
    214. this.timer = start;
    215. this.msg = msg;
    216. this.a = a;
    217. this.countingNums = new ArrayList<Integer>();
    218. for (int i : countingNums) this.countingNums.add(i);
    219. }
    220.  
    221. public void run() {
    222. if (timer == 0) {
    223. a.sendMessage(MessageType.GOOD, "The game has begun!");
    224. a.state = ArenaState.STARTED;
    225. isDone = true;
    226. return;
    227. }
    228.  
    229. if (countingNums.contains(timer)) {
    230. a.sendMessage(MessageType.INFO, msg.replaceAll("%t", timer + ""));
    231. }
    232.  
    233. timer--;
    234. }
    235.  
    236. public boolean isDone() {
    237. return isDone;
    238. }
    239. }
    240.  


    GameListener.java:
    Code:java
    1. package io.github.mamifsidtect.goldengun2.listeners;
    2.  
    3. import io.github.mamifsidtect.goldengun2.managers.ArenaManager;
    4. import io.github.mamifsidtect.goldengun2.managers.MessageManager;
    5. import io.github.mamifsidtect.goldengun2.managers.MessageManager.MessageType;
    6.  
    7. import java.util.ArrayList;
    8.  
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.PlayerDeathEvent;
    13. import org.bukkit.event.player.PlayerRespawnEvent;
    14.  
    15. public class GameListener implements Listener {
    16.  
    17. private static GameListener instance = new GameListener();
    18.  
    19. public static GameListener getInstance() {
    20. return instance;
    21. }
    22.  
    23. public ArrayList<Player> fiveLivesRemaining = new ArrayList<Player>();
    24. public ArrayList<Player> fourLivesRemaining = new ArrayList<Player>();
    25. public ArrayList<Player> threeLivesRemaining = new ArrayList<Player>();
    26. public ArrayList<Player> twoLivesRemaining = new ArrayList<Player>();
    27. public ArrayList<Player> oneLivesRemaining = new ArrayList<Player>();
    28.  
    29. @EventHandler
    30. public void onPlayerDeath(PlayerDeathEvent event) {
    31. Player p = (Player) event.getEntity();
    32. if (ArenaManager.getInstance().getArena(p) != null) {
    33. if (fiveLivesRemaining.contains(p)) {
    34. fiveLivesRemaining.remove(p);
    35. fourLivesRemaining.add(p);
    36. System.out.print("This also worked");
    37.  
    38. } else if (fourLivesRemaining.contains(p)) {
    39. fourLivesRemaining.remove(p);
    40. threeLivesRemaining.add(p);
    41. System.out.print("This also worked");
    42.  
    43. } else if (threeLivesRemaining.contains(p)) {
    44. threeLivesRemaining.remove(p);
    45. twoLivesRemaining.add(p);
    46. System.out.print("This also worked");
    47.  
    48. } else if (twoLivesRemaining.contains(p)) {
    49. twoLivesRemaining.remove(p);
    50. oneLivesRemaining.add(p);
    51. System.out.print("This also worked");
    52.  
    53. } else if (oneLivesRemaining.contains(p)) {
    54. oneLivesRemaining.remove(p);
    55. ArenaManager.getInstance().getArena(p).removePlayer(p);
    56. System.out.print("This also worked");
    57. }
    58. }
    59. }
    60.  
    61. @EventHandler
    62. public void onPlayerRespawn(PlayerRespawnEvent event) {
    63. Player p = event.getPlayer();
    64. if (fourLivesRemaining.contains(p)) {
    65. MessageManager.getInstance().msg(p, MessageType.INFO, "You now have 4 lives remaining");
    66. }
    67. }
    68. }
    69.  


    GoldenGun.java:
    Code:java
    1. package io.github.mamifsidtect.goldengun2;
    2.  
    3. import io.github.mamifsidtect.goldengun2.listeners.ClassSelectInventoryListener;
    4. import io.github.mamifsidtect.goldengun2.listeners.DamageListener;
    5. import io.github.mamifsidtect.goldengun2.listeners.GameListener;
    6. import io.github.mamifsidtect.goldengun2.listeners.HungerListener;
    7. import io.github.mamifsidtect.goldengun2.listeners.JoinListener;
    8. import io.github.mamifsidtect.goldengun2.listeners.ShopInventoryListener;
    9. import io.github.mamifsidtect.goldengun2.managers.ArenaManager;
    10. import io.github.mamifsidtect.goldengun2.managers.CommandManager;
    11.  
    12. import java.util.logging.Logger;
    13.  
    14. import org.bukkit.Bukkit;
    15. import org.bukkit.plugin.Plugin;
    16. import org.bukkit.plugin.PluginDescriptionFile;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class GoldenGun extends JavaPlugin {
    20.  
    21. Logger logger = Logger.getLogger("Minecraft");
    22.  
    23. public static Plugin getPlugin() {
    24. return Bukkit.getServer().getPluginManager().getPlugin("GoldenGun");
    25. }
    26.  
    27. @Override
    28. public void onEnable() {
    29. PluginDescriptionFile pdfile = this.getDescription();
    30. logger.info(pdfile.getName() + " Version: " + pdfile.getVersion() + " is now enabled!");
    31.  
    32. ArenaManager.getInstance().setupArenas();
    33.  
    34. Bukkit.getPluginManager().registerEvents(new GameListener(), this);
    35. Bukkit.getPluginManager().registerEvents(new DamageListener(), this);
    36. Bukkit.getPluginManager().registerEvents(new HungerListener(), this);
    37. Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
    38. Bukkit.getPluginManager().registerEvents(new ClassSelectInventoryListener(), this);
    39. Bukkit.getPluginManager().registerEvents(new ShopInventoryListener(), this);
    40. //Bukkit.getPluginManager().registerEvents(new SignListener(), this);
    41.  
    42. CommandManager cm = new CommandManager();
    43. cm.setup();
    44. getCommand("goldengun").setExecutor(cm);
    45. }
    46. }
    47.  


    Only the "System.out.print("This worked")" in Arena.java works, none of the others. And yes, I know that storing the Player object is bad, but all I need for now is to at least get it working, then I'll switch to UUIDs
     
  2. Offline

    Gater12

    mamifsidtect
    Why don't you use HashMaps? It's much more easier than creating 5 whole List objects.
     
  3. Offline

    superguy9

    Try using a HashMap. For example:
    Code:java
    1. public static HashMap<String, Integer> playerLives = new HashMap<String, Integer>();

    To get how many lives the player has, do:
    Code:java
    1. playerLives.getValue(player.getName());

    And to add/remove lives, do:
    Code:java
    1. playerLives.put(player.getName(), playerLives.getValue(player.getName()) + /*or '-' if you want to remove one */ 1);

    Hope this helped! And if you have any questions about what this means, feel free to ask!
     
  4. Offline

    1Rogue

    You're abandoning the entire principle of OOP...

    Make a player object that contains fields for lives, name, etc. Then keep a map of those objects with some unique identifier for the key.
     
  5. Offline

    mine-care

  6. Offline

    superguy9

    I'd recommend showing example code for that...
     
  7. Offline

    mamifsidtect

    superguy9 I agree with you to show some code, I am a complete noob when it comes to HashMaps 1Rogue
     
  8. Offline

    Garris0n

    glen3b likes this.
  9. Offline

    flaaghara

    He'd basically be writing the entire class for you if he had to give any example code. You have 2 (reasonable) options in my opinion:

    Make a class called ArenaPlayer or something. ArenaPlayer will be your object which has information about each player that pertains to the minigame. Let ArenaPlayer have various instance fields such as an int for lives, String for name, and whatever else you would need. Then you can have one of the Set implementations (probably a HashSet) and iterate through it whenever trying to retrieve players.

    Otherwise, if you really only need their name (UUID) and lives, a HashMap would suffice here. The HashMap would have Keys pertaining to name, and Values pertaining to lives.

    Code:java
    1. Map<String, Integer> playersList = new HashMap<String, Integer>;


    That's the basic syntax for constructing the Map. Then you can access a player by calling his key or get a player with specific lives by getting a List which contains all values. You'd then be able to operate on that however you like.

    http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
     
  10. Offline

    superguy9

    Haha thanks.
    I've used that trick several times but I don't think 1Rogue explained it the best way(no offense).
     
  11. Offline

    1Rogue


    If you want to save iterating, keep a key-value to the object (relevant UUID most likely), and then map to that:

    Code:java
    1. Map<UUID, ArenaPlayer> players = new HashMap<>();
     
  12. Offline

    mamifsidtect

  13. Offline

    superguy9

    mamifsidtect No problem. I'm glad you got it sorted out. :)
     
Thread Status:
Not open for further replies.

Share This Page