Browse Source

performance improvements

MasterCake 5 years ago
parent
commit
d3e0a55fac

+ 2 - 2
pom.xml

@@ -52,14 +52,14 @@
 		<dependency>
 			<groupId>org.spigotmc</groupId>
 			<artifactId>spigot-api</artifactId>
-			<version>1.12.2-R0.1-SNAPSHOT</version>
+			<version>1.14.4-R0.1-SNAPSHOT</version>
 			<scope>provided</scope>
 		</dependency>
 		<!-- Bukkit API -->
 		<dependency>
 			<groupId>org.bukkit</groupId>
 			<artifactId>craftbukkit</artifactId>
-			<version>1.12.2-R0.1-SNAPSHOT</version>
+			<version>1.14.4-R0.1-SNAPSHOT</version>
 			<scope>provided</scope>
 		</dependency>
 		<!-- ASkyBlock -->

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

@@ -1,9 +1,13 @@
 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;
@@ -24,6 +28,21 @@ 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;
 	}
 }

+ 4 - 1
src/xyz/spaceio/customoregen/CustomOreGen.java

@@ -65,6 +65,8 @@ public class CustomOreGen extends JavaPlugin {
 	 * Prefix for the clogger
 	 */
 	private final String PREFIX = "§6[CustomOreGen] ";
+	
+	public static Events eventClass;
 
 	@Override
 	public void onEnable() {
@@ -72,7 +74,8 @@ public class CustomOreGen extends JavaPlugin {
 		
 		PluginManager pm = Bukkit.getPluginManager();
 		
-		pm.registerEvents(new Events(this), this);
+		eventClass = new Events(this);
+		pm.registerEvents(eventClass, this);
 
 		this.loadHook();
 

+ 69 - 37
src/xyz/spaceio/customoregen/Events.java

@@ -1,5 +1,6 @@
 package xyz.spaceio.customoregen;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.Random;
 
@@ -23,8 +24,11 @@ public class Events implements Listener {
 	 */
 	private CustomOreGen plugin;
 	
+	private boolean useLegacyBlockPlaceMethod;
+	
 	public Events(CustomOreGen customOreGen) {
 		this.plugin = customOreGen;
+		this.useLegacyBlockPlaceMethod = Arrays.stream(Block.class.getMethods()).anyMatch(method -> method.getName() == "getTypeId");
 	}
 
 	@SuppressWarnings("deprecation")
@@ -34,20 +38,22 @@ public class Events implements Listener {
 			return;
 		}
 
-		int id = this.getID(event.getBlock());
-
-		if ((id >= 8) && (id <= 11) && event.getFace() != BlockFace.DOWN) {
+		Type fromType = this.getType(event.getBlock());
+		
+		if (fromType != null && event.getFace() != BlockFace.DOWN) {
 			Block b = event.getToBlock();
-			int toid = this.getID(b);
+			Type toType = this.getType(event.getToBlock());
+			
 			Location fromLoc = b.getLocation();
+			
 			// fix for (lava -> water)
-			if (id == 10 || id == 11) {
+			if (fromType == Type.LAVA || fromType == Type.LAVA_STAT) {
 				if(!isSurroundedByWater(fromLoc)){
 					return;
 				}
 			}
 
-			if ((toid == 0 || toid == 9 || toid == 8 || toid == 10 || toid == 11) && (generatesCobble(id, b))) {
+			if (toType != null || b.getType() == Material.AIR && (generatesCobble(fromType, b))) {
 				OfflinePlayer p = plugin.getOwner(b.getLocation());
 				if (p == null)
 					return;
@@ -64,11 +70,18 @@ public class Events implements Listener {
 					return;
 				}
 				event.setCancelled(true);
+
 				//b.setType(Material.getMaterial(winning.getName()));
 				// <Block>.setData(...) is deprecated, but there is no
 				// alternative to it. #spigot
-				if(Arrays.stream(event.getBlock().getClass().getMethods()).anyMatch(method -> method.getName() == "getTypeId")) {
-					b.setTypeIdAndData(Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
+				if(useLegacyBlockPlaceMethod) {
+					 try {
+						b.getClass().getMethod("setTypeIdAndData", Material.class, byte.class, boolean.class).invoke(b, Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
+					} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
+							| NoSuchMethodException | SecurityException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
 				}else {
 					Bukkit.getScheduler().runTask(plugin, () -> {
 						b.setType(Material.getMaterial(winning.getName()));
@@ -82,23 +95,21 @@ public class Events implements Listener {
 
 	}
 
+	private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
+	
 	/**
 	 * 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()),
-				fromLoc.getWorld().getBlockAt(fromLoc.getBlockX() - 1, fromLoc.getBlockY(), fromLoc.getBlockZ()),
-				fromLoc.getWorld().getBlockAt(fromLoc.getBlockX(), fromLoc.getBlockY(), fromLoc.getBlockZ() + 1),
-				fromLoc.getWorld().getBlockAt(fromLoc.getBlockX(), fromLoc.getBlockY(), fromLoc.getBlockZ() - 1) };
-
-		for (Block b : blocks) {
-			if (b.getType().toString().contains("WATER")) {
+
+		for(BlockFace blockFace : blockFaces) {
+			if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
 				return true;
 			}
 		}
+		
 		return false;
 
 	}
@@ -124,36 +135,57 @@ public class Events implements Listener {
 		}
 		return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
 	}
+	
+	private enum Type {
+		WATER, WATER_STAT, LAVA, LAVA_STAT
+	}
+	
+	public Type getType(Block b) {
+		try {
+			Class.forName("org.bukkit.block.data.Levelled");
+			if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
+				org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
+				if(level.getLevel() == 0) {
+					if(level.getMaterial() == Material.WATER) {
+						return Type.WATER_STAT;
+					}else {
+						return Type.LAVA_STAT;
+					}
+				}else {
+					if(level.getMaterial() == Material.WATER) {
+						return Type.WATER;
+					}else {
+						return Type.LAVA;
+					}
+				}
+			}
+		} catch( ClassNotFoundException e ) {
+			switch(b.getType().name()) {
+				case "WATER":
+					return Type.WATER;
+				case "STATIONARY_WATER":
+					return Type.WATER_STAT;
+				case "LAVA":
+					return Type.LAVA;
+				case "STATIONARY_LAVA":
+					return Type.LAVA_STAT;
+			}
+		}
+		return null;
+	}
 
 	private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
 			BlockFace.SOUTH, BlockFace.WEST };
 
-	public boolean generatesCobble(int id, Block b) {
-		int mirrorID1 = (id == 8) || (id == 9) ? 10 : 8;
-		int mirrorID2 = (id == 8) || (id == 9) ? 11 : 9;
+	public boolean generatesCobble(Type type, Block b) {
+		Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
+		Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
 		for (BlockFace face : this.faces) {
 			Block r = b.getRelative(face, 1);
-			if ((this.getID(r) == mirrorID1) || (this.getID(r) == mirrorID2)) {
+			if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
 				return true;
 			}
 		}
 		return false;
 	}
-	
-	/**
-	 * Method for getting the block id from a block
-	 * @param b
-	 * @return
-	 */
-	public int getID(Block b) {
-	    if(Arrays.stream(b.getClass().getMethods()).anyMatch(method -> method.getName() == "getTypeId")) {
-	    	return b.getTypeId();
-	    }else {
-	    	try {
-	    		return Utils.Material113.valueOf(Utils.Material113.class, b.getType().name()).getID();
-	    	} catch(IllegalArgumentException e) {
-	    		return 2;
-	    	}
-	    }
-	}
 }