public static List<Location<World>> getAdjacentExcept(Location<World> location, Direction ... directions) { List<Location<World>> locations = new ArrayList<>(); for(Direction direction : getDirectFaces()) { boolean passes = true; for(Direction direction1 : directions) { if(direction1 == direction) { passes = false; break; } } if (passes) { locations.add(location.getRelative(direction)); } } return locations; }
/** * Gets whether or not the specified {@link BlockState} passes the {@link SpongeBlockFilter}s. * * @param filters The filters * @param state The state to test * @return If it passes */ public static boolean doesStatePassFilters(Collection<SpongeBlockFilter> filters, BlockState state) { for(SpongeBlockFilter filter : filters) if(doesStatePassFilter(filter, state)) return true; return false; }
@Override public List<Location<World>> findPotentialOutputs(Location<World> location, ItemStack itemStack, Direction inputSide) { return BlockUtil.getAdjacentExcept(location, inputSide); } }
public TernaryState isActive(Location<World> block) { boolean isConnected = false; for (Direction direction : BlockUtil.getDirectHorizontalFaces()) { switch (BlockUtil.isPowered(block, direction)) { case TRUE: return TernaryState.TRUE; case NONE: break; case FALSE: isConnected = true; break; } } return isConnected ? TernaryState.FALSE : TernaryState.NONE; }
public static TernaryState isPowered(Location<World> block, Direction face) { Location<World> pow = block.getRelative(face); if (isPowerSource(pow)) { if (getBlockPowerLevel(pow).orElse(0) > 0 || getBlockIndirectPowerLevel(pow).orElse(0) > 0) return TernaryState.TRUE; return TernaryState.FALSE; } return TernaryState.NONE; }
@Test public void getFacingTest() { for (Direction direction : BlockUtil.getDirectFaces()) { Location loc = new Location<>(mock(World.class), 0, 0, 0); assertTrue(BlockUtil.getFacing(loc, loc.getRelative(direction)) == direction); } } }
@Listener public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) { if (event.getCause().containsType(PluginContainer.class)) { return; //Ignore plugin caused breaks. } if(ItemUtil.doesStackPassFilters(allowedItems.getValue(), player.getItemInHand(HandTypes.MAIN_HAND).orElse(null)) && !disabledPlayers.getValue().contains(player.getUniqueId())) { event.getTransactions().stream().filter((t) -> BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), t.getOriginal().getState())).forEach(transaction -> { Optional<TreeType> treeType = transaction.getOriginal().get(Keys.TREE_TYPE); treeType.ifPresent(treeType1 -> transaction.getOriginal().getLocation().ifPresent(location -> { List<Location> traversed = Lists.newArrayList(location); for(Direction dir : BlockUtil.getDirectFaces()) { checkBlocks(location.getRelative(dir), player, treeType1, traversed); } })); }); } }
if(!BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), baseBlock.getBlock())) { if (block.getBlockType() != BlockTypes.WALL_SIGN) { bridgeDirection = Direction.UP; baseBlock = block.getRelative(bridgeDirection); if(!BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), baseBlock.getBlock())) { if (human instanceof CommandSource) ((CommandSource) human).sendMessage(notAllowedMaterial); return true; Location<World> otherSide = BlockUtil.getNextMatchingSign(block, SignUtil.getBack(block), maximumLength.getValue(), this::isMechanicSign); if (otherSide == null) { if (human instanceof CommandSource) ((CommandSource) human).sendMessage(missingOtherEnd); leftBlocks = BlockUtil.getMinimumLength(left, otherLeft, baseBlock.getBlock(), SignUtil.getLeft(block), maximumWidth.getValue()); rightBlocks = BlockUtil.getMinimumLength(right, otherRight, baseBlock.getBlock(), SignUtil.getRight(block), maximumWidth.getValue());
GateColumn(Location<World> topBlock, ConfigValue<List<SpongeBlockFilter>> allowedBlocks) { while (BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), topBlock.getBlock())) { topBlock = topBlock.getRelative(Direction.UP); } topBlock = topBlock.getRelative(Direction.DOWN); this.topBlock = topBlock; }
public static Optional<Integer> getBlockIndirectPowerLevel(Location<?> block) { if (getBlockPowerLevel(block).isPresent()) { return getBlockPowerLevel(block); } else if (block.getProperty(IndirectlyPoweredProperty.class).isPresent()) { return Optional.of(block.getProperty(IndirectlyPoweredProperty.class).get().getValue() ? 15 : 0); } return Optional.empty(); }
@Listener public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock source) { if(isValid(source.getLocation())) { boolean wasPowered = getState(source.getLocation()); List<Location<World>> locations = BlockUtil.getAdjacentExcept(source.getLocation(), Direction.NONE); locations.add(source.getLocation()); Optional<Integer> power = BlockUtil.getDirectBlockPowerLevel(locations.toArray(EMPTY_LOCATION_ARRAY)); power.ifPresent(integer -> { boolean isPowered = integer > 0; if (isPowered != wasPowered) { updateState(source.getLocation(), isPowered); } }); } } }
private ItemStack doPipeIteration(Location<World> location, ItemStack itemStack, Direction fromDirection, Set<Location<World>> traversed) { if(traversed.contains(location)) return itemStack; traversed.add(location); if(itemStack.getQuantity() == 0) return itemStack; PipePart pipePart = getPipePart(location); if(pipePart == null) return itemStack; for (Location<World> location1 : pipePart.findPotentialOutputs(location, itemStack, fromDirection)) { if (pipePart.validateOutput(location, location1, itemStack)) { if (pipePart instanceof OutputPipePart) { Optional<Inventory> inventory = LocationUtil.getInventoryForLocation(location1); if (inventory.isPresent()) { InventoryTransactionResult result = inventory.get().offer(itemStack); if (!result.getRejectedItems().isEmpty()) { for (ItemStackSnapshot snapshot : result.getRejectedItems()) { itemStack = snapshot.createStack(); } } else { itemStack.setQuantity(0); } } } else itemStack = doPipeIteration(location1, itemStack, BlockUtil.getFacing(location1, location), traversed); } } return itemStack; }
if (!BlockUtil.isTrack(block.getBlockType())) { return Optional.empty(); } else { Location<World> sign = null; for (Direction direction : BlockUtil.getDirectHorizontalFaces()) { if (SignUtil.isSign(base.getRelative(direction))) { sign = base.getRelative(direction);
if(!BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), baseBlock.getBlock())) { if (human instanceof CommandSource) ((CommandSource) human).sendMessage(Text.builder("Can't use this material for a door!").build()); return true; Location<World> otherSide = BlockUtil.getNextMatchingSign(block, back, maximumLength.getValue(), this::isMechanicSign); if (otherSide == null) { if (human instanceof CommandSource) ((CommandSource) human).sendMessage(missingOtherEnd);
public static Optional<Integer> getDirectBlockPowerLevel(Location<?> ... blocks) { int power = -1; for(Location<?> block : blocks) { Optional<Integer> optional = getDirectBlockPowerLevel(block); if(optional.isPresent()) { if (power < optional.get()) { power = optional.get(); } } } if (power == -1) { return Optional.empty(); } else { return Optional.of(power); } }
@Override public BlockState deserialize(TypeToken<?> type, ConfigurationNode value) { return BlockUtil.getBlockStateFromString(value.getString()); }
@Override public boolean isValid(Location<World> location) { return BlockUtil.doesStatePassFilters(allowedBlocks.getValue(), location.getBlock()) && (location.getBlockY() == 0 || !location.getRelative(Direction.DOWN).getBlockType().equals(BlockTypes.AIR)) && location.getRelative(Direction.UP).getBlockType().equals(BlockTypes.AIR); }
@Listener public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock source) { if(!allowRedstone.getValue()) return; Location<World> block = source.getLocation(); if (isValid(block)) { Player player = event.getCause().first(Player.class).orElse(null); boolean isPowered = BlockUtil.getBlockPowerLevel(block).orElse(0) > 0; boolean wasPowered = block.get(CraftBookKeys.LAST_POWER).orElse(0) > 0; if (isPowered && !wasPowered) { triggerMechanic(block, player); } block.offer(new LastPowerData(isPowered ? 15 : 0)); } }
public static Optional<Integer> getBlockPowerLevel(Location<?> block) { if (getDirectBlockPowerLevel(block).isPresent()) { return getDirectBlockPowerLevel(block); } else if (block.getProperty(PoweredProperty.class).isPresent()) { return Optional.of(block.getProperty(PoweredProperty.class).get().getValue() ? 15 : 0); } return Optional.empty(); }
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; }