Search in sources :

Example 1 with PacketContext

use of org.spongepowered.common.event.tracking.phase.packet.PacketContext in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method processCreativeInventoryAction.

/**
 * @author blood - June 6th, 2016
 * @author gabizou - June 20th, 2016 - Update for 1.9.4 and minor refactors.
 * @reason Since mojang handles creative packets different than survival, we need to
 * restructure this method to prevent any packets being sent to client as we will
 * not be able to properly revert them during drops.
 *
 * @param packetIn The creative inventory packet
 */
@Overwrite
public void processCreativeInventoryAction(CPacketCreativeInventoryAction packetIn) {
    PacketThreadUtil.checkThreadAndEnqueue(packetIn, (NetHandlerPlayServer) (Object) this, this.player.getServerWorld());
    if (this.player.interactionManager.isCreative()) {
        final PhaseData peek = PhaseTracker.getInstance().getCurrentPhaseData();
        final PacketContext<?> context = (PacketContext<?>) peek.context;
        final boolean ignoresCreative = context.getIgnoringCreative();
        boolean clickedOutside = packetIn.getSlotId() < 0;
        ItemStack itemstack = packetIn.getStack();
        if (!itemstack.isEmpty() && itemstack.hasTagCompound() && itemstack.getTagCompound().hasKey("BlockEntityTag", 10)) {
            NBTTagCompound nbttagcompound = itemstack.getTagCompound().getCompoundTag("BlockEntityTag");
            if (nbttagcompound.hasKey("x") && nbttagcompound.hasKey("y") && nbttagcompound.hasKey("z")) {
                BlockPos blockpos = new BlockPos(nbttagcompound.getInteger("x"), nbttagcompound.getInteger("y"), nbttagcompound.getInteger("z"));
                TileEntity tileentity = this.player.world.getTileEntity(blockpos);
                if (tileentity != null) {
                    NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                    tileentity.writeToNBT(nbttagcompound1);
                    nbttagcompound1.removeTag("x");
                    nbttagcompound1.removeTag("y");
                    nbttagcompound1.removeTag("z");
                    itemstack.setTagInfo("BlockEntityTag", nbttagcompound1);
                }
            }
        }
        boolean clickedHotbar = packetIn.getSlotId() >= 1 && packetIn.getSlotId() <= 45;
        boolean itemValidCheck = itemstack.isEmpty() || itemstack.getMetadata() >= 0 && itemstack.getCount() <= 64 && !itemstack.isEmpty();
        // Sponge start - handle CreativeInventoryEvent
        if (itemValidCheck) {
            if (!ignoresCreative) {
                ClickInventoryEvent.Creative clickEvent = SpongeCommonEventFactory.callCreativeClickInventoryEvent(this.player, packetIn);
                if (clickEvent.isCancelled()) {
                    // Reset slot on client
                    if (packetIn.getSlotId() >= 0 && packetIn.getSlotId() < this.player.inventoryContainer.inventorySlots.size()) {
                        this.player.connection.sendPacket(new SPacketSetSlot(this.player.inventoryContainer.windowId, packetIn.getSlotId(), this.player.inventoryContainer.getSlot(packetIn.getSlotId()).getStack()));
                        this.player.connection.sendPacket(new SPacketSetSlot(-1, -1, ItemStack.EMPTY));
                    }
                    return;
                }
            }
            if (clickedHotbar) {
                if (itemstack.isEmpty()) {
                    this.player.inventoryContainer.putStackInSlot(packetIn.getSlotId(), ItemStack.EMPTY);
                } else {
                    this.player.inventoryContainer.putStackInSlot(packetIn.getSlotId(), itemstack);
                }
                this.player.inventoryContainer.setCanCraft(this.player, true);
            } else if (clickedOutside && this.itemDropThreshold < 200) {
                this.itemDropThreshold += 20;
                EntityItem entityitem = this.player.dropItem(itemstack, true);
                if (entityitem != null) {
                    entityitem.setAgeToCreativeDespawnTime();
                }
            }
        }
    // Sponge end
    }
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) TileEntity(net.minecraft.tileentity.TileEntity) PacketContext(org.spongepowered.common.event.tracking.phase.packet.PacketContext) BlockPos(net.minecraft.util.math.BlockPos) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1 PhaseData (org.spongepowered.common.event.tracking.PhaseData)1 PacketContext (org.spongepowered.common.event.tracking.phase.packet.PacketContext)1