Magic Clock help!

Discussion in 'Plugin Development' started by webbhead, Feb 14, 2014.

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

    webbhead

    Well, I have started working on a Magic Clock plugin and I finished it but when I loaded it to my server it doesn't do anything it is supposed to in the code there is no errors at ALL!
    Here is my code:
    Code:java
    1. package me.webbhead.magicclock;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class MagicClock extends JavaPlugin implements Listener {
    16.  
    17. private ArrayList<Player> vanished = new ArrayList<Player>();
    18.  
    19. public void onEnable() {
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. }
    24.  
    25. public void onDisable() {
    26.  
    27. }
    28.  
    29. @EventHandler
    30. public void onPlayerInteract(PlayerInteractEvent e) {
    31. Player player = e.getPlayer();
    32. for (Player players : Bukkit.getOnlinePlayers()) {
    33. if (!vanished.contains(players)) {
    34. if (e.getAction() == Action.RIGHT_CLICK_BLOCK && player.getItemInHand().getType() == Material.WATCH) {
    35. player.hidePlayer(players);
    36. player.sendMessage(ChatColor.DARK_RED + "All players have been hidden!");
    37. }
    38. vanished.add(players);
    39. }
    40. else {
    41. if (e.getAction() == Action.RIGHT_CLICK_BLOCK && player.getItemInHand().getType() == Material.WATCH) {
    42. player.showPlayer(players);
    43. player.sendMessage(ChatColor.DARK_RED + "All players have been revealed!");
    44. }
    45. vanished.remove(players);
    46. }
    47.  
    48. }
    49. }
    50. }
    51.  
     
  2. Offline

    Blah1

    Are you sure there are no startup errors?
     
  3. Offline

    Skatedog27

    Try this:
    Code:java
    1. for (Player online : getServer().getOnlinePlayers) {
    2. Player player = e.getPlayer();
    3.  
    4. //How to hide the player:
    5. online.hidePlayer(player);
    6.  
    7. //Then to show the player again
    8. online.showPlayer(player);
    9. }
     
  4. Offline

    ShearsSheep

    Code:java
    1. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    2. if(this.players.contains(event.getPlayer().getName())){
    3. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    4. if(player.canSee(targetPlayers)){
    5. player.hidePlayer(targetPlayers);
    6. } else{
    7. event.setCancelled(true);

    Try this code out, I tried it in mine. And it did work.
     
  5. Offline

    webbhead

    ShearsSheep
    I actually got it working.. But I ran into another problem: Im the only one on the server i right click the Clock It sends me the messages correctly.
    However when there is 2 people on the server it sends me the message twice! Please help :/
     
  6. Offline

    rbrick

    That is because you have the server sending the player the message within the for loop. You are basically saying:
    for every player on the server, do this x amount of times.
     
  7. Offline

    webbhead

    rbrick
    So, how would I fix this?
     
  8. Offline

    rbrick

    Have the message out side of the for loop
     
  9. Offline

    ShearsSheep

    Do what rbrick said. It probobly should work.
     
  10. Offline

    webbhead

    rbrick ShearsSheep
    Thank you! I'll for SURE try this! :)

    rbrick ShearsSheep
    put the message like this:
    This screwed everything up It still shows double on each and the plugin stopped working right.
    player.sendMessage(ChatColor.RED + "All Players have been revealed!"
    if (e.getAction() == Action.RIGHT_CLICK_AIR && player.getItemInHand().getType() == Material.WATCH) {
    player.showPlayer(players);
    Well anyways I had to put it back and it still does it. :/ Any Suggestions or could you give me any example of taking it out of the "Loop" because I don't know if I did it right.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  11. Offline

    webbhead

Thread Status:
Not open for further replies.

Share This Page