/fr/tobast/bukkit/columnwireless/BlockListener.java

https://gitlab.com/columnwireless/columnwireless · Java · 108 lines · 59 code · 13 blank · 36 comment · 17 complexity · 23e9f70f52a0f458acf97443431c41ea MD5 · raw file

  1. /*
  2. * PROGRAM:
  3. * ColumnWireless - bukkit plugin
  4. *
  5. * AUTHOR:
  6. * Théophile BASTIAN (a.k.a. Tobast)
  7. *
  8. * CONTACT & WEBSITE:
  9. * http://tobast.fr/ (contact feature included)
  10. * error-report@tobast.fr (error reporting only)
  11. *
  12. * SHORT DESCRIPTION:
  13. * See first license line.
  14. *
  15. * LICENSE:
  16. * ColumnWireless is a plugin that transmits a redstone signal vertically, wirelessly
  17. * Copyright (C) 2012 Théophile BASTIAN
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see http://www.gnu.org/licenses/gpl.txt.
  31. */
  32. package fr.tobast.bukkit.columnwireless;
  33. import org.bukkit.event.Listener;
  34. import org.bukkit.event.EventHandler;
  35. import org.bukkit.event.EventPriority;
  36. import org.bukkit.event.block.BlockRedstoneEvent;
  37. import org.bukkit.block.Block;
  38. import org.bukkit.block.BlockFace;
  39. import org.bukkit.Location;
  40. import org.bukkit.Material;
  41. import org.bukkit.material.Lever;
  42. import org.bukkit.craftbukkit.CraftWorld;
  43. //import java.util.logging.Logger; // TODO DELETE
  44. class BlockListener implements Listener {
  45. // Logger log=Logger.getLogger("Minecraft"); // TMP TODO DELETE
  46. @EventHandler(priority=EventPriority.NORMAL)
  47. public void onBlockRedstoneEvent(BlockRedstoneEvent event) {
  48. if(event.getBlock().getRelative(BlockFace.DOWN).getType() == Material.GOLD_BLOCK) {
  49. Block gold=event.getBlock().getRelative(BlockFace.DOWN);
  50. if((gold.getData() & 0x8) != 0) { // Broadcasted
  51. gold.setData((byte)(gold.getData() ^ 0x8)); // Reset broadcast byte
  52. }
  53. else {
  54. // log.info("Broadcasting to gold blocks state "+(event.getNewCurrent() > 0));
  55. broadcastVertically(event.getNewCurrent() > 0, gold);
  56. checkForReceiver(gold.getLocation(), event.getNewCurrent() > 0);
  57. }
  58. }
  59. }
  60. public void broadcastVertically(boolean currentStatus, Block originBlock) {
  61. Location loc=originBlock.getLocation();
  62. loc.setY(1);
  63. while(loc.getY() < 256)
  64. {
  65. checkForReceiver(loc, currentStatus);
  66. loc.add(0,1,0);
  67. }
  68. }
  69. public void checkForReceiver(Location loc, boolean currentStatus) {
  70. if(loc.getBlock().getType() == Material.GOLD_BLOCK) {
  71. // log.info("Received signal "+currentStatus+" at height y="+loc.getY());
  72. Block gold=loc.getBlock();
  73. if(gold.getRelative(BlockFace.NORTH).getType() == Material.LEVER)
  74. setLever(gold.getRelative(BlockFace.NORTH), currentStatus);
  75. if(gold.getRelative(BlockFace.EAST).getType() == Material.LEVER)
  76. setLever(gold.getRelative(BlockFace.EAST), currentStatus);
  77. if(gold.getRelative(BlockFace.SOUTH).getType() == Material.LEVER)
  78. setLever(gold.getRelative(BlockFace.SOUTH), currentStatus);
  79. if(gold.getRelative(BlockFace.WEST).getType() == Material.LEVER)
  80. setLever(gold.getRelative(BlockFace.WEST), currentStatus);
  81. }
  82. }
  83. public void setLever(Block b, boolean currentState) {
  84. boolean wasOn = (b.getData() & 0x8) > 0;
  85. // make sure we actually change state
  86. if(wasOn != currentState) {
  87. int datavalue=b.getData() ^ 0x8;
  88. b.setData((byte)datavalue);
  89. ((Lever)b.getState().getData()).setPowered(currentState);
  90. b.getState().update();
  91. net.minecraft.server.World w = ((net.minecraft.server.World) ((org.bukkit.craftbukkit.CraftWorld) b.getWorld()).getHandle());
  92. w.applyPhysics(b.getX(), b.getY(), b.getZ(), b.getTypeId());
  93. }
  94. }
  95. }