public void callRedstoneEvent(BlockRedstoneEvent event) {
    boolean on = event.getNewCurrent() > event.getOldCurrent();

    for (BlockFace direction :
        Arrays.asList(
            BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP)) {
      Block block = event.getBlock().getRelative(direction);
      if (SignUtil.isSign(block)) {
        Sign sign = SignUtil.getSign(block);
        List<SignMechanic> list =
            redstoneSignMechanicMap.get(SignUtil.getMechanicsIdentifier(sign));
        if (list == null) {
          list = new ArrayList<SignMechanic>();
        }
        if (redstoneSignMechanicMap.containsKey(null)) {
          list.addAll(redstoneSignMechanicMap.get(null));
        }
        for (SignMechanic mechanic : list) {
          try {
            BlockBagHandler bag = null;
            BlockMap map = null;
            if (mechanic.hasBlockMapper()) {
              map = mechanic.mapBlocks(sign);
            }
            if (mechanic.hasBlockBag()) {
              bag = BlockBagHandler.locate(sign);
            }
            SignMechanicEventData data = new SignMechanicEventData(map, bag);
            if (on) {
              mechanic.onSignPowerOn(data);
            } else {
              mechanic.onSignPowerOff(data);
            }
          } catch (BlockMapException e) {
            BetterMechanics.log(e.getMessage());
          } catch (BlockBagException e) {
            BetterMechanics.log(e.getMessage());
          }
        }
      } else {
        List<BlockMechanic> list = redstoneBlockMechanicMap.get(block.getType());
        if (list == null) {
          list = new ArrayList<BlockMechanic>();
        }
        if (blockMechanicMap.containsKey(null)) {
          list.addAll(blockMechanicMap.get(null));
        }
        for (BlockMechanic mechanic : list) {
          if (on) {
            mechanic.onBlockPowerOn(event.getBlock());
          } else {
            mechanic.onBlockPowerOff(event.getBlock());
          }
        }
      }
    }
  }
  public BMListener(BetterMechanics plugin) {
    mechanicsHandler = plugin.getMechanicsHandler();

    for (SignMechanicListener listener : mechanicsHandler.getSignMechanics()) {
      for (String identifier : listener.getIdentifiers()) {
        idMap.put(identifier.toLowerCase(), new Pair<String>(identifier, listener.getName()));
      }
    }
  }
  public void addMechanic(IMechanic mechanic) {
    // TODO: check if this list works as it's supposed to (with passing a reference)
    if (mechanic instanceof SignMechanic) {
      SignMechanic signMechanic = (SignMechanic) mechanic;
      if (signMechanic.isTriggeredByRedstone()) {
        for (String identifier : signMechanic.getIdentifier()) {
          ArrayList<SignMechanic> list = redstoneSignMechanicMap.get(identifier);
          if (list == null) {
            list = new ArrayList<SignMechanic>();
            list.add(signMechanic);
            redstoneSignMechanicMap.put(identifier, list);
          } else {
            list.add(signMechanic);
            redstoneSignMechanicMap.put(identifier, list);
          }
        }
      } else {
        for (String identifier : signMechanic.getIdentifier()) {
          ArrayList<SignMechanic> list = signMechanicMap.get(identifier);
          if (list == null) {
            list = new ArrayList<SignMechanic>();
            list.add(signMechanic);
            signMechanicMap.put(identifier, list);
          } else {
            list.add(signMechanic);
          }
        }
      }
    } else if (mechanic instanceof BlockMechanic) {
      BlockMechanic blockMechanic = (BlockMechanic) mechanic;
      if (blockMechanic.isTriggeredByRedstone()) {
        for (Material target : blockMechanic.getMechanicTarget()) {
          ArrayList<BlockMechanic> list = redstoneBlockMechanicMap.get(target);
          if (list == null) {
            list = new ArrayList<BlockMechanic>();
            list.add(blockMechanic);
            redstoneBlockMechanicMap.put(target, list);
          } else {
            list.add(blockMechanic);
            redstoneBlockMechanicMap.put(target, list);
          }
        }
      } else {
        for (Material target : blockMechanic.getMechanicTarget()) {
          ArrayList<BlockMechanic> list = blockMechanicMap.get(target);
          if (list == null) {
            list = new ArrayList<BlockMechanic>();
            list.add(blockMechanic);
            blockMechanicMap.put(target, list);
          } else {
            list.add(blockMechanic);
          }
        }
      }
    }

    if (mechanic instanceof ICommandableMechanic) {
      ICommandableMechanic commandableMechanic = (ICommandableMechanic) mechanic;
      if (commandableMechanicMap.containsKey(commandableMechanic.getName())) {
        BetterMechanics.log(
            "Mechanic: "
                + commandableMechanic.getName()
                + " tried to register a command that has already been registered!",
            Level.SEVERE);
      } else {
        commandableMechanicMap.put(commandableMechanic.getName(), commandableMechanic);
      }
    }
  }
 public void callPlayerInteractEvent(PlayerInteractEvent event) {
   if (SignUtil.isSign(event.getClickedBlock())) {
     Sign sign = SignUtil.getSign(event.getClickedBlock());
     String id = SignUtil.getMechanicsIdentifier(sign);
     List<SignMechanic> mechanicsList = signMechanicMap.get(id);
     if (mechanicsList == null) {
       mechanicsList = new ArrayList<SignMechanic>();
     }
     if (signMechanicMap.containsKey(null)) {
       mechanicsList.addAll(signMechanicMap.get(null));
     }
     if (redstoneSignMechanicMap.containsKey(id)) {
       mechanicsList.addAll(redstoneSignMechanicMap.get(id));
     }
     if (redstoneSignMechanicMap.containsKey(null)) {
       mechanicsList.addAll(redstoneSignMechanicMap.get(null));
     }
     for (SignMechanic mechanic : mechanicsList) {
       try {
         BlockBagHandler bag = null;
         BlockMap map = null;
         if (mechanic.hasBlockMapper()) {
           map = mechanic.mapBlocks(sign);
         }
         if (mechanic.hasBlockBag()) {
           bag = BlockBagHandler.locate(sign);
         }
         SignMechanicEventData data = new SignMechanicEventData(map, bag);
         if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
           mechanic.onPlayerLeftClickSign(event.getPlayer(), data);
         } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
           mechanic.onPlayerRightClickSign(event.getPlayer(), data);
         }
       } catch (BlockMapException e) {
         event.getPlayer().sendMessage(ChatColor.DARK_RED + e.getMessage());
         BetterMechanics.log(e.getMessage());
       } catch (BlockBagException e) {
         event.getPlayer().sendMessage(ChatColor.DARK_RED + e.getMessage());
         BetterMechanics.log(e.getMessage());
       }
     }
   } else {
     List<BlockMechanic> mechanicsList = blockMechanicMap.get(event.getClickedBlock().getType());
     if (mechanicsList == null) {
       mechanicsList = new ArrayList<BlockMechanic>();
     }
     if (blockMechanicMap.containsKey(null)) {
       mechanicsList.addAll(blockMechanicMap.get(null));
     }
     if (redstoneBlockMechanicMap.containsKey(event.getClickedBlock().getType())) {
       mechanicsList.addAll(redstoneBlockMechanicMap.get(event.getClickedBlock().getType()));
     }
     if (redstoneBlockMechanicMap.containsKey(null)) {
       mechanicsList.addAll(redstoneBlockMechanicMap.get(null));
     }
     for (BlockMechanic mechanic : mechanicsList) {
       if (mechanic.getMechanicActivator() == null
           || mechanic
               .getMechanicActivator()
               .contains(event.getPlayer().getItemInHand().getType())) {
         if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
           mechanic.onBlockLeftClick(event.getPlayer(), event.getClickedBlock());
         } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
           mechanic.onBlockRightClick(event.getPlayer(), event.getClickedBlock());
         }
       }
     }
   }
 }
 public BMBlockListener(BetterMechanics bm) {
   plugin = bm;
   mechanicsHandler = bm.getMechanicsHandler();
 }
Example #6
0
/**
 * Created by IntelliJ IDEA.
 *
 * @author Edoxile
 */
public class Lift extends SignMechanicListener {
  private final String[] identifiers = new String[] {"[Lift Up]", "[Lift Down]"};
  private final String[] passiveIdentifiers = new String[] {"[Lift]"};

  private ConfigHandler.LiftConfig config =
      BetterMechanics.getInstance().getConfigHandler().getLiftConfig();

  @Override
  public void onPlayerRightClickSign(PlayerEvent event) {
    String activator = event.getMechanicIdentifier();
    BlockFace direction =
        activator.equals("[Lift Up]")
            ? BlockFace.UP
            : (activator.equals("[Lift Down]") ? BlockFace.DOWN : null);
    int maxSearchHeight = config.getMaxSearchHeight();
    if (direction == BlockFace.DOWN || direction == BlockFace.UP) {
      Block block;
      for (int dy = 1; dy < maxSearchHeight; dy++) {
        block = event.getBlock().getRelative(0, (direction == BlockFace.UP ? dy : -dy), 0);
        if (SignUtil.isSign(block)) {
          String id = SignUtil.getMechanicsIdentifier(SignUtil.getSign(block));
          if (id.equals("[Lift]") || id.equals("[Lift Up]") || id.equals("[Lift Down]")) {
            movePlayer(block, event.getPlayer());
          }
        }
      }
    }
  }

  @Override
  public boolean hasBlockMapper() {
    return false;
  }

  @Override
  public boolean hasBlockBag() {
    return false;
  }

  @Override
  public String[] getIdentifiers() {
    return identifiers;
  }

  @Override
  public String[] getPassiveIdentifiers() {
    return passiveIdentifiers;
  }

  @Override
  public boolean isTriggeredByRedstone() {
    return false;
  }

  @Override
  public boolean isTriggeredByPlayer() {
    return true;
  }

  @Override
  public Material[] getMechanicActivators() {
    return voidActor;
  }

  @Override
  public String getName() {
    return "Lift";
  }

  @Override
  public boolean isEnabled() {
    return config.isEnabled();
  }

  protected void movePlayer(Block block, Player player) {
    //
  }
}