Events.java 6.9 KB

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