Player.isOnline()

Discussion in 'Plugin Development' started by Joshua Neicho, Mar 6, 2011.

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

    Joshua Neicho

    hi,
    sorry about the title i made this thread then decided what i was asking would be a /msg type thing
    i want to know how i would broadcast a message across an entire server
     
  2. Offline

    Plague

  3. Offline

    Joshua Neicho

    OMG thank you i was searching there as well
     
  4. Offline

    Plague

    that's strange because i only typed "broadcast" in the search field ;)
     
  5. Offline

    Joshua Neicho

    so would this code work?

    Code:
    package com.JWNJWN.GuestMessage;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class GuestMessage extends JavaPlugin {
    
        @Override
        public void onDisable() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onEnable() {
            // TODO Auto-generated method stub
    
            {
            }
                 try {
                        while (true) {
                broadcastMessage(ChatColor.RED + "type /help for a list of commands");
                broadcastMessage(ChatColor.RED + "DO /apply For instruction on how to get building rights");
                broadcastMessage(ChatColor.RED + "Do /spawn to go to spawn building");
                broadcastMessage(ChatColor.RED + "Do /rules for a list of rules you MUST follow");
                Thread.sleep(120 * 1000);
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
    }
    }
    
        private void broadcastMessage(String string) {
            // TODO Auto-generated method stub
    
        }
    }
    --- merged: Mar 6, 2011 10:07 PM ---
    please help this code seams to freeze my server and not let anyone on i think it might be because of the sleep bit
     
  6. Offline

    xZise

    You should do this in another thread/timer or use the Bukkit Scheduler. At the moment you stops the server for 120.000 seconds then doing 4 broadcasts and stoping the server again (so the server couldn't react on new players etc.).

    You should also do something in broadcastMessage ;)

    My example with scheduler (I'm no pro with the bukkit scheduler so this couldn't working):
    Code:
    this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    	@Override
    	public void run() {
    		// Do stuff here
    	}
    }, 0, 120*1000);
    Fabian
     
  7. Offline

    Edward Hand

    The delay in the scheduler is in ticks (which are 50ms each) so the delay should be 120*20

    Otherwise though, xZise is correct. Putting a never ending loop in your onEnable function will make the server try to enable your plugin and then get stuck in the loop, being unable to do anything else until the loop finishes (which of course it never will) so don't ever do that.
     
  8. Offline

    Joshua Neicho

    Code:
    package com.JWNJWN.GuestMessage;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class GuestMessage extends JavaPlugin {
    
        @Override
        public void onDisable() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onEnable() {
            // TODO Auto-generated method stub
    
            {
                this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                    @Override
                    public void run() {
                        // Do stuff here
                        broadcastMessage(ChatColor.RED + "type /help for a list of commands");
                        broadcastMessage(ChatColor.RED + "DO /apply For instruction on how to get building rights");
                        broadcastMessage(ChatColor.RED + "Do /spawn to go to spawn building");
                        broadcastMessage(ChatColor.RED + "Do /rules for a list of rules you MUST follow");
                    }
                }, 0,  120*20);
            }
        }
    
    
    
        private void broadcastMessage(String string) {
            // TODO Auto-generated method stub
    
        }
    }
    
    this doesn't display the message please help
     
  9. Offline

    Edward Hand

    I believe it's because you need to do theServer.broadcastMessage.
    Perhaps creating your own Runnable class rather than doing everything inline might be the way forward.

    Code:
    public class myBroadcaster implements Runnable
    {
       Server server;
    
       public myBroadcaster(Server _server){
          server = _server;
       }
    
       public void run()
       {
          server.broadcastMessage("the message");
       }
    
    }
    and then:
    Code:
    this.getServer().getScheduler().scheduleSyncRepeatingTask(this,new myBroadcaster(this.getServer()),0,120*20);
     
  10. Offline

    Joshua Neicho

    could you explain this code since i am new with bukkit i don't really understand
     
  11. Offline

    xZise

    You called “broadcastMessage” which does nothing. Edward does the same thing as my code does and it is nothing Bukkit specific. You simply define a new class and add a field Server which stores the server. Because you have to tell the server, that the should broadcast a message. And the run-method is every time called, when the task is ready.

    Fabian
     
  12. Offline

    Joshua Neicho

    thanks alot all of you it works now
     
  13. Offline

    Joshua Neicho

    k sorry for the bump but if anyone can help will you tell me how I would make it read a .txt file with a colour and text then print the txt in the colour specified
     
  14. Offline

    xZise

    I don't know how this is managed here but on other forums it is normally so: New question → New thread.

    To your question: To read a text file simply use google (or the search engine you trust). And I don't know what you mean with the colors. I would suggest that you define a character as color character and then make a replacement? Maybe some regex gurus could show you a regex so you could escape the character. To get the color for a specified number use “ChatColor.getByCode(int)”.

    Fabian
     
Thread Status:
Not open for further replies.

Share This Page