Events.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.Optional;
  6. import java.util.Random;
  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.BlockFormEvent;
  17. import org.bukkit.event.block.BlockFromToEvent;
  18. import org.bukkit.event.player.PlayerJoinEvent;
  19. import org.jetbrains.annotations.Nullable;
  20. public class Events implements Listener {
  21. /*
  22. * CustomOreGen main class
  23. */
  24. private CustomOreGen plugin;
  25. private boolean useLegacyBlockPlaceMethod;
  26. private boolean useLevelledClass;
  27. private Method legacyBlockPlaceMethod;
  28. private boolean enableStoneGenerator;
  29. private Optional<Sound> soundEffect = Optional.empty();
  30. private boolean enableParticleEffect;
  31. public Events(CustomOreGen customOreGen) {
  32. this.plugin = customOreGen;
  33. load();
  34. }
  35. public void load() {
  36. this.useLegacyBlockPlaceMethod = Arrays.stream(Block.class.getMethods()).anyMatch(method -> method.getName() == "setTypeIdAndData");
  37. if(this.useLegacyBlockPlaceMethod) {
  38. try {
  39. legacyBlockPlaceMethod = Block.class.getMethod("setTypeIdAndData", int.class , byte.class, boolean.class);
  40. } catch (NoSuchMethodException | SecurityException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. this.enableStoneGenerator = plugin.getConfig().getBoolean("enable-stone-generator");
  46. this.enableParticleEffect = plugin.getConfig().getBoolean("enable-particle-effect", false);
  47. try {
  48. Class.forName("org.bukkit.block.data.Levelled");
  49. useLevelledClass = true;
  50. } catch( ClassNotFoundException e ) {
  51. useLevelledClass = false;
  52. }
  53. if(enableParticleEffect) {
  54. try {
  55. Class.forName("org.bukkit.Particle");
  56. } catch (ClassNotFoundException e) {
  57. this.plugin.getLogger().info(
  58. String.format("Particle effects are not supported for your bukkit version, disable 'enable-particle-effect' in %s/config.yml to get rid of this message.", this.plugin.getDataFolder().getPath()));
  59. this.enableParticleEffect = false;
  60. }
  61. }
  62. if(plugin.getConfig().getBoolean("enable-sound-effect", false)) {
  63. // disabling sound effects when enum value not present
  64. soundEffect = Arrays.stream(Sound.values()).filter(s -> {
  65. return s.name().equals("BLOCK_FIRE_EXTINGUISH") || s.name().equals("FIZZ");
  66. }).findAny();
  67. }
  68. }
  69. /**
  70. * This is used in minecraft versions >= 1.12 and does nothing on other version.
  71. */
  72. @EventHandler
  73. public void blockFormEvent(BlockFormEvent event) {
  74. if (plugin.getDisabledWorlds().contains(event.getBlock().getLocation().getWorld().getName())) {
  75. return;
  76. }
  77. if(event.getNewState().getType().equals(Material.COBBLESTONE)
  78. || enableStoneGenerator && event.getNewState().getType().equals(Material.STONE)) {
  79. event.setCancelled(true);
  80. GeneratorConfig generatorConfig = this.getGeneratorConfigAtLocation(event.getBlock().getLocation());
  81. if (generatorConfig != null) {
  82. GeneratorItem generatorItem = generatorConfig.getRandomItem();
  83. Material material = Material.getMaterial(generatorItem.getName());
  84. if(material != null) {
  85. // set actual block
  86. placeBlock(event.getBlock(), material, generatorItem.getDamage());
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Used for older mc versions
  93. */
  94. @EventHandler
  95. public void onBlockFromToEvent(BlockFromToEvent event) {
  96. if(this.isGenerator(event)) {
  97. event.setCancelled(true);
  98. GeneratorConfig generatorConfig = this.getGeneratorConfigAtLocation(event.getBlock().getLocation());
  99. if (generatorConfig != null) {
  100. GeneratorItem generatorItem = generatorConfig.getRandomItem();
  101. Material material = Material.getMaterial(generatorItem.getName());
  102. if(material != null) {
  103. // set actual block
  104. placeBlock(event.getToBlock(), material, generatorItem.getDamage());
  105. }
  106. }
  107. event.getBlock().getState().update();
  108. }
  109. }
  110. private void placeBlock(Block block, Material material, byte damage) {
  111. if(useLegacyBlockPlaceMethod) {
  112. try {
  113. legacyBlockPlaceMethod.invoke(block, material.getId(), damage, true);
  114. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. }
  118. }else {
  119. block.setType(material);
  120. block.getState().update(true);
  121. }
  122. soundEffect.ifPresent(sound -> block.getWorld().playSound(block.getLocation(),
  123. sound,
  124. 0.5f, 2.6f + ((float) Math.random() - (float) Math.random()) * 0.8f));
  125. if(enableParticleEffect) {
  126. block.getWorld().spawnParticle(Particle.SMOKE_LARGE,
  127. block.getLocation().getBlockX() + 0.5D,
  128. block.getLocation().getBlockY() + 0.25D,
  129. block.getLocation().getBlockZ() + 0.5D,
  130. 8, 0.5D, 0.25D, 0.5D, 0.0D);
  131. }
  132. plugin.getSkyblockAPICached().sendBlockAcknowledge(block);
  133. }
  134. @Nullable
  135. private GeneratorConfig getGeneratorConfigAtLocation(Location location) {
  136. OfflinePlayer player = plugin.getApplicablePlayer(location);
  137. if (player == null)
  138. return null;
  139. GeneratorConfig gc = plugin.getGeneratorConfigForPlayer(player, location.getWorld().getName());
  140. if (gc == null)
  141. return null;
  142. return gc;
  143. }
  144. private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
  145. /**
  146. * Checks if a block is surrounded by water
  147. * @param fromLoc
  148. * @return
  149. */
  150. public boolean isSurroundedByWater(Location fromLoc) {
  151. for(BlockFace blockFace : blockFaces) {
  152. if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. @EventHandler
  159. public void onJoin(PlayerJoinEvent e) {
  160. plugin.getGeneratorConfigForPlayer(e.getPlayer(), e.getPlayer().getWorld().getName());
  161. }
  162. private enum Type {
  163. WATER, WATER_STAT, LAVA, LAVA_STAT
  164. }
  165. private Type getType(Block b) {
  166. if(useLevelledClass) {
  167. if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
  168. org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
  169. if(level.getLevel() == 0) {
  170. if(level.getMaterial() == Material.WATER) {
  171. return Type.WATER_STAT;
  172. }else if(level.getMaterial() == Material.LAVA) {
  173. return Type.LAVA_STAT;
  174. }
  175. }else {
  176. if(level.getMaterial() == Material.WATER) {
  177. return Type.WATER;
  178. }else if(level.getMaterial() == Material.LAVA) {
  179. return Type.LAVA;
  180. }
  181. }
  182. }
  183. } else {
  184. switch(b.getType().name()) {
  185. case "WATER":
  186. return Type.WATER;
  187. case "STATIONARY_WATER":
  188. return Type.WATER_STAT;
  189. case "LAVA":
  190. return Type.LAVA;
  191. case "STATIONARY_LAVA":
  192. return Type.LAVA_STAT;
  193. }
  194. }
  195. return null;
  196. }
  197. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
  198. BlockFace.SOUTH, BlockFace.WEST };
  199. private boolean generatesCobble(Type type, Block b) {
  200. Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
  201. Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
  202. for (BlockFace face : this.faces) {
  203. Block r = b.getRelative(face, 1);
  204. if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. private boolean isGenerator(BlockFromToEvent event) {
  211. Type type = this.getType(event.getBlock());
  212. if (type != null && (!type.equals(Type.WATER_STAT))
  213. && (event.getFace() != BlockFace.DOWN)) {
  214. if (!enableStoneGenerator) {
  215. if(event.getFace() == BlockFace.DOWN) {
  216. return false;
  217. }
  218. }
  219. Block b = event.getToBlock();
  220. Location fromLoc = b.getLocation();
  221. // fix for (lava <-> water)
  222. if ((type.equals(Type.LAVA) || type.equals(Type.LAVA_STAT))) {
  223. if (!isSurroundedByWater(fromLoc)) {
  224. return false;
  225. }
  226. }
  227. if ((b.getType() == Material.AIR || this.getType(b) == Type.WATER || this.getType(b) == Type.WATER_STAT)
  228. && (generatesCobble(type, b))) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. }