Creating a Custom Weather (0.6 - 0.7a)

How's custom weather defined?/What it is?

When you say custom weather, it's understood( at least by ProperWeather) as an event, which modifies current weather conditions in some world and occurs with some probability.

Let's get started!

To implement a class, we should know what we're doing, so I paste Weather class here, so you'll know what to implement:

package sk.tomsik68.pw.api;

import java.util.UUID;

import sk.tomsik68.pw.plugin.ProperWeather;

public abstract class Weather{
	private UUID worldID;
	public Weather(UUID world){
		worldID = world;
	}

	public final WeatherController getController(){
		return ProperWeather.instance().getWeatherSystem().getWeatherController(worldID);
	}
	
	public abstract boolean canBeStarted(Integer previousID);

	public abstract int getRandomTimeProbability();

	public abstract int getProbability(WeatherSystem ws);

	public abstract void initWeather();

	public abstract void onRandomTime();

	public abstract int getUID();
}

Implementation of Weather method by method

canBeStarted(int previousID)

public boolean canBeStarted(int previousID);

This method tells weather system, whether this weather can be started after different weather. Usage Example: Storm must logically begin after rain. Why ID? Because I find really stupid to save objects of type Weather. Weather is an event, neither a save structure, so I've decided to make ID for each weather. Oh and these are current weather types and their IDs.

  • Clear - 0
  • Rain - 1
  • Storm - 2
  • Hot - 3
  • Meteorite Storm - 4 Example implementation: Let's say your weather can only be started after Meteorite Storm.
public boolean canBeStarted(int previousID){
    return previousID == 4;
}

getRandomTimeProbability()

public int getRandomTimeProbability();

A random time??? Yes. Random time. Random time is used for any further sub-events occured while your weather is running. Example Usage: You don't want to see lightning every second. Ok and what should be returned here? The plugin is expecting number in range 0 - 100 ( % ) Example Implementation:

public int getRandomTimeProbability(){
    return 50;
}

And what do you do if you aren't sure about %s? Just use sk.tomsik68.pw.api.EWeatherRarity to help you, like this:

public int getRandomTimeProbability(){
    return EWeatherRarity.Normal.getApart();
}

getProbability(WeatherSystem ws)

This tells the system probability of weather initialisation. You're getting WeatherSystem to use WeatherSystem.getWeatherProbability(EWeatherRarity rarity). Example Usages: Some weather can be more rare than others, I don't want meteors to destroy my house ;) Example Implementation:

public int getProbability(WeatherSystem ws){
    return ws.getWeatherProbability(EWeatherRarity.Rare);
}

initWeather()

Your weather was chosen by system( or user ;) ) to run. You need to start the basic conditions of your weather. Example Usages: Change sky color, Start raining, Allow Thunders,... Example Implementation:

    public void initWeather(){
          WeatherController wc = getController();
          wc.clear();
          wc.allowThundering();
          wc.setSkyColor(java.awt.Color.BLACK);
    }

onRandomTime()

Here we get to previously mentioned random time. Example Usages: Thundering, Falling Meteorite, Spawning Custom Entities,... Example Implementation(with thunders):

public void onRandomTime() {
		WeatherController controller = getController();
		World world = controller.getWorld();
		Random rand = new Random(world.getSeed() * world.getFullTime());
		for(Chunk chunk : world.getLoadedChunks()){
			for(int x = chunk.getX();x<x+16;x++){
				for(int z = chunk.getZ();z<z+16;z++){
					if(rand.nextInt(0x186a0) == 0 && world.getHighestBlockAt(x, z).getType() != Material.SAND){
						controller.strike(x, world.getHighestBlockYAt(x, z), z);
					}
				}
			}
		}
	}

getUID()

ProperWeather can't make IDs itself, so it needs your help. You need to setup unique ID for your weather. (This is going to be removed in 0.7) Example Usage: This is the id which is being used with canBeStarted(int previousID). Example Implementation:

public int getUID(){
     return 99;
}

How to register weather?

So far, we've implemented our weather, but that's not enought, because ProperWeather can't find it itself, so registration is needed. Luckily, I've made the registration simple:

WeatherMap.register(MyWeather.class);

Let's add the code into main skeleton:

import sk.tomsik68.pw.WeatherMap;
public void setupBridge() {
		Plugin test = getServer().getPluginManager().getPlugin("ProperWeather");
		if (test != null && test instanceof ProperWeather) {
			ProperWeather pw = (ProperWeather) test;
			System.out.println("[MyPlugin] Hooked into ProperWeather");
                        //MyWeather is the Weather implementation you've just created
			WeatherMap.register(MyWeather.class);
		}
	}

Cheers!

You've just made your first (and hopefully not last) Weather for Minecraft! Now go ahead, compile it and try it. If you've got any questions, suggestions or something similar, just PM me or comment ProperWeather.

0.6 -> 0.7a ?

As you may have noticed, I've recently uploaded 0.7b, which is not compatible with these settings because weather modifying was made easy throught the YAML file. Default values which can be changed will be defined in weather itself as of 0.7 release and I'll document that on other page.


Comments

Posts Quoted:
Reply
Clear All Quotes