Events.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.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.BlockFromToEvent;
  16. import org.bukkit.event.player.PlayerJoinEvent;
  17. public class Events implements Listener {
  18. /*
  19. * CustomOreGen main class
  20. */
  21. private CustomOreGen plugin;
  22. private boolean useLegacyBlockPlaceMethod;
  23. private Method legacyBlockPlaceMethod;
  24. private boolean enableStoneGenerator;
  25. public Events(CustomOreGen customOreGen) {
  26. this.plugin = customOreGen;
  27. load();
  28. }
  29. public void load() {
  30. this.useLegacyBlockPlaceMethod = Arrays.stream(Block.class.getMethods()).anyMatch(method -> method.getName() == "setTypeIdAndData");
  31. if(this.useLegacyBlockPlaceMethod) {
  32. try {
  33. legacyBlockPlaceMethod = Block.class.getMethod("setTypeIdAndData", int.class , byte.class, boolean.class);
  34. } catch (NoSuchMethodException | SecurityException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. }
  39. this.enableStoneGenerator = plugin.getConfig().getBoolean("enable-stone-generator");
  40. }
  41. @SuppressWarnings("deprecation")
  42. @EventHandler
  43. public void onFromTo(BlockFromToEvent event) {
  44. if (plugin.getDisabledWorlds().contains(event.getBlock().getLocation().getWorld().getName())) {
  45. return;
  46. }
  47. Type fromType = this.getType(event.getBlock());
  48. if (fromType != null) {
  49. if (!enableStoneGenerator) {
  50. if(event.getFace() == BlockFace.DOWN) {
  51. return;
  52. }
  53. }
  54. Block b = event.getToBlock();
  55. Type toType = this.getType(event.getToBlock());
  56. Location fromLoc = b.getLocation();
  57. // fix for (lava -> water)
  58. if (fromType == Type.LAVA || fromType == Type.LAVA_STAT) {
  59. if(!isSurroundedByWater(fromLoc)){
  60. return;
  61. }
  62. }
  63. if ((toType != null || b.getType() == Material.AIR) && (generatesCobble(fromType, b))) {
  64. OfflinePlayer p = plugin.getOwner(b.getLocation());
  65. if (p == null)
  66. return;
  67. GeneratorConfig gc = plugin.getGeneratorConfigForPlayer(p, event.getBlock().getWorld().getName());
  68. if (gc == null)
  69. return;
  70. if (getObject(gc) == null)
  71. return;
  72. GeneratorItem winning = getObject(gc);
  73. if (Material.getMaterial(winning.getName()) == null)
  74. return;
  75. event.setCancelled(true);
  76. //b.setType(Material.getMaterial(winning.getName()));
  77. // <Block>.setData(...) is deprecated, but there is no
  78. // alternative to it. #spigot
  79. if(useLegacyBlockPlaceMethod) {
  80. try {
  81. legacyBlockPlaceMethod.invoke(b, Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
  82. plugin.getSkyblockAPICached().sendBlockAcknowledge(b);
  83. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. }else {
  88. b.setType(Material.getMaterial(winning.getName()));
  89. b.getState().update(true);
  90. b.getWorld().playSound(b.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 1f, 10f);
  91. plugin.getSkyblockAPICached().sendBlockAcknowledge(b);
  92. }
  93. //b.setData(winning.getDamage(), true);
  94. }
  95. }
  96. }
  97. private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
  98. /**
  99. * Checks if a block is surrounded by water
  100. * @param fromLoc
  101. * @return
  102. */
  103. public boolean isSurroundedByWater(Location fromLoc) {
  104. for(BlockFace blockFace : blockFaces) {
  105. if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. @EventHandler
  112. public void onJoin(PlayerJoinEvent e) {
  113. plugin.getGeneratorConfigForPlayer(e.getPlayer(), e.getPlayer().getWorld().getName());
  114. }
  115. /**
  116. * Chooses a GeneratorItem randomly
  117. * @param gc
  118. * @return
  119. */
  120. public GeneratorItem getObject(GeneratorConfig gc) {
  121. Random random = new Random();
  122. double d = random.nextDouble() * 100;
  123. for (GeneratorItem key : gc.itemList) {
  124. if ((d -= key.getChance()) < 0)
  125. return key;
  126. }
  127. return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
  128. }
  129. private enum Type {
  130. WATER, WATER_STAT, LAVA, LAVA_STAT
  131. }
  132. public Type getType(Block b) {
  133. try {
  134. Class.forName("org.bukkit.block.data.Levelled");
  135. if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
  136. org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
  137. if(level.getLevel() == 0) {
  138. if(level.getMaterial() == Material.WATER) {
  139. return Type.WATER_STAT;
  140. }else if(level.getMaterial() == Material.LAVA) {
  141. return Type.LAVA_STAT;
  142. }
  143. }else {
  144. if(level.getMaterial() == Material.WATER) {
  145. return Type.WATER;
  146. }else if(level.getMaterial() == Material.LAVA) {
  147. return Type.LAVA;
  148. }
  149. }
  150. }
  151. } catch( ClassNotFoundException e ) {
  152. switch(b.getType().name()) {
  153. case "WATER":
  154. return Type.WATER;
  155. case "STATIONARY_WATER":
  156. return Type.WATER_STAT;
  157. case "LAVA":
  158. return Type.LAVA;
  159. case "STATIONARY_LAVA":
  160. return Type.LAVA_STAT;
  161. }
  162. }
  163. return null;
  164. }
  165. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
  166. BlockFace.SOUTH, BlockFace.WEST };
  167. public boolean generatesCobble(Type type, Block b) {
  168. Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
  169. Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
  170. for (BlockFace face : this.faces) {
  171. Block r = b.getRelative(face, 1);
  172. if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. }