2013-02-10 23:24:30 +01:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
import java.util.Map;
|
2013-08-14 21:18:11 +02:00
|
|
|
import java.util.List;
|
2013-07-28 23:53:35 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ListIterator;
|
2013-04-30 21:41:16 +02:00
|
|
|
import java.util.HashMap;
|
2013-06-05 19:44:30 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.File;
|
2014-06-06 01:27:27 +02:00
|
|
|
import java.util.UUID;
|
2014-05-06 23:19:25 +02:00
|
|
|
import java.util.logging.Level;
|
2013-04-30 21:41:16 +02:00
|
|
|
|
2014-04-08 16:11:20 +02:00
|
|
|
import org.bukkit.entity.Player;
|
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;
|
2014-04-09 00:35:08 +02:00
|
|
|
|
|
|
|
import com.dre.brewery.integration.LogBlockBarrel;
|
2013-04-19 16:35:08 +02:00
|
|
|
import org.apache.commons.lang.math.NumberUtils;
|
|
|
|
import org.bukkit.event.HandlerList;
|
|
|
|
import org.bukkit.Bukkit;
|
2013-06-05 19:44:30 +02:00
|
|
|
import org.bukkit.World;
|
2013-07-28 23:53:35 +02:00
|
|
|
import org.bukkit.Location;
|
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;
|
2014-03-17 16:02:22 +01:00
|
|
|
import org.mcstats.Metrics;
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-07-28 23:53:35 +02:00
|
|
|
import com.dre.brewery.listeners.*;
|
|
|
|
|
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-09-09 16:28:35 +02:00
|
|
|
public static boolean debug;
|
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;
|
2014-06-03 21:03:32 +02:00
|
|
|
final public static String dataVersion = "1.1";
|
2014-06-06 01:27:27 +02:00
|
|
|
public static boolean useUUID;
|
2013-05-03 16:08:05 +02:00
|
|
|
|
2014-04-08 16:11:20 +02:00
|
|
|
// Third Party Enabled
|
2014-05-06 23:19:25 +02:00
|
|
|
public boolean useWG; //WorldGuard
|
|
|
|
public boolean useLWC; //LWC
|
|
|
|
public boolean useLB; //LogBlock
|
|
|
|
public boolean useGP; //GriefPrevention
|
2014-04-08 16:11:20 +02:00
|
|
|
|
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-08-26 16:50:27 +02:00
|
|
|
// Language
|
|
|
|
public String language;
|
|
|
|
public LanguageReader languageReader;
|
|
|
|
|
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;
|
|
|
|
|
2014-06-06 01:27:27 +02:00
|
|
|
// Version check
|
|
|
|
String v = Bukkit.getBukkitVersion();
|
|
|
|
useUUID = !v.matches(".*1\\.[1-6].*") && !v.matches(".*1\\.7\\.[0-5].*");
|
|
|
|
|
2013-04-19 16:35:08 +02:00
|
|
|
readConfig();
|
2013-04-30 21:41:16 +02:00
|
|
|
readData();
|
2014-03-17 16:02:22 +01:00
|
|
|
|
|
|
|
// Setup Metrics
|
|
|
|
setupMetrics();
|
2013-08-26 16:50:27 +02:00
|
|
|
|
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-07-28 23:53:35 +02:00
|
|
|
getCommand("Brewery").setExecutor(new CommandListener());
|
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-10-15 18:14:11 +02:00
|
|
|
// Heartbeat
|
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-07-29 18:54:38 +02:00
|
|
|
// save Data to Disk
|
2013-04-30 21:41:16 +02:00
|
|
|
saveData();
|
2013-09-11 14:45:46 +02:00
|
|
|
|
|
|
|
// save LanguageReader
|
|
|
|
languageReader.save();
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-07-29 18:54:38 +02:00
|
|
|
// delete Data from Ram
|
|
|
|
Barrel.barrels.clear();
|
|
|
|
BCauldron.bcauldrons.clear();
|
|
|
|
BIngredients.possibleIngredients.clear();
|
|
|
|
BIngredients.recipes.clear();
|
|
|
|
BIngredients.cookedNames.clear();
|
2014-06-06 01:27:27 +02:00
|
|
|
BPlayer.clear();
|
2013-07-29 18:54:38 +02:00
|
|
|
Brew.potions.clear();
|
|
|
|
Wakeup.wakeups.clear();
|
|
|
|
Words.words.clear();
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
this.log(this.getDescription().getName() + " disabled!");
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
2014-03-17 16:02:22 +01:00
|
|
|
|
|
|
|
public void setupMetrics() {
|
|
|
|
try {
|
|
|
|
Metrics metrics = new Metrics(this);
|
|
|
|
metrics.start();
|
|
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 18:46:33 +02:00
|
|
|
public void reload(CommandSender sender) {
|
2013-10-15 18:14:11 +02:00
|
|
|
// clear all existent config Data
|
2013-07-29 18:54:38 +02:00
|
|
|
BIngredients.possibleIngredients.clear();
|
|
|
|
BIngredients.recipes.clear();
|
|
|
|
BIngredients.cookedNames.clear();
|
|
|
|
Words.words.clear();
|
2013-08-14 21:18:11 +02:00
|
|
|
BPlayer.drainItems.clear();
|
2014-05-06 23:19:25 +02:00
|
|
|
if (useLB) {
|
|
|
|
try {
|
|
|
|
LogBlockBarrel.clear();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-04-09 00:35:08 +02:00
|
|
|
}
|
2013-07-29 18:54:38 +02:00
|
|
|
|
2013-10-15 18:14:11 +02:00
|
|
|
// load the Config
|
2013-07-29 18:54:38 +02:00
|
|
|
readConfig();
|
2013-09-05 18:46:33 +02:00
|
|
|
|
2013-10-15 18:14:11 +02:00
|
|
|
// save and load LanguageReader
|
|
|
|
languageReader.save();
|
|
|
|
languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml"));
|
|
|
|
|
|
|
|
// Reload Recipes
|
2013-09-05 18:46:33 +02:00
|
|
|
Boolean successful = true;
|
|
|
|
for (Brew brew : Brew.potions.values()) {
|
|
|
|
if (!brew.reloadRecipe()) {
|
|
|
|
successful = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!successful) {
|
2013-10-15 18:14:11 +02:00
|
|
|
msg(sender, p.languageReader.get("Error_Recipeload"));
|
2013-09-05 18:46:33 +02:00
|
|
|
}
|
2013-07-29 18:54:38 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void msg(CommandSender sender, String msg) {
|
2013-08-15 18:17:05 +02:00
|
|
|
sender.sendMessage(color("&2[Brewery] &f" + 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-09-09 16:28:35 +02:00
|
|
|
public void debugLog(String msg) {
|
|
|
|
if (debug) {
|
|
|
|
this.msg(Bukkit.getConsoleSender(), "&2[Debug] &f" + msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public void errorLog(String msg) {
|
2014-05-06 23:19:25 +02:00
|
|
|
Bukkit.getLogger().log(Level.SEVERE, 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() {
|
|
|
|
File file = new File(p.getDataFolder(), "config.yml");
|
|
|
|
if (!file.exists()) {
|
2013-04-19 16:35:08 +02:00
|
|
|
saveDefaultConfig();
|
|
|
|
}
|
2013-07-29 18:54:38 +02:00
|
|
|
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
2013-04-19 16:35:08 +02:00
|
|
|
|
2013-11-21 22:08:47 +01:00
|
|
|
// Set the Language
|
|
|
|
language = config.getString("language", "en");
|
|
|
|
|
2014-04-08 16:11:20 +02:00
|
|
|
// Load LanguageReader
|
|
|
|
languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml"));
|
|
|
|
|
2013-11-21 22:08:47 +01:00
|
|
|
// Check if config is the newest version
|
|
|
|
String version = config.getString("version", null);
|
|
|
|
if (version != null) {
|
|
|
|
String currentVersion = getDescription().getVersion();
|
|
|
|
if (!version.equals(currentVersion)) {
|
|
|
|
new ConfigUpdater(file).update(version, language);
|
|
|
|
P.p.log("Config Updated to version: " + currentVersion);
|
|
|
|
config = YamlConfiguration.loadConfiguration(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 23:19:25 +02:00
|
|
|
// Third-Party
|
|
|
|
useWG = config.getBoolean("useWorldGuard", true) && getServer().getPluginManager().isPluginEnabled("WorldGuard");
|
|
|
|
useLWC = config.getBoolean("useLWC", true) && getServer().getPluginManager().isPluginEnabled("LWC");
|
|
|
|
useGP = config.getBoolean("useGriefPrevention", true) && getServer().getPluginManager().isPluginEnabled("GriefPrevention");
|
|
|
|
useLB = config.getBoolean("useLogBlock", false) && getServer().getPluginManager().isPluginEnabled("LogBlock");
|
|
|
|
|
2013-06-05 20:06:44 +02:00
|
|
|
// various Settings
|
|
|
|
autosave = config.getInt("autosave", 3);
|
2013-09-09 16:28:35 +02:00
|
|
|
debug = config.getBoolean("debug", false);
|
2014-06-03 21:03:32 +02:00
|
|
|
BPlayer.pukeItem = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND"));
|
2013-09-01 23:47:01 +02:00
|
|
|
BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60;
|
2013-08-19 16:23:12 +02:00
|
|
|
BPlayer.overdrinkKick = config.getBoolean("enableKickOnOverdrink", false);
|
|
|
|
BPlayer.enableHome = config.getBoolean("enableHome", false);
|
|
|
|
BPlayer.enableLoginDisallow = config.getBoolean("enableLoginDisallow", false);
|
|
|
|
BPlayer.enablePuke = config.getBoolean("enablePuke", false);
|
|
|
|
BPlayer.homeType = config.getString("homeType", null);
|
|
|
|
Brew.colorInBarrels = config.getBoolean("colorInBarrels", false);
|
|
|
|
Brew.colorInBrewer = config.getBoolean("colorInBrewer", false);
|
2014-04-08 16:11:20 +02:00
|
|
|
PlayerListener.openEverywhere = config.getBoolean("openLargeBarrelEverywhere", false);
|
2013-10-25 17:59:21 +02:00
|
|
|
Words.log = config.getBoolean("logRealChat", false);
|
2013-11-17 15:26:28 +01:00
|
|
|
Words.commands = config.getStringList("distortCommands");
|
2013-11-21 17:14:14 +01:00
|
|
|
Words.doSigns = config.getBoolean("distortSignText", false);
|
|
|
|
for (String bypass : config.getStringList("distortBypass")) {
|
|
|
|
Words.ignoreText.add(bypass.split(","));
|
|
|
|
}
|
2013-06-05 20:06:44 +02:00
|
|
|
|
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)) {
|
2013-09-05 23:02:45 +02:00
|
|
|
BRecipe recipe = new BRecipe(configSection, recipeId);
|
|
|
|
if (recipe.isValid()) {
|
|
|
|
BIngredients.recipes.add(recipe);
|
|
|
|
} else {
|
2013-10-15 18:14:11 +02:00
|
|
|
errorLog("Loading the Recipe with id: '" + recipeId + "' failed!");
|
2013-09-05 23:02:45 +02:00
|
|
|
}
|
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-07-03 00:31:34 +02:00
|
|
|
Material mat = Material.matchMaterial(ingredient);
|
2013-09-06 00:22:39 +02:00
|
|
|
if (mat != null) {
|
|
|
|
BIngredients.cookedNames.put(mat, (configSection.getString(ingredient, null)));
|
|
|
|
BIngredients.possibleIngredients.add(mat);
|
|
|
|
} else {
|
2013-10-15 18:14:11 +02:00
|
|
|
errorLog("Unknown Material: " + ingredient);
|
2013-09-06 00:22:39 +02:00
|
|
|
}
|
2013-04-19 16:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 21:18:11 +02:00
|
|
|
// loading drainItems
|
|
|
|
List<String> drainList = config.getStringList("drainItems");
|
|
|
|
if (drainList != null) {
|
|
|
|
for (String drainString : drainList) {
|
|
|
|
String[] drainSplit = drainString.split("/");
|
|
|
|
if (drainSplit.length > 1) {
|
|
|
|
Material mat = Material.matchMaterial(drainSplit[0]);
|
|
|
|
int strength = p.parseInt(drainSplit[1]);
|
|
|
|
if (mat != null && strength > 0) {
|
|
|
|
BPlayer.drainItems.put(mat, strength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2014-06-03 21:03:32 +02:00
|
|
|
// Check if data is the newest version
|
|
|
|
String version = data.getString("Version", null);
|
|
|
|
if (version != null) {
|
|
|
|
if (!version.equals(dataVersion)) {
|
|
|
|
P.p.log("Data File is being updated...");
|
|
|
|
new DataUpdater(data, file).update(version);
|
|
|
|
data = YamlConfiguration.loadConfiguration(file);
|
|
|
|
P.p.log("Data Updated to version: " + dataVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 23:32:09 +02:00
|
|
|
// loading Ingredients into ingMap
|
|
|
|
Map<String, BIngredients> ingMap = new HashMap<String, BIngredients>();
|
|
|
|
ConfigurationSection section = data.getConfigurationSection("Ingredients");
|
|
|
|
if (section != null) {
|
|
|
|
for (String id : section.getKeys(false)) {
|
|
|
|
ConfigurationSection matSection = section.getConfigurationSection(id + ".mats");
|
|
|
|
if (matSection != null) {
|
|
|
|
// matSection has all the materials + amount as Integers
|
|
|
|
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
2014-06-03 21:03:32 +02:00
|
|
|
for (String mat : matSection.getKeys(false)) {
|
|
|
|
ingredients.put(Material.getMaterial(mat), matSection.getInt(mat));
|
2013-09-04 23:32:09 +02:00
|
|
|
}
|
|
|
|
ingMap.put(id, new BIngredients(ingredients, section.getInt(id + ".cookedTime", 0)));
|
|
|
|
} else {
|
|
|
|
errorLog("Ingredient id: '" + id + "' incomplete in data.yml");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// loading Brew
|
2013-09-04 23:32:09 +02:00
|
|
|
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-09-04 23:32:09 +02:00
|
|
|
BIngredients ingredients = getIngredients(ingMap, section.getString(uid + ".ingId"));
|
2013-05-28 16:25:06 +02:00
|
|
|
int quality = section.getInt(uid + ".quality", 0);
|
|
|
|
int distillRuns = section.getInt(uid + ".distillRuns", 0);
|
|
|
|
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
|
2013-09-05 17:09:20 +02:00
|
|
|
float wood = (float) section.getDouble(uid + ".wood", -1.0);
|
2013-05-28 16:25:06 +02:00
|
|
|
String recipe = section.getString(uid + ".recipe", null);
|
2014-06-03 23:10:47 +02:00
|
|
|
boolean unlabeled = section.getBoolean(uid + ".unlabeled", false);
|
|
|
|
boolean persistent = section.getBoolean(uid + ".persist", false);
|
2013-05-28 16:25:06 +02:00
|
|
|
|
2014-06-03 23:10:47 +02:00
|
|
|
new Brew(parseInt(uid), ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, persistent);
|
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)) {
|
2014-06-06 01:27:27 +02:00
|
|
|
try {
|
|
|
|
UUID.fromString(name);
|
|
|
|
if (!useUUID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
if (useUUID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 23:32:09 +02:00
|
|
|
// returns Ingredients by id from the specified ingMap
|
|
|
|
public BIngredients getIngredients(Map<String, BIngredients> ingMap, String id) {
|
|
|
|
if (!ingMap.isEmpty()) {
|
|
|
|
if (ingMap.containsKey(id)) {
|
|
|
|
return ingMap.get(id);
|
2013-05-09 21:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 23:32:09 +02:00
|
|
|
errorLog("Ingredient id: '" + id + "' not found in data.yml");
|
|
|
|
return new BIngredients();
|
|
|
|
}
|
|
|
|
|
|
|
|
// loads BIngredients from an ingredient section
|
|
|
|
public BIngredients loadIngredients(ConfigurationSection section) {
|
|
|
|
if (section != null) {
|
|
|
|
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
2014-06-03 21:03:32 +02:00
|
|
|
for (String mat : section.getKeys(false)) {
|
|
|
|
ingredients.put(Material.getMaterial(mat), section.getInt(mat));
|
2013-09-04 23:32:09 +02:00
|
|
|
}
|
|
|
|
return new BIngredients(ingredients, 0);
|
|
|
|
} else {
|
|
|
|
errorLog("Cauldron is missing Ingredient Section");
|
|
|
|
}
|
2013-05-09 21:47:58 +02:00
|
|
|
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);
|
2014-04-08 16:11:20 +02:00
|
|
|
byte sign = (byte) section.getInt(barrel + ".sign", 0);
|
|
|
|
String[] st = section.getString(barrel + ".st", "").split(",");
|
|
|
|
String[] wo = section.getString(barrel + ".wo", "").split(",");
|
2013-06-05 19:44:30 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (invSection != null) {
|
2014-04-08 16:11:20 +02:00
|
|
|
new Barrel(block, sign, st, wo, 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
|
2014-04-08 16:11:20 +02:00
|
|
|
new Barrel(block, sign, st, wo, null, 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-07-28 23:53:35 +02:00
|
|
|
// loading Wakeup
|
|
|
|
if (data.contains("Wakeup." + uuid)) {
|
|
|
|
ConfigurationSection section = data.getConfigurationSection("Wakeup." + uuid);
|
|
|
|
for (String wakeup : section.getKeys(false)) {
|
|
|
|
// loc of wakeup is splitted into x/y/z/pitch/yaw
|
|
|
|
String loc = section.getString(wakeup);
|
|
|
|
if (loc != null) {
|
|
|
|
String[] splitted = loc.split("/");
|
|
|
|
if (splitted.length == 5) {
|
|
|
|
|
|
|
|
double x = NumberUtils.toDouble(splitted[0]);
|
|
|
|
double y = NumberUtils.toDouble(splitted[1]);
|
|
|
|
double z = NumberUtils.toDouble(splitted[2]);
|
|
|
|
float pitch = NumberUtils.toFloat(splitted[3]);
|
|
|
|
float yaw = NumberUtils.toFloat(splitted[4]);
|
|
|
|
Location location = new Location(world, x, y, z, yaw, pitch);
|
|
|
|
|
|
|
|
Wakeup.wakeups.add(new Wakeup(location));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
errorLog("Incomplete Location-Data in data.yml: " + section.getCurrentPath() + "." + wakeup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// save all Data
|
|
|
|
public void saveData() {
|
2013-08-14 20:08:55 +02:00
|
|
|
long time = System.nanoTime();
|
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-08-14 20:08:55 +02:00
|
|
|
|
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
|
|
|
|
2014-06-06 01:27:27 +02:00
|
|
|
if (!BPlayer.isEmpty()) {
|
2013-05-02 10:06:07 +02:00
|
|
|
BPlayer.save(configFile.createSection("Player"));
|
|
|
|
}
|
2013-04-30 21:41:16 +02:00
|
|
|
|
2013-07-28 23:53:35 +02:00
|
|
|
if (!Wakeup.wakeups.isEmpty() || oldData.contains("Wakeup")) {
|
|
|
|
Wakeup.save(configFile.createSection("Wakeup"), oldData.getConfigurationSection("Wakeup"));
|
|
|
|
}
|
|
|
|
|
2013-09-05 19:53:49 +02:00
|
|
|
saveWorldNames(configFile, oldData.getConfigurationSection("Worlds"));
|
|
|
|
|
2014-06-03 21:03:32 +02:00
|
|
|
configFile.set("Version", dataVersion);
|
2013-08-14 20:08:55 +02:00
|
|
|
|
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-08-14 20:08:55 +02:00
|
|
|
|
|
|
|
time = System.nanoTime() - time;
|
2013-09-09 16:28:35 +02:00
|
|
|
float ftime = (float) (time / 1000000.0);
|
|
|
|
p.debugLog("Writing Data to File (" + ftime + "ms)");
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 19:53:49 +02:00
|
|
|
public void saveWorldNames(FileConfiguration root, ConfigurationSection old) {
|
|
|
|
if (old != null) {
|
|
|
|
root.set("Worlds", old);
|
|
|
|
}
|
|
|
|
for (World world : p.getServer().getWorlds()) {
|
|
|
|
String worldName = world.getName();
|
|
|
|
if (worldName.startsWith("DXL_")) {
|
|
|
|
worldName = getDxlName(worldName);
|
|
|
|
root.set("Worlds." + worldName, 0);
|
|
|
|
} else {
|
|
|
|
worldName = world.getUID().toString();
|
|
|
|
root.set("Worlds." + worldName, world.getName());
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 21:41:16 +02:00
|
|
|
}
|
|
|
|
|
2013-07-28 23:53:35 +02:00
|
|
|
// Utility
|
|
|
|
|
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-07-28 23:53:35 +02:00
|
|
|
// gets the Name of a DXL World
|
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_")) {
|
2013-09-05 19:53:49 +02:00
|
|
|
return file.getName().substring(1).toLowerCase();
|
2013-06-05 19:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 22:37:14 +02:00
|
|
|
return worldName;
|
2013-06-05 19:44:30 +02:00
|
|
|
}
|
|
|
|
|
2013-07-28 23:53:35 +02:00
|
|
|
// create empty World save Sections
|
|
|
|
public void createWorldSections(ConfigurationSection section) {
|
|
|
|
for (World world : p.getServer().getWorlds()) {
|
|
|
|
String worldName = world.getName();
|
|
|
|
if (worldName.startsWith("DXL_")) {
|
|
|
|
worldName = getDxlName(worldName);
|
|
|
|
} else {
|
|
|
|
worldName = world.getUID().toString();
|
|
|
|
}
|
|
|
|
section.createSection(worldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prints a list of Strings at the specified page
|
2013-08-26 16:50:27 +02:00
|
|
|
public void list(CommandSender sender, ArrayList<String> strings, int page) {
|
2013-07-28 23:53:35 +02:00
|
|
|
int pages = (int) Math.ceil(strings.size() / 7F);
|
|
|
|
if (page > pages || page < 1) {
|
|
|
|
page = 1;
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:14:11 +02:00
|
|
|
sender.sendMessage(color("&7-------------- &f" + languageReader.get("Etc_Page") + " &6" + page + "&f/&6" + pages + " &7--------------"));
|
2013-07-28 23:53:35 +02:00
|
|
|
|
|
|
|
ListIterator<String> iter = strings.listIterator((page - 1) * 7);
|
|
|
|
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
if (iter.hasNext()) {
|
2013-08-15 18:17:05 +02:00
|
|
|
sender.sendMessage(color(iter.next()));
|
2013-07-28 23:53:35 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:35:08 +02:00
|
|
|
// Returns true if the Block can be destroyed by the Player or something else (null)
|
2014-04-08 16:11:20 +02:00
|
|
|
public boolean blockDestroy(Block block, Player player) {
|
|
|
|
switch (block.getType()) {
|
2014-05-19 15:17:55 +02:00
|
|
|
case CAULDRON:
|
|
|
|
// will only remove when existing
|
|
|
|
BCauldron.remove(block);
|
|
|
|
return true;
|
|
|
|
case FENCE:
|
|
|
|
case NETHER_FENCE:
|
|
|
|
// remove barrel and throw potions on the ground
|
|
|
|
Barrel barrel = Barrel.getBySpigot(block);
|
|
|
|
if (barrel != null) {
|
|
|
|
if (barrel.hasPermsDestroy(player)) {
|
|
|
|
barrel.remove(null, player);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2014-06-03 21:03:32 +02:00
|
|
|
case SIGN_POST:
|
2014-05-19 15:17:55 +02:00
|
|
|
case WALL_SIGN:
|
|
|
|
// remove small Barrels
|
|
|
|
Barrel barrel2 = Barrel.getBySpigot(block);
|
|
|
|
if (barrel2 != null) {
|
|
|
|
if (!barrel2.isLarge()) {
|
|
|
|
if (barrel2.hasPermsDestroy(player)) {
|
|
|
|
barrel2.remove(null, player);
|
2014-04-08 16:11:20 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-19 15:17:55 +02:00
|
|
|
} else {
|
|
|
|
barrel2.destroySign();
|
2014-04-08 16:11:20 +02:00
|
|
|
}
|
2014-05-19 15:17:55 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case WOOD:
|
|
|
|
case WOOD_STAIRS:
|
|
|
|
case BIRCH_WOOD_STAIRS:
|
|
|
|
case JUNGLE_WOOD_STAIRS:
|
|
|
|
case SPRUCE_WOOD_STAIRS:
|
2014-06-03 21:03:32 +02:00
|
|
|
case ACACIA_STAIRS:
|
|
|
|
case DARK_OAK_STAIRS:
|
2014-05-19 15:17:55 +02:00
|
|
|
Barrel barrel3 = Barrel.getByWood(block);
|
|
|
|
if (barrel3 != null) {
|
|
|
|
if (barrel3.hasPermsDestroy(player)) {
|
|
|
|
barrel3.remove(block, player);
|
|
|
|
} else {
|
|
|
|
return false;
|
2014-04-08 16:11:20 +02:00
|
|
|
}
|
2014-05-19 15:17:55 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2014-04-08 16:11:20 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-15 18:17:05 +02:00
|
|
|
public String color(String msg) {
|
|
|
|
if (msg != null) {
|
2014-02-13 22:07:01 +01:00
|
|
|
msg = ChatColor.translateAlternateColorCodes('&', msg);
|
2013-08-15 18:17:05 +02:00
|
|
|
}
|
|
|
|
return msg;
|
2013-07-04 02:59:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 01:27:27 +02:00
|
|
|
// Returns either uuid or Name of player, depending on bukkit version
|
|
|
|
public static String playerString(Player player) {
|
|
|
|
if (useUUID) {
|
|
|
|
return player.getUniqueId().toString();
|
|
|
|
} else {
|
|
|
|
return player.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the Player if online
|
|
|
|
public static Player getPlayerfromString(String name) {
|
|
|
|
if (useUUID) {
|
|
|
|
try {
|
|
|
|
return Bukkit.getPlayer(UUID.fromString(name));
|
2014-06-06 01:36:22 +02:00
|
|
|
} catch (Exception e) {
|
2014-06-06 01:27:27 +02:00
|
|
|
return Bukkit.getPlayerExact(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Bukkit.getPlayerExact(name);
|
|
|
|
}
|
|
|
|
|
2013-07-28 23:53:35 +02:00
|
|
|
// Runnables
|
|
|
|
|
2013-06-30 23:41:37 +02:00
|
|
|
public class DrunkRunnable implements Runnable {
|
|
|
|
|
|
|
|
public DrunkRunnable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-06-06 01:27:27 +02:00
|
|
|
if (!BPlayer.isEmpty()) {
|
2013-06-30 23:41:37 +02:00
|
|
|
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() {
|
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-09-09 16:28:35 +02:00
|
|
|
debugLog("Update");
|
|
|
|
|
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
|
|
|
}
|