Refractoring

This commit is contained in:
Sn0wStorm
2019-10-16 17:21:04 +02:00
parent 595df50ca5
commit 7c0dcefb1b
20 changed files with 1501 additions and 1400 deletions
+257
View File
@@ -0,0 +1,257 @@
package com.dre.brewery.filedata;
import com.dre.brewery.*;
import com.dre.brewery.integration.WGBarrel;
import com.dre.brewery.integration.WGBarrel7;
import com.dre.brewery.integration.WGBarrelNew;
import com.dre.brewery.integration.WGBarrelOld;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BConfig {
public static final String configVersion = "1.8";
public static boolean updateCheck;
public static CommandSender reloader;
// Third Party Enabled
public static boolean useWG; //WorldGuard
public static WGBarrel wg;
public static boolean useLWC; //LWC
public static boolean useLB; //LogBlock
public static boolean useGP; //GriefPrevention
public static boolean hasVault; // Vault
public static boolean useCitadel; // CivCraft/DevotedMC Citadel
// Barrel
public static boolean openEverywhere;
//BPlayer
public static Map<Material, Integer> drainItems = new HashMap<>();// DrainItem Material and Strength
public static Material pukeItem;
public static int pukeDespawntime;
public static int hangoverTime;
public static boolean overdrinkKick;
public static boolean enableHome;
public static boolean enableLoginDisallow;
public static boolean enablePuke;
public static String homeType;
//Brew
public static boolean colorInBarrels; // color the Lore while in Barrels
public static boolean colorInBrewer; // color the Lore while in Brewer
public static P p = P.p;
private static boolean checkConfigs() {
File cfg = new File(p.getDataFolder(), "config.yml");
if (!cfg.exists()) {
p.errorLog("No config.yml found, creating default file! You may want to choose a config according to your language!");
p.errorLog("You can find them in plugins/Brewery/configs/");
InputStream defconf = p.getResource("config/" + (P.use1_13 ? "v13/" : "v12/") + "en/config.yml");
if (defconf == null) {
p.errorLog("default config file not found, your jarfile may be corrupt. Disabling Brewery!");
return false;
}
try {
BUtil.saveFile(defconf, p.getDataFolder(), "config.yml", false);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (!cfg.exists()) {
p.errorLog("default config file could not be copied, your jarfile may be corrupt. Disabling Brewery!");
return false;
}
copyDefaultConfigs(false);
return true;
}
private static void copyDefaultConfigs(boolean overwrite) {
File configs = new File(p.getDataFolder(), "configs");
File languages = new File(p.getDataFolder(), "languages");
for (String l : new String[] {"de", "en", "fr", "it", "zh", "tw"}) {
File lfold = new File(configs, l);
try {
BUtil.saveFile(p.getResource("config/" + (P.use1_13 ? "v13/" : "v12/") + l + "/config.yml"), lfold, "config.yml", overwrite);
BUtil.saveFile(p.getResource("languages/" + l + ".yml"), languages, l + ".yml", false); // Never overwrite languages for now
} catch (IOException e) {
if (!(l.equals("zh") || l.equals("tw"))) {
e.printStackTrace();
}
}
}
}
public static boolean readConfig() {
File file = new File(P.p.getDataFolder(), "config.yml");
if (!checkConfigs()) {
return false;
}
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
// Set the Language
p.language = config.getString("language", "en");
// Load LanguageReader
p.languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + p.language + ".yml"));
// Has to config still got old materials
boolean oldMat = config.getBoolean("oldMat", false);
// Check if config is the newest version
String version = config.getString("version", null);
if (version != null) {
if (!version.equals(configVersion) || (oldMat && P.use1_13)) {
copyDefaultConfigs(true);
new ConfigUpdater(file).update(version, oldMat, p.language);
P.p.log("Config Updated to version: " + configVersion);
config = YamlConfiguration.loadConfiguration(file);
}
}
// If the Update Checker should be enabled
updateCheck = config.getBoolean("updateCheck", false);
PluginManager plMan = p.getServer().getPluginManager();
// Third-Party
useWG = config.getBoolean("useWorldGuard", true) && plMan.isPluginEnabled("WorldGuard");
if (useWG) {
Plugin plugin = Bukkit.getPluginManager().getPlugin("WorldEdit");
if (plugin != null) {
String wgv = plugin.getDescription().getVersion();
if (wgv.startsWith("6.")) {
wg = new WGBarrelNew();
} else if (wgv.startsWith("5.")) {
wg = new WGBarrelOld();
} else {
wg = new WGBarrel7();
}
}
if (wg == null) {
P.p.errorLog("Failed loading WorldGuard Integration! Opening Barrels will NOT work!");
P.p.errorLog("Brewery was tested with version 5.8, 6.1 and 7.0 of WorldGuard!");
P.p.errorLog("Disable the WorldGuard support in the config and do /brew reload");
}
}
useLWC = config.getBoolean("useLWC", true) && plMan.isPluginEnabled("LWC");
useGP = config.getBoolean("useGriefPrevention", true) && plMan.isPluginEnabled("GriefPrevention");
useLB = config.getBoolean("useLogBlock", false) && plMan.isPluginEnabled("LogBlock");
useCitadel = config.getBoolean("useCitadel", false) && plMan.isPluginEnabled("Citadel");
// The item util has been removed in Vault 1.7+
hasVault = plMan.isPluginEnabled("Vault")
&& Integer.parseInt(plMan.getPlugin("Vault").getDescription().getVersion().split("\\.")[1]) <= 6;
// various Settings
DataSave.autosave = config.getInt("autosave", 3);
P.debug = config.getBoolean("debug", false);
pukeItem = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND"));
hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60;
overdrinkKick = config.getBoolean("enableKickOnOverdrink", false);
enableHome = config.getBoolean("enableHome", false);
enableLoginDisallow = config.getBoolean("enableLoginDisallow", false);
enablePuke = config.getBoolean("enablePuke", false);
pukeDespawntime = config.getInt("pukeDespawntime", 60) * 20;
homeType = config.getString("homeType", null);
colorInBarrels = config.getBoolean("colorInBarrels", false);
colorInBrewer = config.getBoolean("colorInBrewer", false);
openEverywhere = config.getBoolean("openLargeBarrelEverywhere", false);
MCBarrel.maxBrews = config.getInt("maxBrewsInMCBarrels", 6);
// loading recipes
ConfigurationSection configSection = config.getConfigurationSection("recipes");
if (configSection != null) {
for (String recipeId : configSection.getKeys(false)) {
BRecipe recipe = new BRecipe(configSection, recipeId);
if (recipe.isValid()) {
BIngredients.recipes.add(recipe);
} else {
p.errorLog("Loading the Recipe with id: '" + recipeId + "' failed!");
}
}
}
// loading cooked names and possible ingredients
configSection = config.getConfigurationSection("cooked");
if (configSection != null) {
for (String ingredient : configSection.getKeys(false)) {
Material mat = Material.matchMaterial(ingredient);
if (mat == null && hasVault) {
try {
net.milkbowl.vault.item.ItemInfo vaultItem = net.milkbowl.vault.item.Items.itemByString(ingredient);
if (vaultItem != null) {
mat = vaultItem.getType();
}
} catch (Exception e) {
P.p.errorLog("Could not check vault for Item Name");
e.printStackTrace();
}
}
if (mat != null) {
BIngredients.cookedNames.put(mat, (configSection.getString(ingredient, null)));
BIngredients.possibleIngredients.add(mat);
} else {
p.errorLog("Unknown Material: " + ingredient);
}
}
}
// 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 && hasVault && strength > 0) {
try {
net.milkbowl.vault.item.ItemInfo vaultItem = net.milkbowl.vault.item.Items.itemByString(drainSplit[0]);
if (vaultItem != null) {
mat = vaultItem.getType();
}
} catch (Exception e) {
P.p.errorLog("Could not check vault for Item Name");
e.printStackTrace();
}
}
if (mat != null && strength > 0) {
drainItems.put(mat, strength);
}
}
}
}
// Loading Words
if (config.getBoolean("enableChatDistortion", false)) {
for (Map<?, ?> map : config.getMapList("words")) {
new DistortChat(map);
}
for (String bypass : config.getStringList("distortBypass")) {
DistortChat.ignoreText.add(bypass.split(","));
}
DistortChat.commands = config.getStringList("distortCommands");
}
DistortChat.log = config.getBoolean("logRealChat", false);
DistortChat.doSigns = config.getBoolean("distortSignText", false);
return true;
}
}
+258
View File
@@ -0,0 +1,258 @@
package com.dre.brewery.filedata;
import com.dre.brewery.*;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class BData {
// load all Data
public static void readData() {
File file = new File(P.p.getDataFolder(), "data.yml");
if (file.exists()) {
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
Brew.installTime = data.getLong("installTime", System.currentTimeMillis());
MCBarrel.mcBarrelTime = data.getLong("MCBarrelTime", 0);
Brew.loadSeed(data);
// Check if data is the newest version
String version = data.getString("Version", null);
if (version != null) {
if (!version.equals(DataSave.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: " + DataSave.dataVersion);
}
}
// loading Ingredients into ingMap
Map<String, BIngredients> ingMap = new HashMap<>();
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
ArrayList<ItemStack> ingredients = deserializeIngredients(matSection);
ingMap.put(id, new BIngredients(ingredients, section.getInt(id + ".cookedTime", 0), true));
} else {
P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml");
}
}
}
// loading Brew legacy
section = data.getConfigurationSection("Brew");
if (section != null) {
// All sections have the UID as name
for (String uid : section.getKeys(false)) {
BIngredients ingredients = getIngredients(ingMap, section.getString(uid + ".ingId"));
int quality = section.getInt(uid + ".quality", 0);
byte distillRuns = (byte) section.getInt(uid + ".distillRuns", 0);
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
float wood = (float) section.getDouble(uid + ".wood", -1.0);
String recipe = section.getString(uid + ".recipe", null);
boolean unlabeled = section.getBoolean(uid + ".unlabeled", false);
boolean persistent = section.getBoolean(uid + ".persist", false);
boolean stat = section.getBoolean(uid + ".stat", false);
int lastUpdate = section.getInt("lastUpdate", 0);
Brew.loadLegacy(ingredients, P.p.parseInt(uid), quality, distillRuns, ageTime, wood, recipe, unlabeled, persistent, stat, lastUpdate);
}
}
// loading BPlayer
section = data.getConfigurationSection("Player");
if (section != null) {
// keys have players name
for (String name : section.getKeys(false)) {
try {
//noinspection ResultOfMethodCallIgnored
UUID.fromString(name);
if (!P.useUUID) {
continue;
}
} catch (IllegalArgumentException e) {
if (P.useUUID) {
continue;
}
}
int quality = section.getInt(name + ".quality");
int drunk = section.getInt(name + ".drunk");
int offDrunk = section.getInt(name + ".offDrunk", 0);
new BPlayer(name, quality, drunk, offDrunk);
}
}
for (World world : P.p.getServer().getWorlds()) {
if (world.getName().startsWith("DXL_")) {
loadWorldData(BUtil.getDxlName(world.getName()), world);
} else {
loadWorldData(world.getUID().toString(), world);
}
}
} else {
P.p.errorLog("No data.yml found, will create new one!");
}
}
public static ArrayList<ItemStack> deserializeIngredients(ConfigurationSection matSection) {
ArrayList<ItemStack> ingredients = new ArrayList<>();
for (String mat : matSection.getKeys(false)) {
String[] matSplit = mat.split(",");
Material m = Material.getMaterial(matSplit[0]);
if (m == null && P.use1_13) {
if (matSplit[0].equals("LONG_GRASS")) {
m = Material.GRASS;
} else {
m = Material.matchMaterial(matSplit[0], true);
}
P.p.debugLog("converting Data Material from " + matSplit[0] + " to " + m);
}
if (m == null) continue;
ItemStack item = new ItemStack(m, matSection.getInt(mat));
if (matSplit.length == 2) {
item.setDurability((short) P.p.parseInt(matSplit[1]));
}
ingredients.add(item);
}
return ingredients;
}
// returns Ingredients by id from the specified ingMap
public static BIngredients getIngredients(Map<String, BIngredients> ingMap, String id) {
if (!ingMap.isEmpty()) {
if (ingMap.containsKey(id)) {
return ingMap.get(id);
}
}
P.p.errorLog("Ingredient id: '" + id + "' not found in data.yml");
return new BIngredients();
}
// loads BIngredients from an ingredient section
public static BIngredients loadIngredients(ConfigurationSection section) {
if (section != null) {
return new BIngredients(deserializeIngredients(section), 0);
} else {
P.p.errorLog("Cauldron is missing Ingredient Section");
}
return new BIngredients();
}
// load Block locations of given world
public static void loadWorldData(String uuid, World world) {
File file = new File(P.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);
for (String cauldron : section.getKeys(false)) {
// block is splitted into x/y/z
String block = section.getString(cauldron + ".block");
if (block != null) {
String[] splitted = block.split("/");
if (splitted.length == 3) {
Block worldBlock = world.getBlockAt(P.p.parseInt(splitted[0]), P.p.parseInt(splitted[1]), P.p.parseInt(splitted[2]));
BIngredients ingredients = loadIngredients(section.getConfigurationSection(cauldron + ".ingredients"));
int state = section.getInt(cauldron + ".state", 1);
new BCauldron(worldBlock, ingredients, state);
} else {
P.p.errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
} else {
P.p.errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + cauldron);
}
}
}
// loading Barrel
if (data.contains("Barrel." + uuid)) {
ConfigurationSection section = data.getConfigurationSection("Barrel." + uuid);
for (String barrel : section.getKeys(false)) {
// block spigot is splitted into x/y/z
String spigot = section.getString(barrel + ".spigot");
if (spigot != null) {
String[] splitted = spigot.split("/");
if (splitted.length == 3) {
// load itemStacks from invSection
ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv");
Block block = world.getBlockAt(P.p.parseInt(splitted[0]), P.p.parseInt(splitted[1]), P.p.parseInt(splitted[2]));
float time = (float) section.getDouble(barrel + ".time", 0.0);
byte sign = (byte) section.getInt(barrel + ".sign", 0);
String[] st = section.getString(barrel + ".st", "").split(",");
String[] wo = section.getString(barrel + ".wo", "").split(",");
if (invSection != null) {
new Barrel(block, sign, st, wo, invSection.getValues(true), time);
} else {
// Barrel has no inventory
new Barrel(block, sign, st, wo, null, time);
}
} else {
P.p.errorLog("Incomplete Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
} else {
P.p.errorLog("Missing Block-Data in data.yml: " + section.getCurrentPath() + "." + barrel);
}
}
}
// 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 {
P.p.errorLog("Incomplete Location-Data in data.yml: " + section.getCurrentPath() + "." + wakeup);
}
}
}
}
}
}
}