Search in sources :

Example 21 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickTileEntity.

@SuppressWarnings({ "unused", "try" })
public static void tickTileEntity(final TrackedWorldBridge mixinWorldServer, final TickableBlockEntity tile) {
    checkArgument(tile instanceof BlockEntity, "ITickable %s is not a TileEntity!", tile);
    checkNotNull(tile, "Cannot capture on a null ticking tile entity!");
    final net.minecraft.world.level.block.entity.BlockEntity tileEntity = (net.minecraft.world.level.block.entity.BlockEntity) tile;
    final BlockEntityBridge mixinTileEntity = (BlockEntityBridge) tile;
    final BlockPos pos = tileEntity.getBlockPos();
    final LevelChunkBridge chunk = ((ActiveChunkReferantBridge) tile).bridge$getActiveChunk();
    if (!((TrackableBridge) tileEntity).bridge$shouldTick()) {
        return;
    }
    if (chunk == null) {
        ((ActiveChunkReferantBridge) tile).bridge$setActiveChunk((TrackedLevelChunkBridge) tileEntity.getLevel().getChunkAt(tileEntity.getBlockPos()));
    }
    final TileEntityTickContext context = TickPhase.Tick.TILE_ENTITY.createPhaseContext(PhaseTracker.SERVER).source(mixinTileEntity);
    try (final PhaseContext<?> phaseContext = context) {
        if (tile instanceof CreatorTrackedBridge) {
            // Add notifier and owner so we don't have to perform lookups during the phases and other processing
            ((CreatorTrackedBridge) tile).tracker$getNotifierUUID().ifPresent(phaseContext::notifier);
            // Allow the tile entity to validate the owner of itself. As long as the tile entity
            // chunk is already loaded and activated, and the tile entity has already loaded
            // the owner of itself.
            ((CreatorTrackedBridge) tile).tracker$getCreatorUUID().ifPresent(phaseContext::creator);
        }
        // Finally, switch the context now that we have the owner and notifier
        phaseContext.buildAndSwitch();
        try (final Timing timing = ((TimingBridge) tileEntity.getType()).bridge$timings().startTiming()) {
            tile.tick();
        }
        // otherwise the viewing players update this during their ticking
        if (tileEntity instanceof ViewableInventoryBridge) {
            final Set<ServerPlayer> players = ((ViewableInventoryBridge) tileEntity).viewableBridge$getViewers();
            if (players.size() > 0) {
                players.forEach(player -> player.containerMenu.broadcastChanges());
            }
        }
    } catch (final Exception e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, context);
    }
    // We delay clearing active chunk if TE is invalidated during tick so we must remove it after
    if (tileEntity.isRemoved()) {
        ((ActiveChunkReferantBridge) tileEntity).bridge$setActiveChunk(null);
    }
}
Also used : CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) BlockEntityBridge(org.spongepowered.common.bridge.world.level.block.entity.BlockEntityBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge) ViewableInventoryBridge(org.spongepowered.common.bridge.world.inventory.ViewableInventoryBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) TrackedLevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.TrackedLevelChunkBridge) Timing(co.aikar.timings.Timing) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity)

Example 22 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class AbstractContainerMenuMixin_Inventory method impl$sendSlotContents.

private void impl$sendSlotContents(final Integer i, final ItemStack oldStack) {
    for (final ContainerListener listener : this.containerListeners) {
        boolean isChangingQuantityOnly = true;
        if (listener instanceof ServerPlayer) {
            isChangingQuantityOnly = ((ServerPlayer) listener).ignoreSlotUpdateHack;
            ((ServerPlayer) listener).ignoreSlotUpdateHack = false;
        }
        listener.slotChanged(((AbstractContainerMenu) (Object) this), i, oldStack);
        if (listener instanceof ServerPlayer) {
            ((ServerPlayer) listener).ignoreSlotUpdateHack = isChangingQuantityOnly;
        }
    }
}
Also used : AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) ServerPlayer(net.minecraft.server.level.ServerPlayer) ContainerListener(net.minecraft.world.inventory.ContainerListener)

Example 23 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin_Inventory method impl$beforeRemoveItem.

@Override
protected void impl$beforeRemoveItem(final boolean param0, final CallbackInfoReturnable<Boolean> cir) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    this.inventory$effectTransactor = transactor.logDropFromPlayerInventory((ServerPlayer) (Object) this, param0);
}
Also used : TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) ServerPlayer(net.minecraft.server.level.ServerPlayer)

Example 24 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class InventoryEventFactory method displayContainer.

@org.checkerframework.checker.nullness.qual.Nullable
public static AbstractContainerMenu displayContainer(final ServerPlayer player, final Inventory inventory, final Component displayName) {
    final net.minecraft.world.inventory.AbstractContainerMenu previousContainer = player.containerMenu;
    final net.minecraft.world.inventory.AbstractContainerMenu container;
    Optional<ViewableInventory> viewable = inventory.asViewable();
    if (viewable.isPresent()) {
        if (viewable.get() instanceof MenuProvider) {
            MenuProvider namedContainerProvider = (MenuProvider) viewable.get();
            if (displayName != null) {
                namedContainerProvider = new SimpleMenuProvider(namedContainerProvider, SpongeAdventure.asVanilla(displayName));
            }
            player.openMenu(namedContainerProvider);
        } else if (viewable.get() instanceof CarriedInventory) {
            Optional carrier = ((CarriedInventory) viewable.get()).carrier();
            if (carrier.get() instanceof AbstractHorse) {
                player.openHorseInventory(((AbstractHorse) carrier.get()), ((Container) viewable.get()));
            }
        } else if (viewable.get() instanceof Merchant) {
            Merchant merchant = (Merchant) viewable.get();
            net.minecraft.network.chat.Component display = null;
            int level = 0;
            if (merchant instanceof Villager) {
                display = ((Villager) merchant).getDisplayName();
                level = ((Villager) merchant).getVillagerData().getLevel();
            } else if (merchant instanceof WanderingTrader) {
                display = ((WanderingTrader) merchant).getDisplayName();
                level = 1;
            }
            if (displayName != null) {
                display = SpongeAdventure.asVanilla(displayName);
            }
            OptionalInt containerId = player.openMenu(new SimpleMenuProvider((id, playerInv, p) -> new MerchantMenu(id, playerInv, merchant), display));
            if (containerId.isPresent() && !merchant.getOffers().isEmpty()) {
                player.sendMerchantOffers(containerId.getAsInt(), merchant.getOffers(), level, merchant.getVillagerXp(), merchant.showProgressBar(), merchant.canRestock());
            }
        }
    }
    container = player.containerMenu;
    if (previousContainer == container) {
        return null;
    }
    if (!InventoryEventFactory.callInteractContainerOpenEvent(player)) {
        return null;
    }
    if (container instanceof ContainerBridge) {
        // This overwrites the normal container behaviour and allows viewing
        // inventories that are more than 8 blocks away
        // This currently actually only works for the Containers mixed into
        // by InteractableContainerMixin ; but throws no errors for other
        // containers
        // Allow viewing inventory; except when dead
        ((ContainerBridge) container).bridge$setCanInteractWith(p -> !p.removed);
    }
    return container;
}
Also used : EnchantItemEvent(org.spongepowered.api.event.item.inventory.EnchantItemEvent) LivingEntity(net.minecraft.world.entity.LivingEntity) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SimpleMenuProvider(net.minecraft.world.SimpleMenuProvider) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) ContainerBridge(org.spongepowered.common.bridge.world.inventory.container.ContainerBridge) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) Villager(net.minecraft.world.entity.npc.Villager) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) MerchantMenu(net.minecraft.world.inventory.MerchantMenu) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) Transaction(org.spongepowered.api.data.Transaction) HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) InteractContainerEvent(org.spongepowered.api.event.item.inventory.container.InteractContainerEvent) Merchant(net.minecraft.world.item.trading.Merchant) Player(net.minecraft.world.entity.player.Player) EnchantmentInstance(net.minecraft.world.item.enchantment.EnchantmentInstance) InventoryUtil(org.spongepowered.common.inventory.util.InventoryUtil) List(java.util.List) ItemEntity(net.minecraft.world.entity.item.ItemEntity) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Optional(java.util.Optional) MenuProvider(net.minecraft.world.MenuProvider) EnchantmentMenu(net.minecraft.world.inventory.EnchantmentMenu) ItemStack(net.minecraft.world.item.ItemStack) ViewableInventory(org.spongepowered.api.item.inventory.type.ViewableInventory) WanderingTrader(net.minecraft.world.entity.npc.WanderingTrader) Inventory(org.spongepowered.api.item.inventory.Inventory) NonNull(org.checkerframework.checker.nullness.qual.NonNull) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) TransferInventoryEvent(org.spongepowered.api.event.item.inventory.TransferInventoryEvent) ArrayList(java.util.ArrayList) ServerPlayer(net.minecraft.server.level.ServerPlayer) Container(net.minecraft.world.Container) TrackingUtil(org.spongepowered.common.event.tracking.TrackingUtil) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Component(net.kyori.adventure.text.Component) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Nullable(org.checkerframework.checker.nullness.qual.Nullable) UpdateAnvilEvent(org.spongepowered.api.event.item.inventory.UpdateAnvilEvent) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Item(org.spongepowered.api.entity.Item) ContainerUtil(org.spongepowered.common.inventory.util.ContainerUtil) Slot(org.spongepowered.api.item.inventory.Slot) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Entity(org.spongepowered.api.entity.Entity) AnvilMenu(net.minecraft.world.inventory.AnvilMenu) TrackedInventoryBridge(org.spongepowered.common.bridge.world.inventory.container.TrackedInventoryBridge) SpongeRandomEnchantmentListBuilder(org.spongepowered.common.item.enchantment.SpongeRandomEnchantmentListBuilder) AbstractHorse(net.minecraft.world.entity.animal.horse.AbstractHorse) ChangeEntityEquipmentEvent(org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent) PacketPhaseUtil(org.spongepowered.common.event.tracking.phase.packet.PacketPhaseUtil) Collections(java.util.Collections) MerchantMenu(net.minecraft.world.inventory.MerchantMenu) AbstractHorse(net.minecraft.world.entity.animal.horse.AbstractHorse) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Optional(java.util.Optional) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) OptionalInt(java.util.OptionalInt) ContainerBridge(org.spongepowered.common.bridge.world.inventory.container.ContainerBridge) SimpleMenuProvider(net.minecraft.world.SimpleMenuProvider) MenuProvider(net.minecraft.world.MenuProvider) ViewableInventory(org.spongepowered.api.item.inventory.type.ViewableInventory) SimpleMenuProvider(net.minecraft.world.SimpleMenuProvider) Merchant(net.minecraft.world.item.trading.Merchant) Villager(net.minecraft.world.entity.npc.Villager) WanderingTrader(net.minecraft.world.entity.npc.WanderingTrader) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 25 with ServerPlayer

use of net.minecraft.server.level.ServerPlayer in project SpongeCommon by SpongePowered.

the class ServerPlayerMixin_Tracker method tracker$throwItemDrop.

/**
 * @author gabizou - June 4th, 2016
 * @author i509VCB - February 17th, 2020 - 1.14.4
 * @author gabizou - December 31st, 2021 - 1.16.5
 * @reason We inject a construct event for the item drop and conveniently
 * can redirect the super call.
 */
@Redirect(method = "drop", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;drop(Lnet/minecraft/world/item/ItemStack;ZZ)Lnet/minecraft/world/entity/item/ItemEntity;"))
@Nullable
private ItemEntity tracker$throwItemDrop(final Player thisPlayer, final ItemStack droppedItem, final boolean dropAround, final boolean traceItem) {
    if (droppedItem.isEmpty()) {
        return null;
    }
    if (((PlatformEntityBridge) this).bridge$isFakePlayer()) {
        return super.shadow$drop(droppedItem, dropAround, traceItem);
    }
    if (((LevelBridge) this.level).bridge$isFake()) {
        return super.shadow$drop(droppedItem, dropAround, traceItem);
    }
    final double posX1 = this.shadow$getX();
    final double posY1 = this.shadow$getEyeY() - (double) 0.3F;
    final double posZ1 = this.shadow$getZ();
    // Now the real fun begins.
    final ItemStack item;
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(droppedItem);
    final List<ItemStackSnapshot> original = new ArrayList<>();
    original.add(snapshot);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        item = SpongeCommonEventFactory.throwDropItemAndConstructEvent((ServerPlayer) (Object) this, posX1, posY1, posZ1, snapshot, original, frame);
        if (item == null || item.isEmpty()) {
            return null;
        }
        // Here is where we would potentially perform item pre-merging (merge the item stacks with previously captured item stacks
        // and only if those stacks can be stacked (count increased). Otherwise, we'll just continue to throw the entity item.
        // For now, due to refactoring a majority of all of this code, pre-merging is disabled entirely.
        final ItemEntity itemEntity = new ItemEntity(this.level, posX1, posY1, posZ1, droppedItem);
        itemEntity.setPickUpDelay(40);
        if (traceItem) {
            itemEntity.setThrower(this.shadow$getUUID());
        }
        final Random random = this.shadow$getRandom();
        if (dropAround) {
            final float f = random.nextFloat() * 0.5F;
            final float f1 = random.nextFloat() * ((float) Math.PI * 2F);
            itemEntity.setDeltaMovement(-Mth.sin(f1) * f, 0.2F, Mth.cos(f1) * f);
        } else {
            final float f8 = Mth.sin(this.xRot * ((float) Math.PI / 180F));
            final float f2 = Mth.cos(this.xRot * ((float) Math.PI / 180F));
            final float f3 = Mth.sin(this.yRot * ((float) Math.PI / 180F));
            final float f4 = Mth.cos(this.yRot * ((float) Math.PI / 180F));
            final float f5 = this.random.nextFloat() * ((float) Math.PI * 2F);
            final float f6 = 0.02F * this.random.nextFloat();
            itemEntity.setDeltaMovement((double) (-f3 * f2 * 0.3F) + Math.cos(f5) * (double) f6, (-f8 * 0.3F + 0.1F + (this.random.nextFloat() - this.random.nextFloat()) * 0.1F), (double) (f4 * f2 * 0.3F) + Math.sin(f5) * (double) f6);
        }
        return itemEntity;
    }
}
Also used : ItemEntity(net.minecraft.world.entity.item.ItemEntity) ArrayList(java.util.ArrayList) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Random(java.util.Random) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)71 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ServerLevel (net.minecraft.server.level.ServerLevel)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)11 ItemStack (net.minecraft.world.item.ItemStack)10 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)10 Player (org.bukkit.entity.Player)10 MyPet (de.Keyle.MyPet.api.entity.MyPet)8 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)8 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)8 Player (net.minecraft.world.entity.player.Player)7 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)6 ArrayList (java.util.ArrayList)6 ClientboundRemoveEntitiesPacket (net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket)6 ChunkMap (net.minecraft.server.level.ChunkMap)6 ServerGamePacketListenerImpl (net.minecraft.server.network.ServerGamePacketListenerImpl)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemEntity (net.minecraft.world.entity.item.ItemEntity)6 PlayerEntityTagMessage (com.wuest.prefab.proxy.messages.PlayerEntityTagMessage)5