Explorar o código

refactored code

linus %!s(int64=6) %!d(string=hai) anos
pai
achega
5527d48112

+ 115 - 74
src/xyz/spaceio/customoregen/CustomOreGen.java

@@ -29,41 +29,67 @@ import xyz.spaceio.hooks.SkyblockAPIHook;
 import xyz.spaceio.hooks.uSkyBlockHook;
 
 public class CustomOreGen extends JavaPlugin {
-	public static List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
-	public static List<String> disabledWorlds = new ArrayList<String>();
-
-	public static ConsoleCommandSender clogger;
 	
-	private static HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
-	private static JSONConfig cachedOregenJsonConfig;
+	/*
+	 * Configurations for all generators (defined in the config.yml)
+	 */
+	private List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
 	
-	public static String activeInWorldName = "";
+	/*
+	 * Disabled world blacklist
+	 */
+	private List<String> disabledWorlds = new ArrayList<String>();
+
+	/*
+	 * Our logger
+	 */
+	private ConsoleCommandSender clogger;
 	
+	/*
+	 * Cache for GeneratorConfig ID's for each player
+	 */
+	private HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
+	private JSONConfig cachedOregenJsonConfig;
+
+	/*
+	 * The skyblock world name
+	 */
+	private String activeInWorldName = "";
+
+	/*
+	 * API Hook for the corresponding SkyBlock plugin
+	 */
 	private SkyblockAPIHook skyblockAPI;
 	
+	/*
+	 * Prefix for the clogger
+	 */
+	private final String PREFIX = "§6[CustomOreGen] "; 
+
 	@Override
 	public void onEnable() {
 		clogger = getServer().getConsoleSender();
 		PluginManager pm = Bukkit.getPluginManager();
-		pm.registerEvents(new Events(), this);
-		
+		pm.registerEvents(new Events(this), this);
+
 		this.loadHook();
 		activeInWorldName = skyblockAPI.getSkyBlockWorldName();
-		
-		
+
 		Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
-		try{
+
+		try {
 			loadConfig();
-		}catch(IOException e) {
+		} catch (IOException e) {
 			e.printStackTrace();
 		}
-		cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() { }.getType(), this);
+		cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() {
+		}.getType(), this);
 		cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.get();
-		if(cachedOregenConfigs == null){
+		if (cachedOregenConfigs == null) {
 			cachedOregenConfigs = new HashMap<UUID, Integer>();
 		}
 		disabledWorlds = getConfig().getStringList("disabled-worlds");
-		
+
 		new Metrics(this);
 	}
 
@@ -71,34 +97,34 @@ public class CustomOreGen extends JavaPlugin {
 	 * creates a new api hook instance for the used skyblock plugin
 	 */
 	private void loadHook() {
-		if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
+		if (Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
 			skyblockAPI = new ASkyBlockHook();
-			clogger.sendMessage("§6[CustomOreGen] §aUsing ASkyBlock as SkyBlock-Plugin");
-		}else if(Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
+			sendConsole("&aUsing ASkyBlock as SkyBlock-Plugin");
+		} else if (Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
 			skyblockAPI = new AcidIslandHook();
-			clogger.sendMessage("§6[CustomOreGen] §aUsing AcidIsland as SkyBlock-Plugin");
-		}else if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
+			sendConsole("&aUsing AcidIsland as SkyBlock-Plugin");
+		} else if (Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
 			skyblockAPI = new uSkyBlockHook();
-			clogger.sendMessage("§6[CustomOreGen] §aUsing uSkyBlock as SkyBlock-Plugin");
+			sendConsole("&aUsing uSkyBlock as SkyBlock-Plugin");
 		}
 	}
-	
+
 	@Override
 	public void onDisable() {
 		cachedOregenJsonConfig.saveToDisk(cachedOregenConfigs);
 	}
-	
-	public static World getActiveWorld(){
+
+	public World getActiveWorld() {
 		return Bukkit.getWorld(activeInWorldName);
 	}
 
 	public int getLevel(UUID uuid) {
 		return skyblockAPI.getIslandLevel(uuid);
 	}
-	
+
 	public OfflinePlayer getOwner(Location loc) {
 		UUID uuid = skyblockAPI.getIslandOwner(loc);
-		if(uuid == null){
+		if (uuid == null) {
 			return null;
 		}
 		OfflinePlayer p = Bukkit.getOfflinePlayer(uuid);
@@ -110,118 +136,133 @@ public class CustomOreGen extends JavaPlugin {
 		reloadConfig();
 		loadConfig();
 	}
-	
+
 	/**
-	* Just a method that sorts out stupid configuration mistakes made by kids who always give 1-star-reviews on Spigot.
-	*/
+	 * 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()){
+		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);
+			while ((lineLength = out.read(linebuffer)) > 0) {
+				writer.write(linebuffer, 0, lineLength);
 			}
-			writer.close();	
+			writer.close();
 		}
-		 
+
 		this.reloadConfig();
 		generatorConfigs = new ArrayList<GeneratorConfig>();
-		for(String key : this.getConfig().getConfigurationSection("generators").getKeys(false)){
+		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){
-				System.out.println("[CustomOreGen] Config error: generator " + key + " does not have a valid permission entry");
+			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){
-				System.out.println("[CustomOreGen] Config error: generator " + key + " has both a permission and level setup! Be sure to choose one of them!");
+			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("!")){
+
+			for (String raw : this.getConfig().getStringList("generators." + key + ".blocks")) {
+				try {
+					if (!raw.contains("!")) {
 						String material = raw.split(":")[0];
-						if(Material.getMaterial(material.toUpperCase()) == null){
-							System.out.println("[CustomOreGen] Config error: generator " + key + " has an unrecognized material: " + material);
+						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{
+					} else {
 						String material = raw.split("!")[0];
-						if(Material.getMaterial(material.toUpperCase()) == null){
-							System.out.println("[CustomOreGen] Config error: generator " + key + " has an unrecognized material: " + material);
+						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){
-					System.out.println("[CustomOreGen] Config error: general configuration error. Please check you config.yml");
+				} catch (Exception e) {
 					e.printStackTrace();
+					sendConsole("&cConfig error: general configuration error. Please check you config.yml");
 				}
 			}
-			if(totalChance != 100.0){
-				System.out.println("[CustomOreGen] Config error: generator " + key + " does not have a total chance of 100%! Chance: " + totalChance);
+			if (totalChance != 100.0) {
+				sendConsole(String.format("&cConfig error: generator %s does not have a total chance of 100%! Total chance is: %d", key, totalChance));
 			}
 			generatorConfigs.add(gc);
 
-			
 		}
-		//this.saveConfig();
-		clogger.sendMessage("§6[CustomOreGen] §aLoaded §c" + generatorConfigs.size() + " §agenerators");
+
+		sendConsole(String.format("&aLoaded &c%d &agenerators!", generatorConfigs.size()));
 	}
-	public static GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p){
+
+	public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p) {
 		GeneratorConfig gc = null;
 		int id = 0;
 		if (p == null) {
-			gc = CustomOreGen.generatorConfigs.get(0);
+			gc = generatorConfigs.get(0);
 			cacheOreGen(p.getUniqueId(), id);
 		} else {
-			
-			int islandLevel = CustomOreGen.getLevel(p.getUniqueId());
 
-			if(p.isOnline()){
+			int islandLevel = getLevel(p.getUniqueId());
+
+			if (p.isOnline()) {
 				Player realP = p.getPlayer();
-				if (activeInWorldName.equals(
-						realP.getWorld().getName())) {
-					for (GeneratorConfig gc2 : CustomOreGen.generatorConfigs) {
+				if (activeInWorldName.equals(realP.getWorld().getName())) {
+					for (GeneratorConfig gc2 : generatorConfigs) {
 						if (gc2 == null) {
 							continue;
 						}
 						if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0) && islandLevel >= gc2.unlock_islandLevel) {
-							// Weiter
+							// continue
 							gc = gc2;
 							id++;
 						}
 
 					}
-				}	
-			}else{
+				}
+			} else {
 				gc = getCachedGeneratorConfig(p.getUniqueId());
 			}
 		}
-		if(id > 0){
+		if (id > 0) {
 			cacheOreGen(p.getUniqueId(), id - 1);
 		}
 		return gc;
 	}
-	public static GeneratorConfig getCachedGeneratorConfig(UUID uuid){
-		if(cachedOregenConfigs.containsKey(uuid)){
-			return CustomOreGen.generatorConfigs.get(cachedOregenConfigs.get(uuid));
+
+	public List<String> getDisabledWorlds() {
+		return disabledWorlds;
+	}
+
+	public void setDisabledWorlds(List<String> disabledWorlds) {
+		this.disabledWorlds = disabledWorlds;
+	}
+
+	public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
+		if (cachedOregenConfigs.containsKey(uuid)) {
+			return generatorConfigs.get(cachedOregenConfigs.get(uuid));
 		}
 		return null;
 	}
-	public static void cacheOreGen(UUID uuid, int configID){
+
+	public void cacheOreGen(UUID uuid, int configID) {
 		cachedOregenConfigs.put(uuid, configID);
 	}
+	
+	
+	public void sendConsole(String msg) {
+		clogger.sendMessage(PREFIX + msg.replace("&", "§"));
+	}
 }

+ 25 - 4
src/xyz/spaceio/customoregen/Events.java

@@ -13,10 +13,20 @@ import org.bukkit.event.block.BlockFromToEvent;
 import org.bukkit.event.player.PlayerJoinEvent;
 
 public class Events implements Listener {
+	
+	/*
+	 * CustomOreGen main class
+	 */
+	private CustomOreGen plugin;
+	
+	public Events(CustomOreGen customOreGen) {
+		this.plugin = customOreGen;
+	}
+
 	@SuppressWarnings("deprecation")
 	@EventHandler
 	public void onFromTo(BlockFromToEvent event) {
-		if (CustomOreGen.disabledWorlds.contains(event.getBlock().getLocation().getWorld().getName())) {
+		if (plugin.getDisabledWorlds().contains(event.getBlock().getLocation().getWorld().getName())) {
 			return;
 		}
 
@@ -34,10 +44,10 @@ public class Events implements Listener {
 			}
 
 			if ((toid == 0 || toid == 9 || toid == 8) && (generatesCobble(id, b))) {
-				OfflinePlayer p = CustomOreGen.getOwner(b.getLocation());
+				OfflinePlayer p = plugin.getOwner(b.getLocation());
 				if (p == null)
 					return;
-				GeneratorConfig gc = CustomOreGen.getGeneratorConfigForPlayer(p);
+				GeneratorConfig gc = plugin.getGeneratorConfigForPlayer(p);
 				if (gc == null)
 					return;
 				if (getObject(gc) == null)
@@ -59,6 +69,11 @@ public class Events implements Listener {
 
 	}
 
+	/**
+	 * Checks if a block is surrounded by water
+	 * @param fromLoc
+	 * @return
+	 */
 	public boolean isSurroundedByWater(Location fromLoc) {
 		Block[] blocks = {
 				fromLoc.getWorld().getBlockAt(fromLoc.getBlockX() + 1, fromLoc.getBlockY(), fromLoc.getBlockZ()),
@@ -77,9 +92,15 @@ public class Events implements Listener {
 
 	@EventHandler
 	public void onJoin(PlayerJoinEvent e) {
-		CustomOreGen.getGeneratorConfigForPlayer(e.getPlayer());
+		plugin.getGeneratorConfigForPlayer(e.getPlayer());
 	}
 
+	
+	/**
+	 * Chooses a GeneratorItem randomly
+	 * @param gc
+	 * @return
+	 */
 	public GeneratorItem getObject(GeneratorConfig gc) {
 
 		Random random = new Random();