Events.java 7.2 KB

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