/** * Gets the {@link Server} instance from the * {@link Game} instance. * * <p>Note: During various {@link GameState}s, a {@link Server} instance * may <strong>NOT</strong> be available. During these specific states, * calling {@link Game#getServer()} will throw an exception. To double * check, call {@link #isServerAvailable()}</p> * * @see Game#getServer() * @see Game#isServerAvailable() * @return The server instance */ public static Server getServer() { return getGame().getServer(); }
@Override protected Object getValue(String choice) throws IllegalArgumentException { Optional<Player> ret = Sponge.getGame().getServer().getPlayer(choice); if (!ret.isPresent()) { throw new IllegalArgumentException("Input value " + choice + " was not a player"); } return ret.get(); }
@Override protected Object getValue(String choice) throws IllegalArgumentException { Optional<WorldProperties> ret = Sponge.getGame().getServer().getWorldProperties(choice); if (!ret.isPresent()) { throw new IllegalArgumentException("Provided argument " + choice + " did not match a WorldProperties"); } return ret.get(); } }
@Override protected Iterable<String> getChoices(CommandSource source) { return Sponge.getGame().getServer().getAllWorldProperties().stream() .map(input -> input.getWorldName()) .collect(Collectors.toList()); }
@Override protected Iterable<String> getChoices(CommandSource source) { return Sponge.getGame().getServer().getOnlinePlayers().stream() .map(input -> input == null ? null : input.getName()) .collect(Collectors.toList()); }
@Nullable @Override public Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { final String next = args.peek(); if (next.startsWith("#")) { String specifier = next.substring(1); if (specifier.equalsIgnoreCase("first")) { args.next(); return Sponge.getGame().getServer().getAllWorldProperties().stream().filter(input -> input != null && input.isEnabled()) .collect(Collectors.toList()).iterator().next(); } else if (specifier.equalsIgnoreCase("me") && source instanceof Locatable) { args.next(); return ((Locatable) source).getWorld().getProperties(); } else { boolean firstOnly = false; if (specifier.endsWith(":first")) { firstOnly = true; specifier = specifier.substring(0, specifier.length() - 6); } args.next(); args.insertArg(specifier); @SuppressWarnings("unchecked") final DimensionType type = ((Iterable<DimensionType>) this.dimensionTypeElement.parseValue(source, args)).iterator().next(); Iterable<WorldProperties> ret = Sponge.getGame().getServer().getAllWorldProperties().stream().filter(input -> input != null && input.isEnabled() && input.getDimensionType().equals(type)).collect(Collectors.toList()); return firstOnly ? ret.iterator().next() : ret; } } return super.parseValue(source, args); }
private CommandElement getPlayerElement() throws Exception { // Create two fake players Player player1 = Mockito.mock(Player.class); Mockito.when(player1.getName()).thenReturn("test1"); Player player2 = Mockito.mock(Player.class); Mockito.when(player2.getName()).thenReturn("test2"); Server mock = Mockito.mock(Server.class); Mockito.when(mock.getOnlinePlayers()).thenReturn(Lists.newArrayList(player1, player2)); Mockito.when(mock.getPlayer(Mockito.anyString())).thenReturn(Optional.empty()); Mockito.when(mock.getPlayer("test1")).thenReturn(Optional.of(player1)); Mockito.when(mock.getPlayer("test2")).thenReturn(Optional.of(player2)); Game gameMock = Mockito.mock(Game.class); Mockito.when(gameMock.getServer()).thenReturn(mock); Field field = Sponge.class.getDeclaredField("game"); field.setAccessible(true); field.set(null, gameMock); // Create element return GenericArguments.player(Text.of("player")); }
Optional<World> targetWorld = Sponge.getGame().getServer().getWorld(targetWorldProps.getUniqueId()); Vector3d vector = (Vector3d) vec; return new Location<>(targetWorld.get(), vector);
@Override protected Object getValue(String choice) throws IllegalArgumentException { Optional<Player> ret = Sponge.getGame().getServer().getPlayer(choice); if (!ret.isPresent()) { throw new IllegalArgumentException("Input value " + choice + " was not a player"); } return ret.get(); }
@Override protected Object getValue(String choice) throws IllegalArgumentException { Optional<WorldProperties> ret = Sponge.getGame().getServer().getWorldProperties(choice); if (!ret.isPresent()) { throw new IllegalArgumentException("Provided argument " + choice + " did not match a WorldProperties"); } return ret.get(); } }
public static Location<World> locationFromString(String string) { String[] parts = RegexUtil.COMMA_PATTERN.split(string); if(parts.length < 4) return null; World world = Sponge.getGame().getServer().getWorld(parts[0]).orElse(null); if(world == null) return null; double x = Double.parseDouble(parts[1]); double y = Double.parseDouble(parts[2]); double z = Double.parseDouble(parts[3]); return world.getLocation(x, y, z); }
/** * Gets the amount of entities on the server * * @return amount of entities */ private int getEntityCount() { return plugin.getGame().getServer().getWorlds().stream().mapToInt(world -> world.getEntities().size()).sum(); } }
@Override protected Iterable<String> getChoices(CommandSource source) { return Sponge.getGame().getServer().getOnlinePlayers().stream() .map(input -> input == null ? null : input.getName()) .collect(Collectors.toList()); }
@Override protected Iterable<String> getChoices(CommandSource source) { return Sponge.getGame().getServer().getAllWorldProperties().stream() .map(input -> input.getWorldName()) .collect(Collectors.toList()); }
@Override public boolean kickPlayer(UUID uuid, String message) { return game.getServer().getPlayer(uuid).map(player -> { player.kick(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message)); return true; }).orElse(false); }
@Override public List<String> getOnlinePlayers() { List<String> playerList = new ArrayList<>(); for (Player p : loader.getGame().getServer().getOnlinePlayers()) { playerList.add(p.getName()); } return playerList; }
@Override public UUID getUUID(String playerName) { return loader.getGame().getServer().getPlayer(playerName).get().getUniqueId(); } }
@Override public void sendMessage(String playerName, String message) { if (playerName.equals("console")) { caller.getLogger().info(message); return; } loader.getGame().getServer().getPlayer(playerName).get().sendMessage(((SpongeServerCaller)getCaller()).addColorSponge(message)); }
@Override public void initialize() { Sponge.getGame().getScheduler().createTaskBuilder().intervalTicks(2L).execute(new SelfTriggerClock()).submit(CraftBookPlugin.inst()); Sponge.getGame().getEventManager().registerListeners(CraftBookPlugin.inst(), this); for(World world : Sponge.getGame().getServer().getWorlds()) { registerAll(world); } }