How to do this?

Discussion in 'Plugin Development' started by Sarcry, Sep 3, 2016.

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

    Sarcry

    How do I do a timer that sends a message to a player that's in an arraylist?
     
  2. Offline

    AlvinB

  3. Offline

    Sarcry

  4. Offline

    AlvinB

    @Sarcry
    Of course! Just send the "■" as a normal chat messages. In fact, you can make nice little chat games with it, although they are a little glitchy. Here's a snake game I made a while ago:
     
    bwfcwalshy likes this.
  5. Offline

    Sarcry

  6. Offline

    bwfcwalshy Retired Staff

    @AlvinB That is so cool! Mind PMing me the source? Would honestly love to check that out

    @Sarcry Save as UTF-8
     
  7. Offline

    Sarcry

    Oh ok, and what does UTF-8 mean?
     
  8. Offline

    bwfcwalshy Retired Staff

    @Sarcry Its an encoding, google it for more info on encoding. It just means it supports that character and many others
     
  9. Offline

    Sarcry

    Oh. Now I've got another problem. It isnt working(Not getting error, just not executing). The code:
    Code:
    public void PlayerFrozen() {
            Player player = (Player) Bukkit.getServer().getOnlinePlayers();
            Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    
    
                @Override
                public void run() {
                    if (plugin.frozen.contains(player.getName())) {
                        player.sendMessage(ChatColor.WHITE + "■■■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■■■■");
                        player.sendMessage(ChatColor.WHITE + "■■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■" + ChatColor.RED + "■" + ChatColor.WHITE + "■■■");
                        player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■" + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                        player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■" + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                        player.sendMessage(ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■" + ChatColor.BLACK + "■" + ChatColor.GOLD + "■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                        player.sendMessage(ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■■■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                        player.sendMessage(ChatColor.RED + "■" + ChatColor.GOLD + "■■■" + ChatColor.BLACK + "■" + ChatColor.GOLD + "■■■" + ChatColor.RED + "■");
                        player.sendMessage(ChatColor.RED + "■■■■■■■■■");
                        player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "!" + ChatColor.GRAY + "]" + ChatColor.RED + "You have been frozen. You have " + plugin.getConfig().getInt("Time") + " to join teamspeak. The IP is: " + plugin.getConfig().getString("teamspeak-ip"));
                    }
                }
    
            }, 3 * 20);
    
        }
    
    plugin is main class and frozen is a list with <String>
     
  10. Offline

    bwfcwalshy Retired Staff

    @Sarcry You are trying to get 1 player from a collection of players. That will not work and will throw an error. Why are you casting Plugin? You wont need to if it's the main class. You should also store UUID in list rather than name. Oh and follow naming conventions of methods.
     
  11. Offline

    AlvinB

  12. Offline

    Sarcry

    Oh. The casting Plugin fixed it. And how do I get that player without getting error?
     
  13. Offline

    AlvinB

    @Sarcry
    Well, Bukkit.getOnlinePlayers() gives you everyone online on the server, and I assume you don't want to send everyone the message, so who exactly do you want to send the message to?
     
  14. Offline

    Sarcry

    I want to send a message to a player that's in an arraylist.
     
  15. Offline

    bwfcwalshy Retired Staff

    @Sarcry For loop the array list.
     
  16. Offline

    Sarcry

    What do you mean? I don't get it
     
  17. Offline

    bwfcwalshy Retired Staff

    @Sarcry Learning the basics of Java would help you a lot. You wouldn't have an issue at all in this code if you learnt Java.
     
  18. Offline

    Sarcry

    I mean I learnt basics of Java and know for loop and so forth. But when I tried to loop the arraylist didnt work
     
  19. Offline

    timtower Moderator Moderator

    @Sarcry What have you tried then?
     
  20. Offline

    bwfcwalshy Retired Staff

    @Sarcry Show us what you tried. We can't tell you the issue if we can't see any code. We may know programming but we can't just look at your code without you posting it.
     
  21. Offline

    Sarcry

    This is what I've tried
    Code:
    public void PlayerFrozen() {
    
            Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
    
                @Override
                public void run() {
                    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                        if (plugin.frozen.contains(player.getPlayer().getName())) {
                            player.sendMessage(ChatColor.WHITE + "■■■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■■■■");
                            player.sendMessage(ChatColor.WHITE + "■■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(
                                    ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■" + ChatColor.BLACK
                                            + "■" + ChatColor.GOLD + "■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■■■■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.RED + "■" + ChatColor.GOLD + "■■■" + ChatColor.BLACK + "■"
                                    + ChatColor.GOLD + "■■■" + ChatColor.RED + "■");
                            player.sendMessage(ChatColor.RED + "■■■■■■■■■");
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "!" + ChatColor.GRAY + "]"
                                    + ChatColor.RED + "You have been frozen. You have " + plugin.getConfig().getInt("Time")
                                    + " to join teamspeak. The IP is: " + plugin.getConfig().getString("teamspeak-ip"));
                        }
    
                    }
                }
            }, 3 * 20);
    
        }
    
     
    Last edited: Sep 6, 2016
  22. Offline

    AlvinB

    @Sarcry
    And what doesn't work about it?
     
  23. Offline

    DoggyCode™

    Bro, if you really listened to what people tell you, you would already have fixed the problem.

    It's called a BukkitRunnable

    Code:java
    1. ArrayList<Player> players = new ArrayList<Player>();
    2. //add all players you want to send a message to to this arraylist
    3.  
    4. new BukkitRunnable() {
    5. public void run() {
    6. // What you want to schedule goes here
    7. for(Player player : players) {
    8. player.sendMessage("your message");
    9. }
    10. }
    11. }.runTaskLater(this.plugin, 20*10); //20*10 = 10 seconds


    This will execute in 10 seconds.
     
  24. Offline

    Sarcry

    still not working
     
  25. Offline

    Zombie_Striker

    @Sarcry
    Post your updated codel, along with what you mean by "not working". Has anything changed? Have you debugged?
     
  26. Offline

    DoggyCode™

    Posy potentional errors or better just your whole code

    Sent from my SM-N9005 using Tapatalk
     
  27. Offline

    Sarcry

    Code:
    public void PlayerFrozen() {
    
            new BukkitRunnable() {
                public void run() {
                    for (Player player : plugin.frozen) {
                        if (plugin.frozen.contains(player.getPlayer())) {
                            player.sendMessage(ChatColor.WHITE + "■■■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■■■■");
                            player.sendMessage(ChatColor.WHITE + "■■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(
                                    ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■" + ChatColor.BLACK
                                            + "■" + ChatColor.GOLD + "■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■■■■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.RED + "■" + ChatColor.GOLD + "■■■" + ChatColor.BLACK + "■"
                                    + ChatColor.GOLD + "■■■" + ChatColor.RED + "■");
                            player.sendMessage(ChatColor.RED + "■■■■■■■■■");
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "!" + ChatColor.GRAY + "]"
                                    + ChatColor.RED + "You have been frozen. You have " + plugin.getConfig().getInt("Time")
                                    + " to join teamspeak. The IP is: " + plugin.getConfig().getString("teamspeak-ip"));
                       
                        }
                    }
                }
            }.runTaskLater(plugin, 20*5);
    
        }
    
     
  28. Offline

    bwfcwalshy Retired Staff

    @Sarcry Your looping through the list? Then getting an object from its own object therefor just returning the same object then checking if they are in the list? I highly doubt they got removed from the list in the 100 macroseconds between the 2 things.

    And please answer the questions rather than just posting code
     
  29. Offline

    Zombie_Striker

    This will always be true. There is no way a player that is in "forzen" will also not be in frozen. Remove the if statement.

    What is not working? Do you ever call the method?
     
    bwfcwalshy likes this.
  30. Offline

    Sarcry

    What I mean that isn't executing when a player is added into the arraylist [(plugin.frozen) + (plugin is the main class and I use plugin.frozen because frozen it's on the main too)] I tried more stuff but not working still. I want to detect if any of the online players are in the list. If anybody is, I would like to send them the message. Here's the actual code:

    Code:
        public void PlayerFrozen() {
    
            new BukkitRunnable() {
                public void run() {
                    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                        if (plugin.frozen.contains(player)) {
                            player.sendMessage(ChatColor.WHITE + "■■■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■■■■");
                            player.sendMessage(ChatColor.WHITE + "■■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(ChatColor.WHITE + "■■" + ChatColor.RED + "■" + ChatColor.GOLD + "■"
                                    + ChatColor.BLACK + "■" + ChatColor.GOLD + ChatColor.WHITE + "■■");
                            player.sendMessage(
                                    ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■" + ChatColor.BLACK
                                            + "■" + ChatColor.GOLD + "■■" + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.WHITE + "■" + ChatColor.RED + "■" + ChatColor.GOLD + "■■■■■"
                                    + ChatColor.RED + "■" + ChatColor.WHITE + "■");
                            player.sendMessage(ChatColor.RED + "■" + ChatColor.GOLD + "■■■" + ChatColor.BLACK + "■"
                                    + ChatColor.GOLD + "■■■" + ChatColor.RED + "■");
                            player.sendMessage(ChatColor.RED + "■■■■■■■■■");
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "!" + ChatColor.GRAY + "]"
                                    + ChatColor.RED + "You have been frozen. You have " + plugin.getConfig().getInt("Time")
                                    + " to join teamspeak. The IP is: " + plugin.getConfig().getString("teamspeak-ip"));
                        }
                       
                    }
                }
            }.runTaskLater(plugin, 20*5);
    
        }
    
    [teamspeak-ip and time are on the config all set]
     
Thread Status:
Not open for further replies.

Share This Page