Sending messages to only certain players

Discussion in 'Plugin Development' started by PimpDuck, Jun 26, 2014.

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

    PimpDuck

  2. PimpDuck
    Here's a rough example of what you could do:

    Code:java
    1. private ArrayList<Player> players = new ArrayList<Player>();
    2.  
    3. public void sendMessage(String message){
    4. for (Player p : Bukkit.getOnlinePlayers()){
    5. if (!players.contains(p))
    6. p.sendMessage(message);
    7. }
    8. }
     
  3. Offline

    JasonDL13

    EDIT: Well The Gaming Grunts beat me to it.

    Make a for-while loop, this will iterate through all people on the server if you loop through Bukkit.getServer().getOnlinePlayes(); Then check if the shhlotto contains player.getName();

    Example:

    Code:java
    1. List<String> muted = Arrays.asList("JasonDL13", "JasonDL14");
    2. //That's how you make an arraylist in one line of code
    3.  
    4. for(Player p : Bukkit.getOnlinePlayers()) {
    5. //This will loop through everyone online, p being the player
    6.  
    7. if(muted.contains(p.getName())) {
    8. //Muted contains the player that we are looping through
    9. p.sendMessage("You're Muted!");
    10. } else {
    11. //Muted doesn't contain the player that we are looping through
    12. p.sendMessage("You're Not Muted!");
    13. }
    14.  
    15. }


    So use something like that and only send people the message if they're not in your shh list.

    Also, you're variable names aren't really professional. Instead of "shhlist" I might use "mutedLottoMessages"
     
  4. Offline

    PimpDuck

    The Gaming Grunts
    I get this error :/

    Code:java
    1. [21:48:21 WARN]: [DCLottery] Task #104 for DCLottery v1.0 generated an exception
    2.  
    3. at me.PimpDuck.DCLottery.Main$1.run(Main.java:96) ~[?:?]
    4. at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java
    5. :53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    6. at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHea
    7. rtbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d89
    8. 43-b3078jnks]
    9. at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    10. 00) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    11. at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    12. 60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    13. at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    14. 58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    15. at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    16. :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    17. at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    18. 28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    19. >


    Main: http://paste.md-5.net/komebagoce.avrasm CommandClass: http://paste.md-5.net/huworegofu.avrasm
     
  5. Offline

    Gater12

    PimpDuck
    There are no players when the server first starts.
     
    The Gaming Grunts likes this.
  6. Offline

    PimpDuck

    I'm logged in?
     
  7. PimpDuck
    As Gater12 said. You're calling your method on startup, at which time there are no players online, resulting in your NPE.
     
  8. Offline

    PimpDuck

    How would I fix this? .-.

    The Gaming Grunts

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

    Gater12

    PimpDuck
    By checking if there are players online then loop through them.
     
  10. Offline

    PimpDuck

    Gater12

    Code:java
    1. public void Lottery()
    2. {
    3. taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. @SuppressWarnings({ "deprecation", "unused" })
    5. public void run() {
    6. if(seconds == 14400) {
    7. while(Bukkit.getOnlinePlayers() == null){
    8.  
    9. }
    10. for(Player p : Bukkit.getOnlinePlayers()){
    11. if(!CommandClass.shhlist.contains(p.getName())){
    12. int moneypot = getConfig().getInt("DCLottery.MoneyPot");
    13. p.sendMessage(prefix + ChatColor.BLUE + "The lottery will be drawn in " + ChatColor.RED + "4 hours!");
    14. p.sendMessage(prefix + ChatColor.BLUE + "Buy your lottery ticket with " + ChatColor.RED + "'/lottobuy [# of tickets]'");
    15. p.sendMessage(prefix + ChatColor.BLUE + "Get information on the lottery by doing " + ChatColor.RED + "'/lottoinfo'");
    16. alreadypayed = false;
    17. lastCachedTime = 14400;
    18. if((int) timers.getInt("LastCachedTime") != lastCachedTime){
    19. timers.set("LastCachedTime", lastCachedTime, false);
    20. }
    21. timers.saveFile();
    22. seconds--;
    23. }
    24. }



    Could I use something like the while loop?

    Code:java
    1. public void Lottery()
    2. {
    3. taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. @SuppressWarnings({ "deprecation", "unused" })
    5. public void run() {
    6. if(seconds == 14400) {
    7. while(Bukkit.getOnlinePlayers() == null){
    8. seconds = 14400;
    9. }
    10. for(Player p : Bukkit.getOnlinePlayers()){
    11. if(!CommandClass.shhlist.contains(p.getName())){
    12. int moneypot = getConfig().getInt("DCLottery.MoneyPot");
    13. p.sendMessage(prefix + ChatColor.BLUE + "The lottery will be drawn in " + ChatColor.RED + "4 hours!");
    14. p.sendMessage(prefix + ChatColor.BLUE + "Buy your lottery ticket with " + ChatColor.RED + "'/lottobuy [# of tickets]'");
    15. p.sendMessage(prefix + ChatColor.BLUE + "Get information on the lottery by doing " + ChatColor.RED + "'/lottoinfo'");
    16. alreadypayed = false;
    17. lastCachedTime = 14400;
    18. if((int) timers.getInt("LastCachedTime") != lastCachedTime){
    19. timers.set("LastCachedTime", lastCachedTime, false);
    20. }
    21. timers.saveFile();
    22. seconds--;
    23. }
    24. }


    I think I fixed it but I still get the error on "if(!CommandClass.shhlist.contains(p.getName())){
    "

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page