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
}
}
Aggregations