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; }
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; }
private static void checkBlocks(Location<World> block, Player player, TreeType type, List<Location> traversed) { if(traversed.contains(block)) return; traversed.add(block); Optional<TreeType> data = block.getBlock().get(Keys.TREE_TYPE); if(data.isPresent() && data.get().equals(type)) { //Same tree type. // TODO Switch to FakePlayer when implemented. //block.getExtent().digBlockWith(block.getBlockPosition(), player.getItemInHand(HandTypes.MAIN_HAND).orElse(null), Cause.of(NamedCause.simulated(player))); block.removeBlock(); for(Direction dir : BlockUtil.getDirectFaces()) { checkBlocks(block.getRelative(dir), player, type, traversed); } } }
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; }
@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); } })); }); } }
private boolean replaceBlocks(final boolean on, final Location<World> block, final Set<Location> traversedBlocks) { if (traversedBlocks.size() > 15000) { return true; } if (mode == 0) { for (Direction direction : BlockUtil.getDirectFaces()) { final Location<World> b = block.getRelative(direction); if (traversedBlocks.contains(b)) { continue; } traversedBlocks.add(b); if (b.getBlock().equals(blockTypeData.onBlock)) { if (!on) { b.setBlock(blockTypeData.offBlock, physics ? BlockChangeFlags.ALL : BlockChangeFlags.NONE); } Sponge.getScheduler().createTaskBuilder().delayTicks(delay).execute(() -> replaceBlocks(on, b, traversedBlocks)).submit(CraftBookPlugin.spongeInst().container); } else if (b.getBlock().equals(blockTypeData.offBlock)) { if (on) { b.setBlock(blockTypeData.onBlock, physics ? BlockChangeFlags.ALL : BlockChangeFlags.NONE); } Sponge.getScheduler().createTaskBuilder().delayTicks(delay).execute(() -> replaceBlocks(on, b, traversedBlocks)).submit(CraftBookPlugin.spongeInst().container); } } } return traversedBlocks.size() > 0; }
@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); } } }
@Override public void onThink(Location<World> signLocation) { for (Direction directFace : BlockUtil.getDirectFaces()) { Location<World> location = signLocation.getRelative(directFace); if (isValid(location)) {