2013-02-10 23:24:30 +01:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.HashMap;
|
2013-06-05 19:44:30 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.File;
|
2013-04-30 21:41:16 +02:00
|
|
|
|
2013-02-10 23:24:30 +01:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.apache.commons.lang.math.NumberUtils;
|
2013-02-10 23:24:30 +01:00
|
|
|
|
|
|
|
import com.dre.brewery.listeners.BlockListener;
|
2013-04-19 16:35:08 +02:00
|
|
|
import com.dre.brewery.listeners.PlayerListener;
|
2013-04-28 23:57:41 +02:00
|
|
|
import com.dre.brewery.listeners.EntityListener;
|
2013-05-09 21:47:58 +02:00
|
|
|
import com.dre.brewery.listeners.InventoryListener;
|
|
|
|
import com.dre.brewery.listeners.WorldListener;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.bukkit.event.HandlerList;
|
|
|
|
import org.bukkit.Bukkit;
|
2013-06-05 19:44:30 +02:00
|
|
|
import org.bukkit.World;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
2013-05-28 16:25:06 +02:00
|
|
|
import org.bukkit.block.Block;
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public class P extends JavaPlugin {
|
2013-02-10 23:24:30 +01:00
|
|
|
public static P p;
|
2013-04-30 21:41:16 +02:00
|
|
|
public static int lastBackup = 0;
|
2013-06-05 20:06:44 +02:00
|
|
|
public static int lastSave = 1;
|
|
|
|
public static int autosave = 3;
|
2013-05-03 16:08:05 +02:00
|
|
|
|
|
|
|
// Listeners
|
2013-02-10 23:24:30 +01:00
|
|
|
public BlockListener blockListener;
|
2013-04-19 16:35:08 +02:00
|
|
|
public PlayerListener playerListener;
|
2013-04-28 23:57:41 +02:00
|
|
|
public EntityListener entityListener;
|
2013-05-09 21:47:58 +02:00
|
|
|
public InventoryListener inventoryListener;
|
|
|
|
public WorldListener worldListener;
|
2013-05-03 16:08:05 +02:00
|
|
|
|
2013-02-10 23:24:30 +01:00
|
|
|
@Override
|
2013-05-03 16:08:05 +02:00
|
|
|
public void onEnable() {
|
2013-04-19 16:35:08 +02:00
|
|
|
p = this;
|
|
|
|
|
|
|
|
readConfig();
|
2013-04-30 21:41:16 +02:00
|
|
|
readData();
|
2013-05-03 16:08:05 +02:00
|
|
|
|
|
|
|
// Listeners
|
2013-04-19 16:35:08 +02:00
|
|
|
blockListener = new BlockListener();
|
|
|
|
playerListener = new PlayerListener();
|
2013-04-28 23:57:41 +02:00
|
|
|
entityListener = new EntityListener();
|
2013-05-09 21:47:58 +02:00
|
|
|
inventoryListener = new InventoryListener();
|
|
|
|
worldListener = new WorldListener();
|
2013-05-03 16:08:05 +02:00
|
|
|
|
2013-04-19 16:35:08 +02:00
|
|
|
p.getServer().getPluginManager().registerEvents(blockListener, p);
|
|
|
|
p.getServer().getPluginManager().registerEvents(playerListener, p);
|
2013-04-28 23:57:41 +02:00
|
|
|
p.getServer().getPluginManager().registerEvents(entityListener, p);
|
2013-05-09 21:47:58 +02:00
|
|
|
p.getServer().getPluginManager().registerEvents(inventoryListener, p);
|
|
|
|
p.getServer().getPluginManager().registerEvents(worldListener, p);
|
|
|
|
|
2013-06-30 23:41:37 +02:00
|
|
|
p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 650, 1200);
|
|
|
|
p.getServer().getScheduler().runTaskTimer(p, new DrunkRunnable(), 120, 120);
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
this.log(this.getDescription().getName() + " enabled!");
|
2013-02-10 23:24:30 +01:00
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// Disable listeners
|
2013-04-19 16:35:08 +02:00
|
|
|
HandlerList.unregisterAll(p);
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// Stop shedulers
|
2013-04-19 16:35:08 +02:00
|
|
|
p.getServer().getScheduler().cancelTasks(this);
|
2013-05-03 16:08:05 +02:00
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
saveData();
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
this.log(this.getDescription().getName() + " disabled!");
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void msg(CommandSender sender, String msg) {
|
|
|
|
sender.sendMessage(ChatColor.DARK_GREEN + "[Brewery] " + ChatColor.WHITE + msg);
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void log(String msg) {
|
2013-04-19 16:35:08 +02:00
|
|
|
this.msg(Bukkit.getConsoleSender(), msg);
|
2013-02-10 23:24:30 +01:00
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void errorLog(String msg) {
|
|
|
|
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[Brewery] " + ChatColor.DARK_RED + "ERROR: " + ChatColor.RED + msg);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void readConfig() {
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
File file = new File(p.getDataFolder(), "config.yml");
|
|
|
|
if (!file.exists()) {
|
2013-04-19 16:35:08 +02:00
|
|
|
saveDefaultConfig();
|
|
|
|
}
|
|
|
|
FileConfiguration config = getConfig();
|
|
|
|
|
2013-06-05 20:06:44 +02:00
|
|
|
// various Settings
|
|
|
|
autosave = config.getInt("autosave", 3);
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading recipes
|
2013-04-19 16:35:08 +02:00
|
|
|
ConfigurationSection configSection = config.getConfigurationSection("recipes");
|
2013-05-03 16:08:05 +02:00
|
|
|
if (configSection != null) {
|
|
|
|
for (String recipeId : configSection.getKeys(false)) {
|
|
|
|
BIngredients.recipes.add(new BRecipe(configSection, recipeId));
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading cooked names and possible ingredients
|
2013-04-19 16:35:08 +02:00
|
|
|
configSection = config.getConfigurationSection("cooked");
|
2013-05-03 16:08:05 +02:00
|
|
|
if (configSection != null) {
|
|
|
|
for (String ingredient : configSection.getKeys(false)) {
|
2013-05-28 16:25:06 +02:00
|
|
|
BIngredients.cookedNames.put(Material.matchMaterial(ingredient), (configSection.getString(ingredient)));
|
|
|
|
BIngredients.possibleIngredients.add(Material.matchMaterial(ingredient));
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// telling Words the path, it will load it when needed
|
2013-05-02 10:06:07 +02:00
|
|
|
Words.config = config;
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// load all Data
|
|
|
|
public void readData() {
|
|
|
|
File file = new File(p.getDataFolder(), "data.yml");
|
|
|
|
if (file.exists()) {
|
2013-04-30 21:41:16 +02:00
|
|
|
|
|
|
|
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading Brew
|
2013-04-30 21:41:16 +02:00
|
|
|
ConfigurationSection section = data.getConfigurationSection("Brew");
|
2013-05-03 16:08:05 +02:00
|
|
|
if (section != null) {
|
|
|
|
// All sections have the UID as name
|
|
|
|
for (String uid : section.getKeys(false)) {
|
2013-05-28 16:25:06 +02:00
|
|
|
BIngredients ingredients = loadIngredients(section.getConfigurationSection(uid + ".ingredients"));
|
|
|
|
int quality = section.getInt(uid + ".quality", 0);
|
|
|
|
int distillRuns = section.getInt(uid + ".distillRuns", 0);
|
|
|
|
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
|
|
|
|
String recipe = section.getString(uid + ".recipe", null);
|
|
|
|
|
|
|
|
new Brew(parseInt(uid), ingredients, quality, distillRuns, ageTime, recipe);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 21:47:58 +02:00
|
|
|
// loading BPlayer
|
|
|
|
section = data.getConfigurationSection("Player");
|
2013-05-03 16:08:05 +02:00
|
|
|
if (section != null) {
|
2013-05-09 21:47:58 +02:00
|
|
|
// keys have players name
|
|
|
|
for (String name : section.getKeys(false)) {
|
2013-05-28 16:25:06 +02:00
|
|
|
int quality = section.getInt(name + ".quality");
|
|
|
|
int drunk = section.getInt(name + ".drunk");
|
|
|
|
int offDrunk = section.getInt(name + ".offDrunk", 0);
|
|
|
|
boolean passedOut = section.getBoolean(name + ".passedOut", false);
|
|
|
|
|
|
|
|
new BPlayer(name, quality, drunk, offDrunk, passedOut);
|
2013-05-09 21:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 19:44:30 +02:00
|
|
|
for (World world : p.getServer().getWorlds()) {
|
|
|
|
if (world.getName().startsWith("DXL_")) {
|
|
|
|
loadWorldData(getDxlName(world.getName()), world);
|
|
|
|
} else {
|
|
|
|
loadWorldData(world.getUID().toString(), world);
|
|
|
|
}
|
2013-05-09 21:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
errorLog("No data.yml found, will create new one!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loads BIngredients from ingredient section
|
|
|
|
public BIngredients loadIngredients(ConfigurationSection config) {
|
|
|
|
if (config != null) {
|
|
|
|
ConfigurationSection matSection = config.getConfigurationSection("mats");
|
|
|
|
if (matSection != null) {
|
|
|
|
// matSection has all the materials + amount in Integer form
|
|
|
|
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
|
|
|
for (String ingredient : matSection.getKeys(false)) {
|
|
|
|
// convert to Material
|
|
|
|
ingredients.put(Material.getMaterial(parseInt(ingredient)), matSection.getInt(ingredient));
|
|
|
|
}
|
|
|
|
return new BIngredients(ingredients, config.getInt("cookedTime", 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errorLog("Ingredient section not found or incomplete in data.yml");
|
|
|
|
return new BIngredients();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load Block locations of given world
|
2013-06-05 19:44:30 +02:00
|
|
|
public void loadWorldData(String uuid, World world) {
|
2013-05-09 21:47:58 +02:00
|
|
|
|
|
|
|
File file = new File(p.getDataFolder(), "data.yml");
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
|
|
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
|
|
|
|
|
|
|
// loading BCauldron
|
|
|
|
if (data.contains("BCauldron." + uuid)) {
|
|
|
|
ConfigurationSection section = data.getConfigurationSection("BCauldron." + uuid);
|
2013-05-03 16:08:05 +02:00
|
|
|
for (String cauldron : section.getKeys(false)) {
|
2013-05-09 21:47:58 +02:00
|
|
|
// block is splitted into x/y/z
|
2013-05-03 16:08:05 +02:00
|
|
|
String block = section.getString(cauldron + ".block");
|
|
|
|
if (block != null) {
|
2013-04-30 21:41:16 +02:00
|
|
|
String[] splitted = block.split("/");
|
2013-05-09 21:47:58 +02:00
|
|
|
if (splitted.length == 3) {
|
2013-06-05 19:44:30 +02:00
|
|
|
|
|
|
|
Block worldBlock = world.getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2]));
|
2013-05-28 16:25:06 +02:00
|
|
|
BIngredients ingredients = loadIngredients(section.getConfigurationSection(cauldron + ".ingredients"));
|
|
|
|
int state = section.getInt(cauldron + ".state", 1);
|
|
|
|
|
|
|
|
new BCauldron(worldBlock, ingredients, state);
|
2013-04-30 21:41:16 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading Barrel
|
2013-05-09 21:47:58 +02:00
|
|
|
if (data.contains("Barrel." + uuid)) {
|
|
|
|
ConfigurationSection section = data.getConfigurationSection("Barrel." + uuid);
|
2013-05-03 16:08:05 +02:00
|
|
|
for (String barrel : section.getKeys(false)) {
|
2013-05-09 21:47:58 +02:00
|
|
|
// block spigot is splitted into x/y/z
|
2013-05-03 16:08:05 +02:00
|
|
|
String spigot = section.getString(barrel + ".spigot");
|
|
|
|
if (spigot != null) {
|
2013-04-30 21:41:16 +02:00
|
|
|
String[] splitted = spigot.split("/");
|
2013-05-09 21:47:58 +02:00
|
|
|
if (splitted.length == 3) {
|
2013-06-05 19:44:30 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// load itemStacks from invSection
|
|
|
|
ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv");
|
2013-06-05 19:44:30 +02:00
|
|
|
Block block = world.getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2]));
|
2013-05-28 16:25:06 +02:00
|
|
|
float time = (float) section.getDouble(barrel + ".time", 0.0);
|
2013-06-05 19:44:30 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (invSection != null) {
|
2013-05-28 16:25:06 +02:00
|
|
|
new Barrel(block, invSection.getValues(true), time);
|
2013-04-30 21:41:16 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
// Barrel has no inventory
|
2013-05-28 16:25:06 +02:00
|
|
|
new Barrel(block, time);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
2013-06-05 19:44:30 +02:00
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// save all Data
|
|
|
|
public void saveData() {
|
2013-04-30 21:41:16 +02:00
|
|
|
File datafile = new File(p.getDataFolder(), "data.yml");
|
2013-05-09 21:47:58 +02:00
|
|
|
|
2013-05-10 00:01:28 +02:00
|
|
|
FileConfiguration oldData = YamlConfiguration.loadConfiguration(datafile);
|
2013-05-09 21:47:58 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (datafile.exists()) {
|
|
|
|
if (lastBackup > 10) {
|
2013-04-30 21:41:16 +02:00
|
|
|
datafile.renameTo(new File(p.getDataFolder(), "dataBackup.yml"));
|
2013-05-09 21:47:58 +02:00
|
|
|
lastBackup = 0;
|
2013-04-30 21:41:16 +02:00
|
|
|
} else {
|
|
|
|
lastBackup++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileConfiguration configFile = new YamlConfiguration();
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (!Brew.potions.isEmpty()) {
|
2013-04-30 21:41:16 +02:00
|
|
|
Brew.save(configFile.createSection("Brew"));
|
|
|
|
}
|
2013-05-28 16:25:06 +02:00
|
|
|
if (!BCauldron.bcauldrons.isEmpty() || oldData.contains("BCauldron")) {
|
2013-05-10 00:01:28 +02:00
|
|
|
BCauldron.save(configFile.createSection("BCauldron"), oldData.getConfigurationSection("BCauldron"));
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:25:06 +02:00
|
|
|
if (!Barrel.barrels.isEmpty() || oldData.contains("Barrel")) {
|
2013-05-10 00:01:28 +02:00
|
|
|
Barrel.save(configFile.createSection("Barrel"), oldData.getConfigurationSection("Barrel"));
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (!BPlayer.players.isEmpty()) {
|
2013-05-02 10:06:07 +02:00
|
|
|
BPlayer.save(configFile.createSection("Player"));
|
|
|
|
}
|
2013-04-30 21:41:16 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
configFile.save(datafile);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-05 20:06:44 +02:00
|
|
|
|
|
|
|
lastSave = 1;
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public int parseInt(String string) {
|
2013-04-19 16:35:08 +02:00
|
|
|
return NumberUtils.toInt(string, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-05 19:44:30 +02:00
|
|
|
public String getDxlName(String worldName) {
|
|
|
|
File dungeonFolder = new File(worldName);
|
|
|
|
if (dungeonFolder.isDirectory()) {
|
|
|
|
for (File file : dungeonFolder.listFiles()) {
|
|
|
|
if (!file.isDirectory()) {
|
|
|
|
if (file.getName().startsWith(".id_")) {
|
|
|
|
return file.getName().substring(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-30 23:41:37 +02:00
|
|
|
public class DrunkRunnable implements Runnable {
|
|
|
|
|
|
|
|
public DrunkRunnable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!BPlayer.players.isEmpty()) {
|
|
|
|
BPlayer.drunkeness();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public class BreweryRunnable implements Runnable {
|
2013-04-19 16:35:08 +02:00
|
|
|
|
|
|
|
public BreweryRunnable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
p.log("Update");
|
2013-05-03 16:08:05 +02:00
|
|
|
for (BCauldron cauldron : BCauldron.bcauldrons) {
|
|
|
|
cauldron.onUpdate();// runs every min to update cooking time
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
Barrel.onUpdate();// runs every min to check and update ageing time
|
|
|
|
BPlayer.onUpdate();// updates players drunkeness
|
2013-04-30 21:41:16 +02:00
|
|
|
|
2013-06-05 20:06:44 +02:00
|
|
|
if (lastSave >= autosave) {
|
|
|
|
saveData();// save all data
|
|
|
|
} else {
|
|
|
|
lastSave++;
|
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-10 23:24:30 +01:00
|
|
|
}
|