Need help with playerjoin event.

Discussion in 'Plugin Development' started by sailorerik, May 14, 2014.

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

    sailorerik

    Hello,
    Me and my friend made a plugin what vanish all players who is online.
    But if anyone connect, then joined player isnt vanished.
    Code:
    Code:java
    1. package me.sailorerik.mobs;
    2.  
    3. import java.util.ArrayList;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerDropItemEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class main extends JavaPlugin
    17. implements Listener
    18. {
    19. ArrayList<Player> cooldown = new ArrayList();
    20.  
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. @EventHandler
    26. public void onPlayerJoin(PlayerJoinEvent event) {
    27. Player player = event.getPlayer();
    28. ItemStack is = new ItemStack(Material.SLIME_BALL);
    29. if (!player.getInventory().contains(is))
    30. {
    31. player.getInventory().addItem(new ItemStack[] { is });
    32. }
    33. }
    34.  
    35. @EventHandler
    36. public void Nodrop(PlayerDropItemEvent e)
    37. {
    38. if (e.getItemDrop().getItemStack().getType()
    39. .equals(Material.SLIME_BALL))
    40. e.setCancelled(true);
    41. else if (e.getItemDrop().getItemStack().getType().equals(Material.MAGMA_CREAM))
    42. e.setCancelled(true);
    43. }
    44.  
    45. @EventHandler
    46. public void playerInteract(PlayerInteractEvent event)
    47. {
    48. Player player = event.getPlayer();
    49. final Player player2 = event.getPlayer();
    50. Action action = event.getAction();
    51. ItemStack Slime = new ItemStack(Material.MAGMA_CREAM);
    52. ItemStack magma = new ItemStack(Material.SLIME_BALL);
    53.  
    54. if ((player.getItemInHand().getType().equals(Material.SLIME_BALL)) && ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK))) {
    55. if (this.cooldown.contains(player2)) {
    56. player2.sendMessage("&cError: &4You are not allowed to use Slimeball too often.");
    57. }
    58. else
    59. {
    60. for (Player p : Bukkit.getServer().getOnlinePlayers())
    61. player.showPlayer(p);
    62. player.sendMessage("§aAll Players: §cShown!");
    63.  
    64. player.setItemInHand(Slime);
    65.  
    66. this.cooldown.add(player2);
    67. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    68. public void run() {
    69. main.this.cooldown.remove(player2);
    70. }
    71. }
    72. , 150L);
    73. }
    74.  
    75. }
    76. else if ((player.getItemInHand().getType().equals(Material.MAGMA_CREAM)) && ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)))
    77. if (this.cooldown.contains(player2)) {
    78. player2.sendMessage("&cError: &4You are not allowed to use Magma Cream too often.");
    79. }
    80. else
    81. {
    82. for (Player p : Bukkit.getServer().getOnlinePlayers())
    83. player.hidePlayer(p);
    84. player.sendMessage("§aAll Players: §cHidden!");
    85.  
    86. player.setItemInHand(magma);
    87. this.cooldown.add(player2);
    88. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    89. public void run() {
    90. main.this.cooldown.remove(player2);
    91. }
    92. }
    93. , 150L);
    94. return;
    95. }
    96. }
    97. }
     
  2. sailorerik Keep a list of all the players who don't want to see players. On join, hide the player from all of those players.
     
  3. Offline

    Bailey Payne

    You could create a HashMap to store the all players in, add them to the HashMap when they join the server and then hide everyone in the HashMap, that should work...I think.
     
  4. Offline

    sailorerik

    How i do it then? If anyone can waste time and give code
     
  5. Offline

    tommyhoogstra

    I dont really understand your english (obviously pretty bad at it), but I noticed your Itemstack variables for slimeball and magma cream are the wrong way around.

    Code:java
    1. ItemStack Slime = new ItemStack(Material.MAGMA_CREAM);
    2. ItemStack magma = new ItemStack(Material.SLIME_BALL);


    Kind of annoyed me
     
  6. Offline

    sailorerik

    Yes, am sorry for my bad english, and i fixed it.
    But still i need code for hiding players when they connect.
     
  7. Offline

    Bailey Payne

    Step 1: Create The HashMap
    Step 2: Create a onPlayerJoinEvent and add players to it when they join
    Step 3: Hide Everyone in HashMap
    Step 4: Remove player from HashMap when they leave

    The reason your code isn't working is because it checked for players who were online at the time of hiding them, so whenever a new player joins they will be visible since there is no code to update it.

    I would add the code of the steps if I had my coding IDE, its a bit tricky without it, if the issue is still not solved when I am home and am able to use my IDE I will write out the code for you.

    EDIT:

    This may be easier;
    Step 1: Create ArrayList

    ArrayList al = new ArrayList():

    Step 2: Add Players when they join the server

    assume I have done the correct onPlayerJoinEvent and I have initialized the Player entity (Player p = event.getPlayer();)

    al.add(p);

    Step 3: Hide everyone in the ArrayList (done nothing here since you should know how to hide the players!)

    Step 4: Remove Players from the ArrayList on leaving

    assume I have the correct player leaving event

    al.remove(p);


    That Should work.
     
  8. Offline

    sailorerik

    Alright.
    If you are Home, then add code.

    All bests,
    Sailorerik
     
Thread Status:
Not open for further replies.

Share This Page