use of org.spongepowered.common.SpongeServer in project SpongeCommon by SpongePowered.
the class ServerLoginPacketListenerImplMixin method handleAcceptedLogin.
/**
* @author morph - April 27th, 2021
* @author dualspiral - July 17th, 2021
*
* @reason support async ban/whitelist service and user->player syncing.
*/
@Overwrite
public void handleAcceptedLogin() {
if (!this.gameProfile.isComplete()) {
this.gameProfile = this.shadow$createFakeProfile(this.gameProfile);
}
// Sponge start - avoid #tick calling handleAcceptedLogin more than once.
if (this.impl$accepted) {
return;
}
this.impl$accepted = true;
final PlayerList playerList = this.server.getPlayerList();
// Sponge end
// Sponge start - completable future
((PlayerListBridge) playerList).bridge$canPlayerLogin(this.connection.getRemoteAddress(), this.gameProfile).handle((componentOpt, throwable) -> {
if (throwable != null) {
// An error occurred during login checks so we ask to abort.
((ConnectionBridge) this.connection).bridge$setKickReason(new TextComponent("An error occurred checking ban/whitelist status."));
SpongeCommon.logger().error("An error occurred when checking the ban/whitelist status of {}.", this.gameProfile.getId().toString());
SpongeCommon.logger().error(throwable);
} else if (componentOpt != null) {
// We handle this later
((ConnectionBridge) this.connection).bridge$setKickReason(componentOpt);
}
try {
((SpongeServer) SpongeCommon.server()).userManager().handlePlayerLogin(this.gameProfile);
} catch (final IOException e) {
throw new CompletionException(e);
}
return null;
}).handleAsync((ignored, throwable) -> {
if (throwable != null) {
// We're just going to disconnect here, because something went horribly wrong.
if (throwable instanceof CompletionException) {
throw (CompletionException) throwable;
} else {
throw new CompletionException(throwable);
}
}
// Sponge end
this.state = ServerLoginPacketListenerImpl.State.ACCEPTED;
if (this.server.getCompressionThreshold() >= 0 && !this.connection.isMemoryConnection()) {
this.connection.send(new ClientboundLoginCompressionPacket(this.server.getCompressionThreshold()), (param0) -> this.connection.setupCompression(this.server.getCompressionThreshold()));
}
this.connection.send(new ClientboundGameProfilePacket(this.gameProfile));
final ServerPlayer var1 = this.server.getPlayerList().getPlayer(this.gameProfile.getId());
if (var1 != null) {
this.state = ServerLoginPacketListenerImpl.State.DELAY_ACCEPT;
this.delayedAcceptPlayer = this.server.getPlayerList().getPlayerForLogin(this.gameProfile);
} else {
// Sponge start - Also send the channel registrations using the minecraft channel, for compatibility
final ServerSideConnection connection = (ServerSideConnection) this;
((SpongeChannelManager) Sponge.channelManager()).sendChannelRegistrations(connection);
try {
this.server.getPlayerList().placeNewPlayer(this.connection, this.server.getPlayerList().getPlayerForLogin(this.gameProfile));
// invalidate just to be sure there is no user cached for the online player anymore
Sponge.server().userManager().removeFromCache(this.gameProfile.getId());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
return null;
}, SpongeCommon.server()).exceptionally(throwable -> {
// If a throwable exists, we're just going to disconnect the user, better than leaving them in limbo.
if (throwable != null) {
this.impl$disconnectError(throwable, this.state == ServerLoginPacketListenerImpl.State.ACCEPTED || this.state == ServerLoginPacketListenerImpl.State.READY_TO_ACCEPT);
}
return null;
// Sponge End
});
}
use of org.spongepowered.common.SpongeServer in project SpongeCommon by SpongePowered.
the class ServerPlayerMixin_API method hasPlayedBefore.
@Override
public boolean hasPlayedBefore() {
final Instant instant = ((SpongeServer) this.shadow$getServer()).getPlayerDataManager().getFirstJoined(this.uniqueId()).get();
final Instant toTheMinute = instant.truncatedTo(ChronoUnit.MINUTES);
final Instant now = Instant.now().truncatedTo(ChronoUnit.MINUTES);
final Duration timeSinceFirstJoined = Duration.of(now.minusMillis(toTheMinute.toEpochMilli()).toEpochMilli(), ChronoUnit.MINUTES);
return timeSinceFirstJoined.getSeconds() > 0;
}
use of org.spongepowered.common.SpongeServer in project SpongeCommon by SpongePowered.
the class SpongeUserData method save.
public void save() throws IOException {
synchronized (this) {
final SpongeUserManager userManager = ((SpongeServer) SpongeCommon.server()).userManager();
final LevelStorageSource.LevelStorageAccess storageSource = ((MinecraftServerAccessor) Sponge.server()).accessor$storageSource();
final File file = storageSource.getLevelPath(LevelResource.PLAYER_DATA_DIR).resolve(this.uniqueId() + ".dat").toFile();
this.writeCompound(this.compound);
try (final FileOutputStream out = new FileOutputStream(file)) {
NbtIo.writeCompressed(this.compound, out);
userManager.unmarkDirty(this);
} catch (final IOException e) {
// We log the message here because the error may be swallowed by a completable future.
SpongeCommon.logger().warn("Failed to save user file [{}]!", file, e);
throw e;
}
}
}
use of org.spongepowered.common.SpongeServer in project SpongeCommon by SpongePowered.
the class BlockTransactionType method consumeEventsAndMarker.
@Override
protected void consumeEventsAndMarker(PhaseContext<@NonNull ?> context, final Collection<? extends ChangeBlockEvent.All> changeBlockEvents) {
final Multimap<ResourceKey, ChangeBlockEvent.All> eventsByWorld = LinkedListMultimap.create();
changeBlockEvents.forEach(event -> eventsByWorld.put(event.world().key(), event));
eventsByWorld.asMap().forEach((key, events) -> {
final Optional<ServerWorld> serverWorld = ((SpongeServer) SpongeCommon.server()).worldManager().world(key);
if (!serverWorld.isPresent()) {
return;
}
final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
// Gather transactions that were valid
events.stream().filter(event -> !event.isCancelled()).flatMap(event -> event.transactions().stream()).filter(BlockTransaction::isValid).forEach(transactions -> {
// Then "put" the most recent transactions such that we have a complete rebuild of
// each position according to what originally existed and then
// the ultimate final block on that position
final SpongeBlockSnapshot original = (SpongeBlockSnapshot) transactions.original();
positions.put(original.getBlockPos(), original);
positions.put(original.getBlockPos(), (SpongeBlockSnapshot) transactions.finalReplacement());
});
// just return.
if (positions.isEmpty()) {
return;
}
final ImmutableList<BlockTransactionReceipt> transactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
if (snapshots.isEmpty() || snapshots.size() < 2) {
// Error case
return Optional.<BlockTransactionReceipt>empty();
}
final SpongeBlockSnapshot original = snapshots.get(0);
final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
final Operation operation = context.getBlockOperation(original, result);
final BlockTransactionReceipt eventTransaction = new BlockTransactionReceipt(original, result, operation);
context.postBlockTransactionApplication(original.blockChange, eventTransaction);
return Optional.of(eventTransaction);
}).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
final Cause cause = PhaseTracker.getInstance().currentCause();
SpongeCommon.post(SpongeEventFactory.createChangeBlockEventPost(cause, transactions, serverWorld.get()));
});
}
use of org.spongepowered.common.SpongeServer in project SpongeCommon by SpongePowered.
the class BlockEventBasedTransaction method generateEvent.
@Override
public final Optional<ChangeBlockEvent.All> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<ChangeBlockEvent.All>> transactions, final Cause currentCause) {
final Optional<ServerWorld> o = ((SpongeServer) SpongeCommon.server()).worldManager().world(this.worldKey);
if (!o.isPresent()) {
return Optional.empty();
}
final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
for (final GameTransaction<@NonNull ?> transaction : transactions) {
final BlockEventBasedTransaction blockTransaction = (BlockEventBasedTransaction) transaction;
if (!positions.containsKey(blockTransaction.affectedPosition)) {
positions.put(blockTransaction.affectedPosition, blockTransaction.getOriginalSnapshot());
}
if (blockTransaction.getResultingSnapshot() != null) {
positions.put(blockTransaction.affectedPosition, blockTransaction.getResultingSnapshot());
}
}
final ImmutableList<BlockTransaction> eventTransactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
if (snapshots.isEmpty() || snapshots.size() < 2) {
// Error case
return Optional.<BlockTransaction>empty();
}
final SpongeBlockSnapshot original = snapshots.get(0);
final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
final ImmutableList<BlockSnapshot> intermediary;
if (snapshots.size() > 2) {
intermediary = ImmutableList.copyOf(snapshots.subList(1, snapshots.size() - 2));
} else {
intermediary = ImmutableList.of();
}
final Operation operation = context.getBlockOperation(original, result);
final BlockTransaction eventTransaction = new BlockTransaction(original, result, intermediary, operation);
return Optional.of(eventTransaction);
}).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
if (eventTransactions.isEmpty()) {
return Optional.empty();
}
return Optional.of(SpongeEventFactory.createChangeBlockEventAll(currentCause, eventTransactions, o.get()));
}
Aggregations