|
@@ -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;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|