Search in sources :

Example 11 with IMixinContainer

use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callCraftEventPost.

public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
    // Get previous cursor if captured
    ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
    if (previousCursor == null) {
        // or get the current one
        previousCursor = player.inventory.getItemStack();
    }
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
    CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
    SpongeImpl.postEvent(event);
    ((IMixinContainer) container).setCaptureInventory(false);
    // handle slot-transactions
    PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
    if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
        // handle cursor-transaction
        ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
        player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
        if (player instanceof EntityPlayerMP) {
            ((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
        }
    }
    transactions.clear();
    ((IMixinContainer) container).setCaptureInventory(true);
    return event;
}
Also used : IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) Container(net.minecraft.inventory.Container) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack)

Example 12 with IMixinContainer

use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method displayContainer.

@Nullable
public static Container displayContainer(EntityPlayerMP player, Inventory inventory) {
    net.minecraft.inventory.Container previousContainer = player.openContainer;
    net.minecraft.inventory.Container container;
    if (inventory instanceof CustomInventory) {
        if (!checkValidVanillaCustomInventory(((CustomInventory) inventory))) {
            // Invalid size for vanilla inventory ; This is to
            return null;
        // prevent crashing the client with invalid data
        }
    }
    if (inventory instanceof IInteractionObject) {
        final String guiId = ((IInteractionObject) inventory).getGuiID();
        switch(guiId) {
            case "EntityHorse":
                if (inventory instanceof CarriedInventory) {
                    CarriedInventory<?> cinventory = (CarriedInventory<?>) inventory;
                    if (cinventory.getCarrier().isPresent() && cinventory.getCarrier().get() instanceof AbstractHorse) {
                        player.openGuiHorseInventory(((AbstractHorse) cinventory.getCarrier().get()), (IInventory) inventory);
                    }
                }
                break;
            case "minecraft:chest":
                player.displayGUIChest((IInventory) inventory);
                break;
            case "minecraft:crafting_table":
            case "minecraft:anvil":
            case "minecraft:enchanting_table":
                player.displayGui((IInteractionObject) inventory);
                break;
            default:
                player.displayGUIChest((IInventory) inventory);
                break;
        }
    } else if (inventory instanceof IInventory) {
        player.displayGUIChest(((IInventory) inventory));
    } else {
        return null;
    }
    container = player.openContainer;
    if (previousContainer == container) {
        return null;
    }
    if (!callInteractInventoryOpenEvent(player)) {
        return null;
    }
    if (container instanceof IMixinContainer) {
        // 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 MixinContainerCanInteract ; but throws no errors for other
        // containers
        // Allow viewing inventory; except when dead
        ((IMixinContainer) container).setCanInteractWith(p -> !p.isDead);
    }
    return container;
}
Also used : IInventory(net.minecraft.inventory.IInventory) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) AbstractHorse(net.minecraft.entity.passive.AbstractHorse) IInteractionObject(net.minecraft.world.IInteractionObject) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) Container(net.minecraft.inventory.Container) Nullable(javax.annotation.Nullable)

Example 13 with IMixinContainer

use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.

public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
    Sponge.getCauseStackManager().pushCause(player);
    // Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
    // Due to this, we can only send the original item that was clicked in slot
    Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
    if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
        Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
        if (slot != null) {
            ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
            SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
            ((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
        }
    }
    ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    return event;
}
Also used : ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot)

Example 14 with IMixinContainer

use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.

the class DragInventoryStopState method unwindCraftPreview.

public static void unwindCraftPreview(InventoryPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    ((IMixinContainer) player.openContainer).setFirePreview(true);
    Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
    if (craftInv instanceof CraftingInventory) {
        List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
        if (!previewTransactions.isEmpty()) {
            CraftingRecipe recipe = SpongeCraftingRecipeRegistry.getInstance().findMatchingRecipe(((CraftingInventory) craftInv).getCraftingGrid(), ((World) player.world)).orElse(null);
            SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), recipe, player.openContainer, previewTransactions);
            previewTransactions.clear();
        }
    }
}
Also used : IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory)

Aggregations

IMixinContainer (org.spongepowered.common.interfaces.IMixinContainer)14 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)11 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)9 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)9 Transaction (org.spongepowered.api.data.Transaction)7 Entity (org.spongepowered.api.entity.Entity)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)7 ArrayList (java.util.ArrayList)5 EntityItem (net.minecraft.entity.item.EntityItem)5 ClickInventoryEvent (org.spongepowered.api.event.item.inventory.ClickInventoryEvent)5 ItemStack (org.spongepowered.api.item.inventory.ItemStack)5 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)4 Nullable (javax.annotation.Nullable)3 Container (net.minecraft.inventory.Container)3 IInventory (net.minecraft.inventory.IInventory)3 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)3 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)3 Inventory (org.spongepowered.api.item.inventory.Inventory)3 CraftingInventory (org.spongepowered.api.item.inventory.crafting.CraftingInventory)3 CraftingRecipe (org.spongepowered.api.item.recipe.crafting.CraftingRecipe)3