Per world Broadcast.

Discussion in 'Plugin Development' started by Dablakbandit, Nov 22, 2013.

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

    Dablakbandit

    How do i send a message to a player if they are only in a specific world?

    Thanks in advanced!
    Dablakbandit
     
  2. Offline

    GusGold

    Dablakbandit
    Code:java
    1. if(player.getLocation().getWorld().getName().toLowerCase().equals("MyWorldName")){
    2. player.sendMessage("You are in MyWorldName!");
    3. }
     
  3. Offline

    Alshain01

    The only way I can think of is to iterate through getOnlinePlayers() and check their world.

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

    Dablakbandit

    Alshain01

    Thanks, i will test

    Alshain01

    Would you know how to get a list of worlds from a config file?

    structured like

    Code:
    Worlds:
      world
      world_nether
    etc

    And tell the player the listed worlds?

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

    GusGold

    Dablakbandit
    Btw, I was the one to first suggest it :p
    But to get from config, use
    Code:java
    1. List<String> myList = getConfig().getStringList("Path.To.List");
    and your config.yml would look like:
    Code:
    Path:
      To:
        List:
          - world
          - world_nether
    To tell the player all the world in the list:
    Code:java
    1. String myListString = "";
    2. for(String world : myList){
    3. myListString += " " +world;
    4. }
    5. player.sendMessage("The worlds are:" + myListString);
     
  6. Offline

    Alshain01

    To get it from a file you have to create a YAML interface. You can also get all the worlds on the server in a list Bukkit.getServer().getWorlds().
     
  7. Offline

    RainoBoy97

    Code:
    for(Player p : someWorld.getPlayers()) {
      p.sendMessage("hello");
    }
    
     
  8. Offline

    Garris0n

    You did .toLowerCase() and then compared it with something that isn't lowercase...which will always return false.
    It should be
    Code:java
    1. if(player.getWorld().getName().equals("MyWorldName"))
     
  9. Offline

    sanchixx

    Here you go:

    Code:java
    1. static void msgWorld(Player player, String msg)
    2. {
    3. World w = player.getWorld();
    4. for (Player players : Bukkit.getServer().getOnlinePlayers())
    5. {
    6. String name = players.getName();
    7. if(players.getWorld() == w)
    8. players.sendMessage(msg);
    9. }
    10. }
    11.  
    12. static void msgWorld(String world, String msg)
    13. {
    14. World w = Bukkit.getWorld(world);
    15. for (Player players : Bukkit.getServer().getOnlinePlayers())
    16. {
    17. String name = players.getName();
    18. if(players.getWorld() == w)
    19. players.sendMessage(msg);
    20. }
    21. }


    I can't edit my message but I made some better code so here it is;

    Code:java
    1. static void msgWorld(Player player, String msg)
    2. {
    3. World w = player.getWorld();
    4. for(Player p : w.getPlayers())
    5. {
    6. p.sendMessage(msg);
    7. }
    8. }
    9.  
    10. static void msgWorld(String world, String msg)
    11. {
    12. World w = Bukkit.getWorld(world);
    13. for(Player p : w.getPlayers())
    14. {
    15. p.sendMessage(msg);
    16. }
    17. }
     
  10. Offline

    Dablakbandit

    sanchixx Garris0n RainoBoy97 Alshain01 GusGold
    sorry for tagging you all, i just want someone to answer :p

    okay so my code is

    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.FileReader;
    6. import java.io.IOException;
    7. import java.io.InputStream;
    8. import java.io.InputStreamReader;
    9. import java.io.LineNumberReader;
    10. import java.nio.channels.Channels;
    11. import java.nio.channels.ReadableByteChannel;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.plugin.PluginDescriptionFile;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19.  
    20. public class Main extends JavaPlugin{
    21. public static int currentLine = 0;
    22. public static int tid = 0;
    23. public static int running = 1;
    24.  
    25.  
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = getDescription();
    28. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!" );
    29. }
    30.  
    31. public void onEnable() {
    32. PluginDescriptionFile pdfFile = getDescription();
    33. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    34. if (this.getDataFolder().exists())
    35. {
    36. this.getLogger().info("File is there.");
    37. }else
    38. {
    39. createConfig(new File(this.getDataFolder(), "config.yml"));
    40. this.getLogger().info("File didn't exist");
    41. }
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50. final File msgs =new File(getDataFolder() + File.separator + "messages.txt");
    51. if(!msgs.exists()){
    52. try {
    53. msgs.createNewFile();
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. }
    57. }
    58.  
    59. tid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    60.  
    61. @Override
    62. public void run() {
    63.  
    64. try{
    65. broadcastMessages(msgs.getPath());
    66. }catch(IOException e){
    67. e.printStackTrace();
    68. }
    69. }
    70.  
    71. }, 0, this.getConfig().getLong("interval") * 20);
    72.  
    73.  
    74. }
    75.  
    76. public void createConfig(File f){
    77. InputStream cfgStream = this.getResource("config.yml");
    78. if (!this.getDataFolder().exists()){
    79. this.getDataFolder().mkdirs();
    80. }
    81. try {
    82. ReadableByteChannel rbc = Channels.newChannel(cfgStream);
    83. fos.getChannel().transferFrom(rbc, 0, 1 << 24);
    84. fos.close();
    85.  
    86. } catch (Exception e) {
    87. this.getLogger().info("There was an error in creating the config. Using bukkit methods to do so.");
    88. this.getConfig().options().copyDefaults(true);
    89. this.saveConfig();
    90. }
    91. }
    92.  
    93. public void broadcastMessages(String filename) throws IOException{
    94.  
    95. if(numberOfOnlinePlayers() >= 1){
    96.  
    97.  
    98. fs = new FileInputStream(filename);
    99. for(int i = 0; i < currentLine; ++i)
    100. br.readLine();
    101. String line = br.readLine();
    102. String ann = this.getConfig().getString("announcerName");
    103. line = line.replaceAll("&0", ChatColor.BLACK + "");
    104. line = line.replaceAll("&l", ChatColor.BOLD + "");
    105. line = line.replaceAll("&3", ChatColor.DARK_AQUA + "");
    106. line = line.replaceAll("&1", ChatColor.DARK_BLUE + "");
    107. line = line.replaceAll("&8", ChatColor.DARK_GRAY + "");
    108. line = line.replaceAll("&2", ChatColor.DARK_GREEN + "");
    109. line = line.replaceAll("&4", ChatColor.DARK_RED + "");
    110. line = line.replaceAll("&6", ChatColor.GOLD + "");
    111. line = line.replaceAll("&7", ChatColor.GRAY + "");
    112. line = line.replaceAll("&a", ChatColor.GREEN + "");
    113. line = line.replaceAll("&o", ChatColor.ITALIC + "");
    114. line = line.replaceAll("&k", ChatColor.MAGIC + "");
    115. line = line.replaceAll("&c", ChatColor.RED + "");
    116. line = line.replaceAll("&r", ChatColor.RESET + "");
    117. line = line.replaceAll("&m", ChatColor.STRIKETHROUGH + "");
    118. line = line.replaceAll("&n", ChatColor.UNDERLINE + "");
    119. line = line.replaceAll("&f", ChatColor.WHITE + "");
    120. line = line.replaceAll("&e", ChatColor.YELLOW + "");
    121. ann = ann.replaceAll("&0", ChatColor.BLACK + "");
    122. ann = ann.replaceAll("&l", ChatColor.BOLD + "");
    123. ann = ann.replaceAll("&3", ChatColor.DARK_AQUA + "");
    124. ann = ann.replaceAll("&1", ChatColor.DARK_BLUE + "");
    125. ann = ann.replaceAll("&8", ChatColor.DARK_GRAY + "");
    126. ann = ann.replaceAll("&2", ChatColor.DARK_GREEN + "");
    127. ann = ann.replaceAll("&4", ChatColor.DARK_RED + "");
    128. ann = ann.replaceAll("&6", ChatColor.GOLD + "");
    129. ann = ann.replaceAll("&7", ChatColor.GRAY + "");
    130. ann = ann.replaceAll("&a", ChatColor.GREEN + "");
    131. ann = ann.replaceAll("&o", ChatColor.ITALIC + "");
    132. ann = ann.replaceAll("&k", ChatColor.MAGIC + "");
    133. ann = ann.replaceAll("&c", ChatColor.RED + "");
    134. ann = ann.replaceAll("&r", ChatColor.RESET + "");
    135. ann = ann.replaceAll("&m", ChatColor.STRIKETHROUGH + "");
    136. ann = ann.replaceAll("&n", ChatColor.UNDERLINE + "");
    137. ann = ann.replaceAll("&f", ChatColor.WHITE + "");
    138. ann = ann.replaceAll("&e", ChatColor.YELLOW + "");
    139.  
    140.  
    141. Bukkit.getServer().broadcastMessage(ann + ChatColor.WHITE + line);
    142. LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
    143. lnr.skip(Long.MAX_VALUE);
    144. int lastLine = lnr.getLineNumber();
    145. if(currentLine + 1 == lastLine + 1){
    146. currentLine = 0;
    147. } else {
    148. currentLine++;
    149. }
    150. }
    151. }
    152. public static int numberOfOnlinePlayers() {
    153. final Player[] onlinePlayers = Bukkit.getServer().getOnlinePlayers();
    154. return onlinePlayers.length;
    155. }
    156. }


    How would i do this per world?, as i have tried but am out of ideas.

    Thanks,
    Dablakbandit
     
  11. Offline

    AoH_Ruthless

    Dablakbandit
    Do what per world, exactly??

    And they all gave you helpful ways to get the world.
     
  12. Offline

    Garris0n

    Instead of all the replaceAll stuff you can just do this:
    Code:java
    1. line = ChatColor.translateAlternateColorCodes('&', line);
    2. ann = ChatColor.translateAlternateColorCodes('&', ann);


    Second, you've already been given the code to broadcast messages to players in specific worlds.
     
  13. Offline

    Dablakbandit

    Garris0n

    Thanks for that!

    AoH_Ruthless

    I want it to get from a message<worldname>.txt (and create the txt if it doesnt exist) and broadcast those messages to people in that specific world, but im having problems on how to figure this out.

    Thanks,
    Dablakbandit
     
  14. Offline

    AoH_Ruthless

  15. Offline

    Dablakbandit

  16. Offline

    AoH_Ruthless

  17. Offline

    L33m4n123

  18. Offline

    Dablakbandit

    AoH_Ruthless

    I'm getting no errors from the current code, it broadcasts it to all worlds. But when trying to figure out how to make it different per world, i get stuck trying to figure it out.

    Thanks,
    Dablakbandit
     
  19. Offline

    AoH_Ruthless

    Dablakbandit
    Honestly, I would use a YAML file instead of a TXT file for that kind of stuff. Just personal preference.

    And many people previously told you how to do it in several different ways.
     
  20. Offline

    Dablakbandit

    AoH_Ruthless

    I know they have, but I'm having trouble implementing it.

    Thanks,
    Dablakbandit
     
  21. Offline

    AoH_Ruthless

    Dablakbandit
    Where are you posting this code, exactly? The more you actually tell us the less I have to ask these questions and the faster the issue can be resolved, hopefully.
     
  22. Offline

    Dablakbandit

    AoH_Ruthless

    Um? I don't understand?.

    Thanks,
    Dablakbandit
     
  23. Offline

    AoH_Ruthless

    Dablakbandit
    Where exactly are you trying to get the worlds?

    And by the way, your lines 106-141 or so can be summed up in one or 2 lines (read up someone already mentioned it).
     
  24. Offline

    Dablakbandit

    AoH_Ruthless

    Yes i fixed that.

    I haven't implemented getting worlds, as i don't understand how. Also making a int for each world? So that it can count the lines in the messages<worldname>.txt etc? I don't understand how to do this.

    Thanks,
    Dablakbandit
     
  25. Offline

    AoH_Ruthless

    Dablakbandit Post your new code again. As I asked before, why are you using a text file? Use a YAML file instead.
     
  26. Offline

    Dablakbandit

    AoH_Ruthless
    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.FileReader;
    6. import java.io.IOException;
    7. import java.io.InputStream;
    8. import java.io.InputStreamReader;
    9. import java.io.LineNumberReader;
    10. import java.nio.channels.Channels;
    11. import java.nio.channels.ReadableByteChannel;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.plugin.PluginDescriptionFile;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19.  
    20. public class Main extends JavaPlugin{
    21. public static int currentLine = 0;
    22. public static int tid = 0;
    23. public static int running = 1;
    24.  
    25.  
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = getDescription();
    28. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!" );
    29. }
    30.  
    31. public void onEnable() {
    32. PluginDescriptionFile pdfFile = getDescription();
    33. System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    34. if (this.getDataFolder().exists())
    35. {
    36. this.getLogger().info("File is there.");
    37. }else
    38. {
    39. createConfig(new File(this.getDataFolder(), "config.yml"));
    40. this.getLogger().info("File didn't exist");
    41. }
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50. final File msgs =new File(getDataFolder() + File.separator + "messages.txt");
    51. if(!msgs.exists()){
    52. try {
    53. msgs.createNewFile();
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. }
    57. }
    58.  
    59. tid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    60.  
    61. @Override
    62. public void run() {
    63.  
    64. try{
    65. broadcastMessages(msgs.getPath());
    66. }catch(IOException e){
    67. e.printStackTrace();
    68. }
    69. }
    70.  
    71. }, 0, this.getConfig().getLong("interval") * 20);
    72.  
    73.  
    74. }
    75.  
    76. public void createConfig(File f){
    77. InputStream cfgStream = this.getResource("config.yml");
    78. if (!this.getDataFolder().exists()){
    79. this.getDataFolder().mkdirs();
    80. }
    81. try {
    82. ReadableByteChannel rbc = Channels.newChannel(cfgStream);
    83. fos.getChannel().transferFrom(rbc, 0, 1 << 24);
    84. fos.close();
    85.  
    86. } catch (Exception e) {
    87. this.getLogger().info("There was an error in creating the config. Using bukkit methods to do so.");
    88. this.getConfig().options().copyDefaults(true);
    89. this.saveConfig();
    90. }
    91. }
    92.  
    93. public void broadcastMessages(String filename) throws IOException{
    94.  
    95. if(numberOfOnlinePlayers() >= 1){
    96.  
    97.  
    98. fs = new FileInputStream(filename);
    99. for(int i = 0; i < currentLine; ++i)
    100. br.readLine();
    101. String line = br.readLine();
    102. String ann = this.getConfig().getString("announcerName");
    103. line = ChatColor.translateAlternateColorCodes('&', line);
    104. ann = ChatColor.translateAlternateColorCodes('&', ann);
    105.  
    106.  
    107. Bukkit.getServer().broadcastMessage(ann + ChatColor.WHITE + line);
    108. LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
    109. lnr.skip(Long.MAX_VALUE);
    110. int lastLine = lnr.getLineNumber();
    111. if(currentLine + 1 == lastLine + 1){
    112. currentLine = 0;
    113. } else {
    114. currentLine++;
    115. }
    116. }
    117. }
    118. public static int numberOfOnlinePlayers() {
    119. final Player[] onlinePlayers = Bukkit.getServer().getOnlinePlayers();
    120. return onlinePlayers.length;
    121. }
    122. }


    What's wrong with a txt?

    Thanks,
    Dablakbandit
     
  27. Offline

    AoH_Ruthless

    Dablakbandit
    I'm sorry for sounding like an ass, but what's wrong with a YAML? It's a lot easier to use, and instead of calling all those methods with the .txt, you could just make a config.yml.
     
  28. Offline

    Dablakbandit

    AoH_Ruthless

    Can this just be solved please? I asked how to configure my code for per world, not how to use a YAML file for it.

    Thanks,
    Dablakbandit
     
  29. Offline

    AoH_Ruthless

    Dablakbandit
    While you didn't ask for it, I was just trying to give you a suggestion.
    Hopefully someone else can help you, because I am unfamiliar with using txt files, in any case.
    And only you can set it to solved :)
     
  30. Offline

    Dablakbandit

    AoH_Ruthless

    Wow....., just wow....

    You be like "not using YAML?, im out here"
     
Thread Status:
Not open for further replies.

Share This Page