use of org.lanternpowered.server.world.LanternWorldProperties in project LanternServer by LanternPowered.
the class CommandToggleDownfall method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none())).executor((src, args) -> {
final LanternWorldProperties world = CommandHelper.getWorldProperties(src, args);
final WeatherUniverse weatherUniverse = world.getWorld().get().getWeatherUniverse().orElse(null);
final LanternWeather weather = (LanternWeather) weatherUniverse.getWeather();
if (weather.getOptions().getOrDefault(WeatherOptions.RAIN_STRENGTH).get() > 0) {
weatherUniverse.setWeather(Weathers.CLEAR);
} else {
weatherUniverse.setWeather(Weathers.RAIN);
}
return CommandResult.success();
});
}
use of org.lanternpowered.server.world.LanternWorldProperties in project LanternServer by LanternPowered.
the class CommandGameRule method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
final Collection<String> defaultRules = Sponge.getRegistry().getDefaultGameRules();
final ThreadLocal<RuleType<?>> currentRule = new ThreadLocal<>();
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), new CommandElement(Text.of("rule")) {
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
RuleType<?> ruleType = RuleType.getOrCreate(args.next(), RuleDataTypes.STRING, "");
currentRule.set(ruleType);
return ruleType;
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
final String prefix = args.nextIfPresent().orElse("");
return defaultRules.stream().filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
}
}, new CommandElement(Text.of("value")) {
private final List<String> booleanRuleSuggestions = ImmutableList.of("true", "false");
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
RuleType<?> ruleType = currentRule.get();
currentRule.remove();
try {
return ruleType.getDataType().parse(args.next());
} catch (IllegalArgumentException e) {
throw args.createError(t(e.getMessage()));
}
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
RuleType<?> ruleType = context.<RuleType<?>>getOne("rule").get();
if (ruleType.getDataType() == RuleDataTypes.BOOLEAN) {
// match the first part of the string
return this.booleanRuleSuggestions;
}
return Collections.emptyList();
}
}).executor((src, args) -> {
WorldProperties world = CommandHelper.getWorldProperties(src, args);
Object value = args.getOne("value").get();
RuleType ruleType = args.<RuleType>getOne("rule").get();
((LanternWorldProperties) world).getRules().getOrCreateRule(ruleType).setValue(value);
src.sendMessage(t("commands.gamerule.success", ruleType.getName(), ruleType.getDataType().serialize(value)));
return CommandResult.success();
});
}
use of org.lanternpowered.server.world.LanternWorldProperties in project LanternServer by LanternPowered.
the class AbstractUser method setLocation.
@Override
public boolean setLocation(Vector3d position, UUID worldUniqueId) {
checkNotNull(position, "position");
checkNotNull(worldUniqueId, "worldUniqueId");
final WorldProperties world = Lantern.getServer().getWorldManager().getWorldProperties(worldUniqueId).orElseThrow(() -> new IllegalStateException("Cannot find World with the given UUID: " + worldUniqueId));
this.userWorld = (LanternWorldProperties) world;
setRawPosition(position);
return true;
}
use of org.lanternpowered.server.world.LanternWorldProperties in project LanternServer by LanternPowered.
the class NetworkSession method initPlayer.
/**
* Initializes the {@link LanternPlayer} instance
* and spawns it in a world if permitted to join
* the server.
*/
public void initPlayer() {
initKeepAliveTask();
if (this.gameProfile == null) {
throw new IllegalStateException("The game profile must first be available!");
}
this.player = new LanternPlayer(this.gameProfile, this);
this.player.setNetworkId(EntityProtocolManager.acquireEntityId());
this.player.setEntityProtocolType(EntityProtocolTypes.PLAYER);
LanternWorld world = this.player.getWorld();
if (world == null) {
LanternWorldProperties worldProperties = this.player.getUserWorld();
boolean fixSpawnLocation = false;
if (worldProperties == null) {
Lantern.getLogger().warn("The player [{}] attempted to login in a non-existent world, this is not possible " + "so we have moved them to the default's world spawn point.", this.gameProfile.getName().get());
worldProperties = (LanternWorldProperties) Lantern.getServer().getDefaultWorld().get();
fixSpawnLocation = true;
} else if (!worldProperties.isEnabled()) {
Lantern.getLogger().warn("The player [{}] attempted to login in a unloaded and not-enabled world [{}], this is not possible " + "so we have moved them to the default's world spawn point.", this.gameProfile.getName().get(), worldProperties.getWorldName());
worldProperties = (LanternWorldProperties) Lantern.getServer().getDefaultWorld().get();
fixSpawnLocation = true;
}
final Optional<World> optWorld = Lantern.getWorldManager().loadWorld(worldProperties);
// Use the raw method to avoid triggering any network messages
this.player.setRawWorld((LanternWorld) optWorld.get());
this.player.setUserWorld(null);
if (fixSpawnLocation) {
// TODO: Use a proper spawn position
this.player.setRawPosition(new Vector3d(0, 100, 0));
this.player.setRawRotation(new Vector3d(0, 0, 0));
}
}
// The kick reason
Text kickReason = null;
final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
// Check whether the player is banned and kick if necessary
Ban ban = banService.getBanFor(this.gameProfile).orElse(null);
if (ban == null) {
final SocketAddress address = getChannel().remoteAddress();
if (address instanceof InetSocketAddress) {
ban = banService.getBanFor(((InetSocketAddress) address).getAddress()).orElse(null);
}
}
if (ban != null) {
final Optional<Instant> optExpirationDate = ban.getExpirationDate();
final Optional<Text> optReason = ban.getReason();
// Generate the kick message
Text.Builder builder = Text.builder();
if (ban instanceof Ban.Profile) {
builder.append(t("multiplayer.disconnect.ban.banned"));
} else {
builder.append(t("multiplayer.disconnect.ban.ip_banned"));
}
// There is optionally a reason
optReason.ifPresent(reason -> builder.append(Text.NEW_LINE).append(t("multiplayer.disconnect.ban.reason", reason)));
// And a expiration date if present
optExpirationDate.ifPresent(expirationDate -> {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(tr("multiplayer.disconnect.ban.expiration_date_format").get());
builder.append(Text.NEW_LINE).append(t("multiplayer.disconnect.ban.expiration", formatter.format(expirationDate)));
});
kickReason = builder.build();
// Check for white-list
} else if (!isWhitelisted(this.gameProfile)) {
kickReason = t("multiplayer.disconnect.not_whitelisted");
// Check whether the server is full
} else if (Lantern.getServer().getOnlinePlayers().size() >= Lantern.getServer().getMaxPlayers() && !canBypassPlayerLimit(this.gameProfile)) {
kickReason = t("multiplayer.disconnect.server_full");
}
final MessageEvent.MessageFormatter messageFormatter = new MessageEvent.MessageFormatter(kickReason != null ? kickReason : t("multiplayer.disconnect.not_allowed_to_join"));
final Cause cause = Cause.builder().append(this).build(EventContext.builder().add(EventContextKeys.PLAYER, this.player).build());
final Transform<World> fromTransform = this.player.getTransform();
final ClientConnectionEvent.Login loginEvent = SpongeEventFactory.createClientConnectionEventLogin(cause, fromTransform, fromTransform, this, messageFormatter, this.gameProfile, this.player, false);
if (kickReason != null) {
loginEvent.setCancelled(true);
}
Sponge.getEventManager().post(loginEvent);
if (loginEvent.isCancelled()) {
disconnect(loginEvent.isMessageCancelled() ? t("multiplayer.disconnect.generic") : loginEvent.getMessage());
return;
}
Lantern.getLogger().debug("The player {} successfully to joined from {}.", this.gameProfile.getName().get(), this.channel.remoteAddress());
// Update the first join and last played data
final Instant lastJoined = Instant.now();
this.player.offer(Keys.LAST_DATE_PLAYED, lastJoined);
if (!this.player.get(Keys.FIRST_DATE_PLAYED).isPresent()) {
this.player.offer(Keys.FIRST_DATE_PLAYED, lastJoined);
}
final Transform<World> toTransform = loginEvent.getToTransform();
world = (LanternWorld) toTransform.getExtent();
final WorldConfig config = world.getProperties().getConfig();
// Update the game mode if necessary
if (config.isGameModeForced() || this.player.get(Keys.GAME_MODE).get().equals(GameModes.NOT_SET)) {
this.player.offer(Keys.GAME_MODE, config.getGameMode());
}
// Reset the raw world
this.player.setRawWorld(null);
// Set the transform, this will trigger the initial
// network messages to be send
this.player.setTransform(toTransform);
final MessageChannel messageChannel = this.player.getMessageChannel();
final Text joinMessage;
final GameProfile previousProfile = this.channel.attr(PREVIOUS_GAME_PROFILE).getAndSet(null);
if (previousProfile != null && previousProfile.getName().isPresent() && !previousProfile.getName().get().equals(this.gameProfile.getName().get())) {
joinMessage = t("multiplayer.player.joined.renamed", this.player.getName(), previousProfile.getName().get());
} else {
joinMessage = t("multiplayer.player.joined", this.player.getName());
}
final ClientConnectionEvent.Join joinEvent = SpongeEventFactory.createClientConnectionEventJoin(cause, messageChannel, Optional.of(messageChannel), new MessageEvent.MessageFormatter(joinMessage), this.player, false);
Sponge.getEventManager().post(joinEvent);
if (!joinEvent.isMessageCancelled()) {
joinEvent.getChannel().ifPresent(channel -> channel.send(this.player, joinEvent.getMessage()));
}
this.server.getDefaultResourcePack().ifPresent(this.player::sendResourcePack);
this.player.resetIdleTimeoutCounter();
}
use of org.lanternpowered.server.world.LanternWorldProperties in project LanternServer by LanternPowered.
the class CommandWeather method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), new PatternMatchingCommandElement(Text.of("type")) {
@Override
protected Iterable<String> getChoices(CommandSource source) {
Collection<Weather> weathers = Sponge.getRegistry().getAllOf(Weather.class);
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (Weather weather : weathers) {
builder.add(weather.getId());
builder.addAll(((LanternWeather) weather).getAliases());
}
return builder.build();
}
@Override
protected Object getValue(String choice) throws IllegalArgumentException {
final Optional<Weather> optWeather = Sponge.getRegistry().getType(Weather.class, choice);
if (!optWeather.isPresent()) {
return Sponge.getRegistry().getAllOf(Weather.class).stream().filter(weather -> {
for (String alias : ((LanternWeather) weather).getAliases()) {
if (alias.equalsIgnoreCase(choice)) {
return true;
}
}
return false;
}).findAny().orElseThrow(() -> new IllegalArgumentException("Invalid input " + choice + " was found"));
}
return optWeather.get();
}
}, GenericArguments.optional(GenericArguments.integer(Text.of("duration")))).executor((src, args) -> {
LanternWorldProperties world = CommandHelper.getWorldProperties(src, args);
WeatherUniverse weatherUniverse = world.getWorld().get().getWeatherUniverse().orElse(null);
Weather type = args.<Weather>getOne("type").get();
if (weatherUniverse != null) {
if (args.hasAny("duration")) {
weatherUniverse.setWeather(type, args.<Integer>getOne("duration").get() * 20);
} else {
weatherUniverse.setWeather(type);
}
}
src.sendMessage(t("Changing to " + type.getName() + " weather"));
return CommandResult.success();
});
}
Aggregations