Please help with auto broadcaster

Discussion in 'Plugin Development' started by Septango, Feb 14, 2012.

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

    Septango

    Code:
    package me.Septango.Broadcaster;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Broadcaster extends JavaPlugin{
        public static Broadcaster plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static int currentLine = 0;
        public static int tid = 0;
        public static int running = 1;
        public static long interval = 10;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now disabled.");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now enabled.");
           
            tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    try {
                        broadcastMessage("plugins/Broadcast/messages.txt");
                    } catch (IOException e) {
                       
                    }
                }
            }, 0, interval * 20);
        }
       
        public static void broadcastMessage(String fileName) throws IOException {
            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();
            line = line.replaceAll("$f", ChatColor.WHITE + "");
            line = line.replaceAll("&e", ChatColor.YELLOW + "");
            line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
            line = line.replaceAll("&a", ChatColor.GREEN + "");
            Bukkit.getServer().broadcastMessage(ChatColor.DARK_GREEN + "[Broadcast] " + 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 boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("haltbroadcast")) {
                if(running == 1) {
                    Bukkit.getServer().getScheduler().cancelTask(tid);
                    Player player = (Player) sender;
                    player.sendMessage("Cancelled broadcasts. ");
                    running = 0;
                } else {
                    Player player = (Player) sender;
                    player.sendMessage("They aren't running!");
                }
            } else if(commandLabel.equalsIgnoreCase("startbroadcast")) {
                if(running == 1) {
                    Player player = (Player) sender;
                    player.sendMessage("They are already running!");
                } else {
                    tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            try {
                                broadcastMessage("plugins/Broadcast/messages.txt");
                            } catch (IOException e) {
                               
                            }
                        }
                    }, 0, interval * 20);
                    Player player = (Player) sender;
                    player.sendMessage("Started broadcasts.");
                    running = 1;
                }
            }
            return false;
        }
    }
    I'm working on making my own autobroadcasting plugin and was wondering if anyone could tell me whats wrong with my code. I have no errors it is just the server doesn't broadcast them.
     
  2. Offline

    javoris767

    Try changing
    Code:java
    1. broadcastMessage("plugins/Broadcast/messages.txt");

    To
    Code:java
    1. Bukkit.broadcastMessage(line);

    See if that works.
     
  3. Offline

    Septango

    nope right when i try it i get Description
    line cannot be resolved to a variable
    as an error
     
  4. Offline

    thehutch

    You should check if the sender actually is a Player otherwise you get ClassCastException is it could be a console,
     
  5. Offline

    Morthis

    Maybe an obvious question but have you made sure you're not getting an exception since you're ignoring it when you catch it?

    Also minor nitpick but you don't have to cast sender as player before sending a message, you can just do sender.sendMessage.
     
  6. Offline

    Septango

    This any better as for the player online part?
    Code:
    package me.Septango.Broadcaster;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Broadcaster extends JavaPlugin{
        public static Broadcaster plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static int currentLine = 0;
        public static int tid = 0;
        public static int running = 1;
        public static long interval = 10;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now disabled.");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is now enabled.");
           
            tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    try {
                        broadcastMessage("plugins/Broadcast/messages.txt");
                    } catch (IOException e) {
                       
                    }
                }
            }, 0, interval * 20);
        }
       
        public static void broadcastMessage(String fileName) throws IOException {
            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();
            line = line.replaceAll("$f", ChatColor.WHITE + "");
            line = line.replaceAll("&e", ChatColor.YELLOW + "");
            line = line.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
            line = line.replaceAll("&a", ChatColor.GREEN + "");
            Bukkit.getServer().broadcastMessage(ChatColor.DARK_GREEN + "[Broadcast] " + 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 boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("haltbroadcast")) {
                Player player = (Player) sender;
                if(player.getServer().getPlayer(args[0]) != null) {
                if(running == 1) {
                    Bukkit.getServer().getScheduler().cancelTask(tid);
                    player.sendMessage("Cancelled broadcasts. ");
                    running = 0;
                } else {
                    player.sendMessage("They aren't running!");
                }
              }
            } else if(commandLabel.equalsIgnoreCase("startbroadcast")) {
                Player player = (Player) sender;
                if(player.getServer().getPlayer(args[0]) != null) {
                if(running == 1) {
                    player.sendMessage("They are already running!");
                } else {
                    tid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            try {
                                broadcastMessage("plugins/Broadcast/messages.txt");
                            } catch (IOException e) {
                               
                            }
                        }
                    }, 0, interval * 20);
                    player.sendMessage("Started broadcasts.");
                    running = 1;
                }
            }
            }
            return false;
        }
    }
    Oh and also how do I check if there is a erroe since im throwing it out. Do I just remove the catch (IOException e) part?

    This thread may be closed now I have solved my own problem and it wasn't even one with my code. I saved a document as messages.txt and my setting was to auto put in the .txt but to not show the the real name of the file was messages.txt now messages as was supposed to be. Thanks for those who responded and helped me and giving me the idea to check to see if it was a player issuing the commands.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  7. if(currentLine + 1 == lastLine + 1) {

    ^^ Not try to be smart, :p
     
  8. Offline

    messageofdeath

    i can tell you something watch the gtotechnology tutorial right because i remember that code that exact code from him your kinda stealing and not giving proper credit saying "im working on my OWN! autobroadcasting plugin" so yeah
     
  9. Offline

    dillyg10

    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers())
    {
    p.sendMessage("message")
    }
    if you wnat to
     
  10. Offline

    Njol

     
  11. Offline

    dillyg10

    again, either way works :D
     
  12. Offline

    Njol

    He has a whole function to do the broadcasting, i.e. your comment was useless. Next time read the thread before posing.
     
Thread Status:
Not open for further replies.

Share This Page