Source

Source code

The source code for version 0.12

I probably will not be keeping this project massively up to date, so people can compile a fixed version and post it for others if need be:)

Code:

package net.bukkit.piedo_bear.ForeverWeather;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.java.JavaPlugin;

public class ForeverWeather extends JavaPlugin {
	public static Permission perms = null;
	File configFile = new File(getDataFolder(), "config.yml");
	Boolean enabled = true;
	List<String> worlds = null;

	@Override
	public void onEnable() {
		if (!this.configFile.exists()) {
			createConfiguration();
		}

		loadConfig();

		if (!enabled) {
			System.out.println("[ForeverWeather] Disabling... You must enable in the config file!");
			this.setEnabled(enabled);

		} else {
			loadFrozenConfigWorlds(this);
			getServer().getPluginManager().addPermission(new Permission("ForeverWeather.access"));
		}
	}

	@Override
	public void onDisable() {
		System.out.println("[ForeverWeather] has been disabled.");
	}

	// Configuration
	public void createConfiguration() {
		String[] worlds = { "world" };
		getConfig().options().header("~ForeverWeather~ by PiedoBear");
		getConfig().addDefault("EnablePlugin", false);
		getConfig().addDefault("Worlds", Arrays.asList(worlds));
		getConfig().addDefault("world.time", 6000L);
		getConfig().addDefault("world.storming", true);
		getConfig().options().copyDefaults(true);
		saveConfig();
	}

	@SuppressWarnings("unchecked")
	public void loadConfig() {
		this.enabled = getConfig().getBoolean("EnablePlugin");
		this.worlds = (List<String>) getConfig().getList("Worlds");
	}

	public void addWorldToConfig(World w, Long time, Boolean storm) {
		loadConfig();

		if (w == null) {
			return;
		}

		String name = w.getName();
		if (!this.worlds.contains(name)) {
			this.worlds.add(name);
			this.getConfig().set("Worlds", this.worlds);
			this.getConfig().set(name + ".time", time);
			this.getConfig().set(name + ".storming", storm);
			this.saveConfig();
		}
	}

	public void removeWorldFromConfig(World w) {
		loadConfig();

		if (w == null) {
			return;
		}

		String name = w.getName();
		if (this.worlds.contains(name)) {
			this.worlds.remove(name);
			this.getConfig().set(name + ".time", null);
			this.getConfig().set(name + ".storming", null);
			this.saveConfig();
		}
	}

	public Long getConfigTime(String world) {
		return this.getConfig().getLong(world + ".time");
	}

	public Boolean getConfigStorming(String world) {
		return this.getConfig().getBoolean(world + ".storming");
	}

	public void loadFrozenConfigWorlds(final ForeverWeather pl) {
		// delay so every has loaded before starting to make changes
		getServer().getScheduler().scheduleSyncDelayedTask(pl, new Runnable() {

			@Override
			public void run() {
				int l = pl.worlds.size();

				for (int i = 0; i < l; i++) {
					World w = pl.getServer().getWorld(worlds.get(i));

					if (w != null) {
						String name = w.getName();
						Long time = getConfigTime(name);
						Boolean storm = getConfigStorming(name);
						keepWorldFrozen(w, time, storm);
						System.out.println("[ForeverWeather] Loaded " + worlds.get(i) + " and set the time and weather.");
					} else {
						System.out.println("[ForeverWeather] World does not exist: " + worlds.get(i));
					}

				}
			}
		}, 300L);

	}

	// Weather functions
	private void setTime(World w, Long time) {
		w.setTime(time);
	}

	private void setWeather(World w, Boolean storm) {
		w.setStorm(storm);
	}

	private Long getTime(World w) {
		return w.getTime();
	}

	private Boolean getWeather(World w) {
		return w.hasStorm();
	}

	private void freezeWorld(World w, Long time, Boolean storm) {
		addWorldToConfig(w, time, storm);
		keepWorldFrozen(w, time, storm);
	}

	private void unfreezeWorld(World w) {
		removeWorldFromConfig(w);
	}

	// set world to keep in it's saved state every 10 seconds
	private void keepWorldFrozen(final World w, final Long time, final Boolean storm) {
		getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
			@Override
			public void run() {
				setTime(w, time);
				setWeather(w, storm);
			}
		}, 60L, 200L);
	}

	// Commands for weather
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if (cmd.getName().equalsIgnoreCase("fe")) {
			// Firstly check for permissions
			if (!sender.hasPermission("ForeverWeather.access")) {
				sender.sendMessage(ChatColor.RED + "You lack to permissions to use that command!");
				return true;
			}

			int l = args.length;
			switch (l) {
			case 0:
				// Display help/commands
				sender.sendMessage(ChatColor.RED + "=======~ " + ChatColor.YELLOW + "ForeverWeather " + ChatColor.RED + "~=======");
				sender.sendMessage(ChatColor.GREEN + "Created by PiedoBear");
				sender.sendMessage(ChatColor.GREEN + "/fe help for command info");
				return true;

			case 1:
				if (args[0].equalsIgnoreCase("help")) {
					sender.sendMessage(ChatColor.RED + "=======~ " + ChatColor.YELLOW + "ForeverWeather " + ChatColor.RED + "~=======");
					sender.sendMessage(ChatColor.GREEN + "Commands:");
					sender.sendMessage(ChatColor.YELLOW + "/fe freeze [world] " + ChatColor.GRAY + "- freezes the world's weather");
					sender.sendMessage(ChatColor.YELLOW + "/fe unfreeze [world] " + ChatColor.GRAY + "- returns the world to a normal cycle");
					sender.sendMessage(ChatColor.YELLOW + "/fe status [world] " + ChatColor.GRAY + "- tells you the time and weather");
					sender.sendMessage(ChatColor.YELLOW + "/fe setfreeze [world] [time] [weather] " + ChatColor.GRAY + "- freezes the world");
					sender.sendMessage(ChatColor.GRAY + "to a state you define.");

					return true;
				}

				if (args[0].equalsIgnoreCase("reload")) {
					loadConfig();
					sender.sendMessage(ChatColor.YELLOW + "Config reloaded!");
					return true;
				}
				break;

			case 2:
				// Try get world, if null, return error.
				World w = this.getServer().getWorld(args[1]);

				if (args[0].equalsIgnoreCase("freeze")) {
					if (w != null) {
						freezeWorld(w, getTime(w), getWeather(w));
						sender.sendMessage(ChatColor.YELLOW + w.getName() + " is now frozen.");
					} else {
						sender.sendMessage(ChatColor.RED + args[1] + " is not a valid world!");
					}
					return true;
				}

				if (args[0].equalsIgnoreCase("unfreeze")) {
					if (w != null) {
						unfreezeWorld(w);
						sender.sendMessage(ChatColor.YELLOW + w.getName() + " is now unfrozen.");
					} else {
						sender.sendMessage(ChatColor.RED + args[1] + " is not a valid world!");
					}
					return true;
				}

				if (args[0].equalsIgnoreCase("status")) {
					if (w != null) {
						sender.sendMessage(ChatColor.GREEN + "Current status for world: " + w.getName());
						sender.sendMessage(ChatColor.YELLOW + "Time (in ticks): " + getTime(w));
						sender.sendMessage(ChatColor.YELLOW + "Raining/Storming: " + getWeather(w));
					} else {
						sender.sendMessage(ChatColor.RED + args[1] + " is not a valid world!");
					}
					return true;
				}

				break;

			case 4:
				if (args[0].equalsIgnoreCase("setfreeze")) {
					World world = this.getServer().getWorld(args[1]);
					Boolean storm;
					Long time = 0L;

					// ensure time is a number
					try {
						time = Long.parseLong(args[2]);
					} catch (NumberFormatException nfe) {
						sender.sendMessage(ChatColor.RED + "The time must be a number! eg- 6000");
					}

					// ensure we can get true/false values
					if (args[3].equalsIgnoreCase("storm") || args[3].equalsIgnoreCase("rain")) {
						storm = true;
					} else if (args[3].equalsIgnoreCase("sun")) {
						storm = false;
					} else {
						sender.sendMessage(ChatColor.RED + "You must choose a weather state: sun or storm/rain!");
						return true;
					}

					// check null world
					if (world != null) {
						// proceed to freeze the world
						freezeWorld(world, time, storm);
						sender.sendMessage(ChatColor.YELLOW + world.getName() + " is now frozen.");
						return true;
					} else {
						sender.sendMessage(ChatColor.RED + args[1] + " is not a valid world!");
						return true;
					}
				}
				break;

			default:
				sender.sendMessage(ChatColor.RED + "Incorrect number of parameters!");
				return true;
			}
		}
		return false;
	}
}

Comments

Posts Quoted:
Reply
Clear All Quotes