use of net.minecraft.network.play.server.SPacketCloseWindow in project SpongeForge by SpongePowered.
the class MixinPlayerInteractionManager method processRightClickBlock.
/**
* @author gabizou - May 5th, 2016
* @reason Rewrite the firing of interact block events with forge hooks
* Note: This is a dirty merge of Aaron's SpongeCommon writeup of the interaction events and
* Forge's additions. There's some overlay between the two events, specifically that there
* is a SpongeEvent thrown before the ForgeEvent, and yet both are checked in various
* if statements.
*/
@Overwrite
public EnumActionResult processRightClickBlock(EntityPlayer player, World worldIn, ItemStack stack, EnumHand hand, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (this.gameType == GameType.SPECTATOR) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof ILockableContainer) {
Block block = worldIn.getBlockState(pos).getBlock();
ILockableContainer ilockablecontainer = (ILockableContainer) tileentity;
if (ilockablecontainer instanceof TileEntityChest && block instanceof BlockChest) {
ilockablecontainer = ((BlockChest) block).getLockableContainer(worldIn, pos);
}
if (ilockablecontainer != null) {
player.displayGUIChest(ilockablecontainer);
return EnumActionResult.SUCCESS;
}
} else if (tileentity instanceof IInventory) {
player.displayGUIChest((IInventory) tileentity);
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
// Store reference of current player's itemstack in case it changes
ItemStack oldStack = stack.copy();
InteractBlockEvent.Secondary event;
BlockSnapshot currentSnapshot = ((org.spongepowered.api.world.World) worldIn).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
event = SpongeCommonEventFactory.callInteractBlockEventSecondary(player, oldStack, VecHelper.toVector3d(pos.add(hitX, hitY, hitZ)), currentSnapshot, DirectionFacingProvider.getInstance().getKey(facing).get(), hand);
if (!ItemStack.areItemStacksEqual(oldStack, this.player.getHeldItem(hand))) {
SpongeCommonEventFactory.playerInteractItemChanged = true;
}
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (event.isCancelled()) {
final IBlockState state = worldIn.getBlockState(pos);
if (state.getBlock() == Blocks.COMMAND_BLOCK) {
// CommandBlock GUI opens solely on the client, we need to force it close on cancellation
this.player.connection.sendPacket(new SPacketCloseWindow(0));
} else if (state.getProperties().containsKey(BlockDoor.HALF)) {
// client to resolve this
if (state.getValue(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.LOWER) {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.up()));
} else {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.down()));
}
} else if (!stack.isEmpty()) {
// Stopping the placement of a door or double plant causes artifacts (ghosts) on the top-side of the block. We need to remove it
if (stack.getItem() instanceof ItemDoor || (stack.getItem() instanceof ItemBlock && ((ItemBlock) stack.getItem()).getBlock().equals(Blocks.DOUBLE_PLANT))) {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.up(2)));
}
}
// since we don't want to undo that
if (tileEntity != null && this.player.openContainer instanceof ContainerPlayer) {
this.player.closeScreen();
}
SpongeCommonEventFactory.interactBlockEventCancelled = true;
return EnumActionResult.FAIL;
}
net.minecraft.item.Item item = stack.isEmpty() ? null : stack.getItem();
EnumActionResult ret = item == null ? EnumActionResult.PASS : item.onItemUseFirst(player, worldIn, pos, facing, hitX, hitY, hitZ, hand);
if (ret != EnumActionResult.PASS) {
return ret;
}
boolean bypass = true;
final ItemStack[] itemStacks = { player.getHeldItemMainhand(), player.getHeldItemOffhand() };
for (ItemStack s : itemStacks) {
bypass = bypass && (s.isEmpty() || s.getItem().doesSneakBypassUse(s, worldIn, pos, player));
}
EnumActionResult result = EnumActionResult.PASS;
if (!player.isSneaking() || bypass || event.getUseBlockResult() == Tristate.TRUE) {
// also, store the result instead of returning immediately
if (event.getUseBlockResult() != Tristate.FALSE) {
IBlockState iblockstate = worldIn.getBlockState(pos);
Container lastOpenContainer = player.openContainer;
result = iblockstate.getBlock().onBlockActivated(worldIn, pos, iblockstate, player, hand, facing, hitX, hitY, hitZ) ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
// if itemstack changed, avoid restore
if (!ItemStack.areItemStacksEqual(oldStack, this.player.getHeldItem(hand))) {
SpongeCommonEventFactory.playerInteractItemChanged = true;
}
result = this.handleOpenEvent(lastOpenContainer, this.player, currentSnapshot, result);
} else {
this.player.connection.sendPacket(new SPacketBlockChange(this.world, pos));
result = TristateUtil.toActionResult(event.getUseItemResult());
}
}
// This handles the event not cancelled and block not activated
if (result != EnumActionResult.SUCCESS && tileEntity != null && hand == EnumHand.MAIN_HAND) {
this.player.closeScreen();
}
// store result instead of returning
if (stack.isEmpty()) {
result = EnumActionResult.PASS;
} else if (player.getCooldownTracker().hasCooldown(stack.getItem())) {
result = EnumActionResult.PASS;
} else if (stack.getItem() instanceof ItemBlock && ((ItemBlock) stack.getItem()).getBlock() instanceof BlockCommandBlock && !player.canUseCommand(2, "")) {
result = EnumActionResult.FAIL;
} else {
if ((result != EnumActionResult.SUCCESS && event.getUseItemResult() != Tristate.FALSE || result == EnumActionResult.SUCCESS && event.getUseItemResult() == Tristate.TRUE)) {
int meta = stack.getMetadata();
int size = stack.getCount();
result = stack.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
// nest isCreative check instead of calling the method twice.
if (this.isCreative()) {
stack.setItemDamage(meta);
stack.setCount(size);
}
}
}
if (!ItemStack.areItemStacksEqual(player.getHeldItem(hand), oldStack) || result != EnumActionResult.SUCCESS) {
player.openContainer.detectAndSendChanges();
}
return result;
// Sponge end
}
use of net.minecraft.network.play.server.SPacketCloseWindow in project SpongeCommon by SpongePowered.
the class MixinPlayerInteractionManager method processRightClickBlock.
/**
* @author Aaron1011
* @author gabizou - May 28th, 2016 - Rewritten for 1.9.4
*
* @reason Fire interact block event.
*/
@Overwrite
public EnumActionResult processRightClickBlock(EntityPlayer player, net.minecraft.world.World worldIn, ItemStack stack, EnumHand hand, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (this.gameType == GameType.SPECTATOR) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof ILockableContainer) {
Block block = worldIn.getBlockState(pos).getBlock();
ILockableContainer ilockablecontainer = (ILockableContainer) tileentity;
if (ilockablecontainer instanceof TileEntityChest && block instanceof BlockChest) {
ilockablecontainer = ((BlockChest) block).getLockableContainer(worldIn, pos);
}
if (ilockablecontainer != null) {
// TODO - fire event
player.displayGUIChest(ilockablecontainer);
return EnumActionResult.SUCCESS;
}
} else if (tileentity instanceof IInventory) {
// TODO - fire event
player.displayGUIChest((IInventory) tileentity);
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
// else { // Sponge - Remove unecessary else
// Sponge Start - Create an interact block event before something happens.
final ItemStack oldStack = stack.copy();
final BlockSnapshot currentSnapshot = ((World) worldIn).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
final InteractBlockEvent.Secondary event = SpongeCommonEventFactory.callInteractBlockEventSecondary(player, oldStack, new Vector3d(pos.getX() + hitX, pos.getY() + hitY, pos.getZ() + hitZ), currentSnapshot, DirectionFacingProvider.getInstance().getKey(facing).get(), hand);
if (!ItemStack.areItemStacksEqual(oldStack, this.player.getHeldItem(hand))) {
SpongeCommonEventFactory.playerInteractItemChanged = true;
}
if (event.isCancelled()) {
final IBlockState state = (IBlockState) currentSnapshot.getState();
if (state.getBlock() == Blocks.COMMAND_BLOCK) {
// CommandBlock GUI opens solely on the client, we need to force it close on cancellation
this.player.connection.sendPacket(new SPacketCloseWindow(0));
} else if (state.getProperties().containsKey(BlockDoor.HALF)) {
// client to resolve this
if (state.getValue(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.LOWER) {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.up()));
} else {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.down()));
}
} else if (!oldStack.isEmpty()) {
// Stopping the placement of a door or double plant causes artifacts (ghosts) on the top-side of the block. We need to remove it
final Item item = oldStack.getItem();
if (item instanceof ItemDoor || (item instanceof ItemBlock && ((ItemBlock) item).getBlock().equals(Blocks.DOUBLE_PLANT))) {
this.player.connection.sendPacket(new SPacketBlockChange(worldIn, pos.up(2)));
}
}
SpongeCommonEventFactory.interactBlockEventCancelled = true;
return EnumActionResult.FAIL;
}
if (!player.isSneaking() || player.getHeldItemMainhand().isEmpty() && player.getHeldItemOffhand().isEmpty()) {
// Also, store the result instead of returning immediately
if (event.getUseBlockResult() != Tristate.FALSE) {
IBlockState iblockstate = (IBlockState) currentSnapshot.getState();
Container lastOpenContainer = player.openContainer;
EnumActionResult result = iblockstate.getBlock().onBlockActivated(worldIn, pos, iblockstate, player, hand, facing, hitX, hitY, hitZ) ? EnumActionResult.SUCCESS : EnumActionResult.PASS;
// if itemstack changed, avoid restore
if (!ItemStack.areItemStacksEqual(oldStack, this.player.getHeldItem(hand))) {
SpongeCommonEventFactory.playerInteractItemChanged = true;
}
result = this.handleOpenEvent(lastOpenContainer, this.player, currentSnapshot, result);
if (result != EnumActionResult.PASS) {
return result;
}
} else {
// Need to send a block change to the client, because otherwise, they are not
// going to be told about the block change.
this.player.connection.sendPacket(new SPacketBlockChange(this.world, pos));
// it wasn't cancelled, but perform no further processing.
return EnumActionResult.FAIL;
}
// Sponge End
}
if (stack.isEmpty()) {
return EnumActionResult.PASS;
} else if (player.getCooldownTracker().hasCooldown(stack.getItem())) {
return EnumActionResult.PASS;
} else if (stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
if ((block instanceof BlockCommandBlock || block instanceof BlockStructure) && !player.canUseCommandBlock()) {
return EnumActionResult.FAIL;
}
}
// else if (this.isCreative()) { // Sponge - Rewrite this to handle an isCreative check after the result, since we have a copied stack at the top of this method.
// int j = stack.getMetadata();
// int i = stack.stackSize;
// EnumActionResult enumactionresult = stack.onItemUse(player, worldIn, pos, hand, facing, offsetX, offsetY, offsetZ);
// stack.setItemDamage(j);
// stack.stackSize = i;
// return enumactionresult;
// } else {
// return stack.onItemUse(player, worldIn, pos, hand, facing, offsetX, offsetY, offsetZ);
// }
// } // Sponge - Remove unecessary else bracket
// Sponge Start - complete the method with the micro change of resetting item damage and quantity from the copied stack.
final EnumActionResult result = stack.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
if (this.isCreative()) {
stack.setItemDamage(oldStack.getItemDamage());
stack.setCount(oldStack.getCount());
}
if (!ItemStack.areItemStacksEqual(player.getHeldItem(hand), oldStack) || result != EnumActionResult.SUCCESS) {
player.openContainer.detectAndSendChanges();
}
return result;
// Sponge end
// } // Sponge - Remove unecessary else bracket
}
Aggregations