Developers please help new guy!

Discussion in 'Plugin Development' started by Ziddia, Mar 14, 2011.

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

    Ziddia

    Ok, so I wanted to get started making a plugin- for my first one, a MOTD that says "Welcome (playername) to our server" by default. Later on, I'll try some harder stuff, but for now while I'm learning I want to try this.

    I would need to know:
    1. What to download- a text editor, obviously, but what one? And will I need anything else?
    2. What classes I can extend/edit (or whatever bukkit calls them- classes or something else). A list would be handy.
    3. A little tutorial on making a MOTD plugin, and on using other essential parts of plugin creation.

    An answer in WRITING would be appreciated; I don't want videos please.
    Please help me live my dream!
     
  2. Offline

    Sammy

    Please someone answer this guy before I freak out with is laziness ^^
     
  3. Offline

    Ziddia

    ? What?
     
  4. Offline

    MadMonkeyCo

  5. Offline

    Ziddia

    Oh yeah, forgot- do I need to add java to my PATH for this? And what would I add? PS my jre and jdk are both in program files x86
     
  6. Offline

    iPhysX

    Ziddia, here goes.
    Your JDK is probably in x64, and your jre is probably in x32.
    However, just ignore jre for now.
    The JDK 'should', be added to eclipse by default, if it is not im sure you will be able to figure out how.
    After that is all set up you will be able to create new java projects, and edit old ones.
    Hopefully MadMonkey Expands on this since im relatively new to making plugins :/
     
  7. Offline

    MadMonkeyCo

    Makes sure you have the latest build of craftbukkit or this tutorial might not work.
    Tutorial:
    1. Open Eclipse and create a new Java Project. NOTE: When you first start Eclipse it will ask you where you want your workspace. Set the location you want your projects to be stored and remember to check the option to always use this workspace.
    2. Right click your project in the Package Explorer and click Properties. Go to Java Build Path->Libraries and click Add external JARs... and find the CraftBukkit JAR file. Select the file and click Open, then click OK.
    3. Right click src in your newly created project. Select New->Package and a window will pop up. Set the field Name as nation.author.project where nation is your nations web suffix (Example Iceland: is, Denmark: dk), author is your username and project is the project name. The package name IS case sensitive. When done click Finish.
    4. Right click the package and click New->Class. Set the name as the same as the project's name. This is your main class. Then create another class with the name <ProjectName>PlayerListener (Example: Project name is MOTD so the class name would be MOTDPlayerListener). This is your plugin's PlayerListener class.
    And now the code, you may need to change stuff like the package name and your clss names.
    5. The code for your main class:
    PHP:
    package com.Ziddia.motd// just an example package name for you
    // import everything that you are using in this class from bukkit.
    // NOTE: You will need to import more classes in other plugins since they will be more complicated.
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    motd extends JavaPlugin{
        public static 
    motd instance;
        private final 
    motdPlayerListener playerListener = new motdPlayerListener();
        
    // get your plugin's .yml data
        
    PluginDescriptionFile pdfFile this.getDescription();
        @
    Override
        
    public void onEnable() {
            
    PluginManager pm getServer().getPluginManager();
            
    pm.registerEvent(Event.Type.PLAYER_LOGINplayerListenerPriority.Normalthis);
            
    System.out.printlnpdfFile.getName() + " version " pdfFile.getVersion() + " is enabled!");
        }
        @
    Override
        
    public void onDisable() {
            
    System.out.printlnpdfFile.getName() + " version " pdfFile.getVersion() + " is disabled!");
        }
    Your listener's code:
    PHP:
    package com.Ziddia.motd//as before you may need to change the name.
    //import once again...
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerLoginEvent.Result;

    public class 
    motdPlayerListener extends PlayerListener{
        @
    Override
        
    public void onPlayerLogin(PlayerLoginEvent event) {
            
    Player player event.getPlayer(); //get the player who logged in.
            
    player.sendMessage("WELCOME TO AN AWESOME SERVER!!!"); //send the player a message
        
    }
    }
    6. Right click on your project again and click Create->File and type in "plugin.yml". Then open the file and add:
    Code:
    name: <name>
    main: <entire packagename.mainclass>
    version: <version>
    If the file was opened in an external editor you need to right-click it and click Refresh.
    7. Finally exporting it to a JAR. File->Export and everything is pretty self explanatory. You need to click the box next to your project's name, it will be filled. Then select a location and a name (Example: <server folder>\plugins\motd.jar) to export it to ( I use the plugins folder so I don't have to move the file later, I recommend using it) and click Finish.
    8. Testing. Start up your server, make sure your plugin is in the plugins folder.
    If you don't understand something in the tutorial or need help with something else just let me know.
    -The Mad Monkey
     
  8. Offline

    Ziddia

    Thankyou, I will be trying this out today.

    Also: if I was making a plugin which hooks into permissions to give each rank a dfferent MOTD, how would I create a config file for it I've got hooking from nijikokuns post about it, but how do o make a config, that would say like "admin-MOTD" etc.
    And how do I make nodes for these then, for permissions?
     
  9. Offline

    MadMonkeyCo

    Well, I use this class (it's like pseudo-yaml):
    PHP:
    public final class TFLSettings {
        private static final 
    Logger log Logger.getLogger("Minecraft");
        private 
    Properties properties;
        private 
    String fileName;

        public 
    TFLSettings(String fileName) {
            
    this.fileName fileName;
            
    this.properties = new Properties();
            
    File file = new File(fileName);

            if (
    file.exists()) {
                
    load();
            } else {
                
    save();
            }
        }
     
        public 
    void load() {
            try {
                
    this.properties.load(new FileInputStream(this.fileName));
            } catch (
    IOException ex) {
                
    log.info("[TheForgottenLands] Unable to load " this.fileName);
            }
        }

        public 
    void save() {
            try {
                
    this.properties.store(new FileOutputStream(this.fileName), "Minecraft Properties File");
            } catch (
    IOException ex) {
                
    log.info("[TheForgottenLands] Unable to save " this.fileName);
            }
        }
     
        public 
    void removeKey(String key) {
            
    this.properties.remove(key);
            
    save();
        }

        public 
    boolean keyExists(String key) {
            return 
    this.properties.containsKey(key);
        }

        public 
    String getString(String key) {
            if (
    this.properties.containsKey(key)) {
                return 
    this.properties.getProperty(key);
            }

            return 
    "";
        }

        public 
    String getString(String keyString value) {
            if (
    this.properties.containsKey(key)) {
        return 
    this.properties.getProperty(key);
            }
            
    setString(keyvalue);
            return 
    value;
        }

        public 
    void setString(String keyString value) {
            
    this.properties.setProperty(keyvalue);
            
    save();
        }

        public 
    int getInt(String key) {
            if (
    this.properties.containsKey(key)) {
                return 
    Integer.parseInt(this.properties.getProperty(key));
            }

            return 
    0;
        }

            public 
    int getInt(String keyint value) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Integer.parseInt(this.properties.getProperty(key));
                }

                
    setInt(keyvalue);
                return 
    value;
            }

            public 
    void setInt(String keyint value) {
                
    this.properties.setProperty(keyString.valueOf(value));
                
    save();
            }

            public 
    double getDouble(String key) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Double.parseDouble(this.properties.getProperty(key));
                }

                return 
    0;
            }

            public 
    double getDouble(String keydouble value) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Double.parseDouble(this.properties.getProperty(key));
                }

                
    setDouble(keyvalue);
                return 
    value;
            }

            public 
    void setDouble(String keydouble value) {
                
    this.properties.setProperty(keyString.valueOf(value));
                
    save();
            }

            public 
    long getLong(String key) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Long.parseLong(this.properties.getProperty(key));
            }

                return 
    0;
            }

            public 
    long getLong(String keylong value) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Long.parseLong(this.properties.getProperty(key));
                }

                
    setLong(keyvalue);
                return 
    value;
            }

            public 
    void setLong(String keylong value) {
                
    this.properties.setProperty(keyString.valueOf(value));
                
    save();
            }

            public 
    boolean getBoolean(String key) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Boolean.parseBoolean(this.properties.getProperty(key));
                }

                return 
    false;
            }

            public 
    boolean getBoolean(String keyboolean value) {
                if (
    this.properties.containsKey(key)) {
                    return 
    Boolean.parseBoolean(this.properties.getProperty(key));
                }

                
    setBoolean(keyvalue);
                return 
    value;
            }

            public 
    void setBoolean(String keyboolean value) {
                
    this.properties.setProperty(keyString.valueOf(value));
                
    save();
            }
        }
    It's simple to use. Just use the getString(<group>).
    Sample code how to setup in Main class:
    PHP:
    TFLSettings configFile;
    private 
    void setup(){
        
    String directory this.getFile().getParent() +" \\motd";
        (new 
    File(directory)).mkdir();
        
    configFile = new TFLSettings(directory+\\messages.yml);
    }
    Call setup() at the end of onEnable(). Then you can find the users group and check what the message for his group is.
    I'll demonstrate how the getString above would work in the .yml file:
    Code:
    Default: "You are in the default group!"
    Admins: "You are an admin!"
    Now if you would use getString("Default") it would return "You are in the default group!"
    I'm not familiar with how permissions work since I use GroupManager.
    Hope this helps.
     
  10. Offline

    Ziddia

    I would replace file name with the MOTD.yml, and tflsettings with my plugin name right? Probably wrong but anywa, what bits of your code would need replacing?
     
  11. Offline

    MadMonkeyCo

    Yes, call the yml file whatever you want and TFLSettings should be <PluginName>Settings or whatever you want. Also in the Settings class you need to change the text that is put into the log in the save and load methods.
     
  12. Offline

    Ziddia

    Eclipse wont let me use it because apparently my JDK is not installed. Will be fixed by tonight I hope. And once that's done, ultimate MOTD plugin coming on!
     
Thread Status:
Not open for further replies.

Share This Page