Kaynağa Gözat

fixed generator bug

mastercake10 7 yıl önce
ebeveyn
işleme
1bdbb0d866

+ 10 - 22
src/de/Linus122/customoregen/Events.java

@@ -2,11 +2,13 @@ package de.Linus122.customoregen;
 
 import java.util.Random;
 import org.bukkit.Material;
+import org.bukkit.OfflinePlayer;
 import org.bukkit.block.Block;
 import org.bukkit.entity.Player;
 import org.bukkit.event.EventHandler;
 import org.bukkit.event.Listener;
 import org.bukkit.event.block.BlockFormEvent;
+import org.bukkit.event.player.PlayerJoinEvent;
 
 public class Events implements Listener {
 	@SuppressWarnings("deprecation")
@@ -23,27 +25,9 @@ public class Events implements Listener {
 		//System.out.println("To: " + newBlock.name());
 		
 		if (newBlock.equals(Material.COBBLESTONE) || newBlock.equals(Material.STONE)) {
-			GeneratorConfig gc = null;
 
-			Player p = Main.getOwner(b.getLocation());
-			if (p == null) {
-				gc = Main.generatorConfigs.get(0);
-			} else {
-				int islandLevel = Main.getLevel(p);
-
-				if (Main.activeInWorld.getName().equals(b.getWorld().getName())) {
-					for (GeneratorConfig gc2 : Main.generatorConfigs) {
-						if (gc2 == null) {
-							continue;
-						}
-						if ((p.hasPermission(gc2.permission) || gc2.permission.length() == 0) && islandLevel >= gc2.unlock_islandLevel) {
-							// Weiter
-							gc = gc2;
-						}
-
-					}
-				}
-			}
+			OfflinePlayer p = Main.getOwner(b.getLocation());
+			GeneratorConfig gc = Main.getGeneratorConfigForPlayer(p);
 			if (gc == null)
 				return;
 			if (getObject(gc) == null)
@@ -56,10 +40,14 @@ public class Events implements Listener {
 				return;
 			}
 			event.setCancelled(true);
-			b.setTypeIdAndData(Material.getMaterial(winning.name).getId(), winning.damage, true);
+			b.setType(Material.getMaterial(winning.name));
+			b.setData(winning.damage, true);
 		}
 	}
-	
+	@EventHandler
+	public void onJoin(PlayerJoinEvent e){
+		Main.getGeneratorConfigForPlayer(e.getPlayer());
+	}
 
 	public GeneratorItem getObject(GeneratorConfig gc) {
 

+ 67 - 22
src/de/Linus122/customoregen/Main.java

@@ -12,12 +12,15 @@ import java.util.Set;
 import java.util.UUID;
 import org.bukkit.Bukkit;
 import org.bukkit.Location;
+import org.bukkit.OfflinePlayer;
 import org.bukkit.World;
 import org.bukkit.command.ConsoleCommandSender;
 import org.bukkit.entity.Player;
 import org.bukkit.plugin.PluginManager;
 import org.bukkit.plugin.java.JavaPlugin;
 
+import com.google.gson.reflect.TypeToken;
+
 
 public class Main extends JavaPlugin {
 	public static List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
@@ -26,7 +29,10 @@ public class Main extends JavaPlugin {
 	public static World activeInWorld;
 
 	public static ConsoleCommandSender clogger;
-
+	
+	private static HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
+	private static JSONConfig cachedOregenJsonConfig;
+	
 	public void onEnable() {
 		clogger = getServer().getConsoleSender();
 		PluginManager pm = Bukkit.getPluginManager();
@@ -37,6 +43,11 @@ public class Main extends JavaPlugin {
 		}catch(IOException e) {
 			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>();
+		}
 		disabledWorlds = getConfig().getStringList("disabled-worlds");
 		if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
 			activeInWorld = com.wasteofplastic.askyblock.ASkyBlock.getIslandWorld();
@@ -55,25 +66,23 @@ public class Main extends JavaPlugin {
 	}
 
 	public void onDisable() {
-		
+		cachedOregenJsonConfig.saveToDisk(cachedOregenConfigs);
 	}
 
-	public static int getLevel(Player p) {
+	public static int getLevel(UUID uuid) {
 		if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
-			return com.wasteofplastic.askyblock.ASkyBlockAPI.getInstance().getIslandLevel(p.getUniqueId());
+			return com.wasteofplastic.askyblock.ASkyBlockAPI.getInstance().getIslandLevel(uuid);
 		}
 		if(Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
-			return com.wasteofplastic.acidisland.ASkyBlockAPI.getInstance().getIslandLevel(p.getUniqueId());
+			return com.wasteofplastic.acidisland.ASkyBlockAPI.getInstance().getIslandLevel(uuid);
 		}
 		if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
-			return (int) Math.floor(us.talabrek.ultimateskyblock.uSkyBlock.getAPI().getIslandLevel(p));
+			return (int) Math.floor(us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getIslandInfo(us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getPlayerInfo(uuid)).getLevel());
 		}
 		return 0;
 	}
 	
-	static HashMap<UUID, Player> map = new HashMap<UUID, Player>();
-
-	public static Player getOwner(Location loc) {
+	public static OfflinePlayer getOwner(Location loc) {
 		Set<Location> set = new HashSet<Location>();
 		set.add(loc);
 
@@ -87,20 +96,11 @@ public class Main extends JavaPlugin {
 		}else if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
 			String player = us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getIslandInfo(loc).getLeader();
 			if((Bukkit.getPlayer(player) != null) && (Bukkit.getPlayer(player).getUniqueId() != null)) {
-				uuid = Bukkit.getPlayer(player).getUniqueId();
+				uuid = Bukkit.getOfflinePlayer(player).getUniqueId();
 			}
 		}
 
-		Player p = Bukkit.getPlayer(uuid);
-
-		if(p != null) {
-			map.put(uuid, p);
-			if(p.isOnline()) {
-				activeInWorld = p.getWorld();
-			}
-		}else if(map.containsKey(uuid)) {
-			p = (Player) map.get(uuid);
-		}
+		OfflinePlayer p = Bukkit.getOfflinePlayer(uuid);
 
 		return p;
 	}
@@ -111,6 +111,7 @@ public class Main extends JavaPlugin {
 	}
 
 	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();
@@ -126,12 +127,12 @@ public class Main extends JavaPlugin {
 			writer.close();	
 		}
 		 
-
+		this.reloadConfig();
 		generatorConfigs = new ArrayList<GeneratorConfig>();
 		int i = 0;
 		while(true){
 			i++;
-			if(this.getConfig().contains("generators.generator" + i)){
+			if(this.getConfig().contains("generators.generator" + i, true)){
 				GeneratorConfig gc = new GeneratorConfig();
 				gc.permission = this.getConfig().getString("generators.generator" + i + ".permission");
 				gc.unlock_islandLevel = this.getConfig().getInt("generators.generator" + i + ".unlock_islandLevel");
@@ -148,6 +149,7 @@ public class Main extends JavaPlugin {
 							gc.itemList.add(new GeneratorItem(material, (byte) damage, percent));
 						}
 					}catch(Exception e){
+						e.printStackTrace();
 					}
 				}
 				generatorConfigs.add(gc);
@@ -159,4 +161,47 @@ public class Main extends JavaPlugin {
 		//this.saveConfig();
 		clogger.sendMessage("§6[CustomOreGen] §aLoaded §c" + generatorConfigs.size() + " §agenerators");
 	}
+	public static GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p){
+		GeneratorConfig gc = null;
+		int id = 0;
+		if (p == null) {
+			gc = Main.generatorConfigs.get(0);
+			cacheOreGen(p.getUniqueId(), id);
+		} else {
+			
+			int islandLevel = Main.getLevel(p.getUniqueId());
+
+			if(p.isOnline()){
+				Player realP = p.getPlayer();
+				if (Main.activeInWorld.getName().equals(realP.getWorld().getName())) {
+					for (GeneratorConfig gc2 : Main.generatorConfigs) {
+						if (gc2 == null) {
+							continue;
+						}
+						if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0) && islandLevel >= gc2.unlock_islandLevel) {
+							// Weiter
+							gc = gc2;
+							id++;
+						}
+
+					}
+				}	
+			}else{
+				gc = getCachedGeneratorConfig(p.getUniqueId());
+			}
+		}
+		if(id > 0){
+			cacheOreGen(p.getUniqueId(), id - 1);
+		}
+		return gc;
+	}
+	public static GeneratorConfig getCachedGeneratorConfig(UUID uuid){
+		if(cachedOregenConfigs.containsKey(uuid)){
+			return Main.generatorConfigs.get(cachedOregenConfigs.get(uuid));
+		}
+		return null;
+	}
+	public static void cacheOreGen(UUID uuid, int configID){
+		cachedOregenConfigs.put(uuid, configID);
+	}
 }

+ 1 - 1
src/plugin.yml

@@ -1,5 +1,5 @@
 name: CustomOreGen
-version: 1.2.32
+version: 1.2.33
 description: Controls the Ore-Generator
 author: Linus122
 soft-depends: [ASkyBlock, AcidIsland, uSkyBlock]