SampleModule

SampleModule

RealWeather is now sort of API and all effects are in modules. To start you need to add RealWeather.jar as library. Structure inside is quite similar to bukkit plugin.

  • ModuleSample.jar
    • module.yml
    • my.module.package
      • FSample.java

module.yml

There must be 2 fields:

  • main: my.module.main.class.FSample
  • author: creezo

The FSample could be whatever you want. FModule, FDie, FSurvival, etc. The name itself doesn't matter, but try to keep the convention.

Main module class

package org.creezo.realweather.feature.air;

import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.creezo.realweather.feature.Feature;
import org.creezo.realweather.thread.ThreadManager;

/**
 *
 * @author Creezo
 */
public class FSample extends Feature {
    private EventsListener events; // Events listener is handled the same way as in CB plugin
    public FSample(JavaPlugin plugin) { // plugin reference is given when the module is created
        super("sample", plugin); // You should give there the name of module, use lowercase and no spaces, so try to make it as simple as possible, because it is used in module path for lang and config file. Also forward plugin reference.
    }

    @Override
    public void onEnable() { // Called when all modules are created and starts loading. You should initialize all Events and Threads here.
        events = new EventsListener(this); 
    }

    @Override
    public void onDisable() { // Called before RealWeather unloads itself
        
    }

    @Override
    public void registerEvents(PluginManager pm) { // Provides you PluginManager for events registration
        pm.registerEvents(events, getPlugin()); // for plugin reference use "getPlugin()"
    }

    @Override
    public void initThreads(ThreadManager tm) { // This is time for your thread creation
        // more in ThreadManager
    }

    @Override
    public void run(Player player, double temp) { // Called by ThreadManager from playerTempThread pool. Dependent sync thread as it is originally called by CB sync repeating task.
        
    }

    @Override
    public void run() { // Called by CraftBukkit sync repeating tasks pool. Independent sync task.
        
    }
}

ThreadManager

    @Override
    public void initThreads(ThreadManager tm) {
        tm.runDependent(player, temperature); // Only for internal use! Calls run(Player player, double temp)
        tm.scheduleDependentThread(this); // Schedules RW main temperature thread dependent thread. Is called in check right after temperature calculation. Calls  run(Player player, double temp)
        tm.scheduleIndependentThread(this, start, repeat); // Schedules standard CB sync repeating task, but is handled and eventually disabled by RW. Requires Feature instance instead of Runnable. "start" and "repeat" are in ticks. Calls run()
        tm.startTempThread(player); // Only for internal use! Starts temperature thread for player. Use only when you know what are you doing.
        tm.stopAllThreadsForFeature(this); // Only for internal use! Stops all scheduled threads in Feature
        tm.stopTempThread(player); // Similar to start
        tm.stopThread(this); // Only for internal use! Stops dependent thread by Feature instance
        tm.stopThread(id); // Only for internal use! Stops independent thread by ID
    }

Configuration

There is easy configuration system for modules sou you don't have to make it from scratch.

file is located in plugins/RealWeather/features/<feature name>/config.yml

Code below is just my usage of this config, it is quite easy to use then. This solution is usable only inside package.

    // to initialize just call initConfig() in onEnable()
    Config conf;
    private void initConfig() throws IOException {
        if(!getConfig().getYaml().contains("enable")) getConfig().getYaml().set("enable", true);
        getConfig().save();
        conf = new Config(getConfig().getYaml());
    }
    
    class Config {
        private final YamlConfiguration conf;

        public Config(YamlConfiguration conf) {
            this.conf = conf;
        }
        
        public boolean isEnabled() {
            return conf.getBoolean("enable");
        }
    }

Module Localization

Module localization supports UTF-8 and by default is loaded from .../features/<module name>/eng.lang

If the file does not exist nothing will be loaded and information will be in console about it. This file is not created automatically, so you have to create it with specific encoding (UTF-8).

.lang file is pure "key:value" system so you cant make any sections, lists, etc.

Example:

playerDied:Player just died!

NOTE! if you make space before or after ":" it will count to key or value! Also do not use quotes.

usage in module:

getConfig().getLocale().getValue("playerDied"); // returns String

to change language set "language" key to the name of the lang file without '.lang' (Default is "eng") in default config

getConfig().getYaml().set("language", "eng");

Because the "language" value can be be changed only after first locale load, without pre-created config.yml with language:"<language>" it will try to load default "eng" on first run. But I don't think that you will want to change language inside the code.

Module path

getConfig().getDataFolder(); // plugins/RealWeather/features/<feature name>/, (returns File)

Comments

Posts Quoted:
Reply
Clear All Quotes