'Broadcasting' a message to all players?

Discussion in 'Plugin Development' started by DrAgonmoray, Feb 13, 2011.

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

    DrAgonmoray

    I'm new to developing plugins, so I'm not entirely sure how to do all this stuff. By looking at other people's source, I've managed to find things that I'd probably use to do this, but I'm not sure how to make them work for what I need.
    getServer()
    getOnlinePlayers()
    sendMessage()

    I assume I'd need to do something like this...
    [pseudo-code]

    for (plr : ???.getServer().getOnlinePlayers()) {
    plr.sendMessage("Testing")
    }

    I'm trying to accomplish something of that sort. Any help would be appreciated.
     
  2. Offline

    Colecf

    Use:
    Code:
    server.broadcastMessage("Foo");
     
  3. Offline

    PrivateAlpha

    this.getServer().broadcastMessage("")
    --- merged: Feb 13, 2011 10:55 PM ---
    ninja'd =[
     
  4. Offline

    DrAgonmoray

    Wow, thanks guys. >:D
    --- merged: Feb 13, 2011 11:08 PM ---
    Oh, but I have another question if you don't mind. How would I go about making something happen every so often, say five minutes. (though it doesn't really matter how long)
     
  5. Offline

    Kekec852

    Code:
    Timer t = new Timer();
    t.schedule(new Runnbale(){
    //this code will execute every 5 minuts
    },0,300000);
    
    schedule(Runnable r,int startDelayInMiliseconds,int intervalInMiliseconds);
     
  6. Offline

    DrAgonmoray

    Thank's Kekec, that helped alot.

    Now I'm having trouble with actual code. There appears to be an error here on the line that says "LoadSettings.load()"
    Announcer.java: (part)
    Code:
    public void onEnable() {
            System.out.println("Loading");
            new File(maindirectory).mkdir();
            LoadSettings.load();//It's this line according to the server console.
            Timer t = new Timer();
            t.scheduleAtFixedRate(new TimerTask() {
                int i = 0;
                public void run() {
                getServer().broadcastMessage(LoadSettings.announcments[i]);
                i += 1;
                if (i == LoadSettings.announcments.length) {
                    i = 0;
                }
    
                }
            }, 0, LoadSettings.interval);
    
        }
    LoadSettings.java: (whole thing)
    Code:
    package com.bukkit.DrAgonmoray.Announcer;
    
    public class LoadSettings {
        static int interval;
        static String[] announcments;
        public static void load() {
            String propertiesFile = Announcer.maindirectory + "Config.properties";
            PluginProperties properties = new PluginProperties(propertiesFile);
            properties.loadfile();
            String[] announcments = properties.getMessages("Announcments");
            int interval = properties.getInterval("Interval", 1);//Second argument is default. (minutes) >:D
            properties.save("===Announcer Main Configuration===");
    
        }
    }
    PluginProperties.java: (whole thing)
    Code:
    public class PluginProperties extends Properties {
        static final long serialVersionUID = 0L;
        private String fileName;
        public PluginProperties(String file) {
            this.fileName = file;
    
        }
        public void loadfile() {
            File file = new File(this.fileName);
            if (file.exists()) {
                try {
                    load(new FileInputStream(this.fileName));
                } catch(IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        public void save(String start) {
            try {
                store(new FileOutputStream(this.fileName), start);
            } catch(IOException ex) {
                ex.printStackTrace();
            }
        }
        public String[] getMessages(String key) {
            if (containsKey(key)) {
                String[] split = getProperty(key).split("~");
                return split;
            }
            return null;
        }
        public int getInterval(String key, int def) {
            if (containsKey(key)) {
                return Integer.parseInt(getProperty(key))*60000;
            }
            put(key, def*60000);
            return def;
        }
    }
    [​IMG]

    I can't figure it out. Help would be very much appreciated. :D
     
  7. No it is here the problem lies.
    Code:
    public void save(String start) {
            try {
                store(new FileOutputStream(this.fileName), start);
            } catch(IOException ex) {
                ex.printStackTrace();
            }
        }
    
    More precisley the line "store(new FileOutputStream(this.fileName), start);"

    You are trying to convert an int to a string in the wrong way it seems.
     
  8. Offline

    eisental

    Don't use a thread timer to send player messages. Check out the Bukkit Scheduler. There are quite a number of posts discussing it.
     
Thread Status:
Not open for further replies.

Share This Page