}else if (Lobby.size() == 4){

Discussion in 'Plugin Development' started by BlazeEyezz, Nov 28, 2013.

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

    BlazeEyezz

    Hello guys,

    I want: When the hashmap size of Lobby (= Name) is 4, the players of different hashmaps get teleported to different locations.

    I have:


    Code:java
    1. }else if (Lobby.size() == 4){
    2. for (Player key : Lobby.keySet()){
    3. Game.put(key, null);
    4. Lobby.remove(key);
    5. Bukkit.broadcastMessage("Does it work?");
    6. }
    7. for (Player key1 : Attacker.keySet()){
    8. key1.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De Game is begonnen! Vernietig de nexussen!");
    9. Location loc = new Location(key1.getWorld(), 288.02, 69.05, 272.02);
    10. key1.teleport(loc);
    11. }
    12. for (Player key2 : Defender.keySet()){
    13. key2.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De Game is begonnen! Bescherm het Kasteel!");
    14. Location loc2 = new Location(key2.getWorld(), 280.02, 70.05, 272.02);
    15. key2.teleport(loc2);
    16. }
    17.  
    18. }


    But it doesn't do anything. It also doesn't give any errors...
    Can someone help me ?
     
  2. Offline

    ArthurHoeke

    Same problem :S
     
  3. Offline

    neol3108

    What a shock :O
     
  4. Offline

    SuperOmegaCow

    BlazeEyezz I need to see the deceleration of the hashmap.
     
  5. Offline

    BlazeEyezz

    Coming:

    public final HashMap<Player, String> Lobby = new HashMap();
    public final HashMap<Player, String> Game = new HashMap();
    public final HashMap<Player, String> Attacker = new HashMap();
    public final HashMap<Player, String> Defender = new HashMap();

    SuperOmegaCow
     
  6. Offline

    felixfritz

    BlazeEyezz But the method gets triggered right? If that's the case, it might help if you post the entire java class.
     
  7. Offline

    BlazeEyezz

    Okey, coming, it's a long class but:

    Code:java
    1. public class main extends JavaPlugin
    2. implements Listener
    3. {
    4. private Menu menu;
    5. public final HashMap<Player, String> Lobby = new HashMap();
    6. public final HashMap<Player, String> Game = new HashMap();
    7. public final HashMap<Player, String> Attacker = new HashMap();
    8. public final HashMap<Player, String> Defender = new HashMap();
    9.  
    10.  
    11. int Nexus1 = 30;
    12. int Nexus2 = 10;
    13. int Test = 10;
    14.  
    15. public void onDisable() {
    16. }
    17.  
    18. public void onEnable() {
    19. this.menu = new Menu(this);
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. }
    22. @EventHandler
    23. public void onItemDrop(PlayerDropItemEvent e) { Player p = e.getPlayer();
    24. if (this.Lobby.containsKey(p)) {
    25. Item drop = e.getItemDrop();
    26. if (drop.getItemStack().getTypeId() == 360)
    27. e.setCancelled(false);
    28. else
    29. e.setCancelled(true);
    30. }
    31. }
    32. @EventHandler
    33. public void Join(SignChangeEvent e) {
    34. if (e.getLine(0).equalsIgnoreCase("[CD]")) {
    35. e.setLine(0, "§cCastleDefend");
    36. e.setLine(1, "Click to join");
    37. e.setLine(2, "CastleDefend!");
    38. Player p = e.getPlayer();
    39. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "CastleDefend join sign gemaakt!");
    40. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10.0F, 2.0F);
    41. }
    42. }
    43. @EventHandler
    44. public void Leave(SignChangeEvent e) {
    45. if (e.getLine(0).equalsIgnoreCase("[leave]")) {
    46. e.setLine(0, "§c[Leave]");
    47. e.setLine(1, "Click to leave");
    48. e.setLine(2, "CastleDefend!");
    49. Player p = e.getPlayer();
    50. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "CastleDefend leave sign gemaakt!");
    51. p.playSound(p.getLocation(), Sound.NOTE_PLING, 10.0F, 2.0F);
    52. p.getInventory().clear();
    53. }
    54. }
    55. @EventHandler
    56. public void Join(PlayerInteractEvent e) {
    57. if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    58. if ((e.getClickedBlock().getState() instanceof Sign)) {
    59. Sign s = (Sign)e.getClickedBlock().getState();
    60. if (s.getLine(0).equalsIgnoreCase("§cCastleDefend")) {
    61. final Player p = e.getPlayer();
    62. if (this.Lobby.containsKey(p)) {
    63. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "Je zit al in de lobby!");
    64. } else {
    65. this.Lobby.put(p, null);
    66. Location loc = new Location(p.getWorld(), 204.68487, 110, 267.37788);
    67. p.teleport(loc);
    68. Bukkit.broadcastMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + p.getName() + " Is CastleDefend gejoint! " + ChatColor.GOLD + "[" + ChatColor.RED + Lobby.size() + "/24" + ChatColor.GOLD + "]");
    69. ItemStack EmpireWand = new ItemStack(Material.EMERALD, 1);
    70. ArrayList EmpireWand1 = new ArrayList();
    71. EmpireWand1.add(ChatColor.GOLD + "Rechter muis knop op je kit te kiezen!");
    72. ItemMeta im = EmpireWand.getItemMeta();
    73. im.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Kits");
    74. im.setLore(EmpireWand1);
    75. EmpireWand.setItemMeta(im);
    76. p.getInventory().addItem(new ItemStack[] { EmpireWand });
    77. }
    78. //Scoreboard begint hier!
    79. ScoreboardManager manager = Bukkit.getScoreboardManager();
    80. Scoreboard board = manager.getNewScoreboard();
    81. p.setScoreboard(board);
    82.  
    83. final Objective objective = board.registerNewObjective("pvpscore", "dummy");
    84.  
    85. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    86. objective.setDisplayName(ChatColor.WHITE + "[" + ChatColor.BLUE + ChatColor.BOLD + "CastleDefend" + ChatColor.WHITE + "]");
    87.  
    88. Score Nexus = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Nexus 1:"));
    89. Score Nexus2e = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.WHITE + "Nexus 2:"));
    90. Score Players = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.WHITE + "Players:"));
    91. Nexus.setScore(Nexus1);
    92. Nexus2e.setScore(Nexus2);
    93. Players.setScore(Lobby.size());
    94. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    95.  
    96. public void run(){
    97. ScoreboardManager manager = Bukkit.getScoreboardManager();
    98. Scoreboard board = manager.getNewScoreboard();
    99. p.setScoreboard(board);
    100. final Objective objective = board.registerNewObjective("pvpscore", "dummy");
    101. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    102. objective.setDisplayName(ChatColor.WHITE + "[" + ChatColor.BLUE + ChatColor.BOLD + "CastleDefend" + ChatColor.WHITE + "]");
    103. Score Nexus = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Nexus 1:"));
    104. Score Nexus2e = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Nexus 2:"));
    105. Score Players = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Players:"));
    106. Nexus.setScore(Nexus1);
    107. Nexus2e.setScore(Nexus2);
    108. Players.setScore(Lobby.size());
    109. }
    110. }, 0L, 10L);
    111. }
    112. }
    113. }
    114. @EventHandler
    115. public void Teams(PlayerInteractEvent e) {
    116. if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    117. if ((e.getClickedBlock().getState() instanceof Sign)) {
    118. Sign s = (Sign)e.getClickedBlock().getState();
    119. if (s.getLine(2).equalsIgnoreCase("CastleDefend!")) {
    120. final Player p = e.getPlayer();
    121. if (Attacker.size() >= Defender.size()){
    122. Defender.put(p, null);
    123. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "Je bent een " + ChatColor.GOLD + "Defender!");
    124. }else{
    125. Attacker.put(p, null);
    126. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "Je bent een " + ChatColor.GOLD + "Attacker!");
    127. }
    128. }}}
    129.  
    130. @EventHandler
    131. public void onShoot(EntityShootBowEvent e) throws Exception {
    132. if (((e.getEntity() instanceof Player)) &&
    133. (e.getBow().hasItemMeta()) &&
    134. (e.getBow().getItemMeta().getDisplayName().equals("Test"))) {
    135. Player p = (Player)e.getEntity();
    136. {
    137. Location location = p.getEyeLocation();
    138. World world = p.getWorld();
    139. BlockIterator blocksToAdd = new BlockIterator(location, 0, 25);
    140. Location blockToAdd;
    141. while(blocksToAdd.hasNext()) {
    142. FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
    143. FireworkEffect fe = FireworkEffect.builder().with(Type.BURST).withColor(Color.PURPLE).withFade(Color.BLACK).flicker(true).build();
    144. blockToAdd = blocksToAdd.next().getLocation();
    145. ParticleEffect.LARGE_SMOKE.display(blockToAdd, 0.1F, 0.1F, 0.1F, 0.1F, 5);
    146. ParticleEffect.WITCH_MAGIC.display(blockToAdd, 0.1F, 0.1F, 0.1F, 0.1F, 5);
    147. ParticleEffect.HAPPY_VILLAGER.display(blockToAdd, 0.1F, 0.1F, 0.1F, 0.1F, 5);
    148. ParticleEffect.ENCHANTMENT_TABLE.display(blockToAdd, 0.600F, 0.600F, 0.600F, 0.1F, 5);
    149. }
    150. Arrow Arrow = (Arrow) e.getEntity();
    151. Location loc = Arrow.getLocation();
    152. World world2 = Arrow.getWorld();
    153. double x = loc.getX();
    154. double y = loc.getY();
    155. double z = loc.getZ();
    156.  
    157. world2.createExplosion(x, y, z, 1, false, false);
    158. }
    159. }
    160. }
    161. @EventHandler
    162. public void onHit(EntityDamageByEntityEvent e) {
    163. Entity e1 = e.getEntity();
    164. Entity e2 = e.getDamager();
    165. if (((e1 instanceof Player)) && ((e2 instanceof WitherSkull))) {
    166. Player player = (Player)e1;
    167. e2.getWorld().playSound(e2.getLocation(), Sound.WITHER_HURT, 1.0F, 0.0F);
    168. }
    169. }
    170. @EventHandler
    171. public void onPlayerInteract(PlayerInteractEvent e) {
    172. if (e.getAction() != Action.RIGHT_CLICK_AIR) return;
    173.  
    174. if (e.getItem().getType() != Material.EMERALD) return;
    175.  
    176. e.getPlayer().playEffect(e.getPlayer().getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    177. this.menu.show(e.getPlayer());
    178. }
    179. @EventHandler
    180. public void Leave(PlayerInteractEvent e) { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    181. if ((e.getClickedBlock().getState() instanceof Sign)) {
    182. Sign s = (Sign)e.getClickedBlock().getState();
    183. if (s.getLine(0).equalsIgnoreCase("§c[Leave]")) {
    184. Player p = e.getPlayer();
    185. if (this.Lobby.containsKey(p)) {
    186. this.Lobby.remove(p);
    187. Location loc = new Location(p.getWorld(), 204.68487, 110, 267.37788);
    188. p.teleport(loc);
    189. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "Je hebt CastleDefend geleaved!");
    190. Bukkit.broadcastMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + p.getName() + " is geleaved! " + ChatColor.GOLD + "[" + ChatColor.RED + Lobby.size() + "/24" + ChatColor.GOLD + "]");
    191. } else {
    192. p.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "Je zit niet in CastleDefend!");
    193. }
    194. }
    195. } }
    196.  
    197. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    198. {
    199. if ((commandLabel.equalsIgnoreCase("cd")) || (commandLabel.equalsIgnoreCase("castledefence"))) {
    200. Player p = (Player)sender;
    201. p.sendMessage(ChatColor.GOLD + "0o==========" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "==========o0");
    202. p.sendMessage(ChatColor.RED + "/cd help " + ChatColor.WHITE + "- Laat je dit menu zien");
    203. p.sendMessage(ChatColor.GOLD + "0o=================================o0");
    204. }
    205. return false;
    206. }
    207. @EventHandler
    208. public void blockBreak(BlockBreakEvent e) throws Exception {
    209. Location Nexus1 = new Location(Bukkit.getWorld("CastleDefend"), 277.58666, 70, 262.44370);
    210. Location Firework = new Location(Bukkit.getWorld("Humilation"), 1582.38222D, 14.0D, -705.40920000000006D);
    211.  
    212. Block block = Nexus1.getBlock();
    213.  
    214. if (e.getBlock().getLocation().equals(block.getLocation()))
    215. {
    216. Player p = e.getPlayer();
    217. FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
    218. FireworkEffect fe = FireworkEffect.builder().with(Type.STAR).withColor(Color.AQUA).withFade(Color.BLACK).flicker(true).build();
    219. World world2 = Bukkit.getWorld("CastleDefend");
    220. Bukkit.broadcastMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De nexus is gedamaged door " + p.getName() + "!");
    221. this.Nexus1 -= 1;
    222. block.getWorld().playEffect(block.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    223. block.getWorld().playEffect(block.getLocation(), Effect.ENDER_SIGNAL, 1);
    224. p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1.0F, 0.5F);
    225. Location loc = new Location(world2, 277.58666, 70, 262.44370);
    226. Location loc2 = new Location(world2, 277.58666, 77, 262.44370);
    227. fplayer.playFirework(world2, loc2, fe);
    228. ParticleEffect.FIREWORKS_SPARK.display(loc, 0.5F, 0.0F, 0.5F, 1.0F, 500);
    229. ParticleEffect.WITCH_MAGIC.display(loc, 0.5F, 0.0F, 0.5F, 1.0F, 500);
    230. if (this.Nexus1 >= 0) {
    231. e.setCancelled(true);
    232. if (this.Nexus1 == 0)
    233. Bukkit.broadcastMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De attackers hebben Nexus 1 gesloopt!");
    234. if (this.Nexus1 == 0)
    235. block.getWorld().createExplosion(block.getLocation(), 0.0F);
    236. if (this.Nexus1 == 0)
    237. p.playSound(p.getLocation(), Sound.AMBIENCE_THUNDER, 1.0F, 1.0F);
    238. if (this.Nexus1 == 0)
    239. p.playSound(p.getLocation(), Sound.ANVIL_BREAK, 1.0F, 0.5F);
    240. if (this.Nexus1 == 0)
    241. p.playSound(p.getLocation(), Sound.ENDERDRAGON_GROWL, 1.0F, 0.5F);
    242. if (this.Nexus1 == 0)
    243. p.playSound(p.getLocation(), Sound.ENDERDRAGON_HIT, 1.0F, 0.5F);
    244. if (this.Nexus1 == 0)
    245. p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, 0.5F);
    246. if (this.Nexus1 == 0)
    247. block.setType(Material.BEDROCK);
    248. }
    249. }else if (Lobby.size() == 4){
    250. for (Player key : Lobby.keySet()){
    251. Game.put(key, null);
    252. Lobby.remove(key);
    253. Bukkit.broadcastMessage("ff om te testen of dit wel werkt!");
    254. }
    255. for (Player key1 : Attacker.keySet()){
    256. key1.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De Game is begonnen! Vernietig de nexussen!");
    257. Location loc = new Location(key1.getWorld(), 288.02, 69.05, 272.02);
    258. key1.teleport(loc);
    259. }
    260. for (Player key2 : Defender.keySet()){
    261. key2.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + ChatColor.BOLD + "CastleDefend" + ChatColor.GOLD + "] " + ChatColor.GREEN + "De Game is begonnen! Bescherm het Kasteel!");
    262. Location loc2 = new Location(key2.getWorld(), 280.02, 70.05, 272.02);
    263. key2.teleport(loc2);
    264. }
    265.  
    266. }
    267.  
    268. }
    269. }


    @felixfritz

    Anyone ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. BlazeEyezz
    NEVER store an instance of a player in a HashMap, as it can cause memory leaks. Instead, store their name.
     
  9. Offline

    BlazeEyezz

  10. Offline

    afistofirony

    BlazeEyezz I'm not sure if this has anything to do with it, but your HashMap declarations might be ambiguous and thus confuse the compiler. Also, you shouldn't store players in collections:
    Code:
    HashMap<String, String> lobby = new HashMap<String, String>();
    Then you should store their names.

    (PS: if you're using Java 7, that initialisation can be reduced to new HashMap<>().)
     
  11. BlazeEyezz
    Replace Player in your hashmap with String. Then when you add a key to it, such as a player's name, just do "mapName.put(player.getName)"
     
  12. Offline

    neol3108

    Why the hell would it cause memory leaks?
     
  13. Offline

    xTrollxDudex

    The Gaming Grunts
    It's actually not recommended to store player objects, not NEVER. If you know how to handle and track and take the trouble to remove and or use a WeakReference or a WeakHashMap to store player objects, then you should be fine.
    neol3108
    Do you even code? Player objects are kept in memory even after they leave, so the Java GC can't garbage collect them. This causes all sorts of problems such as keeping the chunk in memory and such. ferrybig and Double0negative can explain.
     
  14. Offline

    neol3108

    xTrollxDudex
    Kind of embarrasing i didn't knew that :S
    And yes I code..
     
  15. Offline

    xTrollxDudex

    neol3108
    Great! You learn something new everyday don't you? ;)
     
  16. Offline

    neol3108

    Yes I do :D
     
  17. Offline

    Bart

    I've only skim-read but did you mean >= 4? == 4 will of course only execute if the player count is exactly 4.
     
  18. Offline

    ZeusAllMighty11 Retired Staff

    neol3108

    The problem isn't really just a memory issue, but moreso a confusion for the JVM. A player object should not be stored, because if not dealt with correctly, you can end up with two player objects that should be the same, but are not... but they are (but not really).

    By using a Weak Reference, you can ensure (Yes?) the object will be cleaned by the garbage collector.

    V10lator said:
    A String is also much smaller, as string bytes are not expensive as all the bytes in a Player object.

    ferrybig says:

    So basically the same thing as above.

    Memory Leaks
     
Thread Status:
Not open for further replies.

Share This Page