/src/mechanisms/com/sk89q/craftbook/mech/LightSwitch.java

https://github.com/SquallSeeD31/craftbook · Java · 164 lines · 92 code · 20 blank · 52 comment · 27 complexity · f3a784cee17e1f15fb7f91b7057e1ad6 MD5 · raw file

  1. // $Id$
  2. /*
  3. * CraftBook
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.craftbook.mech;
  20. import java.util.logging.Logger;
  21. import org.bukkit.World;
  22. import org.bukkit.block.*;
  23. import org.bukkit.event.block.*;
  24. import com.sk89q.craftbook.*;
  25. import com.sk89q.craftbook.bukkit.BukkitUtil;
  26. import com.sk89q.craftbook.bukkit.MechanismsPlugin;
  27. import com.sk89q.craftbook.util.BlockWorldVector;
  28. import com.sk89q.craftbook.util.HistoryHashMap;
  29. import com.sk89q.craftbook.util.WorldVector;
  30. import com.sk89q.worldedit.BlockVector;
  31. import com.sk89q.worldedit.blocks.BlockID;
  32. /**
  33. * Handler for Light switches. Toggles all torches in the area from being redstone
  34. * to normal torches. This is done every time a sign with [|] or [I] is right
  35. * clicked by a player.
  36. *
  37. * @author fullwall
  38. */
  39. public class LightSwitch extends Mechanic {
  40. public static class Factory implements MechanicFactory<LightSwitch> {
  41. protected MechanismsPlugin plugin;
  42. public Factory(MechanismsPlugin plugin) {
  43. this.plugin = plugin;
  44. }
  45. @Override
  46. public LightSwitch detect(BlockWorldVector pt) {
  47. Block block = pt.toBlock();
  48. // check if this looks at all like something we're interested in first
  49. if (block.getTypeId() != BlockID.WALL_SIGN)
  50. return null;
  51. String line = ((Sign) block.getState()).getLine(1);
  52. if (!line.equalsIgnoreCase("[|]") && !line.equalsIgnoreCase("[I]"))
  53. return null;
  54. // okay, now we can start doing exploration of surrounding blocks
  55. // and if something goes wrong in here then we throw fits.
  56. return new LightSwitch(pt, plugin);
  57. }
  58. }
  59. /**
  60. * Store a list of recent light toggles to prevent spamming. Someone
  61. * clever can just use two signs though.
  62. */
  63. private HistoryHashMap<BlockWorldVector,Long> recentLightToggles = new HistoryHashMap<BlockWorldVector,Long>(20);
  64. /**
  65. * Configuration.
  66. */
  67. protected MechanismsPlugin plugin;
  68. private BlockWorldVector pt;
  69. /**
  70. * Construct a LightSwitch for a location.
  71. *
  72. * @param pt
  73. * @param plugin
  74. */
  75. private LightSwitch(BlockWorldVector pt, MechanismsPlugin plugin) {
  76. super();
  77. this.pt = pt;
  78. this.plugin = plugin;
  79. }
  80. @Override
  81. public void onRightClick(BlockRightClickEvent event) {
  82. if (!BukkitUtil.toWorldVector(event.getBlock()).equals(pt)) return; //wth? our manager is insane
  83. toggleLights(pt);
  84. }
  85. /**
  86. * Toggle lights in the immediate area.
  87. *
  88. * @param pt
  89. * @return
  90. */
  91. private boolean toggleLights(BlockWorldVector pt) {
  92. Logger log = Logger.getLogger("Minecraft");
  93. World world = pt.getWorld();
  94. int wx = pt.getBlockX();
  95. int wy = pt.getBlockY();
  96. int wz = pt.getBlockZ();
  97. int aboveID = world.getBlockTypeIdAt(wx, wy + 1, wz);
  98. if (aboveID == BlockID.TORCH || aboveID == BlockID.REDSTONE_TORCH_OFF
  99. || aboveID == BlockID.REDSTONE_TORCH_ON) {
  100. // Check if block above is a redstone torch.
  101. // Used to get what to change torches to.
  102. boolean on = (aboveID != BlockID.TORCH);
  103. // Prevent spam
  104. Long lastUse = recentLightToggles.remove(pt);
  105. long currTime = System.currentTimeMillis();
  106. if (lastUse != null && currTime - lastUse < 500) {
  107. recentLightToggles.put(pt, lastUse);
  108. return true;
  109. }
  110. recentLightToggles.put(pt, currTime);
  111. int changed = 0;
  112. for (int x = -10 + wx; x <= 10 + wx; x++) {
  113. for (int y = -10 + wy; y <= 10 + wy; y++) {
  114. for (int z = -5 + wz; z <= 5 + wz; z++) {
  115. int id = world.getBlockTypeIdAt(x, y, z);
  116. if (id == BlockID.TORCH || id == BlockID.REDSTONE_TORCH_OFF
  117. || id == BlockID.REDSTONE_TORCH_ON) {
  118. // Limit the maximum number of changed lights
  119. if (changed >= 20) {
  120. return true;
  121. }
  122. if (on) {
  123. world.getBlockAt(x, y, z).setTypeId(BlockID.TORCH);
  124. } else {
  125. world.getBlockAt(x, y, z).setTypeId(BlockID.REDSTONE_TORCH_ON);
  126. }
  127. changed++;
  128. }
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. return false;
  135. }
  136. @Override
  137. public void unload() {
  138. /* No persistence. */
  139. }
  140. @Override
  141. public boolean isActive() {
  142. return false; /* Keeps no state */
  143. }
  144. }