Events.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. private boolean enableSoundEffect;
  28. private boolean enableParticleEffect;
  29. public Events(CustomOreGen customOreGen) {
  30. this.plugin = customOreGen;
  31. load();
  32. }
  33. public void load() {
  34. this.useLegacyBlockPlaceMethod = Arrays.stream(Block.class.getMethods()).anyMatch(method -> method.getName() == "setTypeIdAndData");
  35. if(this.useLegacyBlockPlaceMethod) {
  36. try {
  37. legacyBlockPlaceMethod = Block.class.getMethod("setTypeIdAndData", int.class , byte.class, boolean.class);
  38. } catch (NoSuchMethodException | SecurityException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43. this.enableStoneGenerator = plugin.getConfig().getBoolean("enable-stone-generator");
  44. this.enableSoundEffect = plugin.getConfig().getBoolean("enable-sound-effect", false);
  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(enableSoundEffect) {
  62. // disabling sound effects when enum value not present
  63. enableSoundEffect = Arrays.asList(Sound.values()).stream().map(Sound::name).anyMatch(s -> s.equals("BLOCK_FIRE_EXTINGUISH"));
  64. }
  65. }
  66. @SuppressWarnings("deprecation")
  67. @EventHandler
  68. public void onFromTo(BlockFromToEvent event) {
  69. if (plugin.getDisabledWorlds().contains(event.getBlock().getLocation().getWorld().getName())) {
  70. return;
  71. }
  72. Type fromType = this.getType(event.getBlock());
  73. if (fromType != null) {
  74. if (!enableStoneGenerator) {
  75. if(event.getFace() == BlockFace.DOWN || fromType == Type.WATER_STAT) {
  76. return;
  77. }
  78. }
  79. Block b = event.getToBlock();
  80. Type toType = this.getType(event.getToBlock());
  81. Location fromLoc = b.getLocation();
  82. // fix for (lava -> water)
  83. if (fromType == Type.LAVA || fromType == Type.LAVA_STAT) {
  84. if(!isSurroundedByWater(fromLoc)){
  85. return;
  86. }
  87. }
  88. if ((toType != null || b.getType() == Material.AIR) && (this.generatesCobble(fromType, b))) {
  89. if (this.getTouchingFace(fromType, b) == BlockFace.DOWN) {
  90. b = b.getLocation().add(0, -1, 0).getBlock();
  91. } else {
  92. event.setCancelled(true);
  93. }
  94. OfflinePlayer p = plugin.getOwner(b.getLocation());
  95. if (p == null)
  96. return;
  97. GeneratorConfig gc = plugin.getGeneratorConfigForPlayer(p, event.getBlock().getWorld().getName());
  98. if (gc == null)
  99. return;
  100. if (getObject(gc) == null)
  101. return;
  102. GeneratorItem winning = getObject(gc);
  103. if (Material.getMaterial(winning.getName()) == null)
  104. return;
  105. //b.setType(Material.getMaterial(winning.getName()));
  106. // <Block>.setData(...) is deprecated, but there is no
  107. // alternative to it. #spigot
  108. if(useLegacyBlockPlaceMethod) {
  109. try {
  110. legacyBlockPlaceMethod.invoke(b, Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
  111. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  112. // TODO Auto-generated catch block
  113. e.printStackTrace();
  114. }
  115. }else {
  116. b.setType(Material.getMaterial(winning.getName()));
  117. b.getState().update(true);
  118. }
  119. if(enableSoundEffect)
  120. b.getWorld().playSound(b.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 0.5f, 2.6f + ((float) Math.random() - (float) Math.random()) * 0.8f);
  121. if(enableParticleEffect)
  122. 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);
  123. plugin.getSkyblockAPICached().sendBlockAcknowledge(b);
  124. //b.setData(winning.getDamage(), true);
  125. }
  126. }
  127. }
  128. private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
  129. /**
  130. * Checks if a block is surrounded by water
  131. * @param fromLoc
  132. * @return
  133. */
  134. public boolean isSurroundedByWater(Location fromLoc) {
  135. for(BlockFace blockFace : blockFaces) {
  136. if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. @EventHandler
  143. public void onJoin(PlayerJoinEvent e) {
  144. plugin.getGeneratorConfigForPlayer(e.getPlayer(), e.getPlayer().getWorld().getName());
  145. }
  146. /**
  147. * Chooses a GeneratorItem randomly
  148. * @param gc
  149. * @return
  150. */
  151. public GeneratorItem getObject(GeneratorConfig gc) {
  152. Random random = new Random();
  153. double d = random.nextDouble() * 100;
  154. for (GeneratorItem key : gc.itemList) {
  155. if ((d -= key.getChance()) < 0)
  156. return key;
  157. }
  158. return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
  159. }
  160. private enum Type {
  161. WATER, WATER_STAT, LAVA, LAVA_STAT
  162. }
  163. public Type getType(Block b) {
  164. if(useLevelledClass) {
  165. if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
  166. org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
  167. if(level.getLevel() == 0) {
  168. if(level.getMaterial() == Material.WATER) {
  169. return Type.WATER_STAT;
  170. }else if(level.getMaterial() == Material.LAVA) {
  171. return Type.LAVA_STAT;
  172. }
  173. }else {
  174. if(level.getMaterial() == Material.WATER) {
  175. return Type.WATER;
  176. }else if(level.getMaterial() == Material.LAVA) {
  177. return Type.LAVA;
  178. }
  179. }
  180. }
  181. } else {
  182. switch(b.getType().name()) {
  183. case "WATER":
  184. return Type.WATER;
  185. case "STATIONARY_WATER":
  186. return Type.WATER_STAT;
  187. case "LAVA":
  188. return Type.LAVA;
  189. case "STATIONARY_LAVA":
  190. return Type.LAVA_STAT;
  191. }
  192. }
  193. return null;
  194. }
  195. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
  196. BlockFace.SOUTH, BlockFace.WEST };
  197. public BlockFace getTouchingFace(Type type, Block b) {
  198. Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
  199. Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
  200. for (BlockFace face : this.faces) {
  201. Block r = b.getRelative(face, 1);
  202. if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
  203. return face;
  204. }
  205. }
  206. return null;
  207. }
  208. public boolean generatesCobble(Type type, Block b) {
  209. if (this.getTouchingFace(type, b) != null) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. }