Redstone... with timer...

Discussion in 'Plugin Development' started by MrGermanrain, Aug 21, 2013.

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

    MrGermanrain

    I am making a plugin that gives vanishes you for 7 seconds once you right click a redstone...
    this is my first project really working with a timer.

    Here is my code:

    Code:java
    1. package me.mrgermanrain.scheal;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14.  
    15.  
    16. public class scheal extends JavaPlugin implements Listener{
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static scheal plugin;
    19. public int number = 7;
    20.  
    21. @Override
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " has been Disabled!");
    25. }
    26.  
    27. @Override
    28. public void onEnable() {
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been Enabled!");
    31. getServer().getPluginManager().registerEvents(this, this);
    32. }
    33.  
    34. @EventHandler
    35. public void PlayerIteractEvent(PlayerInteractEvent e){
    36.  
    37. if(e.getItem().getType() == Material.REDSTONE){
    38. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    39. final Player player = e.getPlayer();
    40. for (final Player p : plugin.getServer().getOnlinePlayers()) {
    41. p.hidePlayer(p);
    42. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy" + ChatColor.GRAY + "] " + "You have now been vanished for 7 seconds");
    43. this.getServer().getScheduler().scheduleSyncRepeatingTask(
    44. this,
    45. new Runnable() {
    46. public void run(){
    47. if(number != -1){
    48. if(number != 7){
    49. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy"+ ChatColor.GRAY + "] " + number);
    50. number--;
    51. }else{
    52. p.showPlayer(player);
    53. p.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy"
    54. + ChatColor.GRAY + "] " + "You are now unvanished!");
    55. number--;
    56. }
    57. }
    58. }
    59. },
    60. 0L,
    61. 20L);
    62.  
    63. }
    64.  
    65. }
    66.  
    67.  
    68. }
    69. }
    70.  
    71. }
    72.  
    73.  


    I don't get error messages though when I right click nothing happens.
     
  2. Offline

    Rockon999

    MrGermanrain
    Okay... first thing is first :p
    .hidePlayer(Player p) hides the player inputted from the player its called for... so to hide that player from everyone you'd do:
    Code:java
    1. Player p = (Player) event.getEntity();
    2. for(Player pl : Bukkit.getOnlinePlayers()){
    3. pl.hidePlayer(p);
    4. }

    :)

    MrGermanrain
    Okay... after some work I rewrote this, and I hope you can learn from it :)
    Task:
    Code:
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Entry<String, Long> en : players.entrySet()) {
                        Player p = Bukkit.getPlayerExact(en.getKey());
                        if (System.currentTimeMillis() - en.getValue() >= 7000) {
                            for (Player pl : Bukkit.getOnlinePlayers()) {
                                pl.showPlayer(Bukkit.getPlayerExact(en.getKey()));
                            }
                            players.remove(en.getKey());
                            p.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy" + ChatColor.GRAY + "] " + "You are now unvanished!");
                        } else {
                            p.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy" + ChatColor.GRAY + "] " + (7 - ((System.currentTimeMillis() - en.getValue()) / 1000)));
                        }
                    }
                }
            }, 0L, 20L);
    Listener:

    Code:
    @EventHandler
        public void PlayerIteractEvent(PlayerInteractEvent e) {
            if (e.getItem().getType() == Material.REDSTONE) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    final Player p = e.getPlayer();
                    for (Player pl : Bukkit.getOnlinePlayers()) {
                        pl.hidePlayer(p);
                    }
                    players.put(p.getName(), System.currentTimeMillis());
                    p.sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_AQUA + "Infamy" + ChatColor.GRAY + "] " + "You have now been vanished for 7 seconds");
                }
     
            }
     
        }
    HashMap:

    Code:
    final HashMap<String, Long> players = new HashMap<String, Long>();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  3. Offline

    MrGermanrain

    Thanks :D Ill see if this works!
     
  4. Offline

    Rockon999

    MrGermanrain
    I tested it and it is working for me :) (Well the timer part is... and I THINK the hiding is too... but I don't have a second player to view it so... you'll have to test that out yourself :p)
     
  5. Offline

    MrGermanrain

    thanks :D I am terrible with HashMaps I need to figure this out :p


    EDIT: Yes, I cant figure this out D:
     
Thread Status:
Not open for further replies.

Share This Page