doors.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. event.load("block_click");
  2. doors = list.new();
  3. orig_value_map = map.new();
  4. auto_door_tag = block.getTag("km:auto_door");
  5. doors_tag = block.getTag("minecraft:wooden_doors");
  6. trapdoors_tag = block.getTag("minecraft:wooden_trapdoors");
  7. fencegates_tag = block.getTag("minecraft:fence_gates");
  8. half_prop = block.getProperty("double_block_half");
  9. open_prop = block.getProperty("open");
  10. block_sound_category = sound.getCategory("block");
  11. open_door_sound = sound.get("block.wooden_door.open");
  12. close_door_sound = sound.get("block.wooden_door.close");
  13. open_trapdoor_sound = sound.get("block.wooden_trapdoor.open");
  14. close_trapdoor_sound = sound.get("block.wooden_trapdoor.close");
  15. open_fencegate_sound = sound.get("block.fence_gate.open");
  16. close_fencegate_sound = sound.get("block.fence_gate.close");
  17. msg("dev", "§bDoors §rloaded.");
  18. @wait
  19. wait();
  20. loc = entity.getLocation(player);
  21. world_name = world.getName(loc.getWorld(loc));
  22. if(word.isSurvName(world_name) || world.isStoryName(world_name)) {
  23. ignoreGoto(event);
  24. }
  25. goto("wait");
  26. @block_click
  27. if(block.hasTag(auto_door_tag, block) && action == "right") {
  28. player_spec = player.getAutoCloseDoor(player);
  29. open_value = door.isOpen(block_loc);
  30. if(block.hasTag(doors_tag, block)) {
  31. if(!open_value) {
  32. if(player_spec) {
  33. list.add(doors, block_loc);
  34. sgoto(60, "closedoor");
  35. }
  36. }
  37. //Double door
  38. door_loc_2 = block.getSecondDoor(block_loc);
  39. if(door_loc_2 == null) {
  40. goto("wait");
  41. }
  42. open_value_2 = door.isOpen(door_loc_2);
  43. if(open_value && open_value_2) {
  44. door.close(door_loc_2);
  45. } elseif(!open_value && !open_value_2) {
  46. door.open(door_loc_2);
  47. if(player_spec) {
  48. list.add(doors, door_loc_2);
  49. sgoto(60, "closedoor");
  50. }
  51. }
  52. goto("wait");
  53. }
  54. if(player_spec) {
  55. if(!map.contains(orig_value_map, block_loc)) {
  56. map.add(orig_value_map, block_loc, open_value);
  57. }
  58. list.add(doors, block_loc);
  59. sgoto(60, "closedoor");
  60. }
  61. }
  62. goto("wait");
  63. @closedoor
  64. door_loc = list.getIndex(doors, 0);
  65. list.removeIndex(doors, 0);
  66. block = block.get(door_loc);
  67. if(block.hasTag(auto_door_tag, block)) {
  68. //close door
  69. open_value = door.isOpen(door_loc);
  70. if(block.hasTag(doors_tag, block)) {
  71. if(open_value) {
  72. door.close(door_loc);
  73. }
  74. goto("wait");
  75. }
  76. //toggle door
  77. orig_value = map.get(orig_value_map, door_loc);
  78. if(orig_value == null) {
  79. goto("wait");
  80. }
  81. map.remove(orig_value_map, door_loc);
  82. if(orig_value == open_value) {
  83. goto("wait");
  84. }
  85. if(open_value) {
  86. door.close(door_loc);
  87. goto("wait");
  88. }
  89. door.open(door_loc);
  90. }
  91. goto("wait");
  92. function door.isOpen(location) {
  93. return block.property.getValue(location, $open_prop);
  94. }
  95. function door.open(location) {
  96. block.property.setBool(location, $open_prop, true);
  97. //half
  98. half_value = block.property.getValue(location, $half_prop);
  99. if(half_value != null) {
  100. if(half_value == "upper") {
  101. half_loc = loc.mod(location, 0, -1, 0);
  102. } else {
  103. half_loc = loc.mod(location, 0, 1, 0);
  104. }
  105. block.property.setBool(half_loc, $open_prop, true);
  106. }
  107. //sound
  108. block = block.get(location);
  109. if(block.hasTag($doors_tag, block)) {
  110. sound.spawn(location, $open_door_sound, $block_sound_category);
  111. } elseif(block.hasTag($trapdoors_tag, block)) {
  112. sound.spawn(location, $open_trapdoor_sound, $block_sound_category);
  113. } elseif(block.hasTag($fencegates_tag, block)) {
  114. sound.spawn(location, $open_fencegate_sound, $block_sound_category);
  115. }
  116. }
  117. function door.close(location) {
  118. block.property.setBool(location, $open_prop, false);
  119. //half
  120. half_value = block.property.getValue(location, $half_prop);
  121. if(half_value != null) {
  122. if(half_value == "upper") {
  123. half_loc = loc.mod(location, 0, -1, 0);
  124. } else {
  125. half_loc = loc.mod(location, 0, 1, 0);
  126. }
  127. block.property.setBool(half_loc, $open_prop, false);
  128. }
  129. //sound
  130. block = block.get(location);
  131. if(block.hasTag($doors_tag, block)) {
  132. sound.spawn(location, $close_door_sound, $block_sound_category);
  133. } elseif(block.hasTag($trapdoors_tag, block)) {
  134. sound.spawn(location, $close_trapdoor_sound, $block_sound_category);
  135. } elseif(block.hasTag($fencegates_tag, block)) {
  136. sound.spawn(location, $close_fencegate_sound, $block_sound_category);
  137. }
  138. }