@Override public void run() { List<UUID> onlineIds = new ArrayList<>(); for (Player player : Sponge.getServer().getOnlinePlayers()) { onlineIds.add(player.getUniqueId()); } this.onlineIds = new CopyOnWriteArraySet<>(onlineIds); }
@Override public Collection<Actor> getConnectedUsers() { List<Actor> users = new ArrayList<>(); for (org.spongepowered.api.entity.living.player.Player player : Sponge.getServer().getOnlinePlayers()) { users.add(new SpongePlayer(this, player)); } return users; } }
@Override protected Iterable<String> getChoices(CommandSource source) { Set<String> worldEntities = Sponge.getServer().getWorlds().stream().flatMap(x -> x.getEntities().stream()) .filter(this::checkEntity) .map(x -> x.getUniqueId().toString()) .collect(Collectors.toSet()); Collection<Player> players = Sponge.getServer().getOnlinePlayers(); if (!players.isEmpty() && checkEntity(players.iterator().next())) { final Set<String> setToReturn = new HashSet<>(worldEntities); // to ensure mutability players.forEach(x -> setToReturn.add(x.getName())); return setToReturn; } return worldEntities; }
/** * Creates a {@link GameProfile} from the provided ID and name. * * @param uniqueId The unique id * @param name The name * @return The created profile */ static GameProfile of(UUID uniqueId, @Nullable String name) { return Sponge.getServer().getGameProfileManager().createProfile(uniqueId, name); }
@Nullable @Override public Player matchPlayer(Player player) { if (player instanceof SpongePlayer) { return player; } else { Optional<org.spongepowered.api.entity.living.player.Player> optPlayer = Sponge.getServer().getPlayer(player.getUniqueId()); return optPlayer.<Player>map(player1 -> new SpongePlayer(this, player1)).orElse(null); } }
/** * Creates a new signed property. * * <p>Depending on the property name, if the signature is provided it must * originate from Mojang.</p> * * @param name The name for the property * @param value The value of the property * @param signature The signature of the property * @return The new property */ static ProfileProperty of(String name, String value, @Nullable String signature) { return Sponge.getServer().getGameProfileManager().createProfileProperty(name, value, signature); }
/** * Gets this spawn position as a {@link Location} object, if the world is * available. * * @return The position object, if available. */ public Optional<Location<World>> asLocation() { Optional<World> world = Sponge.getServer().getWorld(getWorldUniqueId()); if (!world.isPresent()) { return Optional.empty(); } return Optional.of(new Location<>(world.get(), getPosition())); }
@Override public List<? extends com.sk89q.worldedit.world.World> getWorlds() { Collection<org.spongepowered.api.world.World> worlds = Sponge.getServer().getWorlds(); List<com.sk89q.worldedit.world.World> ret = new ArrayList<>(worlds.size()); for (org.spongepowered.api.world.World world : worlds) { ret.add(SpongeWorldEdit.inst().getAdapter().getWorld(world)); } return ret; }
/** * Sets the location of this entity to a new position in a world which does * not have to be loaded (but must at least be enabled). * * <p>If the target world is loaded then this is equivalent to * setting the location via {@link TargetedLocationData}.</p> * * <p>If the target world is unloaded but is enabled according to its * {@link WorldArchetype#isEnabled()} then this will first load the world * before transferring the entity to that world.</p> * * <p>If the target world is unloaded and not enabled then the transfer * will fail.</p> * * @param worldName The name of the world to transfer to * @param position The position in the target world * @return True if the teleport was successful */ default boolean transferToWorld(String worldName, Vector3d position) { return Sponge.getServer().getWorld(worldName) .map(world -> transferToWorld(world, position)) .orElse(false); }
@Override protected Object getValue(String choice) throws IllegalArgumentException { UUID uuid; try { uuid = UUID.fromString(choice); } catch (IllegalArgumentException ignored) { // Player could be a name return Sponge.getServer().getPlayer(choice) .orElseThrow(() -> new IllegalArgumentException("Input value " + choice + " does not represent a valid entity")); } boolean found = false; for (World world : Sponge.getServer().getWorlds()) { Optional<Entity> ret = world.getEntity(uuid); if (ret.isPresent()) { Entity entity = ret.get(); if (this.checkEntity(entity)) { return entity; } found = true; } } if (found) { throw new IllegalArgumentException("Input value " + choice + " was not an entity of the required type!"); } throw new IllegalArgumentException("Input value " + choice + " was not an entity"); }
/** * Sets the location of this entity to a new position in a world which does * not have to be loaded (but must at least be enabled). * * <p>If the target world is loaded then this is equivalent to setting the * location via {@link TargetedLocationData}.</p> * * <p>If the target world is unloaded but is enabled according to its * {@link WorldArchetype#isEnabled()} then this will first load the world * before transferring the entity to that world.</p> * * <p>If the target world is unloaded and not enabled then the transfer * will fail.</p> * * @param uuid The UUID of the target world to transfer to * @param position The position in the target world * @return True if the teleport was successful */ default boolean transferToWorld(UUID uuid, Vector3d position) { return Sponge.getServer().getWorld(uuid) .map(world -> transferToWorld(world, position)) .orElse(false); }
/** * Create a Sponge world from a WorldEdit world. * * @param world the WorldEdit world * @return a Sponge world */ public static org.spongepowered.api.world.World adapt(World world) { checkNotNull(world); if (world instanceof SpongeWorld) { return ((SpongeWorld) world).getWorld(); } else { org.spongepowered.api.world.World match = Sponge.getServer().getWorld(world.getName()).orElse(null); if (match != null) { return match; } else { throw new IllegalArgumentException("Can't find a Sponge world for " + world); } } }
/** * Gets the loaded chunk at the given block coordinate position. * * @param bx The x coordinate * @param by The y coordinate * @param bz The z coordinate * @return The chunk, if available */ default Optional<Chunk> getChunkAtBlock(int bx, int by, int bz) { return getChunk(Sponge.getServer().getChunkLayout().forceToChunk(bx, by, bz)); }
/** * Gets the underlying chunk position. * * @return The underlying chunk position */ public Vector3i getChunkPosition() { if (this.chunkPosition == null) { this.chunkPosition = Sponge.getServer().getChunkLayout().forceToChunk(getBlockPosition()); } return this.chunkPosition; }
/** * Gets the chunk in the given direction from this chunk. * * @param direction The cardinal or ordinal direction to get the chunk from * @param shouldLoad Whether the server should load or generate the chunk * if unavailable * @return The neighbor chunk, if available or if {@code shouldLoad} is true */ default Optional<Chunk> getNeighbor(Direction direction, boolean shouldLoad) { Optional<Vector3i> neighborPosition = Sponge.getServer().getChunkLayout().moveToChunk(getPosition(), direction); return neighborPosition.flatMap(vector3i -> getWorld().loadChunk(vector3i, shouldLoad)); }
@Nullable @Override public World matchWorld(World world) { if (world instanceof SpongeWorld) { return world; } else { for (org.spongepowered.api.world.World ws : Sponge.getServer().getWorlds()) { if (ws.getName().equals(world.getName())) { return SpongeWorldEdit.inst().getAdapter().getWorld(ws); } } return null; } }
@Override protected Object getValue(String choice) throws IllegalArgumentException { UUID uuid = UUID.fromString(choice); for (World world : Sponge.getServer().getWorlds()) { Optional<Entity> ret = world.getEntity(uuid); if (ret.isPresent()) { return ret.get(); } } throw new IllegalArgumentException("Input value " + choice + " was not an entity"); }
@Override public void run() { List<UUID> players = new ArrayList<>(handCache.keySet()); for (Player p : Sponge.getServer().getOnlinePlayers()) { handCache.put(p.getUniqueId(), convert(grabber.getItem(p))); players.remove(p.getUniqueId()); } // Remove offline players for (UUID uuid : players) { handCache.remove(uuid); } }
/** * Gets the chunk in the given direction from this chunk. * * @param direction The cardinal or ordinal direction to get the chunk from * @param shouldLoad Whether the server should load or generate the chunk if unavailable * @return The neighbor chunk, if available or if {@code shouldLoad} is true */ default Optional<Chunk> getNeighbor(Direction direction, boolean shouldLoad) { Optional<Vector3i> neighborPosition = Sponge.getServer().getChunkLayout().moveToChunk(getPosition(), direction); return neighborPosition.isPresent() ? getWorld().loadChunk(neighborPosition.get(), shouldLoad) : Optional.empty(); }
/** * Get the loaded chunk at the given block coordinate position. * * @param bx The x coordinate * @param by The y coordinate * @param bz The z coordinate * @return The chunk, if available */ default Optional<Chunk> getChunkAtBlock(int bx, int by, int bz) { return getChunk(Sponge.getServer().getChunkLayout().forceToChunk(bx, by, bz)); }