timber.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. event.load("block_break");
  2. prop_persistent = block.getProperty("persistent");
  3. tag_log_burn = block.getTag("minecraft:logs_that_burn");
  4. axe_tag = item.getTag("km:axe");
  5. horizontal_dist_map = map.new();
  6. map.add(horizontal_dist_map, "minecraft:oak_log", 9);
  7. map.add(horizontal_dist_map, "minecraft:spruce_log", 8);
  8. map.add(horizontal_dist_map, "minecraft:birch_log", 2);
  9. map.add(horizontal_dist_map, "minecraft:jungle_log", 12);
  10. map.add(horizontal_dist_map, "minecraft:acacia_log", 9);
  11. map.add(horizontal_dist_map, "minecraft:dark_oak_log", 6);
  12. msg("dev", "§bTimber §rloaded.");
  13. @wait
  14. wait();
  15. if(event == "block_break") {
  16. if(cancel) {
  17. goto("wait");
  18. }
  19. duration = data.getTimer(player, "timber");
  20. if(duration < 0) {
  21. goto("wait");
  22. }
  23. if(!player.checkHandsForTag(player, axe_tag)) {
  24. goto("wait");
  25. }
  26. tree_type = block_type;
  27. horizontal_spec_dist = map.getOrDefault(horizontal_dist_map, tree_type, 2);
  28. origin_x = loc.getX(block_loc);
  29. origin_z = loc.getZ(block_loc);
  30. if(block.isTree(block_loc)) {
  31. block.removeTree(block_loc);
  32. }
  33. }
  34. goto("wait");
  35. function block.isTree(break_loc) {
  36. block = block.get(break_loc);
  37. if(!block.hasTag($tag_log_burn, block)) {
  38. return false;
  39. }
  40. //neighbours on same level found? return false
  41. mod_loc = loc.mod(break_loc, 1, 0, 0);
  42. if(block.getType(mod_loc) == $tree_type) {
  43. return false;
  44. }
  45. loc.add(mod_loc, -2, 0, 0);
  46. if(block.getType(mod_loc) == $tree_type) {
  47. return false;
  48. }
  49. loc.add(mod_loc, 1, 0, 1);
  50. if(block.getType(mod_loc) == $tree_type) {
  51. return false;
  52. }
  53. loc.add(mod_loc, 0, 0, -2);
  54. if(block.getType(mod_loc) == $tree_type) {
  55. return false;
  56. }
  57. loc.add(mod_loc, 0, 0, 1);
  58. //search top logs
  59. while(block.getType(mod_loc) == $tree_type) {
  60. loc.add(mod_loc, 0, 1, 0);
  61. }
  62. state = block.property.getValue(mod_loc, $prop_persistent);
  63. if(state == null || state) {
  64. if($tree_type == "minecraft:acacia_log") {
  65. for(i = 0; i < 5; i++) {
  66. state = block.property.getValue(mod_loc, $prop_persistent);
  67. if(state != null && !state) {
  68. return true;
  69. }
  70. mod_loc = loc.mod(mod_loc, 0, 1, 0);
  71. }
  72. }
  73. return false;
  74. }
  75. return true;
  76. }
  77. function block.removeTree(break_loc) {
  78. c = 0;
  79. $tree_list = list.new();
  80. $tree_set = set.new();
  81. list.add($tree_list, break_loc);
  82. while(list.getSize($tree_list) != 0) {
  83. index = list.getSize($tree_list) - 1;
  84. loc = list.getIndex($tree_list, index);
  85. list.removeIndex($tree_list, index);
  86. mod_loc = loc.mod(loc, 1, 0, 0);
  87. block.addIfTreePart(mod_loc);
  88. mod_loc = loc.mod(mod_loc, -2, 0, 0);
  89. block.addIfTreePart(mod_loc);
  90. mod_loc = loc.mod(mod_loc, 1, 0, 1);
  91. block.addIfTreePart(mod_loc);
  92. mod_loc = loc.mod(mod_loc, 0, 0, -2);
  93. block.addIfTreePart(mod_loc);
  94. mod_loc = loc.mod(mod_loc, 0, 1, 1);
  95. block.addIfTreePart(mod_loc);
  96. if($tree_type == "minecraft:acacia_log") {
  97. mod_loc = loc.mod(mod_loc, 1, 0, 0);
  98. block.addIfTreePart(mod_loc);
  99. mod_loc = loc.mod(mod_loc, -2, 0, 0);
  100. block.addIfTreePart(mod_loc);
  101. mod_loc = loc.mod(mod_loc, 1, 0, 1);
  102. block.addIfTreePart(mod_loc);
  103. mod_loc = loc.mod(mod_loc, 0, 0, -2);
  104. block.addIfTreePart(mod_loc);
  105. }
  106. if($tree_type == "minecraft:dark_oak_log") {
  107. mod_loc = loc.mod(mod_loc, 1, 0, 1);
  108. block.addIfLog(mod_loc);
  109. mod_loc = loc.mod(mod_loc, -2, 0, 0);
  110. block.addIfLog(mod_loc);
  111. mod_loc = loc.mod(mod_loc, 0, 0, -2);
  112. block.addIfLog(mod_loc);
  113. mod_loc = loc.mod(mod_loc, 2, 0, 0);
  114. block.addIfLog(mod_loc);
  115. }
  116. c++;
  117. if(c > 1000) {
  118. break;
  119. }
  120. }
  121. return;
  122. }
  123. function block.addIfLog(location) {
  124. if(block.isLog(location)) {
  125. block.addTreePart(location);
  126. }
  127. }
  128. function block.isLog(location) {
  129. horizontal_dist = math.abs($origin_x - loc.getX(location));
  130. if(horizontal_dist > $horizontal_spec_dist) {
  131. return false;
  132. }
  133. horizontal_dist = math.abs($origin_z - loc.getZ(location));
  134. if(horizontal_dist > $horizontal_spec_dist) {
  135. return false;
  136. }
  137. temp_type = block.getType(location);
  138. if(temp_type == $tree_type) {
  139. return true;
  140. }
  141. set.add($tree_set, location);
  142. return false;
  143. }
  144. function block.addIfTreePart(location) {
  145. if(block.isTreePart(location)) {
  146. block.addTreePart(location);
  147. }
  148. }
  149. function block.isTreePart(location) {
  150. horizontal_dist = math.abs($origin_x - loc.getX(location));
  151. if(horizontal_dist > $horizontal_spec_dist) {
  152. return false;
  153. }
  154. horizontal_dist = math.abs($origin_z - loc.getZ(location));
  155. if(horizontal_dist > $horizontal_spec_dist) {
  156. return false;
  157. }
  158. temp_type = block.getType(location);
  159. if(temp_type == $tree_type) {
  160. return true;
  161. }
  162. if(temp_type == "minecraft:vine") {
  163. return true;
  164. }
  165. state = block.property.getValue(location, $prop_persistent);
  166. if(state != null && !state) {
  167. return true;
  168. }
  169. set.add($tree_set, location);
  170. return false;
  171. }
  172. function block.addTreePart(location) {
  173. if(!set.contains($tree_set, location)) {
  174. list.add($tree_list, location);
  175. set.add($tree_set, location);
  176. block.break(location, player);
  177. }
  178. }