linus před 6 roky
rodič
revize
29346f4760

+ 1 - 1
src/xyz/spaceio/customoregen/JSONConfig.java → src/xyz/spaceio/config/JSONConfig.java

@@ -1,4 +1,4 @@
-package xyz.spaceio.customoregen;
+package xyz.spaceio.config;
 
 import java.io.File;
 import java.io.FileInputStream;

+ 30 - 81
src/xyz/spaceio/customoregen/CustomOreGen.java

@@ -26,6 +26,8 @@ import org.bukkit.plugin.java.JavaPlugin;
 import com.google.gson.reflect.TypeToken;
 
 import de.Linus122.SpaceIOMetrics.Metrics;
+import xyz.spaceio.config.ConfigHandler;
+import xyz.spaceio.config.JSONConfig;
 import xyz.spaceio.hooks.HookASkyBlock;
 import xyz.spaceio.hooks.HookAcidIsland;
 import xyz.spaceio.hooks.HookBentoBox;
@@ -33,22 +35,30 @@ import xyz.spaceio.hooks.SkyblockAPIHook;
 import xyz.spaceio.hooks.HookuSkyBlock;
 
 public class CustomOreGen extends JavaPlugin {
-	
+
 	/*
 	 * Configurations for all generators (defined in the config.yml)
 	 */
 	private List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
-	
+
 	/*
 	 * Disabled world blacklist
 	 */
 	private List<String> disabledWorlds = new ArrayList<String>();
 
+	public List<GeneratorConfig> getGeneratorConfigs() {
+		return generatorConfigs;
+	}
+
+	public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
+		this.generatorConfigs = generatorConfigs;
+	}
+
 	/*
 	 * Our logger
 	 */
 	private ConsoleCommandSender clogger;
-	
+
 	/*
 	 * Cache for GeneratorConfig ID's for each player
 	 */
@@ -59,11 +69,16 @@ public class CustomOreGen extends JavaPlugin {
 	 * API Hook for the corresponding SkyBlock plugin
 	 */
 	private SkyblockAPIHook skyblockAPI;
-	
+
+	/*
+	 * Object that handles the loading process of the config.yml file
+	 */
+	private ConfigHandler configHandler = new ConfigHandler(this, "plugins/CustomOreGen/config.yml");;
+
 	/*
 	 * Prefix for the clogger
 	 */
-	private final String PREFIX = "§6[CustomOreGen] "; 
+	private final String PREFIX = "§6[CustomOreGen] ";
 
 	@Override
 	public void onEnable() {
@@ -76,13 +91,16 @@ public class CustomOreGen extends JavaPlugin {
 		Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
 
 		try {
-			loadConfig();
+			configHandler.loadConfig();
 		} catch (IOException e) {
+			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
+
 		cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() {
 		}.getType(), this);
 		cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.get();
+		
 		if (cachedOregenConfigs == null) {
 			cachedOregenConfigs = new HashMap<UUID, Integer>();
 		}
@@ -116,7 +134,8 @@ public class CustomOreGen extends JavaPlugin {
 	}
 
 	public List<World> getActiveWorlds() {
-		return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v)).collect(Collectors.toList());
+		return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
+				.collect(Collectors.toList());
 	}
 
 	public int getLevel(UUID uuid, String world) {
@@ -135,77 +154,7 @@ public class CustomOreGen extends JavaPlugin {
 
 	public void reload() throws IOException {
 		reloadConfig();
-		loadConfig();
-	}
-
-	/**
-	 * Just a method that sorts out stupid configuration mistakes made by kids who
-	 * always give 1-star-reviews on Spigot.
-	 */
-	public void loadConfig() throws IOException {
-		// Writing default config to data directory
-		File cfg = new File("plugins/CustomOreGen/config.yml");
-		File dir = new File("plugins/CustomOreGen/");
-		if (!dir.exists())
-			dir.mkdirs();
-		if (!cfg.exists()) {
-			FileOutputStream writer = new FileOutputStream(new File(getDataFolder() + "/config.yml"));
-			InputStream out = this.getClassLoader().getResourceAsStream("config.yml");
-			byte[] linebuffer = new byte[4096];
-			int lineLength = 0;
-			while ((lineLength = out.read(linebuffer)) > 0) {
-				writer.write(linebuffer, 0, lineLength);
-			}
-			writer.close();
-		}
-
-		this.reloadConfig();
-		generatorConfigs = new ArrayList<GeneratorConfig>();
-		for (String key : this.getConfig().getConfigurationSection("generators").getKeys(false)) {
-			double totalChance = 0d;
-			GeneratorConfig gc = new GeneratorConfig();
-			gc.permission = this.getConfig().getString("generators." + key + ".permission");
-			gc.unlock_islandLevel = this.getConfig().getInt("generators." + key + ".unlock_islandLevel");
-			if (gc.permission == null) {
-				sendConsole(String.format("&cConfig error: generator %s does not have a valid permission entry!", key));
-			}
-			if (gc.unlock_islandLevel > 0 && gc.permission.length() > 1) {
-				sendConsole(String.format("&cConfig error: generator %s has both a permission and level setup! Be sure to choose one of them!", key));
-			}
-
-			for (String raw : this.getConfig().getStringList("generators." + key + ".blocks")) {
-				try {
-					if (!raw.contains("!")) {
-						String material = raw.split(":")[0];
-						if (Material.getMaterial(material.toUpperCase()) == null) {
-							sendConsole(String.format("&cConfig error: generator %s has an unrecognized material: %s", key, material));
-						}
-						double percent = Double.parseDouble(raw.split(":")[1]);
-						totalChance += percent;
-						gc.itemList.add(new GeneratorItem(material, (byte) 0, percent));
-					} else {
-						String material = raw.split("!")[0];
-						if (Material.getMaterial(material.toUpperCase()) == null) {
-							sendConsole(String.format("&cConfig error: generator %s has an unrecognized material: %s", key, material));
-						}
-						double percent = Double.parseDouble(raw.split(":")[1]);
-						totalChance += percent;
-						int damage = Integer.parseInt(raw.split("!")[1].split(":")[0]);
-						gc.itemList.add(new GeneratorItem(material, (byte) damage, percent));
-					}
-				} catch (Exception e) {
-					e.printStackTrace();
-					sendConsole("&cConfig error: general configuration error. Please check you config.yml");
-				}
-			}
-			if (totalChance != 100.0) {
-				sendConsole(String.format("&cConfig error: generator %s does not have a total chance of 100.0! Total chance is: %f", key, totalChance));
-			}
-			generatorConfigs.add(gc);
-
-		}
-
-		sendConsole(String.format("&aLoaded &c%d &agenerators!", generatorConfigs.size()));
+		configHandler.loadConfig();
 	}
 
 	public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p, String world) {
@@ -225,7 +174,8 @@ public class CustomOreGen extends JavaPlugin {
 						if (gc2 == null) {
 							continue;
 						}
-						if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0) && islandLevel >= gc2.unlock_islandLevel) {
+						if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
+								&& islandLevel >= gc2.unlock_islandLevel) {
 							// continue
 							gc = gc2;
 							id++;
@@ -261,8 +211,7 @@ public class CustomOreGen extends JavaPlugin {
 	public void cacheOreGen(UUID uuid, int configID) {
 		cachedOregenConfigs.put(uuid, configID);
 	}
-	
-	
+
 	public void sendConsole(String msg) {
 		clogger.sendMessage(PREFIX + msg.replace("&", "§"));
 	}

+ 5 - 5
src/xyz/spaceio/customoregen/Events.java

@@ -53,17 +53,17 @@ public class Events implements Listener {
 				if (getObject(gc) == null)
 					return;
 				GeneratorItem winning = getObject(gc);
-				if (Material.getMaterial(winning.name) == null)
+				if (Material.getMaterial(winning.getName()) == null)
 					return;
 
-				if (Material.getMaterial(winning.name).equals(Material.COBBLESTONE) && winning.damage == 0) {
+				if (Material.getMaterial(winning.getName()).equals(Material.COBBLESTONE) && winning.getDamage() == 0) {
 					return;
 				}
 				event.setCancelled(true);
-				b.setType(Material.getMaterial(winning.name));
+				b.setType(Material.getMaterial(winning.getName()));
 				// <Block>.setData(...) is deprecated, but there is no
 				// alternative to it. #spigot
-				b.setData(winning.damage, true);
+				b.setData(winning.getDamage(), true);
 			}
 		}
 
@@ -106,7 +106,7 @@ public class Events implements Listener {
 		Random random = new Random();
 		double d = random.nextDouble() * 100;
 		for (GeneratorItem key : gc.itemList) {
-			if ((d -= key.chance) < 0)
+			if ((d -= key.getChance()) < 0)
 				return key;
 		}
 		return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT

+ 41 - 4
src/xyz/spaceio/customoregen/GeneratorItem.java

@@ -2,12 +2,49 @@ package xyz.spaceio.customoregen;
 
 public class GeneratorItem {
 
-	String name;
-	Byte damage = 0;
-	double chance = 0d;
-	GeneratorItem(String name, byte damage, double chance){
+	/**
+	 * Name of the material
+	 */
+	private String name;
+	
+	/**
+	 * Damage value, only for mc < 1.13
+	 */
+	private Byte damage = 0;
+	
+	/**
+	 * The chance in double. From 0 to 1.
+	 */
+	private double chance = 0d;
+	
+	public GeneratorItem(String name, byte damage, double chance){
 		this.name = name;
 		this.damage = damage;
 		this.chance = chance;
 	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Byte getDamage() {
+		return damage;
+	}
+
+	public void setDamage(Byte damage) {
+		this.damage = damage;
+	}
+
+	public double getChance() {
+		return chance;
+	}
+
+	public void setChance(double chance) {
+		this.chance = chance;
+	}
+	
 }