Events.java 8.2 KB

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