Events.java 5.7 KB

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