Events.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package de.Linus122.customoregen;
  2. import java.util.AbstractMap;
  3. import java.util.Map.Entry;
  4. import java.util.Random;
  5. import org.bukkit.Material;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.block.BlockFace;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.block.BlockFromToEvent;
  12. public class Events implements Listener {
  13. @SuppressWarnings({ "deprecation" })
  14. @EventHandler
  15. public void onFromTo(BlockFromToEvent event){
  16. //System.out.println("From: " + event.getBlock().getType().name());
  17. //System.out.println("To: " + event.getToBlock().getType().name());
  18. //System.out.println("Face: " + event.getFace().name());
  19. int id = event.getBlock().getTypeId();
  20. if ((id >= 8) && (id <= 11)){
  21. Block b = event.getToBlock();
  22. Entry<Boolean, Boolean> e = generatesCobble(id, b);
  23. boolean generatesCobble = e.getKey();
  24. boolean stoneGen = e.getValue();
  25. int toid = b.getTypeId();
  26. if ((toid == 0) && (generatesCobble)){
  27. GeneratorConfig gc = null;
  28. Player p = Main.getOwner(b.getLocation());
  29. if(p == null){
  30. gc = Main.generatorConfigs.get(0);
  31. }else{
  32. int islandLevel = Main.getLevel(p);
  33. if(Main.activeInWorld.getName().equals(b.getWorld().getName())){
  34. for(GeneratorConfig gc2 : Main.generatorConfigs){
  35. if(gc2 == null){
  36. continue;
  37. }
  38. if(p.hasPermission(gc2.permission) &&
  39. islandLevel >=
  40. gc2.unlock_islandLevel ){
  41. //Weiter
  42. gc = gc2;
  43. }
  44. }
  45. }
  46. }
  47. if(gc == null) return;
  48. if(getObject(gc) == null) return;
  49. GeneratorItem winning = getObject(gc);
  50. if(Material.getMaterial(winning.name) == null) return;
  51. //b.setType(Material.getMaterial(winning));
  52. if (Material.getMaterial(winning.name).equals(Material.COBBLESTONE) && winning.damage == 0 && stoneGen) {
  53. return;
  54. }
  55. b.setTypeIdAndData(Material.getMaterial(winning.name).getId() , winning.damage, true);
  56. }
  57. }
  58. }
  59. public GeneratorItem getObject(GeneratorConfig gc){
  60. Random random = new Random();
  61. double d = random.nextDouble() * 100;
  62. for(GeneratorItem key : gc.itemList){
  63. if ((d -= key.chance) < 0) return key;
  64. }
  65. return new GeneratorItem("COBBLESTONE", (byte) 0, 0); //DEFAULT
  66. }
  67. private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
  68. @SuppressWarnings("deprecation")
  69. public Entry<Boolean, Boolean> generatesCobble(int id, Block b){
  70. int mirrorID1 = (id == 8) || (id == 9) ? 10 : 8;
  71. int mirrorID2 = (id == 8) || (id == 9) ? 11 : 9;
  72. for(BlockFace face : this.faces){
  73. Block r = b.getRelative(face, 1);
  74. if ((r.getTypeId() == mirrorID1) || (r.getTypeId() == mirrorID2)) {
  75. Entry<Boolean, Boolean> e = new AbstractMap.SimpleEntry<Boolean, Boolean>(true, false);
  76. if (face.equals(BlockFace.UP)) {
  77. e.setValue(true);
  78. } else {
  79. e.setValue(false);
  80. }
  81. return e;
  82. }
  83. }
  84. Entry<Boolean, Boolean> e = new AbstractMap.SimpleEntry<Boolean, Boolean>(false, false);
  85. return new AbstractMap.SimpleEntry<Boolean, Boolean>(false, false);
  86. }
  87. }