public static boolean isWithinSphericalRadius(Location<World> location1, Location<World> location2, double radius) { return location1.getExtent().equals(location2.getExtent()) && Math.floor(getDistanceSquared(location1, location2)) <= radius * radius; // Floor for more accurate readings }
block = getRelativeBlock(block, right, offsetX); } else if (offsetX < 0) { block = getRelativeBlock(block, left, offsetX); block = getRelativeBlock(block, front, offsetZ); } else if (offsetZ < 0) { block = getRelativeBlock(block, back, offsetZ); block = getRelativeBlock(block, Direction.UP, offsetY); } else if (offsetY < 0) { block = getRelativeBlock(block, Direction.DOWN, offsetY);
public static Location<World> getRelativeOffset(Location<World> block, int offsetX, int offsetY, int offsetZ) { return getRelativeOffset(SignUtil.getBackBlock(block), SignUtil.getFacing(block), offsetX, offsetY, offsetZ); }
@Test public void testCartesianToEuler() { Vector3d cartesian = new Vector3d(1.0, 0.0, 0.0); Vector3d euler = LocationUtil.cartesianToEuler(cartesian); assertEquals(euler, new Vector3d(-0.0, 270.0, 0.0)); cartesian = new Vector3d(1.0, 0.0, 1.0); euler = LocationUtil.cartesianToEuler(cartesian); assertEquals(euler, new Vector3d(-0.0, 315.0, 0.0)); cartesian = new Vector3d(1.0, 0.0, -1.0); euler = LocationUtil.cartesianToEuler(cartesian); assertEquals(euler, new Vector3d(-0.0, 225.0, 0.0)); cartesian = new Vector3d(1.0, -1.0, 0.0); euler = LocationUtil.cartesianToEuler(cartesian); assertEquals(euler, new Vector3d(45.0, 270.0, 0.0)); } }
public static Location<World> parseBlockLocation(Sign sign, String line, LocationCheckType relative) { Location<World> target = SignUtil.getBackBlock(sign.getLocation()); if (line.contains("!")) relative = LocationCheckType.getTypeFromChar('!'); else if (line.contains("^")) relative = LocationCheckType.getTypeFromChar('^'); else if (line.contains("&")) relative = LocationCheckType.getTypeFromChar('&'); Vector3d offsets = new Vector3d(0,0,0); try { offsets = parseUnsafeBlockLocation(line); } catch (NumberFormatException | ArrayIndexOutOfBoundsException ignored) { } if(offsets.getFloorX() == 0 && offsets.getFloorY() == 0 && offsets.getFloorZ() == 0) return target; switch (relative) { case RELATIVE: target = LocationUtil.getRelativeOffset(sign.getLocation(), offsets.getFloorX(), offsets.getFloorY(), offsets.getFloorZ()); break; case OFFSET: target = LocationUtil.getOffset(target, offsets.getFloorX(), offsets.getFloorY(), offsets.getFloorZ()); break; case ABSOLUTE: target = target.getExtent().getLocation(offsets.getX(), offsets.getY(), offsets.getZ()); break; } return target; }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> offset = dispenser.getLocation().getRelative(face); int distance = 0; while (offset.getBlockType() == BlockTypes.AIR && distance < 5) { for (Entity e : LocationUtil.getEntitiesAtLocation(offset)) { e.setVelocity(face.asOffset().mul(5 - distance).mul(-1.0)); } distance ++; offset = offset.getRelative(face); } return true; } return false; } }
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; }
public static Collection<Entity> getEntitiesAtLocation(Location<World> location) { Chunk chunk = location.getExtent().getChunk(location.getChunkPosition()).orElse(null); if (chunk == null) { return Collections.emptyList(); } return chunk.getEntities(entity -> isEntityInLocation(location, entity)); }
int xp = 0; for (Entity entity : LocationUtil.getNearbyEntities(location, new Vector3d(distance, distance, distance))) { if (entity instanceof ExperienceOrb) { experienceOrbs.add((ExperienceOrb) entity);
@Listener public void onEntityMove(MoveEntityEvent event) { if(!allowJumpLifts.getValue()) return; if(!LocationUtil.isLocationWithinWorld(event.getToTransform().getLocation())) return; Location<World> groundLocation = event.getToTransform().getLocation().getRelative(Direction.DOWN); //Look for dat sign for (Location<World> location : SignUtil.getAttachedSigns(groundLocation)) { Sign sign = (Sign) location.getTileEntity().orElse(null); if (sign != null && "[Lift UpDown]".equals(SignUtil.getTextRaw(sign, 1))) { if (event.getTargetEntity() instanceof Subject) { if (!usePermissions.hasPermission((Subject) event.getTargetEntity())) { return; } } if (event.getToTransform().getPosition().getY() > event.getFromTransform().getPosition().getY()) { transportEntity(event.getTargetEntity(), location, Direction.UP); //Jump is up } else if (event.getTargetEntity().get(Keys.IS_SNEAKING).orElse(false)) { transportEntity(event.getTargetEntity(), location, Direction.DOWN); //Sneak is down } break; } } }
private void addChair(Player player, Location<World> location) { Entity entity = location.getExtent().createEntity(EntityTypes.ARMOR_STAND, location.getBlockPosition().toDouble().sub(-0.5, 1.2, -0.5)); entity.offer(Keys.INVISIBLE, true); entity.offer(Keys.HAS_GRAVITY, false); if (faceCorrectDirection.getValue() && location.supports(Keys.DIRECTION)) { Vector3d euler = LocationUtil.cartesianToEuler(location.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite().asOffset()); entity.setRotation(euler); player.setRotation(euler); } Sponge.getCauseStackManager().pushCause(player); location.getExtent().spawnEntity(entity); Sponge.getCauseStackManager().popCause(); Chair<?> chair = new Chair<>((ArmorStand) entity, location, player.getLocation()); entity.addPassenger(player); player.sendMessage(Text.of(TextColors.YELLOW, "You sit down!")); chairs.put(player.getUniqueId(), chair); }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> offset = dispenser.getLocation().getRelative(face); int distance = 0; while (offset.getBlockType() == BlockTypes.AIR && distance < 5) { for (Entity e : LocationUtil.getEntitiesAtLocation(offset)) { e.setVelocity(face.asOffset().mul(5 - distance)); } distance ++; offset = offset.getRelative(face); } return true; } return false; } }
private void performPipeAction(Location<World> location) { Direction direction = location.get(Keys.DIRECTION).get(); Location<World> inventorySource = location.getRelative(direction); //Let's try and find a source of items! LocationUtil.getInventoryForLocation(inventorySource).ifPresent(inv -> { while (inv.peek(1).isPresent()) { Optional<ItemStack> itemStackOptional = inv.poll(1); itemStackOptional.ifPresent(itemStack -> { Set<Location<World>> traversed = new HashSet<>(); try { itemStack = doPipeIteration(location, itemStack, direction, traversed); } catch (StackOverflowError e) { CraftBookPlugin.spongeInst().getLogger() .error("Pipe overflow. Please report this issue to the developers with images of setup.", e); } if (itemStack.getQuantity() > 0) { for (ItemStackSnapshot snapshot : inv.offer(itemStack).getRejectedItems()) { Item item = (Item) location.getExtent().createEntity(EntityTypes.ITEM, location.getPosition()); item.offer(Keys.REPRESENTED_ITEM, snapshot); location.getExtent().spawnEntity(item); } } }); if (stackPerPull.getValue()) { break; } } }); }