Events.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. event.setCancelled(true);
  66. //b.setType(Material.getMaterial(winning.getName()));
  67. // <Block>.setData(...) is deprecated, but there is no
  68. // alternative to it. #spigot
  69. if(useLegacyBlockPlaceMethod) {
  70. try {
  71. legacyBlockPlaceMethod.invoke(b, Material.getMaterial(winning.getName()).getId() , winning.getDamage(), true);
  72. } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76. }else {
  77. b.setType(Material.getMaterial(winning.getName()));
  78. b.getState().update(true);
  79. b.getWorld().playSound(b.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 1f, 10f);
  80. }
  81. //b.setData(winning.getDamage(), true);
  82. }
  83. }
  84. }
  85. private BlockFace[] blockFaces = { BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH };
  86. /**
  87. * Checks if a block is surrounded by water
  88. * @param fromLoc
  89. * @return
  90. */
  91. public boolean isSurroundedByWater(Location fromLoc) {
  92. for(BlockFace blockFace : blockFaces) {
  93. if(this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER || this.getType(fromLoc.getBlock().getRelative(blockFace)) == Type.WATER_STAT) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. @EventHandler
  100. public void onJoin(PlayerJoinEvent e) {
  101. plugin.getGeneratorConfigForPlayer(e.getPlayer(), e.getPlayer().getWorld().getName());
  102. }
  103. /**
  104. * Chooses a GeneratorItem randomly
  105. * @param gc
  106. * @return
  107. */
  108. public GeneratorItem getObject(GeneratorConfig gc) {
  109. Random random = new Random();
  110. double d = random.nextDouble() * 100;
  111. for (GeneratorItem key : gc.itemList) {
  112. if ((d -= key.getChance()) < 0)
  113. return key;
  114. }
  115. return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
  116. }
  117. private enum Type {
  118. WATER, WATER_STAT, LAVA, LAVA_STAT
  119. }
  120. public Type getType(Block b) {
  121. try {
  122. Class.forName("org.bukkit.block.data.Levelled");
  123. if(b.getBlockData() != null && b.getBlockData() instanceof org.bukkit.block.data.Levelled) {
  124. org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) b.getBlockData();
  125. if(level.getLevel() == 0) {
  126. if(level.getMaterial() == Material.WATER) {
  127. return Type.WATER_STAT;
  128. }else {
  129. return Type.LAVA_STAT;
  130. }
  131. }else {
  132. if(level.getMaterial() == Material.WATER) {
  133. return Type.WATER;
  134. }else {
  135. return Type.LAVA;
  136. }
  137. }
  138. }
  139. } catch( ClassNotFoundException e ) {
  140. switch(b.getType().name()) {
  141. case "WATER":
  142. return Type.WATER;
  143. case "STATIONARY_WATER":
  144. return Type.WATER_STAT;
  145. case "LAVA":
  146. return Type.LAVA;
  147. case "STATIONARY_LAVA":
  148. return Type.LAVA_STAT;
  149. }
  150. }
  151. return null;
  152. }
  153. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
  154. BlockFace.SOUTH, BlockFace.WEST };
  155. public boolean generatesCobble(Type type, Block b) {
  156. Type mirrorType1 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA_STAT : Type.WATER_STAT;
  157. Type mirrorType2 = (type == Type.WATER_STAT) || (type == Type.WATER) ? Type.LAVA : Type.WATER;
  158. for (BlockFace face : this.faces) {
  159. Block r = b.getRelative(face, 1);
  160. if ((this.getType(r) == mirrorType1) || (this.getType(r) == mirrorType2)) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. }