timber.txt 5.2 KB

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