/** * Checks if this {@link CartMechanismBlocks} contains a sign. * * @return If it contains a sign */ public boolean hasSign() { return sign != null && SignUtil.isSign(sign); }
public static Location<World> getNextMatchingSign(Location<World> block, Direction back, int maximumLength, Predicate<Sign> predicate) { for (int i = 0; i < maximumLength; i++) { block = block.getRelative(back); if (SignUtil.isSign(block)) { Sign sign = (Sign) block.getTileEntity().get(); if (predicate.test(sign)) { return block; } } } return null; }
@Override public boolean isValid(Location<World> location) { if (SignUtil.isSign(location)) { Sign sign = (Sign) location.getTileEntity().get(); return isMechanicSign(sign); } return false; }
public static List<Location<World>> getAttachedSigns(Location<World> block) { List<Location<World>> attachedSigns = Lists.newArrayList(); for (Direction directFace : BlockUtil.getDirectFaces()) { if (SignUtil.isSign(block.getRelative(directFace))) { attachedSigns.add(block.getRelative(directFace)); } } return attachedSigns; }
@Override public boolean isValid(Location<World> location) { return SignUtil.isSign(location) && ICManager.getICType(SignUtil.getTextRaw((Sign) location.getTileEntity().get(), 1)) != null; }
@Override public boolean isValid(Location<World> location) { if (location.getBlockY() >= 1) { Location<World> below = location.getRelative(Direction.DOWN); if (SignUtil.isSign(below)) { Sign sign = (Sign) below.getTileEntity().get(); return isMechanicSign(sign); } } return false; }
if (SignUtil.isSign(below)) { sign = below.getTileEntity().map(tile -> (Sign) tile).get(); } else {
@Override public boolean isValid(Location<World> location) { if (location.getBlock().equals(block.getValue())) { if (requireSign.getValue()) { for (Location<World> attachedSign : SignUtil.getAttachedSigns(location)) { if (isMechanicSign(attachedSign.getTileEntity().map(tile -> (Sign) tile).get())) { return true; } } } else { return true; } } else { return SignUtil.isSign(location) && isMechanicSign(location.getTileEntity().map(tile -> (Sign) tile).get()); } return false; }
public static Location<World> getNextSign(Location<World> sign, String criterea, int searchRadius) { Location<World> otherBlock = sign; Direction way = getBack(sign); boolean found = false; for (int i = 0; i < searchRadius; i++) { if (isSign(otherBlock.getRelative(way))) { otherBlock = otherBlock.getRelative(way); if(getTextRaw((Sign) otherBlock.getTileEntity().get(), 1).equalsIgnoreCase(criterea)) { found = true; break; } } else otherBlock = otherBlock.getRelative(way); } if (!found) return null; return otherBlock; }
@Override public BlockBag getBlockBag(Location<World> location) { BlockBag mainBlockBag = super.getBlockBag(location); if (SignUtil.isSign(location)) { Direction back = "[Door Up]".equals(SignUtil.getTextRaw((Sign) location.getTileEntity().get(), 1)) ? Direction.UP : Direction.DOWN; Location<World> next = BlockUtil.getNextMatchingSign(location, back, maximumLength.getValue() + 2, this::isMechanicSign); if (next != null) { BlockBag nextBlockBag = super.getBlockBag(next); if (nextBlockBag != null) { return new MultiBlockBag(mainBlockBag, nextBlockBag); } } } return mainBlockBag; }
/** * Gets whether or not the block at this location is a sign. * * @param block The location to check * @return If it is a sign */ public static boolean isSign(Location<World> block) { if (isSign(block.getBlock())) { if (!block.getTileEntity().isPresent()) { CraftBookAPI.inst().getLogger().warn("Corrupted tile entity (Sign) at " + block.getBlockPosition() + " in world " + block.getExtent().getName()); return false; } return true; } return false; }
private boolean hasSign(Location<World> location, List<Location<World>> searched, Location<World> original) { boolean found = false; for (Direction face : BlockUtil.getDirectFaces()) { Location<World> otherBlock = location.getRelative(face); if (searched.contains(otherBlock)) continue; searched.add(otherBlock); if (found) break; if (location.getPosition().distanceSquared(original.getPosition()) > Math.pow(maxSignDistance.getValue(), 2)) continue; if (SignUtil.isSign(otherBlock) && SignUtil.getFront(otherBlock) == face) { found = true; break; } if (Objects.equals(location.getBlockType(), otherBlock.getBlockType())) { found = hasSign(otherBlock, searched, original); } } return found; }
/** * Find a {@link CartMechanismBlocks} from an unknown starting block. * * @param block The block to search from. * @return The CartMechanismBlocks, if found */ public static Optional<CartMechanismBlocks> find(Location<World> block) { if (SignUtil.isSign(block)) { return findBySign(block); } else if (BlockUtil.isTrack(block.getBlockType())) { return findByRail(block); } else { return findByBase(block); } }
@Listener public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) { event.getTargetBlock().getLocation().ifPresent((location) -> { Location<World> signLocation = location; if (allowButtonLifts.getValue() && signLocation.getBlockType() == BlockTypes.STONE_BUTTON || signLocation.getBlockType() == BlockTypes.WOODEN_BUTTON) { Direction backDir = SignUtil.getBack(signLocation); signLocation = signLocation.getRelative(backDir).getRelative(backDir); } if (SignUtil.isSign(signLocation)) { Sign sign = (Sign) signLocation.getTileEntity().get(); Optional<Vector3d> interactionPoint = event.getInteractionPoint(); boolean down = "[Lift Down]".equals(SignUtil.getTextRaw(sign, 1)) || ("[Lift UpDown]".equals(SignUtil.getTextRaw(sign, 1)) && interactionPoint.isPresent() && interactionPoint.get().getY() < 0.5); if (down || "[Lift Up]".equals(SignUtil.getTextRaw(sign, 1)) || ("[Lift UpDown]".equals(SignUtil.getTextRaw(sign, 1)) && interactionPoint.isPresent() && interactionPoint.get().getY() > 0.5)) { if (!usePermissions.hasPermission(player)) { player.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this mechanic!")); return; } transportEntity(player, location, down ? Direction.DOWN : Direction.UP); } } }); }
@Listener public void onInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) { event.getTargetBlock().getLocation().ifPresent((location) -> { if (SignUtil.isSign(location)) { Sign sign = (Sign) location.getTileEntity().get(); if (isMechanicSign(sign)) { if (!player.hasPermission(usePermission.getNode())) { player.sendMessage(USE_PERMISSIONS); return; } String variable = SignUtil.getTextRaw(sign, 2); String line3 = SignUtil.getTextRaw(sign, 3); String var = Variables.instance.getVariable(line3.isEmpty() ? Variables.GLOBAL_NAMESPACE : line3, variable); if(var == null || var.isEmpty()) var = "variable.missing"; player.sendMessage(Text.of(var)); event.setCancelled(true); } } }); }
@Listener public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock source) { if(!allowRedstone.getValue()) return; if(!SignUtil.isSign(source.getLocation())) return; Location<World> block = source.getLocation(); Sign sign = (Sign) block.getTileEntity().get(); if (isMechanicSign(sign)) { Player player = event.getCause().first(Player.class).orElse(null); if(player != null) { if(!usePermissions.hasPermission(player)) { player.sendMessage(USE_PERMISSIONS); return; } } boolean isPowered = BlockUtil.getBlockPowerLevel(block).orElse(0) > 0; boolean wasPowered = block.get(CraftBookKeys.LAST_POWER).orElse(0) > 0; if (isPowered != wasPowered) { triggerMechanic(block, sign, player, isPowered); block.offer(new LastPowerData(isPowered ? 15 : 0)); } } }
@Listener public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock source) { if(!allowRedstone.getValue()) return; if(!SignUtil.isSign(source.getLocation())) return; Location<World> block = source.getLocation(); Sign sign = (Sign) block.getTileEntity().get(); if (isMechanicSign(sign)) { Player player = event.getCause().first(Player.class).orElse(null); if(player != null) { if(!usePermissions.hasPermission(player)) { player.sendMessage(USE_PERMISSIONS); return; } } boolean isPowered = BlockUtil.getBlockPowerLevel(block).orElse(0) > 0; boolean wasPowered = block.get(CraftBookKeys.LAST_POWER).orElse(0) > 0; if (isPowered != wasPowered) { triggerMechanic(block, sign, isPowered); block.offer(new LastPowerData(isPowered ? 15 : 0)); } } }
@Listener public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock source) { if(!redstoneFuel.getValue()) return; if(!SignUtil.isSign(source.getLocation())) return; Location<World> block = source.getLocation(); Sign sign = (Sign) block.getTileEntity().get(); if (isMechanicSign(sign)) { Player player = event.getCause().first(Player.class).orElse(null); if(player != null) { if(!refuelPermissions.hasPermission(player)) { player.sendMessage(Text.of("You don't have permission to refuel this mechanic!")); return; } } int newPower = BlockUtil.getBlockPowerLevel(block).orElse(0); int oldPower = block.get(CraftBookKeys.LAST_POWER).orElse(0); if (newPower != oldPower) { if (newPower > oldPower) { increaseMultiplier(sign, newPower - oldPower); } block.offer(new LastPowerData(newPower)); } } }
@Listener public void onEntityMove(MoveEntityEvent event) { Vector3d fromPosition = event.getFromTransform().getPosition(); Vector3d toPosition = event.getToTransform().getPosition(); if(Math.abs(toPosition.getY() - fromPosition.getY()) > sensitivity.getValue() && fromPosition.getY() - fromPosition.getFloorY() < 0.25) { Location<World> block = event.getFromTransform().getLocation().getRelative(Direction.DOWN); if (BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), block.getBlock())) { Location<World> signBlock = block.getRelative(Direction.DOWN); if (SignUtil.isSign(signBlock)) { Sign sign = (Sign) signBlock.getTileEntity().get(); if (isMechanicSign(sign)) { doAction(event.getTargetEntity(), SignUtil.getTextRaw(sign, 2), event.getToTransform().getRotation()); } } } else { for (Map.Entry<SpongeBlockFilter, String> blockFilterStringEntry : autoBlocks.getValue().entrySet()) { if (blockFilterStringEntry.getKey().getApplicableBlocks().contains(block.getBlock())) { doAction(event.getTargetEntity(), blockFilterStringEntry.getValue(), event.getToTransform().getRotation()); } } } } }
/** * Gets the destination of an Elevator. If there is none, it returns the start. * * @param block The starting block. * @param direction The direction to move in. * @return The elevator destination. */ private Location<World> findDestination(Location<World> block, Direction direction) { int y = block.getBlockY(); if (direction == Direction.UP || direction == Direction.DOWN) { while (direction == Direction.UP ? y < 255 : y > 0) { y += direction == Direction.UP ? 1 : -1; Location<World> test = block.getExtent().getLocation(block.getBlockX(), y, block.getBlockZ()); if (allowButtonLifts.getValue() && test.getBlockType() == BlockTypes.STONE_BUTTON || test.getBlockType() == BlockTypes.WOODEN_BUTTON) { Direction backDir = SignUtil.getBack(test); test = test.getRelative(backDir).getRelative(backDir); } if (SignUtil.isSign(test)) { // It's a sign. if(isValid(test)) return test; } } } // We don't currently support non-up/down elevators, this isn't a Roald Dahl novel. return block; }