Per world Broadcast.

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

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

    1Rogue

    What are you trying to do (in summary) ?
     
  2. Offline

    Dablakbandit

    1Rogue

    Thanks,
    Dablakbandit
     
  3. Offline

    AoH_Ruthless

    Dablakbandit
    I'm sorry it came out that way. I wasn't trying to be stubborn, I just have no idea how to use text files and I am not sure if they are the same as YAML, seeing as they don't have a definite hierarchy.
     
  4. Offline

    Dablakbandit

    AoH_Ruthless

    I gave the code, its pretty simple really.
     
  5. Offline

    1Rogue


    What would the text file contain?
     
  6. Offline

    Dablakbandit

    1Rogue

    Code:java
    1. Bukkit.getServer().broadcastMessage(ann + ChatColor.WHITE + line);
    2. LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
    3. lnr.skip(Long.MAX_VALUE);
    4. int lastLine = lnr.getLineNumber();
    5. if(currentLine + 1 == lastLine + 1){
    6. currentLine = 0;
    7. } else {
    8. currentLine++;
    9. }


    Well as that code says, it reads the first line from the message.txt then add's one to the line number, and reads the second line etc, until it reaches the end of the txt where it goes back to the first line.

    Thanks,
    Dablakbandit
     
  7. Offline

    1Rogue

    I have documentation on a LinkedQueue class that would make this rather simple once you add the lines into the program. (Edit) I've created a resource thread for it here: http://forums.bukkit.org/threads/linkedqueue-allows-endless-iteration.197993/

    Essentially, just load the messages into the queue, and then you can use .getNext() on the queue endlessly and read the messages.

    As for reading from a file:
    Code:java
    1. public LinkedQueue<String> getMessageQueue(String filePath) {
    2. LinkedQueue<String> back = new LinkedQueue<>();
    3. File file = new File(filePath);
    4. if (!file.exists()) {
    5. file.createNewFile();
    6. return back;
    7. }
    8. FileInputStream fis = null;
    9. InputStreamReader isr = null;
    10. BufferedReader reader = null;
    11. try {
    12. fis = new FileInputStream(file);
    13. isr = new InputStreamReader(isr);
    14. if (isr.ready()) {
    15. reader = new BufferedReader(isr);
    16. while (reader.ready()) {
    17. back.add(br.readLine());
    18. }
    19. }
    20. } catch (IOException ex) {
    21. /* log error */
    22. } finally {
    23. try {
    24. if (fis != null) {
    25. fis.close();
    26. }
    27. if (isr != null) {
    28. isr.close();
    29. }
    30. if (reader != null) {
    31. reader.close();
    32. }
    33. } catch (IOException e) {
    34. /* log closing error */
    35. }
    36. }
    37. return back;
    38. }


    This would basically allow you to do this if you have the file path:

    Code:java
    1. Map<String, LinkedQueue<String>> worlds = new HashMap<>();
    2. for (World world : Bukkit.getServer().getWorlds()) {
    3. String filename = "messages-" + world.getName() + ".txt";
    4. worlds.put(world.getName(), getMessageQueue(filename));
    5. }


    Which really simplifies it down :)
     
  8. Offline

    Dablakbandit

    1Rogue

    I am so confused. Can i not just get a simple answer? Please???
     
  9. Offline

    1Rogue


    Sorry, I hadn't finished writing my answer when I hit submit

    To append to the last post, you would be able to get a world by name and submit a message via .getNext() in essentially two lines of code from there:

    Code:java
    1. /*in your task*/
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. p.sendMessage(worlds.get(p.getLocation().getWorld().getName()).getNext());
    4. }


    Which would also allow you to randomize each message for different players :D
     
  10. Offline

    Dablakbandit

    1Rogue

    But how do i implement it?
    Thanks,
    Dablakbandit
     
  11. Offline

    1Rogue

    Add the LinkedQueue class to your plugin, then add the getMessageQueue method I showed above somewhere.

    From there it's your choice, I showed an example using a map, and you could probably run the task in a runnable of some sort.
     
  12. Offline

    Dablakbandit

    1Rogue

    I asked for a simple fix, to be honest it should only be a few lines of code, I've tried, but I'm new to coding java. But a class with 477 lines of code for a simple fix? really? I've provided the code, it just needs a bit of chopping/changing up, not a completely different code.
     
  13. Offline

    1Rogue

    From what I read you wanted

    1) to be able to read a list of messages from a text file
    2) to load different messages per world
    3) if the file didn't exist, make it
    4) have endless iteration of messages

    I essentially just remade that and took the OOP to some of the best of my ability. I didn't see any code you had that would load the files and read them, however you can use whatever data structure you want for storing the messages (a map with a world name and a list, for example), but you would need to keep track of index and such.
     
    Garris0n and AoH_Ruthless like this.
  14. Offline

    Dablakbandit

    1Rogue

    1. i already got
    2. yes that is needed
    3. your provided code does this yes
    4. it does this anyway

    So really all i need is number 2 not all of them.
     
  15. Offline

    1Rogue

    Then just save/load different files and keep them in a map based on world name...?
     
  16. Offline

    Dablakbandit

    1Rogue

    Again you have confused me?

    So no-one can do what i have asked?

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

    1Rogue


    I already did, and even provided an alternative. Just use a map and store the lines.
     
  18. Offline

    Dablakbandit

    1Rogue

    I don't know what you mean by that...
     
  19. Offline

    1Rogue


    Use a map.
     
  20. Offline

    Dablakbandit

    1Rogue

    I don't know what you mean? What is a map?, like come on I don't know java......., you gave the code:

    Code:java
    1. Map<String, LinkedQueue<String>> worlds = new HashMap<>();
    2. for (World world : Bukkit.getServer().getWorlds()) {
    3. String filename = "messages-" + world.getName() + ".txt";
    4. worlds.put(world.getName(), getMessageQueue(filename));
    5. }


    But how do i use it?? So far nobody has actualy helped but given me codes that i don't even know how to use or even put into my code. Please just listen to what i am asking and help.

    Thank you,
    Dablakbandit
    Butbu
     
  21. Offline

    1Rogue

    Pour toi, then: http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
     
  22. Offline

    Dablakbandit

    1Rogue
    Really??? really??? come on, did you only read the first like 10 words???

    Can someone with some decency answer, cause these people obviously cant even read english.
     
  23. Offline

    1Rogue


    I read your entire post. You want to learn how to use a map but don't even want to take the time to learn basic java. Did you even read what I linked?
     
  24. Offline

    Dablakbandit

    1Rogue

    I don't know java that's right, at least you got that. I'm not looking to learn the whole java language, I'm just wanting to write something basic, and i though the forums where meant to help, instead of frustrating me with answers that don't even answer my question.

    I'm going to ask it once again just in case you don't understand

    With the code

    Code:
    package me.Dablakbandit.PWBroadcaster;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin{
            public static int currentLine = 0;
            public static int tid =  0;
            public static int running = 1;
     
     
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!" );
        }
     
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
            if (this.getDataFolder().exists())
            {
            this.getLogger().info("File is there.");
            }else
            {
            createConfig(new File(this.getDataFolder(), "config.yml"));
            this.getLogger().info("File didn't exist");
            }
           
           
           
           
           
           
           
           
            final File msgs =new File(getDataFolder() + File.separator + "messages.txt");
                    if(!msgs.exists()){
                            try {
                                    msgs.createNewFile();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                    }
     
                    tid = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
     
                            @Override
                            public void run() {
                                   
                                    try{
                                        broadcastMessages(msgs.getPath());
                                    }catch(IOException e){
                                        e.printStackTrace();
                                    }
                            }
                           
                    }, 0, this.getConfig().getLong("interval") * 20);
     
     
    }
       
        public void createConfig(File f){
            InputStream cfgStream = this.getResource("config.yml");
            if (!this.getDataFolder().exists()){
                this.getDataFolder().mkdirs();
            }
            try {
                FileOutputStream fos = new FileOutputStream(f);
                ReadableByteChannel rbc = Channels.newChannel(cfgStream);
                fos.getChannel().transferFrom(rbc, 0, 1 << 24);
                fos.close();
           
            } catch (Exception e) {
                this.getLogger().info("There was an error in creating the config. Using bukkit methods to do so.");
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
            }
            }
       
            public  void broadcastMessages(String filename) throws IOException{
                   
                    if(numberOfOnlinePlayers() >= 1){
                           
                   
                    FileInputStream fs;
                    fs = new FileInputStream(filename);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                    for(int i = 0; i < currentLine; ++i)
                            br.readLine();
                    String line = br.readLine();
                    String ann = this.getConfig().getString("announcerName");
                    line = ChatColor.translateAlternateColorCodes('&', line);
                    ann = ChatColor.translateAlternateColorCodes('&', ann);     
     
     
                    Bukkit.getServer().broadcastMessage(ann + ChatColor.WHITE + line);
                    LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
                    lnr.skip(Long.MAX_VALUE);
                    int lastLine = lnr.getLineNumber();
                    if(currentLine + 1 == lastLine + 1){
                            currentLine = 0;
                    } else {
                            currentLine++;
                    }
            }
            }
            public static int numberOfOnlinePlayers() {
                    final Player[] onlinePlayers = Bukkit.getServer().getOnlinePlayers();
                    return onlinePlayers.length;
            }
    }
    This broadcasts to all worlds, I want it changed so depending on what world the player is in, it reads a different message<worldname>.txt and sends them the message from in that text file.

    Thank you in advance,
    Dablakbandit
     
  25. Offline

    1Rogue

    Whoa, hold up. Just because you don't understand the answers because of a lack of prior knowledge doesn't mean that they weren't helpful and/or not answering your question. If you choose to not know the language for what you're working with that's like navigating the sea without direction.

    Try reading the tutorial I linked, and grasp an understand of what a map does. It will clarify what you need to do and you will be able to understand how to implement it on your own. It's better for you to learn than to have other people write your plugin for you.
     
  26. Offline

    blha303

    1. You come here asking for help with Bukkit plugin development. You should learn the basics of how Java works before asking for help with fundamentals.
    2. You don't bother to Google the responses given. "java how to use map" comes up with some brilliant resources.
    3. You outright ignore what someone says, ask for a new person to talk to. It's at this point I would start ignoring the thread, because you're not willing to help yourself. Everything can't be handed to you on a platter, then you wouldn't learn anything.
    http://www.catb.org/esr/faqs/smart-questions.html
     
  27. Offline

    Dablakbandit

    WOW. JUST BLOODY WOW.

    I thought you guys where here to help people.

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

    blha303

  29. Offline

    Dablakbandit

    blha303

    ....... im not asking for links to other site with a heap of text that i cant read cause it screws with my mind. I'm asking for a quick fix. If you aren't gonna help THEN STOP POSTING.
     
  30. Offline

    1Rogue

    If you can't read that's a pretty serious issue for a coder.

    Moving on, learn java, then ask for help here. We aren't here to teach you how to code, we're here to help people with issues relating to the bukkit api.
     
Thread Status:
Not open for further replies.

Share This Page