Search in sources :

Example 1 with ServerPlayer

use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin method impl$handleClientSettings.

@SuppressWarnings({ "ConstantConditions", "UnstableApiUsage" })
@Inject(method = "updateOptions", at = @At("HEAD"))
private void impl$handleClientSettings(final ServerboundClientInformationPacket packet, final CallbackInfo ci) {
    if (!ShouldFire.PLAYER_CHANGE_CLIENT_SETTINGS_EVENT) {
        return;
    }
    final ServerboundClientInformationPacketAccessor $packet = (ServerboundClientInformationPacketAccessor) packet;
    final Locale newLocale = LocaleCache.getLocale($packet.accessor$language());
    final ImmutableSet<SkinPart> skinParts = Sponge.game().registry(RegistryTypes.SKIN_PART).stream().map(part -> (SpongeSkinPart) part).filter(part -> part.test(packet.getModelCustomisation())).collect(ImmutableSet.toImmutableSet());
    final int viewDistance = $packet.accessor$viewDistance();
    // Post before the player values are updated
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        final ChatVisibility visibility = (ChatVisibility) (Object) packet.getChatVisibility();
        final PlayerChangeClientSettingsEvent event = SpongeEventFactory.createPlayerChangeClientSettingsEvent(frame.currentCause(), visibility, skinParts, newLocale, (ServerPlayer) this, packet.getChatColors(), viewDistance);
        SpongeCommon.post(event);
    }
}
Also used : Locale(java.util.Locale) Inject(org.spongepowered.asm.mixin.injection.Inject) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PermissionService(org.spongepowered.api.service.permission.PermissionService) PlayerList(net.minecraft.server.players.PlayerList) Team(net.minecraft.world.scores.Team) MinecraftServer(net.minecraft.server.MinecraftServer) Mixin(org.spongepowered.asm.mixin.Mixin) ClientboundPlayerCombatPacket(net.minecraft.network.protocol.game.ClientboundPlayerCombatPacket) ServerScoreboardBridge(org.spongepowered.common.bridge.server.ServerScoreboardBridge) ServerPlayerBridge(org.spongepowered.common.bridge.server.level.ServerPlayerBridge) Living(org.spongepowered.api.entity.living.Living) BossEventBridge(org.spongepowered.common.bridge.world.BossEventBridge) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Set(java.util.Set) InteractionResult(net.minecraft.world.InteractionResult) Locales(org.spongepowered.api.util.locale.Locales) EntityUtil(org.spongepowered.common.entity.EntityUtil) TextComponent(net.minecraft.network.chat.TextComponent) PlatformHooks(org.spongepowered.common.hooks.PlatformHooks) TicketType(net.minecraft.server.level.TicketType) Shadow(org.spongepowered.asm.mixin.Shadow) ItemStack(net.minecraft.world.item.ItemStack) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) Overwrite(org.spongepowered.asm.mixin.Overwrite) SpongeSkinPart(org.spongepowered.common.data.type.SpongeSkinPart) ClientboundLevelEventPacket(net.minecraft.network.protocol.game.ClientboundLevelEventPacket) LevelData(net.minecraft.world.level.storage.LevelData) ChatVisibility(org.spongepowered.api.entity.living.player.chat.ChatVisibility) ServerboundClientInformationPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundClientInformationPacketAccessor) DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) RotateEntityEvent(org.spongepowered.api.event.entity.RotateEntityEvent) Stats(net.minecraft.stats.Stats) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Opcodes(org.objectweb.asm.Opcodes) Identity(net.kyori.adventure.identity.Identity) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) ShouldFire(org.spongepowered.common.event.ShouldFire) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) DataUtil(org.spongepowered.common.data.DataUtil) Cause(org.spongepowered.api.event.Cause) Channel(io.netty.channel.Channel) ChunkPos(net.minecraft.world.level.ChunkPos) VecHelper(org.spongepowered.common.util.VecHelper) ClientboundChangeDifficultyPacket(net.minecraft.network.protocol.game.ClientboundChangeDifficultyPacket) InteractionHand(net.minecraft.world.InteractionHand) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ClientboundGameEventPacket(net.minecraft.network.protocol.game.ClientboundGameEventPacket) Either(com.mojang.datafixers.util.Either) LivingEntity(net.minecraft.world.entity.LivingEntity) Score(net.minecraft.world.scores.Score) PlayerBridge(org.spongepowered.common.bridge.world.entity.player.PlayerBridge) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) ClientboundBossEventPacket(net.minecraft.network.protocol.game.ClientboundBossEventPacket) LocaleCache(org.spongepowered.common.util.LocaleCache) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) PortalLogic(org.spongepowered.common.world.portal.PortalLogic) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) PlayerChangeClientSettingsEvent(org.spongepowered.api.event.entity.living.player.PlayerChangeClientSettingsEvent) ServerGamePacketListenerImplBridge(org.spongepowered.common.bridge.server.network.ServerGamePacketListenerImplBridge) CriteriaTriggers(net.minecraft.advancements.CriteriaTriggers) ChatFormatting(net.minecraft.ChatFormatting) ServerboundClientInformationPacket(net.minecraft.network.protocol.game.ServerboundClientInformationPacket) Locale(java.util.Locale) At(org.spongepowered.asm.mixin.injection.At) Stat(net.minecraft.stats.Stat) BiomeManager(net.minecraft.world.level.biome.BiomeManager) ImmutableSet(com.google.common.collect.ImmutableSet) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) KickPlayerEvent(org.spongepowered.api.event.entity.living.player.KickPlayerEvent) GameRules(net.minecraft.world.level.GameRules) Sponge(org.spongepowered.api.Sponge) ChatType(net.minecraft.network.chat.ChatType) UUID(java.util.UUID) Final(org.spongepowered.asm.mixin.Final) ClientboundUpdateMobEffectPacket(net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket) Player(net.minecraft.world.entity.player.Player) Tristate(org.spongepowered.api.util.Tristate) HoverEvent(net.minecraft.network.chat.HoverEvent) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) PlayerMixin(org.spongepowered.common.mixin.core.world.entity.player.PlayerMixin) Optional(java.util.Optional) Level(net.minecraft.world.level.Level) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ClientboundPlayerAbilitiesPacket(net.minecraft.network.protocol.game.ClientboundPlayerAbilitiesPacket) Unit(net.minecraft.util.Unit) ResultSlot(net.minecraft.world.inventory.ResultSlot) ServerLevel(net.minecraft.server.level.ServerLevel) HashSet(java.util.HashSet) ChangeEntityWorldEvent(org.spongepowered.api.event.entity.ChangeEntityWorldEvent) Component(net.kyori.adventure.text.Component) DamageSource(net.minecraft.world.damagesource.DamageSource) ObjectiveCriteria(net.minecraft.world.scores.criteria.ObjectiveCriteria) SkinPart(org.spongepowered.api.data.type.SkinPart) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Audiences(org.spongepowered.api.adventure.Audiences) PortalInfo(net.minecraft.world.level.portal.PortalInfo) SpongeCommon(org.spongepowered.common.SpongeCommon) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) MovementTypes(org.spongepowered.api.event.cause.entity.MovementTypes) ServerPlayerGameMode(net.minecraft.server.level.ServerPlayerGameMode) Entity(net.minecraft.world.entity.Entity) SubjectBridge(org.spongepowered.common.bridge.permissions.SubjectBridge) ClientboundRespawnPacket(net.minecraft.network.protocol.game.ClientboundRespawnPacket) Vector3d(org.spongepowered.math.vector.Vector3d) ConnectionAccessor(org.spongepowered.common.accessor.network.ConnectionAccessor) Slice(org.spongepowered.asm.mixin.injection.Slice) PlayerOwnBorderListener(org.spongepowered.common.world.border.PlayerOwnBorderListener) PlayerChangeClientSettingsEvent(org.spongepowered.api.event.entity.living.player.PlayerChangeClientSettingsEvent) ChatVisibility(org.spongepowered.api.entity.living.player.chat.ChatVisibility) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerboundClientInformationPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundClientInformationPacketAccessor) SpongeSkinPart(org.spongepowered.common.data.type.SpongeSkinPart) SkinPart(org.spongepowered.api.data.type.SkinPart) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with ServerPlayer

use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin method impl$copyDataOnRespawn.

@Inject(method = "restoreFrom(Lnet/minecraft/server/level/ServerPlayer;Z)V", at = @At("HEAD"))
private void impl$copyDataOnRespawn(final net.minecraft.server.level.ServerPlayer oldPlayer, final boolean respawnFromEnd, final CallbackInfo ci) {
    // Copy Sponge data
    if (oldPlayer instanceof DataCompoundHolder) {
        final DataCompoundHolder oldEntity = (DataCompoundHolder) oldPlayer;
        DataUtil.syncDataToTag(oldEntity);
        final CompoundTag compound = oldEntity.data$getCompound();
        ((DataCompoundHolder) this).data$setCompound(compound);
        DataUtil.syncTagToData(this);
    }
    // Update boss bars
    SpongeAdventure.forEachBossBar(bar -> ((BossEventBridge) bar).bridge$replacePlayer(oldPlayer, (net.minecraft.server.level.ServerPlayer) (Object) this));
}
Also used : DataCompoundHolder(org.spongepowered.common.bridge.data.DataCompoundHolder) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) CompoundTag(net.minecraft.nbt.CompoundTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with ServerPlayer

use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$handlePlayerDisconnect.

@Redirect(method = "onDisconnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;Ljava/util/UUID;)V"))
public void impl$handlePlayerDisconnect(final PlayerList playerList, final net.minecraft.network.chat.Component component, final ChatType chatType, UUID uuid) {
    // be fired either.
    if (this.player.connection == null) {
        return;
    }
    final ServerPlayer spongePlayer = (ServerPlayer) this.player;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(this.player);
        final Component message = SpongeAdventure.asAdventure(component);
        Audience audience = Sponge.server().broadcastAudience();
        final ServerSideConnectionEvent.Disconnect event = SpongeEventFactory.createServerSideConnectionEventDisconnect(PhaseTracker.getCauseStackManager().currentCause(), audience, Optional.of(audience), message, message, spongePlayer.connection(), spongePlayer);
        SpongeCommon.post(event);
        event.audience().ifPresent(a -> a.sendMessage(spongePlayer, event.message()));
    }
    ((ServerPlayerBridge) this.player).bridge$getWorldBorderListener().onPlayerDisconnect();
}
Also used : Audience(net.kyori.adventure.audience.Audience) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) Component(net.kyori.adventure.text.Component) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 4 with ServerPlayer

use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.

the class SpongeInventoryMenu method setCurrentInventory.

@Override
public void setCurrentInventory(ViewableInventory inventory) {
    if (inventory.getClass().equals(this.inventory.getClass()) && inventory instanceof ViewableCustomInventory && inventory.capacity() == this.inventory.capacity()) {
        this.inventory = inventory;
        for (Map.Entry<Container, ServerPlayer> entry : this.tracked.entrySet()) {
            final net.minecraft.world.inventory.AbstractContainerMenu container = (net.minecraft.world.inventory.AbstractContainerMenu) entry.getKey();
            final ServerPlayer player = entry.getValue();
            // create a new container for the viewable inventory
            final net.minecraft.world.inventory.AbstractContainerMenu newContainer = ((ViewableCustomInventory) inventory).createMenu(-1, ((Player) player).inventory, (Player) player);
            for (int i = 0; i < inventory.capacity(); i++) {
                // And put its slots into the old container
                final Slot slot = newContainer.slots.get(i);
                container.slots.set(i, slot);
                // Update Container items
                ((AbstractContainerMenuAccessor) container).accessor$lastSlots().set(i, slot.getItem());
            }
            // send update to Client
            for (ContainerListener listener : ((AbstractContainerMenuAccessor) container).accessor$containerListeners()) {
                listener.refreshContainer(container, ((AbstractContainerMenuAccessor) container).accessor$lastSlots());
            }
        }
    } else {
        // Get all distinct players and reopen inventory for them
        this.inventory = inventory;
        this.reopen();
    }
}
Also used : Container(org.spongepowered.api.item.inventory.Container) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) AbstractContainerMenuAccessor(org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor) Slot(net.minecraft.world.inventory.Slot) ContainerListener(net.minecraft.world.inventory.ContainerListener) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ServerPlayer

use of org.spongepowered.api.entity.living.player.server.ServerPlayer in project SpongeCommon by SpongePowered.

the class AdvancementProgressMixin method impl$revokeCriterion.

private boolean impl$revokeCriterion(String rawCriterion) {
    final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(rawCriterion);
    if (criterionProgress == null || !criterionProgress.isDone()) {
        return false;
    }
    if (SpongeScoreCriterion.BYPASS_EVENT) {
        criterionProgress.revoke();
        return true;
    }
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final ServerPlayer player = ((PlayerAdvancementsBridge) this.impl$playerAdvancements).bridge$getPlayer();
    final CriterionProgress progress = (CriterionProgress) criterionProgress;
    final AdvancementCriterion criterion = progress.criterion();
    final CriterionBridge criterionBridge = (CriterionBridge) criterion;
    // The score criterion needs special care
    final SpongeScoreCriterion scoreCriterion = criterionBridge.bridge$getScoreCriterion();
    final CriterionEvent event;
    final Advancement advancement = ((org.spongepowered.api.advancement.AdvancementProgress) this).advancement();
    if (scoreCriterion != null) {
        final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) this.impl$progressMap.get(scoreCriterion.name());
        final int lastScore = scoreProgress.score();
        final int score = lastScore + 1;
        if (lastScore == scoreCriterion.goal()) {
            event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, scoreCriterion, player, lastScore, score);
        } else if (score == scoreCriterion.goal()) {
            event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, scoreCriterion, player, Instant.now(), lastScore, score);
        } else {
            event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, scoreCriterion, player, lastScore, score);
        }
    } else {
        event = SpongeEventFactory.createCriterionEventRevoke(cause, advancement, criterion, player);
    }
    if (SpongeCommon.post(event)) {
        return false;
    }
    criterionProgress.revoke();
    return true;
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress) Cause(org.spongepowered.api.event.Cause) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Advancement(org.spongepowered.api.advancement.Advancement) SpongeAndCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeAndCriterionProgress) SpongeOrCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeOrCriterionProgress) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress) ImplementationBackedCriterionProgress(org.spongepowered.common.advancement.criterion.ImplementationBackedCriterionProgress) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge)

Aggregations

ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)32 Component (net.kyori.adventure.text.Component)13 Cause (org.spongepowered.api.event.Cause)9 Listener (org.spongepowered.api.event.Listener)8 Sponge (org.spongepowered.api.Sponge)7 Inject (org.spongepowered.asm.mixin.injection.Inject)7 TypeToken (io.leangen.geantyref.TypeToken)6 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)6 Inject (com.google.inject.Inject)5 UUID (java.util.UUID)5 Identity (net.kyori.adventure.identity.Identity)5 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)5 BlockPos (net.minecraft.core.BlockPos)5 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)5 CommandResult (org.spongepowered.api.command.CommandResult)5 CommandException (org.spongepowered.api.command.exception.CommandException)5 CommandContext (org.spongepowered.api.command.parameter.CommandContext)5 Map (java.util.Map)4 Command (org.spongepowered.api.command.Command)4 Parameter (org.spongepowered.api.command.parameter.Parameter)4