Search in sources :

Example 1 with ContainerBridge

use of io.izzel.arclight.common.bridge.inventory.container.ContainerBridge in project Arclight by IzzelAliz.

the class ServerPlayerEntityMixin method openHorseInventory.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void openHorseInventory(final AbstractHorseEntity entityhorseabstract, final IInventory iinventory) {
    this.nextContainerCounter();
    Container container = new HorseInventoryContainer(this.currentWindowId, this.inventory, iinventory, entityhorseabstract);
    ((ContainerBridge) container).bridge$setTitle(entityhorseabstract.getDisplayName());
    container = CraftEventFactory.callInventoryOpenEvent((ServerPlayerEntity) (Object) this, container);
    if (container == null) {
        iinventory.closeInventory((ServerPlayerEntity) (Object) this);
        return;
    }
    if (this.openContainer != this.container) {
        this.closeScreen();
    }
    this.connection.sendPacket(new SOpenHorseWindowPacket(this.currentWindowId, iinventory.getSizeInventory(), entityhorseabstract.getEntityId()));
    (this.openContainer = container).addListener((ServerPlayerEntity) (Object) this);
    net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open((ServerPlayerEntity) (Object) this, this.openContainer));
}
Also used : HorseInventoryContainer(net.minecraft.inventory.container.HorseInventoryContainer) Container(net.minecraft.inventory.container.Container) HorseInventoryContainer(net.minecraft.inventory.container.HorseInventoryContainer) SOpenHorseWindowPacket(net.minecraft.network.play.server.SOpenHorseWindowPacket) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ContainerBridge(io.izzel.arclight.common.bridge.inventory.container.ContainerBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with ContainerBridge

use of io.izzel.arclight.common.bridge.inventory.container.ContainerBridge in project Arclight by IzzelAliz.

the class ContainerMixin method transferTo.

public void transferTo(Container other, CraftHumanEntity player) {
    InventoryView source = this.getBukkitView();
    InventoryView destination = ((ContainerBridge) other).bridge$getBukkitView();
    ((IInventoryBridge) ((CraftInventory) source.getTopInventory()).getInventory()).onClose(player);
    ((IInventoryBridge) ((CraftInventory) source.getBottomInventory()).getInventory()).onClose(player);
    ((IInventoryBridge) ((CraftInventory) destination.getTopInventory()).getInventory()).onOpen(player);
    ((IInventoryBridge) ((CraftInventory) destination.getBottomInventory()).getInventory()).onOpen(player);
}
Also used : CraftInventoryView(org.bukkit.craftbukkit.v.inventory.CraftInventoryView) InventoryView(org.bukkit.inventory.InventoryView) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) ContainerBridge(io.izzel.arclight.common.bridge.inventory.container.ContainerBridge)

Example 3 with ContainerBridge

use of io.izzel.arclight.common.bridge.inventory.container.ContainerBridge in project Arclight by IzzelAliz.

the class WorkbenchContainerMixin method updateCraftingResult.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
protected static void updateCraftingResult(int i, World world, PlayerEntity playerEntity, CraftingInventory inventory, CraftResultInventory resultInventory) {
    Container container = ArclightCaptures.getWorkbenchContainer();
    if (!world.isRemote) {
        ServerPlayerEntity serverplayerentity = (ServerPlayerEntity) playerEntity;
        ItemStack itemstack = ItemStack.EMPTY;
        Optional<ICraftingRecipe> optional = world.getServer().getRecipeManager().getRecipe(IRecipeType.CRAFTING, inventory, world);
        if (optional.isPresent()) {
            ICraftingRecipe icraftingrecipe = optional.get();
            if (resultInventory.canUseRecipe(world, serverplayerentity, icraftingrecipe)) {
                itemstack = icraftingrecipe.getCraftingResult(inventory);
            }
        }
        itemstack = CraftEventFactory.callPreCraftEvent(inventory, resultInventory, itemstack, ((ContainerBridge) container).bridge$getBukkitView(), false);
        resultInventory.setInventorySlotContents(0, itemstack);
        serverplayerentity.connection.sendPacket(new SSetSlotPacket(i, 0, itemstack));
    }
}
Also used : Container(net.minecraft.inventory.container.Container) WorkbenchContainer(net.minecraft.inventory.container.WorkbenchContainer) SSetSlotPacket(net.minecraft.network.play.server.SSetSlotPacket) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) WorkbenchContainerBridge(io.izzel.arclight.common.bridge.inventory.container.WorkbenchContainerBridge) ContainerBridge(io.izzel.arclight.common.bridge.inventory.container.ContainerBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 4 with ContainerBridge

use of io.izzel.arclight.common.bridge.inventory.container.ContainerBridge in project Arclight by IzzelAliz.

the class ServerPlayerEntityMixin method arclight$sendExtra.

@Inject(method = "sendAllContents", at = @At("RETURN"))
private void arclight$sendExtra(Container container, NonNullList<ItemStack> itemsList, CallbackInfo ci) {
    ArclightCaptures.captureContainerOwner((ServerPlayerEntity) (Object) this);
    if (EnumSet.of(InventoryType.CRAFTING, InventoryType.WORKBENCH).contains(((ContainerBridge) container).bridge$getBukkitView().getType())) {
        this.connection.sendPacket(new SSetSlotPacket(container.windowId, 0, container.getSlot(0).getStack()));
    }
    ArclightCaptures.resetContainerOwner();
}
Also used : SSetSlotPacket(net.minecraft.network.play.server.SSetSlotPacket) ContainerBridge(io.izzel.arclight.common.bridge.inventory.container.ContainerBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 5 with ContainerBridge

use of io.izzel.arclight.common.bridge.inventory.container.ContainerBridge in project Arclight by IzzelAliz.

the class ServerPlayNetHandlerMixin method processClickWindow.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void processClickWindow(CClickWindowPacket packet) {
    PacketThreadUtil.checkThreadAndEnqueue(packet, (ServerPlayNetHandler) (Object) this, this.player.getServerWorld());
    if (((ServerPlayerEntityBridge) this.player).bridge$isMovementBlocked()) {
        return;
    }
    this.player.markPlayerActive();
    if (this.player.openContainer.windowId == packet.getWindowId() && this.player.openContainer.getCanCraft(this.player) && this.player.openContainer.canInteractWith(this.player)) {
        boolean cancelled = this.player.isSpectator();
        if (packet.getSlotId() < -1 && packet.getSlotId() != -999) {
            return;
        }
        InventoryView inventory = ((ContainerBridge) this.player.openContainer).bridge$getBukkitView();
        InventoryType.SlotType type = inventory.getSlotType(packet.getSlotId());
        org.bukkit.event.inventory.ClickType click = org.bukkit.event.inventory.ClickType.UNKNOWN;
        InventoryAction action = InventoryAction.UNKNOWN;
        ItemStack itemstack = ItemStack.EMPTY;
        switch(packet.getClickType()) {
            case PICKUP:
                {
                    if (packet.getUsedButton() == 0) {
                        click = org.bukkit.event.inventory.ClickType.LEFT;
                    } else if (packet.getUsedButton() == 1) {
                        click = org.bukkit.event.inventory.ClickType.RIGHT;
                    }
                    if (packet.getUsedButton() != 0 && packet.getUsedButton() != 1) {
                        break;
                    }
                    action = InventoryAction.NOTHING;
                    if (packet.getSlotId() == -999) {
                        if (!this.player.inventory.getItemStack().isEmpty()) {
                            action = ((packet.getUsedButton() == 0) ? InventoryAction.DROP_ALL_CURSOR : InventoryAction.DROP_ONE_CURSOR);
                            break;
                        }
                        break;
                    } else {
                        if (packet.getSlotId() < 0) {
                            action = InventoryAction.NOTHING;
                            break;
                        }
                        Slot slot = this.player.openContainer.getSlot(packet.getSlotId());
                        if (slot == null) {
                            break;
                        }
                        ItemStack clickedItem = slot.getStack();
                        ItemStack cursor = this.player.inventory.getItemStack();
                        if (clickedItem.isEmpty()) {
                            if (!cursor.isEmpty()) {
                                action = ((packet.getUsedButton() == 0) ? InventoryAction.PLACE_ALL : InventoryAction.PLACE_ONE);
                                break;
                            }
                            break;
                        } else {
                            if (!slot.canTakeStack(this.player)) {
                                break;
                            }
                            if (cursor.isEmpty()) {
                                action = ((packet.getUsedButton() == 0) ? InventoryAction.PICKUP_ALL : InventoryAction.PICKUP_HALF);
                                break;
                            }
                            if (slot.isItemValid(cursor)) {
                                if (clickedItem.isItemEqual(cursor) && ItemStack.areItemStackTagsEqual(clickedItem, cursor)) {
                                    int toPlace = (packet.getUsedButton() == 0) ? cursor.getCount() : 1;
                                    toPlace = Math.min(toPlace, clickedItem.getMaxStackSize() - clickedItem.getCount());
                                    toPlace = Math.min(toPlace, slot.inventory.getInventoryStackLimit() - clickedItem.getCount());
                                    if (toPlace == 1) {
                                        action = InventoryAction.PLACE_ONE;
                                        break;
                                    }
                                    if (toPlace == cursor.getCount()) {
                                        action = InventoryAction.PLACE_ALL;
                                        break;
                                    }
                                    if (toPlace < 0) {
                                        action = ((toPlace != -1) ? InventoryAction.PICKUP_SOME : InventoryAction.PICKUP_ONE);
                                        break;
                                    }
                                    if (toPlace != 0) {
                                        action = InventoryAction.PLACE_SOME;
                                        break;
                                    }
                                    break;
                                } else {
                                    if (cursor.getCount() <= slot.getSlotStackLimit()) {
                                        action = InventoryAction.SWAP_WITH_CURSOR;
                                        break;
                                    }
                                    break;
                                }
                            } else {
                                if (cursor.getItem() == clickedItem.getItem() && ItemStack.areItemStackTagsEqual(cursor, clickedItem) && clickedItem.getCount() >= 0 && clickedItem.getCount() + cursor.getCount() <= cursor.getMaxStackSize()) {
                                    action = InventoryAction.PICKUP_ALL;
                                    break;
                                }
                                break;
                            }
                        }
                    }
                }
            case QUICK_MOVE:
                {
                    if (packet.getUsedButton() == 0) {
                        click = org.bukkit.event.inventory.ClickType.SHIFT_LEFT;
                    } else if (packet.getUsedButton() == 1) {
                        click = org.bukkit.event.inventory.ClickType.SHIFT_RIGHT;
                    }
                    if (packet.getUsedButton() != 0 && packet.getUsedButton() != 1) {
                        break;
                    }
                    if (packet.getSlotId() < 0) {
                        action = InventoryAction.NOTHING;
                        break;
                    }
                    Slot slot = this.player.openContainer.getSlot(packet.getSlotId());
                    if (slot != null && slot.canTakeStack(this.player) && slot.getHasStack()) {
                        action = InventoryAction.MOVE_TO_OTHER_INVENTORY;
                        break;
                    }
                    action = InventoryAction.NOTHING;
                    break;
                }
            case SWAP:
                {
                    if (packet.getUsedButton() < 0 || packet.getUsedButton() >= 9) {
                        break;
                    }
                    click = org.bukkit.event.inventory.ClickType.NUMBER_KEY;
                    Slot clickedSlot = this.player.openContainer.getSlot(packet.getSlotId());
                    if (!clickedSlot.canTakeStack(this.player)) {
                        action = InventoryAction.NOTHING;
                        break;
                    }
                    ItemStack hotbar = this.player.inventory.getStackInSlot(packet.getUsedButton());
                    boolean canCleanSwap = hotbar.isEmpty() || (clickedSlot.inventory == this.player.inventory && clickedSlot.isItemValid(hotbar));
                    if (clickedSlot.getHasStack()) {
                        if (canCleanSwap) {
                            action = InventoryAction.HOTBAR_SWAP;
                            break;
                        }
                        action = InventoryAction.HOTBAR_MOVE_AND_READD;
                        break;
                    } else {
                        if (!clickedSlot.getHasStack() && !hotbar.isEmpty() && clickedSlot.isItemValid(hotbar)) {
                            action = InventoryAction.HOTBAR_SWAP;
                            break;
                        }
                        action = InventoryAction.NOTHING;
                        break;
                    }
                }
            case CLONE:
                {
                    if (packet.getUsedButton() != 2) {
                        click = org.bukkit.event.inventory.ClickType.UNKNOWN;
                        action = InventoryAction.UNKNOWN;
                        break;
                    }
                    click = org.bukkit.event.inventory.ClickType.MIDDLE;
                    if (packet.getSlotId() < 0) {
                        action = InventoryAction.NOTHING;
                        break;
                    }
                    Slot slot = this.player.openContainer.getSlot(packet.getSlotId());
                    if (slot != null && slot.getHasStack() && this.player.abilities.isCreativeMode && this.player.inventory.getItemStack().isEmpty()) {
                        action = InventoryAction.CLONE_STACK;
                        break;
                    }
                    action = InventoryAction.NOTHING;
                    break;
                }
            case THROW:
                {
                    if (packet.getSlotId() < 0) {
                        click = org.bukkit.event.inventory.ClickType.LEFT;
                        if (packet.getUsedButton() == 1) {
                            click = org.bukkit.event.inventory.ClickType.RIGHT;
                        }
                        action = InventoryAction.NOTHING;
                        break;
                    }
                    if (packet.getUsedButton() == 0) {
                        click = org.bukkit.event.inventory.ClickType.DROP;
                        Slot slot = this.player.openContainer.getSlot(packet.getSlotId());
                        if (slot != null && slot.getHasStack() && slot.canTakeStack(this.player) && !slot.getStack().isEmpty() && slot.getStack().getItem() != Item.getItemFromBlock(Blocks.AIR)) {
                            action = InventoryAction.DROP_ONE_SLOT;
                            break;
                        }
                        action = InventoryAction.NOTHING;
                        break;
                    } else {
                        if (packet.getUsedButton() != 1) {
                            break;
                        }
                        click = org.bukkit.event.inventory.ClickType.CONTROL_DROP;
                        Slot slot = this.player.openContainer.getSlot(packet.getSlotId());
                        if (slot != null && slot.getHasStack() && slot.canTakeStack(this.player) && !slot.getStack().isEmpty() && slot.getStack().getItem() != Item.getItemFromBlock(Blocks.AIR)) {
                            action = InventoryAction.DROP_ALL_SLOT;
                            break;
                        }
                        action = InventoryAction.NOTHING;
                        break;
                    }
                }
            case QUICK_CRAFT:
                {
                    itemstack = this.player.openContainer.slotClick(packet.getSlotId(), packet.getUsedButton(), packet.getClickType(), this.player);
                    break;
                }
            case PICKUP_ALL:
                {
                    click = org.bukkit.event.inventory.ClickType.DOUBLE_CLICK;
                    action = InventoryAction.NOTHING;
                    if (packet.getSlotId() < 0 || this.player.inventory.getItemStack().isEmpty()) {
                        break;
                    }
                    ItemStack cursor2 = this.player.inventory.getItemStack();
                    action = InventoryAction.NOTHING;
                    if (inventory.getTopInventory().contains(CraftMagicNumbers.getMaterial(cursor2.getItem())) || inventory.getBottomInventory().contains(CraftMagicNumbers.getMaterial(cursor2.getItem()))) {
                        action = InventoryAction.COLLECT_TO_CURSOR;
                        break;
                    }
                    break;
                }
        }
        if (packet.getClickType() != net.minecraft.inventory.container.ClickType.QUICK_CRAFT) {
            InventoryClickEvent event;
            if (click == org.bukkit.event.inventory.ClickType.NUMBER_KEY) {
                event = new InventoryClickEvent(inventory, type, packet.getSlotId(), click, action, packet.getUsedButton());
            } else {
                event = new InventoryClickEvent(inventory, type, packet.getSlotId(), click, action);
            }
            Inventory top = inventory.getTopInventory();
            if (packet.getSlotId() == 0 && top instanceof org.bukkit.inventory.CraftingInventory) {
                Recipe recipe = ((org.bukkit.inventory.CraftingInventory) top).getRecipe();
                if (recipe != null) {
                    if (click == org.bukkit.event.inventory.ClickType.NUMBER_KEY) {
                        event = new CraftItemEvent(recipe, inventory, type, packet.getSlotId(), click, action, packet.getUsedButton());
                    } else {
                        event = new CraftItemEvent(recipe, inventory, type, packet.getSlotId(), click, action);
                    }
                }
            }
            event.setCancelled(cancelled);
            Container oldContainer = this.player.openContainer;
            this.server.getPluginManager().callEvent(event);
            if (this.player.openContainer != oldContainer) {
                return;
            }
            switch(event.getResult()) {
                case DEFAULT:
                case ALLOW:
                    {
                        itemstack = this.player.openContainer.slotClick(packet.getSlotId(), packet.getUsedButton(), packet.getClickType(), this.player);
                        break;
                    }
                case DENY:
                    {
                        switch(action) {
                            case PICKUP_ALL:
                            case MOVE_TO_OTHER_INVENTORY:
                            case HOTBAR_MOVE_AND_READD:
                            case HOTBAR_SWAP:
                            case COLLECT_TO_CURSOR:
                            case UNKNOWN:
                                {
                                    this.player.sendContainerToPlayer(this.player.openContainer);
                                    break;
                                }
                            case PICKUP_SOME:
                            case PICKUP_HALF:
                            case PICKUP_ONE:
                            case PLACE_ALL:
                            case PLACE_SOME:
                            case PLACE_ONE:
                            case SWAP_WITH_CURSOR:
                                {
                                    this.player.connection.sendPacket(new SSetSlotPacket(-1, -1, this.player.inventory.getItemStack()));
                                    this.player.connection.sendPacket(new SSetSlotPacket(this.player.openContainer.windowId, packet.getSlotId(), this.player.openContainer.getSlot(packet.getSlotId()).getStack()));
                                    break;
                                }
                            case DROP_ALL_SLOT:
                            case DROP_ONE_SLOT:
                                {
                                    this.player.connection.sendPacket(new SSetSlotPacket(this.player.openContainer.windowId, packet.getSlotId(), this.player.openContainer.getSlot(packet.getSlotId()).getStack()));
                                    break;
                                }
                            case DROP_ALL_CURSOR:
                            case DROP_ONE_CURSOR:
                            case CLONE_STACK:
                                {
                                    this.player.connection.sendPacket(new SSetSlotPacket(-1, -1, this.player.inventory.getItemStack()));
                                    break;
                                }
                        }
                        return;
                    }
            }
            if (event instanceof CraftItemEvent) {
                this.player.sendContainerToPlayer(this.player.openContainer);
            }
        }
        if (ItemStack.areItemStacksEqual(packet.getClickedItem(), itemstack)) {
            this.player.connection.sendPacket(new SConfirmTransactionPacket(packet.getWindowId(), packet.getActionNumber(), true));
            this.player.isChangingQuantityOnly = true;
            this.player.openContainer.detectAndSendChanges();
            this.player.updateHeldItem();
            this.player.isChangingQuantityOnly = false;
        } else {
            this.pendingTransactions.put(this.player.openContainer.windowId, packet.getActionNumber());
            this.player.connection.sendPacket(new SConfirmTransactionPacket(packet.getWindowId(), packet.getActionNumber(), false));
            this.player.openContainer.setCanCraft(this.player, false);
            NonNullList<ItemStack> nonnulllist1 = NonNullList.create();
            for (int j = 0; j < this.player.openContainer.inventorySlots.size(); ++j) {
                ItemStack itemstack2 = this.player.openContainer.inventorySlots.get(j).getStack();
                nonnulllist1.add(itemstack2.isEmpty() ? ItemStack.EMPTY : itemstack2);
            }
            this.player.sendAllContents(this.player.openContainer, nonnulllist1);
        }
    }
}
Also used : Recipe(org.bukkit.inventory.Recipe) ContainerBridge(io.izzel.arclight.common.bridge.inventory.container.ContainerBridge) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) SConfirmTransactionPacket(net.minecraft.network.play.server.SConfirmTransactionPacket) MerchantContainer(net.minecraft.inventory.container.MerchantContainer) Container(net.minecraft.inventory.container.Container) SSetSlotPacket(net.minecraft.network.play.server.SSetSlotPacket) CraftItemEvent(org.bukkit.event.inventory.CraftItemEvent) InventoryType(org.bukkit.event.inventory.InventoryType) InventoryAction(org.bukkit.event.inventory.InventoryAction) InventoryClickEvent(org.bukkit.event.inventory.InventoryClickEvent) InventoryView(org.bukkit.inventory.InventoryView) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) Slot(net.minecraft.inventory.container.Slot) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) PlayerInventory(net.minecraft.entity.player.PlayerInventory) Inventory(org.bukkit.inventory.Inventory) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

ContainerBridge (io.izzel.arclight.common.bridge.inventory.container.ContainerBridge)6 SSetSlotPacket (net.minecraft.network.play.server.SSetSlotPacket)4 Overwrite (org.spongepowered.asm.mixin.Overwrite)4 Container (net.minecraft.inventory.container.Container)3 ItemStack (net.minecraft.item.ItemStack)3 InventoryView (org.bukkit.inventory.InventoryView)3 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 CraftItemStack (org.bukkit.craftbukkit.v.inventory.CraftItemStack)2 InventoryType (org.bukkit.event.inventory.InventoryType)2 ServerPlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge)1 IInventoryBridge (io.izzel.arclight.common.bridge.inventory.IInventoryBridge)1 WorkbenchContainerBridge (io.izzel.arclight.common.bridge.inventory.container.WorkbenchContainerBridge)1 PlayerInventory (net.minecraft.entity.player.PlayerInventory)1 HorseInventoryContainer (net.minecraft.inventory.container.HorseInventoryContainer)1 MerchantContainer (net.minecraft.inventory.container.MerchantContainer)1 Slot (net.minecraft.inventory.container.Slot)1 WorkbenchContainer (net.minecraft.inventory.container.WorkbenchContainer)1 ICraftingRecipe (net.minecraft.item.crafting.ICraftingRecipe)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 SConfirmTransactionPacket (net.minecraft.network.play.server.SConfirmTransactionPacket)1