Browse Source

increased performance by caching

MasterCake 5 years ago
parent
commit
a71adeaa10

+ 6 - 0
src/xyz/spaceio/customoregen/CachedGeneratorConfig.java

@@ -0,0 +1,6 @@
+package xyz.spaceio.customoregen;
+
+public class CachedGeneratorConfig {
+	public GeneratorConfig generatorConfig;
+	public long cachedOn;
+}

+ 0 - 17
src/xyz/spaceio/customoregen/Cmd.java

@@ -2,12 +2,9 @@ package xyz.spaceio.customoregen;
 
 import java.io.IOException;
 
-import org.bukkit.block.Block;
 import org.bukkit.command.Command;
 import org.bukkit.command.CommandExecutor;
 import org.bukkit.command.CommandSender;
-import org.bukkit.entity.Player;
-import org.bukkit.event.block.BlockFromToEvent;
 
 public class Cmd implements CommandExecutor {
 	CustomOreGen plugin;
@@ -29,20 +26,6 @@ public class Cmd implements CommandExecutor {
 			cs.sendMessage("§aConfig reloaded!");
 		}
 		
-		Player p = (Player) cs;
-		
-		Block from = p.getWorld().getBlockAt(1200,76,1194);
-		Block to = p.getWorld().getBlockAt(1200,76,1195);
-		
-		long before = System.currentTimeMillis();
-		
-		for(int i = 0; i < 10000; i++) {
-			CustomOreGen.eventClass.onFromTo(new BlockFromToEvent(from, to));
-		}
-		
-		p.sendMessage(System.currentTimeMillis() - before + "");
-	
-		
 		return true;
 	}
 }

+ 21 - 7
src/xyz/spaceio/customoregen/CustomOreGen.java

@@ -25,6 +25,7 @@ import xyz.spaceio.configutils.ConfigHandler;
 import xyz.spaceio.configutils.JSONConfig;
 import xyz.spaceio.hooks.HookInfo;
 import xyz.spaceio.hooks.HookVanilla;
+import xyz.spaceio.hooks.SkyblockAPICached;
 import xyz.spaceio.hooks.SkyblockAPIHook;
 import xyz.spaceio.misc.NamePlaceholder;
 
@@ -55,6 +56,12 @@ public class CustomOreGen extends JavaPlugin {
 	 * API Hook for the corresponding SkyBlock plugin
 	 */
 	private SkyblockAPIHook skyblockAPI;
+	
+	/*
+	 * API Hook but cached
+	 */
+	private SkyblockAPICached skyblockAPICached;
+
 
 	/*
 	 * Object that handles the loading process of the config.yml file
@@ -65,8 +72,7 @@ public class CustomOreGen extends JavaPlugin {
 	 * Prefix for the clogger
 	 */
 	private final String PREFIX = "§6[CustomOreGen] ";
-	
-	public static Events eventClass;
+
 
 	@Override
 	public void onEnable() {
@@ -74,8 +80,7 @@ public class CustomOreGen extends JavaPlugin {
 		
 		PluginManager pm = Bukkit.getPluginManager();
 		
-		eventClass = new Events(this);
-		pm.registerEvents(eventClass, this);
+		pm.registerEvents(new Events(this), this);
 
 		this.loadHook();
 
@@ -137,6 +142,8 @@ public class CustomOreGen extends JavaPlugin {
 			sendConsole("§cYou are not using any supported skyblock plugin! Will use the vanilla range check hook instead.");
 			skyblockAPI = new HookVanilla();
 		}
+		
+		skyblockAPICached = new SkyblockAPICached(skyblockAPI);
 	}
 
 	/**
@@ -155,7 +162,7 @@ public class CustomOreGen extends JavaPlugin {
 	 * @return player's island level
 	 */
 	public int getLevel(UUID uuid, String world) {
-		return skyblockAPI.getIslandLevel(uuid, world);
+		return this.getSkyblockAPICached().getIslandLevel(uuid, world);
 	}
 
 	/**
@@ -165,10 +172,10 @@ public class CustomOreGen extends JavaPlugin {
 	 * @return owner
 	 */
 	public OfflinePlayer getOwner(Location loc) {
-		if (skyblockAPI.getIslandOwner(loc) == null) {
+		if (this.getSkyblockAPICached().getIslandOwner(loc) == null) {
 			return null;
 		}
-		Optional<UUID> uuid = skyblockAPI.getIslandOwner(loc);
+		Optional<UUID> uuid = this.getSkyblockAPICached().getIslandOwner(loc);
 		if (!uuid.isPresent()) {
 			return null;
 		}
@@ -274,4 +281,11 @@ public class CustomOreGen extends JavaPlugin {
 	public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
 		this.generatorConfigs = generatorConfigs;
 	}
+	
+	/**
+	 * @return the skyblockAPICached
+	 */
+	public SkyblockAPICached getSkyblockAPICached() {
+		return skyblockAPICached;
+	}
 }

+ 70 - 0
src/xyz/spaceio/hooks/SkyblockAPICached.java

@@ -0,0 +1,70 @@
+package xyz.spaceio.hooks;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.Map.Entry;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import org.bukkit.Location;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Maps;
+
+public class SkyblockAPICached {
+	private SkyblockAPIHook hook;
+	
+	public SkyblockAPICached(SkyblockAPIHook hook) {
+		this.hook = hook;
+	}
+	
+	LoadingCache<Entry<UUID, String>, Integer> cachedIslandLevel = CacheBuilder.newBuilder()
+			   .maximumSize(1000)
+			   .expireAfterWrite(10, TimeUnit.SECONDS)
+			   .build(
+			       new CacheLoader<Entry<UUID, String>, Integer>() {
+			         public Integer load(Entry<UUID, String> key) {
+			           return hook.getIslandLevel(key.getKey(), key.getValue());
+			         }
+			       });
+	
+	LoadingCache<Location, Optional<UUID>> cachedIslandOwner = CacheBuilder.newBuilder()
+			   .maximumSize(1000)
+			   .expireAfterWrite(100, TimeUnit.SECONDS)
+			   .build(
+			       new CacheLoader<Location, Optional<UUID>>() {
+			         public Optional<UUID> load(Location key) {
+			           return hook.getIslandOwner(key);
+			         }
+			       });
+	
+	
+	public int getIslandLevel(UUID owner, String world) {
+		Entry<UUID, String> entry = Maps.immutableEntry(owner, world);
+		
+		try {
+			return cachedIslandLevel.get(entry);
+		} catch (ExecutionException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return 0;
+		}
+	}
+	
+	public Optional<UUID> getIslandOwner(Location loc){
+		try {
+			return cachedIslandOwner.get(loc);
+		} catch (ExecutionException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	public String[] getSkyBlockWorldNames() {
+		return hook.getSkyBlockWorldNames();
+	}
+}