My first shot at the Bukkit API

Discussion in 'Plugin Development' started by maetthew, Jul 21, 2011.

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

    maetthew

    Okay, first some of my "programming backgrounds". I would consider myself "quite good" at PHP, altough I haven't used it much in the last 1-2 years, I feel confident that I could whip up almost anything PHP wise given there is time both for the actual work, and for me figuring out which paths to take on a specific problem. Earlier this year I started studying some Ruby, I got quite the hang of it but I never ended up doing any projects with it and I haven't used it for a couple of months due to lack of interest. A couple of years back I had a meager stab at Java but never got too far with it. However, I perhaps should say that when I'm reading Java source code, I can understand most of it.

    I am now keen on getting wet with the Bukkit API. I have watched some of the video tutorials out there, but I'm struggling with which classes to use and was hoping I could get some help with this.

    Whenever I do a /reload on my server, I try to make it a habit to announce to everyone online that a server reload are due. As a first small project I have in mind a function that automatically detects a server reload, and precedes it with a notice in the lines of "Server reload imminent, EXPECT HUGE LAG".

    Could someone assist me on what class(es) I'm looking for to pull of something like this. Would also appreciate ANY other tips :)

    Thanks
    - Mattias

    No one? :oops:

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

    insanj

    Well, try starting with some of the basic classes.
    From tutorials online, you should have gathered how to set up an Eclipse workspace with the libraries you need and such. Below are the examples of the two files that you need. I pretty much wrote most of the code you'd need for you, I just forgot how to send messages to the world/server down at the bottom of the listener.

    Good luck!

    EDITED: TO GIVE ALL CODE!

    Reloader.java
    Code:
    //package
    //imports
    
    public class Reloader extends JavaPlugin{
    
       private final ReloaderListener playerListener = new ReloaderListener(this);
    
       @Override
       public void onEnable(){
       //when plugin is enabled...
          PluginManager pm = getServer().getPluginManager();
          pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
       }
    
       @Override
       public void onDisable(){
       //when plugin is disabled...
       }
    
    ReloaderListener.java
    Code:
    //package
    //imports
    
    public class ReloaderListener extends PlayerListener
    {
        public static Reloader plugin;
    
        public ReloaderListener(Reloader instance){
            plugin = instance;
        }
    
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
                //when command in processed...
                if(event.getMessage().equals("reload") && event.getPlayer().isOp() )
                    event.getServer().broadcastMessage("Server reload imminent, EXPECT HUGE LAG");
        }
     
  3. For the reload listener, it would be:
    plugin.getServer().broadcastMessage("Server reload imminent, EXPECT HUGE LAG");
    (To broadcast, i mean)
     
    insanj likes this.
  4. Offline

    captainawesome7

    Of course you would have to take into account the player's permissions, if they type /reload but don't have permission to reload the server the notification shouldn't come up.
     
    insanj likes this.
  5. Offline

    insanj

    Edited my post to incorporate those answers.
    Hopefully the OP gets it now. :p
     
  6. Offline

    maetthew

    Thank you @insanj!
    After some studying I came with basically the same thing, but I was trying to find some equivalent to Event.Type.PLAYER_COMMAND_PREPROCESS (Perhaps Event.Type.SERVER_COMMAND_PREPROCESS? Seems like no). So my question, would this also invoke commands entered straight into the console?
     
  7. yes...
    Wait, a minute... @insanj that doesn't check if /reload is sent by the console, because if it did it would be event.getSender() and you would check if sender instanceof Player, not just event.getPlayer() :O
     
  8. Offline

    insanj

    For the method onPlayerCommandPreprocess only players can call it, so checking if the sender is a player isn't necessary, and isn't even possible... I'm busy right now, but there probably is a command preprocess event type deal that works for the console as well, and your correct there would be needed, though.
     
  9. Offline

    maetthew

    I'm sorry you lost me at "Wait, a minute..."

    Okay to rephrase my question. Would PLAYER_COMMAND_PREPROCESS invoke commands sent from the console?
     
  10. Offline

    insanj

    No, it doesn't look like onPlayerCommandPreprocess tracks the console's commands, but I'm sure there's another way, I'm just a bit busy right now. Also, for clarification, the other user above was saying that you would have to check if the sender was the Console or a Player, as well as if the Player was an Operator when the command was sent. However, seeing as the PlayerPreprocess situation isn't compatible with the console, that solution will only work with another method.

     
  11. Offline

    maetthew

    Ah okay, big thanks! Will see if I can find an equivalent of onPlayerCommandPreprocess for the console.
     
  12. i would guess onServerCommandPreprocess is called for server commands. the solution: Try it! :p
     
    insanj likes this.
  13. Offline

    krinsdeath

    To differentiate between Server and Player commands, you need two separate listeners.

    PlayerListener, which you've already extended and interfaced with [above], and ServerListener, which has the method onServerCommand(ServerCommandEvent event). Separating the two methods could give you some modicum of control over whether ops can indeed reload the server (for example, you could nullify the permission altogether by setting event.setCancelled(true) in your onPlayerCommandPreprocess() method.

    I won't go through the intricacies of the methods here, but this should give you enough information to build your plugin.

    Good luck!
     
    maetthew likes this.
  14. And that sums it up. onServerCommand is the way to go(although i still thin onServerCommandPreprocess is better :O, i've just never used it)
    Good summary @krinsdeath !
     
  15. Offline

    maetthew

    THANK YOU! Ecellent explanation of what I need to do :) Will see if I can do this tonight or tomorrow.
     
Thread Status:
Not open for further replies.

Share This Page