use of org.spongepowered.api.util.ban.Ban in project SpongeCommon by SpongePowered.
the class SpongeBanService method addBan.
@Override
public Optional<? extends Ban> addBan(Ban ban) {
Optional<? extends Ban> prevBan;
if (ban.getType().equals(BanTypes.PROFILE)) {
prevBan = this.getBanFor(((Ban.Profile) ban).getProfile());
User user = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).getOrCreate(((Ban.Profile) ban).getProfile());
Sponge.getEventManager().post(SpongeEventFactory.createBanUserEvent(Sponge.getCauseStackManager().getCurrentCause(), (Ban.Profile) ban, user));
UserListUtils.addEntry(this.getUserBanList(), (UserListEntry<?>) ban);
} else if (ban.getType().equals(BanTypes.IP)) {
prevBan = this.getBanFor(((Ban.Ip) ban).getAddress());
Sponge.getEventManager().post(SpongeEventFactory.createBanIpEvent(Sponge.getCauseStackManager().getCurrentCause(), (Ban.Ip) ban));
UserListUtils.addEntry(this.getIPBanList(), (UserListEntry<?>) ban);
} else {
throw new IllegalArgumentException(String.format("Ban %s had unrecognized BanType %s!", ban, ban.getType()));
}
return prevBan;
}
use of org.spongepowered.api.util.ban.Ban in project SpongeCommon by SpongePowered.
the class SpongeBanBuilder method build.
@Override
public Ban build() {
checkState(this.banType != null, "BanType cannot be null!");
String sourceName = this.source != null ? SpongeTexts.toLegacy(this.source) : null;
if (this.banType == BanTypes.PROFILE) {
checkState(this.profile != null, "User cannot be null!");
return (Ban) new UserListBansEntry((GameProfile) this.profile, Date.from(this.start), sourceName, this.toDate(this.end), this.reason != null ? SpongeTexts.toLegacy(this.reason) : null);
}
checkState(this.address != null, "Address cannot be null!");
// This *should* be a static method, but apparently not...
UserListIPBans ipBans = SpongeImpl.getServer().getPlayerList().getBannedIPs();
return (Ban) new UserListIPBansEntry(ipBans.addressToString(new InetSocketAddress(this.address, 0)), Date.from(this.start), sourceName, this.toDate(this.end), this.reason != null ? SpongeTexts.toLegacy(this.reason) : null);
}
use of org.spongepowered.api.util.ban.Ban in project LanternServer by LanternPowered.
the class BanConfig method removeBan.
@Override
public boolean removeBan(Ban ban) {
checkNotNull(ban, "ban");
if (this.entries0.remove(ban)) {
final CauseStack causeStack = CauseStack.currentOrEmpty();
// Post the pardon events
final Event event;
final Cause cause = causeStack.getCurrentCause();
if (ban instanceof Ban.Ip) {
event = SpongeEventFactory.createPardonIpEvent(cause, (Ban.Ip) ban);
} else {
final Ban.Profile profileBan = (Ban.Profile) ban;
// Check if the pardoned player is online (not yet been kicked)
final Optional<Player> optTarget = Sponge.getServer().getPlayer(profileBan.getProfile().getUniqueId());
if (optTarget.isPresent()) {
event = SpongeEventFactory.createPardonUserEventTargetPlayer(cause, profileBan, optTarget.get(), optTarget.get());
} else {
event = SpongeEventFactory.createPardonUserEvent(cause, profileBan, Lantern.getGame().getServiceManager().provideUnchecked(UserStorageService.class).getOrCreate(profileBan.getProfile()));
}
}
// Just ignore for now the fact that they may be cancellable,
// only the PardonIpEvent seems to be cancellable
// TODO: Should they all be cancellable or none of them?
Sponge.getEventManager().post(event);
return true;
}
return false;
}
use of org.spongepowered.api.util.ban.Ban in project LanternServer by LanternPowered.
the class LanternPlayer method handleDeath.
@Override
protected void handleDeath(CauseStack causeStack) {
// Call the harvest event
final boolean keepsInventory = getWorld().getOrCreateRule(RuleTypes.KEEP_INVENTORY).getValue();
final int exp = keepsInventory ? 0 : Math.min(100, get(Keys.EXPERIENCE_LEVEL).orElse(0) * 7);
// Humanoids get their own sub-interface for the event
final HarvestEntityEvent.TargetPlayer harvestEvent = SpongeEventFactory.createHarvestEntityEventTargetPlayer(causeStack.getCurrentCause(), exp, exp, this, keepsInventory, keepsInventory, 0);
Sponge.getEventManager().post(harvestEvent);
if (!harvestEvent.isCancelled()) {
final List<ItemStackSnapshot> drops = new ArrayList<>();
if (!harvestEvent.keepsInventory()) {
// Make a copy of all the items in the players inventory, and put them in the drops
getInventory().<AbstractSlot>slots().forEach(slot -> slot.peek().ifPresent(itemStack -> drops.add(LanternItemStackSnapshot.wrap(itemStack))));
}
if (!harvestEvent.keepsLevel()) {
offer(Keys.EXPERIENCE_LEVEL, harvestEvent.getLevel());
}
// Finalize the harvest event
finalizeHarvestEvent(causeStack, harvestEvent, drops);
}
// Ban the player if the world is hardcode
if (getWorld().getProperties().isHardcore()) {
final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
// Add a permanent ban
banService.addBan(Ban.of(getProfile(), t("gameMode.hardcore.banMessage")));
// Bye, bye!
kick(t("deathScreen.title.hardcore"));
}
}
use of org.spongepowered.api.util.ban.Ban 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();
}
Aggregations