Events.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package xyz.spaceio.customoregen;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.OfflinePlayer;
  10. import org.bukkit.Particle;
  11. import org.bukkit.Sound;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.BlockFace;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.block.BlockFromToEvent;
  17. import org.bukkit.event.player.PlayerJoinEvent;
  18. public class Events implements Listener {
  19. /*
  20. * CustomOreGen main class
  21. */
  22. private CustomOreGen plugin;
  23. private boolean useLegacyBlockPlaceMethod;
  24. private boolean useLevelledClass;
  25. private Method legacyBlockPlaceMethod;
  26. private boolean enableStoneGenerator;
  27. public Events(CustomOreGen customOreGen) {
  28. this.plugin = customOreGen;
  29. load();
  30. }
  31. public void load() {
  32. this.useLegacyBlockPlaceMethod = Arrays.stream(Block.class.getMethods()).anyMatch(method -> method.getName() == "setTypeIdAndData");
  33. if(this.useLegacyBlockPlaceMethod) {
  34. try {
  35. legacyBlockPlaceMethod = Block.class.getMethod("setTypeIdAndData", int.class , byte.class, boolean.class);
  36. } catch (NoSuchMethodException | SecurityException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41. this.enableStoneGenerator = plugin.getConfig().getBoolean("enable-stone-generator");
  42. try {
  43. Class.forName("org.bukkit.block.data.Levelled");
  44. useLevelledClass = true;
  45. } catch( ClassNotFoundException e ) {
  46. useLevelledClass = false;
  47. }
  48. }
  49. @SuppressWarnings("deprecation")
  50. @EventHandler
  51. public void onFromTo(BlockFromToEvent event) {
  52. if (plugin.getDisabledWorlds().contains(event.getBlock().getLocation().getWorld().getName())) {
  53. return;
  54. }
  55. Type fromType = this.getType(event.getBlock());
  56. if (fromType != null) {
  57. if (!enableStoneGenerator) {
  58. if(event.getFace() == BlockFace.DOWN || fromType == Type.WATER_STAT) {
  59. return;
  60. }
  61. }
  62. Block b = event.getToBlock();
  63. Type toType = this.getType(event.getToBlock());
  64. Location fromLoc = b.getLocation();
  65. // fix for (lava -> water)
  66. if (fromType == Type.LAVA || fromType == Type.LAVA_STAT) {
  67. if(!isSurroundedByWater(fromLoc)){
  68. return;
  69. }
  70. }
  71. if ((toType != null || b.getType() == Material.AIR) && (this.generatesCobble(fromType, b))) {
  72. if (this.getTouchingFace(fromType, b) == BlockFace.DOWN) {
  73. b = b.getLocation().add(0, -1, 0).getBlock();
  74. } else {
  75. event.setCancelled(true);
  76. }
  77. OfflinePlayer p = plugin.getOwner(b.getLocation());
  78. if (p == null)
  79. return;
  80. GeneratorConfig gc = plugin.getGeneratorConfigForPlayer(p, event.getBlock().getWorld().getName());
  81. if (gc == null)
  82. return;
  83. if (getObject(gc) == null)
  84. return;
  85. GeneratorItem winning = getObject(gc);
  86. if (Material.getMaterial(winning.getName()) == null)
  87. return;
  88. //b.setType(Material.getMaterial(winning.getName()));
  89. // <Block>.setData(...) is deprecated, but there is no
  90. // alternative to it. #spigot
  91. if(useLegacyBlockPlaceMethod) {
  92. try {
  93. legacyBlockPlaceMethod.invoke(b, Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
  94. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. }else {
  99. b.setType(Material.getMaterial(winning.getName()));
  100. b.getState().update(true);
  101. }
  102. b.getWorld().playSound(b.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 0.5f, 2.6f + ((float) Math.random() - (float) Math.random()) * 0.8f);
  103. b.getWorld().spawnParticle(Particle.SMOKE_LARGE, b.getLocation().getBlockX() + 0.5D, b.getLocation().getBlockY() + 0.25D, b.getLocation().getBlockZ() + 0.5D, 8, 0.5D, 0.25D, 0.5D, 0.0D);
  104. plugin.getSkyblockAPICached().sendBlockAcknowledge(b);
  105. //b.setData(winning.getDamage(), true);
  106. }
  107. }
  108. }
  109. private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
  110. /**
  111. * Checks if a block is surrounded by water
  112. * @param fromLoc
  113. * @return
  114. */
  115. public boolean isSurroundedByWater(Location fromLoc) {
  116. for(BlockFace blockFace : blockFaces) {
  117. if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. @EventHandler
  124. public void onJoin(PlayerJoinEvent e) {
  125. plugin.getGeneratorConfigForPlayer(e.getPlayer(), e.getPlayer().getWorld().getName());
  126. }
  127. /**
  128. * Chooses a GeneratorItem randomly
  129. * @param gc
  130. * @return
  131. */
  132. public GeneratorItem getObject(GeneratorConfig gc) {
  133. Random random = new Random();
  134. double d = random.nextDouble() * 100;
  135. for (GeneratorItem key : gc.itemList) {
  136. if ((d -= key.getChance()) < 0)
  137. return key;
  138. }
  139. return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
  140. }
  141. private enum Type {
  142. WATER, WATER_STAT, LAVA, LAVA_STAT
  143. }
  144. public Type getType(Block b) {
  145. if(useLevelledClass) {
  146. if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
  147. org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
  148. if(level.getLevel() == 0) {
  149. if(level.getMaterial() == Material.WATER) {
  150. return Type.WATER_STAT;
  151. }else if(level.getMaterial() == Material.LAVA) {
  152. return Type.LAVA_STAT;
  153. }
  154. }else {
  155. if(level.getMaterial() == Material.WATER) {
  156. return Type.WATER;
  157. }else if(level.getMaterial() == Material.LAVA) {
  158. return Type.LAVA;
  159. }
  160. }
  161. }
  162. } else {
  163. switch(b.getType().name()) {
  164. case "WATER":
  165. return Type.WATER;
  166. case "STATIONARY_WATER":
  167. return Type.WATER_STAT;
  168. case "LAVA":
  169. return Type.LAVA;
  170. case "STATIONARY_LAVA":
  171. return Type.LAVA_STAT;
  172. }
  173. }
  174. return null;
  175. }
  176. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
  177. BlockFace.SOUTH, BlockFace.WEST };
  178. public BlockFace getTouchingFace(Type type, Block b) {
  179. Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
  180. Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
  181. for (BlockFace face : this.faces) {
  182. Block r = b.getRelative(face, 1);
  183. if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
  184. return face;
  185. }
  186. }
  187. return null;
  188. }
  189. public boolean generatesCobble(Type type, Block b) {
  190. if (this.getTouchingFace(type, b) != null) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. }