Search in sources :

Example 1 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project SpongeCommon by SpongePowered.

the class MixinSlotCrafting method afterTake.

/**
 * Create CraftItemEvent.Post result is also handled by
 * {@link MixinContainer#redirectTransferStackInSlot} or
 * {@link MixinContainer#redirectOnTakeThrow}
 */
@Inject(method = "onTake", cancellable = true, at = @At("RETURN"))
private void afterTake(EntityPlayer thePlayer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir) {
    if (((IMixinWorld) thePlayer.world).isFake()) {
        return;
    }
    ((IMixinContainer) thePlayer.openContainer).detectAndSendChanges(true);
    ((IMixinContainer) thePlayer.openContainer).setCaptureInventory(false);
    Container container = thePlayer.openContainer;
    Inventory craftInv = ((Inventory) container).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
    if (!(craftInv instanceof CraftingInventory)) {
        SpongeImpl.getLogger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
        return;
    }
    // retain only last slot-transactions on output slot
    SlotTransaction first = null;
    List<SlotTransaction> capturedTransactions = ((IMixinContainer) container).getCapturedTransactions();
    for (Iterator<SlotTransaction> iterator = capturedTransactions.iterator(); iterator.hasNext(); ) {
        SlotTransaction trans = iterator.next();
        Optional<SlotIndex> slotIndex = trans.getSlot().getInventoryProperty(SlotIndex.class);
        if (slotIndex.isPresent() && slotIndex.get().getValue() == 0) {
            iterator.remove();
            if (first == null) {
                first = trans;
            }
        }
    }
    ItemStackSnapshot craftedItem;
    // if we got a transaction on the crafting-slot use this
    if (first != null) {
        capturedTransactions.add(first);
        craftedItem = first.getOriginal().copy();
    } else {
        if (stack.isEmpty()) {
            // if stack is empty this was probably shift-crafting so we use the captured itemstack instead
            craftedItem = ItemStackUtil.snapshotOf(this.craftedStack);
        } else {
            craftedItem = ItemStackUtil.snapshotOf(stack);
        }
    }
    CraftingInventory craftingInventory = (CraftingInventory) craftInv;
    CraftItemEvent.Craft event = SpongeCommonEventFactory.callCraftEventPost(thePlayer, craftingInventory, craftedItem, this.lastRecipe, container, capturedTransactions);
    ((IMixinContainer) container).setLastCraft(event);
    ((IMixinContainer) container).setFirePreview(true);
    this.craftedStack = null;
    SlotTransaction last = new SlotTransaction(craftingInventory.getResult(), ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(this.getStack()));
    List<SlotTransaction> previewTransactions = ((IMixinContainer) container).getPreviewTransactions();
    CraftingRecipe newRecipe = Sponge.getRegistry().getCraftingRecipeRegistry().findMatchingRecipe(craftingInventory.getCraftingGrid(), ((World) player.world)).orElse(null);
    SpongeCommonEventFactory.callCraftEventPre(thePlayer, craftingInventory, last, newRecipe, container, previewTransactions);
    previewTransactions.clear();
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) Container(net.minecraft.inventory.Container) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) IInventory(net.minecraft.inventory.IInventory) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project SpongeCommon by SpongePowered.

the class InventoryArchetypeRegistryModule method registerDefaults.

@Override
public void registerDefaults() {
    InventoryArchetype SLOT = new SlotArchetype(ImmutableMap.of(CustomInventory.INVENTORY_DIMENSION, new InventoryDimension(1, 1)));
    InventoryArchetype MENU_ROW;
    InventoryArchetype MENU_COLUMN;
    InventoryArchetype MENU_GRID;
    InventoryArchetype CHEST;
    InventoryArchetype DOUBLE_CHEST;
    InventoryArchetype FURNACE;
    InventoryArchetype DISPENSER;
    InventoryArchetype WORKBENCH;
    InventoryArchetype BREWING_STAND;
    InventoryArchetype HOPPER;
    InventoryArchetype BEACON;
    InventoryArchetype ANVIL;
    InventoryArchetype ENCHANTING_TABLE;
    InventoryArchetype VILLAGER;
    InventoryArchetype HORSE;
    InventoryArchetype HORSE_WITH_CHEST;
    InventoryArchetype PLAYER;
    InventoryArchetype CRAFTING;
    InventoryArchetype UNKNOWN;
    final SpongeInventoryArchetypeBuilder builder = new SpongeInventoryArchetypeBuilder();
    for (int i = 0; i < 9; i++) {
        builder.with(new SpongeInventoryArchetypeBuilder().from(SLOT).property(SlotIndex.of(i)).build("minecraft:slot" + i, "Slot"));
    }
    MENU_ROW = builder.property(new InventoryDimension(9, 1)).build("sponge:menu_row", "Menu Row");
    MENU_COLUMN = builder.property(new InventoryDimension(9, 1)).build("sponge:menu_column", "Menu Column");
    MENU_GRID = builder.reset().with(MENU_ROW).with(MENU_ROW).with(MENU_ROW).property(new InventoryDimension(9, 3)).build("sponge:menu_grid", "Menu Grid");
    CHEST = builder.reset().with(MENU_GRID).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.chest")))).property(new GuiIdProperty(GuiIds.CHEST)).container((i, p) -> new ContainerChest(p.inventory, i, p)).build("minecraft:chest", "Chest");
    DOUBLE_CHEST = builder.reset().with(CHEST).property(new InventoryDimension(9, 6)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.chestDouble")))).property(new GuiIdProperty(GuiIds.CHEST)).container((i, p) -> new ContainerChest(p.inventory, i, p)).build("minecraft:double_chest", "DoubleChest");
    FURNACE = builder.reset().with(new SpongeInventoryArchetypeBuilder().from(SLOT).property(new SlotIndex(0)).build("minecraft:furnace_input", "FurnaceInput")).with(new SpongeInventoryArchetypeBuilder().from(SLOT).property(new SlotIndex(1)).property(AcceptsItems.of()).build("minecraft:furnace_fuel", "FurnaceFuel")).with(new SpongeInventoryArchetypeBuilder().from(SLOT).property(new SlotIndex(2)).property(AcceptsItems.of()).build("minecraft:furnace_output", "FurnaceOutput")).property(new InventoryTitle(Text.of(new SpongeTranslation("container.furnace")))).property(new InventoryDimension(3, 1)).property(new GuiIdProperty(GuiIds.FURNACE)).container((i, p) -> new ContainerFurnace(p.inventory, i)).build("minecraft:furnace", "Furnace");
    DISPENSER = builder.reset().with(MENU_GRID).property(new InventoryDimension(3, 3)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.dispenser")))).property(new GuiIdProperty(GuiIds.DISPENSER)).container((i, p) -> new ContainerDispenser(p.inventory, i)).build("minecraft:dispenser", "Dispenser");
    WORKBENCH = builder.reset().with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(3, 3)).build("minecraft:workbench_grid", "Workbench Grid")).with(SLOT).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.crafting")))).property(new GuiIdProperty(GuiIds.CRAFTING_TABLE)).container((i, p) -> {
        ContainerWorkbench container = new ContainerWorkbench(p.inventory, p.getEntityWorld(), p.getPosition());
        // Pre-Fills the container input with the items from the inventory
        for (int index = 0; index < container.craftMatrix.getSizeInventory(); index++) {
            container.craftMatrix.setInventorySlotContents(index, i.getStackInSlot(index));
        }
        return container;
    }).build("minecraft:workbench", "Workbench");
    BREWING_STAND = builder.reset().with(MENU_ROW).property(new InventoryDimension(5, 1)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.brewing")))).property(new GuiIdProperty(GuiIds.BREWING_STAND)).container((i, p) -> new ContainerBrewingStand(p.inventory, i)).build("minecraft:brewing_stand", "BrewingStand");
    HOPPER = builder.reset().with(MENU_ROW).property(new InventoryDimension(5, 1)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.hopper")))).property(new GuiIdProperty(GuiIds.HOPPER)).container((i, p) -> new ContainerHopper(p.inventory, i, p)).build("minecraft:hopper", "Hopper");
    BEACON = builder.reset().with(SLOT).property(new InventoryDimension(1, 1)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.beacon")))).property(new GuiIdProperty(GuiIds.BEACON)).container((i, p) -> new ContainerBeacon(p.inventory, i)).build("minecraft:beacon", "Beacon");
    ENCHANTING_TABLE = builder.reset().with(SLOT).with(SLOT).property(new InventoryDimension(2, 1)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.enchant")))).property(new GuiIdProperty(GuiIds.ENCHANTING_TABLE)).container((i, p) -> {
        ContainerEnchantment container = new ContainerEnchantment(p.inventory, p.getEntityWorld(), p.getPosition());
        // Pre-Fills the container with the items from the inventory
        for (int index = 0; index < container.tableInventory.getSizeInventory(); index++) {
            container.tableInventory.setInventorySlotContents(index, i.getStackInSlot(index));
        }
        return container;
    }).build("minecraft:enchanting_table", "EnchantingTable");
    ANVIL = builder.reset().with(SLOT).with(SLOT).with(SLOT).property(new InventoryDimension(3, 1)).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.repair")))).property(new GuiIdProperty(GuiIds.ANVIL)).container((i, p) -> {
        ContainerRepair container = new ContainerRepair(p.inventory, p.getEntityWorld(), p.getPosition(), p);
        // Pre-Fills the container input with the items from the inventory
        for (int index = 0; index < container.inputSlots.getSizeInventory(); index++) {
            container.inputSlots.setInventorySlotContents(index, i.getStackInSlot(index));
        }
        return container;
    }).build("minecraft:anvil", "Anvil");
    VILLAGER = builder.reset().with(SLOT).with(SLOT).with(SLOT).property(new InventoryDimension(3, 1)).property(new GuiIdProperty(GuiIds.VILLAGER)).container((i, p) -> {
        if (i instanceof CarriedInventory && ((CarriedInventory) i).getCarrier().isPresent() && ((CarriedInventory) i).getCarrier().get() instanceof IMerchant) {
            IMerchant merchant = ((IMerchant) ((CarriedInventory) i).getCarrier().get());
            ContainerMerchant container = new ContainerMerchant(p.inventory, merchant, p.getEntityWorld());
            // TODO Pre-Fill the Container?
            return container;
        }
        throw new IllegalArgumentException("Cannot open merchant inventory without a merchant as Carrier");
    }).build("minecraft:villager", "Villager");
    HORSE = builder.reset().with(SLOT).with(SLOT).property(new InventoryDimension(2, 1)).property(// hardcoded openGuiHorseInventory
    new GuiIdProperty(GuiIds.HORSE)).container((i, p) -> {
        if (i instanceof CarriedInventory && ((CarriedInventory) i).getCarrier().isPresent() && ((CarriedInventory) i).getCarrier().get() instanceof AbstractHorse) {
            AbstractHorse horse = ((AbstractHorse) ((CarriedInventory) i).getCarrier().get());
            return new ContainerHorseInventory(p.inventory, i, horse, p);
        }
        throw new IllegalArgumentException("Cannot open horse inventory without a horse as Carrier");
    }).build("minecraft:horse", "Horse");
    HORSE_WITH_CHEST = builder.reset().with(HORSE).with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(5, 3)).build("horse_grid", "HorseGrid")).property(// hardcoded openGuiHorseInventory
    new GuiIdProperty(GuiIds.HORSE)).container((i, p) -> {
        if (i instanceof CarriedInventory && ((CarriedInventory) i).getCarrier().isPresent() && ((CarriedInventory) i).getCarrier().get() instanceof AbstractHorse) {
            AbstractHorse horse = ((AbstractHorse) ((CarriedInventory) i).getCarrier().get());
            // TODO size
            return new ContainerHorseInventory(p.inventory, i, horse, p);
        }
        throw new IllegalArgumentException("Cannot open horse inventory without a horse as Carrier");
    }).build("minecraft:horse_with_chest", "Horse with Chest");
    CRAFTING = builder.reset().with(SLOT).with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(2, 2)).build("minecraft:crafting_grid", "Crafting Grid")).property(InventoryTitle.of(Text.of(new SpongeTranslation("container.crafting")))).build("minecraft:crafting", "Crafting");
    PLAYER = builder.reset().with(CRAFTING).with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(1, 4)).build("minecraft:armor", "Armor")).with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(9, 3)).build("minecraft:player_main", "Player Main")).with(new SpongeInventoryArchetypeBuilder().from(MENU_GRID).property(new InventoryDimension(9, 1)).build("minecraft:player_hotbar", "Player Hotbar")).with(new SpongeInventoryArchetypeBuilder().from(SLOT).property(new InventoryDimension(1, 1)).build("minecraft:player_offhand", "Player Offhand")).build("minecraft:player", "Player");
    UNKNOWN = builder.reset().build("minecraft:unknown", "UKNOWN");
    registerAdditionalCatalog(SLOT);
    registerAdditionalCatalog(MENU_ROW);
    registerAdditionalCatalog(MENU_COLUMN);
    registerAdditionalCatalog(MENU_GRID);
    registerAdditionalCatalog(CHEST);
    SpongeInventoryBuilder.registerInventory(TileEntityChest.class, CHEST);
    SpongeInventoryBuilder.registerContainer(ContainerChest.class, CHEST);
    registerAdditionalCatalog(DOUBLE_CHEST);
    registerAdditionalCatalog(FURNACE);
    SpongeInventoryBuilder.registerInventory(TileEntityFurnace.class, FURNACE);
    SpongeInventoryBuilder.registerContainer(ContainerFurnace.class, FURNACE);
    registerAdditionalCatalog(DISPENSER);
    SpongeInventoryBuilder.registerInventory(TileEntityDispenser.class, DISPENSER);
    SpongeInventoryBuilder.registerInventory(TileEntityDropper.class, DISPENSER);
    SpongeInventoryBuilder.registerContainer(ContainerDispenser.class, DISPENSER);
    registerAdditionalCatalog(WORKBENCH);
    SpongeInventoryBuilder.registerContainer(ContainerWorkbench.class, WORKBENCH);
    registerAdditionalCatalog(BREWING_STAND);
    SpongeInventoryBuilder.registerInventory(TileEntityBrewingStand.class, BREWING_STAND);
    SpongeInventoryBuilder.registerContainer(ContainerBrewingStand.class, BREWING_STAND);
    registerAdditionalCatalog(HOPPER);
    SpongeInventoryBuilder.registerInventory(TileEntityHopper.class, HOPPER);
    SpongeInventoryBuilder.registerContainer(ContainerHopper.class, HOPPER);
    registerAdditionalCatalog(BEACON);
    SpongeInventoryBuilder.registerInventory(TileEntityBeacon.class, BEACON);
    SpongeInventoryBuilder.registerContainer(ContainerBeacon.class, BEACON);
    registerAdditionalCatalog(ENCHANTING_TABLE);
    SpongeInventoryBuilder.registerContainer(ContainerEnchantment.class, ENCHANTING_TABLE);
    registerAdditionalCatalog(ANVIL);
    SpongeInventoryBuilder.registerContainer(ContainerRepair.class, ANVIL);
    registerAdditionalCatalog(VILLAGER);
    // TODO internal Villager Inventory? make Villager Carrier?
    SpongeInventoryBuilder.registerContainer(ContainerMerchant.class, VILLAGER);
    registerAdditionalCatalog(HORSE);
    // TODO Horse IInventory? SpongeInventoryBuilder.registerInventory(EntityHorse.class, HORSE);
    SpongeInventoryBuilder.registerContainer(ContainerHorseInventory.class, HORSE);
    registerAdditionalCatalog(HORSE_WITH_CHEST);
    registerAdditionalCatalog(CRAFTING);
    registerAdditionalCatalog(PLAYER);
    registerAdditionalCatalog(UNKNOWN);
    // Helper Archetypes for Menu
    InventoryArchetype MENU_ICON;
    InventoryArchetype MENU_BUTTON;
    InventoryArchetype MENU_CHECKBOX;
    InventoryArchetype MENU_SPINNER;
    MENU_ICON = builder.reset().with(SLOT).build("sponge:menu_icon", "Menu Icon");
    MENU_BUTTON = builder.reset().with(MENU_ICON).build("sponge:menu_button", "Menu Button");
    MENU_CHECKBOX = builder.reset().with(MENU_ICON).build("sponge:menu_checkbox", "Menu Checkbox");
    MENU_SPINNER = builder.reset().with(MENU_ICON).build("sponge:menu_spinner", "Menu Spinner");
    registerAdditionalCatalog(MENU_ICON);
    registerAdditionalCatalog(MENU_BUTTON);
    registerAdditionalCatalog(MENU_CHECKBOX);
    registerAdditionalCatalog(MENU_SPINNER);
}
Also used : SlotArchetype(org.spongepowered.common.item.inventory.archetype.SlotArchetype) AbstractHorse(net.minecraft.entity.passive.AbstractHorse) SpongeTranslation(org.spongepowered.common.text.translation.SpongeTranslation) GuiIdProperty(org.spongepowered.api.item.inventory.property.GuiIdProperty) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) ContainerDispenser(net.minecraft.inventory.ContainerDispenser) ContainerHopper(net.minecraft.inventory.ContainerHopper) ContainerBeacon(net.minecraft.inventory.ContainerBeacon) IMerchant(net.minecraft.entity.IMerchant) ContainerHorseInventory(net.minecraft.inventory.ContainerHorseInventory) ContainerBrewingStand(net.minecraft.inventory.ContainerBrewingStand) SpongeInventoryArchetypeBuilder(org.spongepowered.common.item.inventory.archetype.SpongeInventoryArchetypeBuilder) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) ContainerWorkbench(net.minecraft.inventory.ContainerWorkbench) ContainerRepair(net.minecraft.inventory.ContainerRepair) ContainerFurnace(net.minecraft.inventory.ContainerFurnace) ContainerChest(net.minecraft.inventory.ContainerChest) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) ContainerMerchant(net.minecraft.inventory.ContainerMerchant) InventoryDimension(org.spongepowered.api.item.inventory.property.InventoryDimension) InventoryArchetype(org.spongepowered.api.item.inventory.InventoryArchetype) ContainerEnchantment(net.minecraft.inventory.ContainerEnchantment)

Example 3 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project SpongeCommon by SpongePowered.

the class FurnaceInventoryLens method init.

@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
    this.addChild(new OrderedInventoryLensImpl(0, 3, 1, slots));
    this.input = new InputSlotLensImpl(0, (i) -> true, (i) -> true);
    // TODO SlotFurnaceFuel
    this.fuel = new FuelSlotLensImpl(1, (i) -> true, (i) -> true);
    // SlotFurnaceOutput
    this.output = new OutputSlotLensImpl(2, (i) -> false, (i) -> false);
    this.addSpanningChild(this.input, new SlotIndex(0));
    this.addSpanningChild(this.fuel, new SlotIndex(1));
    this.addSpanningChild(this.output, new SlotIndex(2));
}
Also used : ItemStack(net.minecraft.item.ItemStack) InventoryAdapter(org.spongepowered.common.item.inventory.adapter.InventoryAdapter) SlotProvider(org.spongepowered.common.item.inventory.lens.SlotProvider) InputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.InputSlotLensImpl) IInventory(net.minecraft.inventory.IInventory) FuelSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.FuelSlotLensImpl) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) RealLens(org.spongepowered.common.item.inventory.lens.impl.RealLens) OutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.OutputSlotLensImpl) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) InputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.InputSlotLensImpl) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) FuelSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.FuelSlotLensImpl) OutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.OutputSlotLensImpl)

Example 4 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project SpongeCommon by SpongePowered.

the class InventoryQueryTest method onInitialization.

@Listener
public void onInitialization(GameInitializationEvent e) {
    CommandSpec inventoryType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory hotbar = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
        src.sendMessage(Text.of("You have ", hotbar.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec itemType = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory sticks = inventory.query(QueryOperationTypes.ITEM_TYPE.of(ItemTypes.STICK));
        src.sendMessage(Text.of("You have ", sticks.totalItems(), " sticks in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackGeneral = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.totalItems(), " lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackSpecific = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        ItemStack lapis = ItemStack.of(ItemTypes.DYE, 4);
        lapis.offer(Keys.DYE_COLOR, DyeColors.BLUE);
        Inventory lapisItems = inventory.query(QueryOperationTypes.ITEM_STACK_EXACT.of(lapis));
        src.sendMessage(Text.of("You have ", lapisItems.size(), " stacks of 4 lapis lazuli in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec itemStackCustom = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory evenCountStacks = inventory.query(QueryOperationTypes.ITEM_STACK_CUSTOM.of(x -> x.getQuantity() > 0 && x.getQuantity() % 2 == 0));
        src.sendMessage(Text.of("You have ", evenCountStacks.size(), " stacks with an even number of items in your inventory."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryProperty = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_PROPERTY.of(new SlotIndex(3, Property.Operator.LESS)));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in the first 3 slots of your hotbar."));
        return CommandResult.success();
    }).build();
    CommandSpec inventoryTranslation = CommandSpec.builder().executor((src, args) -> {
        Inventory inventory = getPlayerInventory(src);
        Inventory slots = ((PlayerInventory) inventory).getHotbar().query(QueryOperationTypes.INVENTORY_TRANSLATION.of(Sponge.getRegistry().getTranslationById("slot.name").get()));
        src.sendMessage(Text.of("You have ", slots.totalItems(), " items in your hotbar."));
        return CommandResult.success();
    }).build();
    Sponge.getCommandManager().register(this, CommandSpec.builder().child(inventoryType, "inventorytype").child(itemType, "itemtype").child(itemStackGeneral, "itemstackgeneral").child(itemStackSpecific, "itemstackspecific").child(itemStackCustom, "itemstackcustom").child(inventoryProperty, "inventoryproperty").child(inventoryTranslation, "inventorytranslation").build(), "invquery");
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) CommandResult(org.spongepowered.api.command.CommandResult) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Keys(org.spongepowered.api.data.key.Keys) CommandSource(org.spongepowered.api.command.CommandSource) Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) GameInitializationEvent(org.spongepowered.api.event.game.state.GameInitializationEvent) ItemTypes(org.spongepowered.api.item.ItemTypes) Sponge(org.spongepowered.api.Sponge) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) Plugin(org.spongepowered.api.plugin.Plugin) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DyeColors(org.spongepowered.api.data.type.DyeColors) QueryOperationTypes(org.spongepowered.api.item.inventory.query.QueryOperationTypes) Text(org.spongepowered.api.text.Text) Property(org.spongepowered.api.data.Property) Player(org.spongepowered.api.entity.living.player.Player) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Listener(org.spongepowered.api.event.Listener) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) Listener(org.spongepowered.api.event.Listener)

Example 5 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project LanternServer by LanternPowered.

the class ContainerTileEntityStore method deserialize.

@Override
public void deserialize(T object, DataView dataView) {
    final List<DataView> itemViews = dataView.getViewList(ITEMS).orElse(null);
    if (itemViews != null) {
        dataView.remove(ITEMS);
        final OrderedInventory inventory = (OrderedInventory) object.getInventory();
        final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
        for (DataView itemView : itemViews) {
            final int slot = itemView.getByte(SLOT).get() & 0xff;
            final LanternItemStack itemStack = itemStackSerializer.deserialize(itemView);
            inventory.set(new SlotIndex(slot), itemStack);
        }
    }
    super.deserialize(object, dataView);
}
Also used : DataView(org.spongepowered.api.data.DataView) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Aggregations

SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)15 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)3 DataView (org.spongepowered.api.data.DataView)3 IInventory (net.minecraft.inventory.IInventory)2 Inventory (org.spongepowered.api.item.inventory.Inventory)2 Slot (org.spongepowered.api.item.inventory.Slot)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 IMerchant (net.minecraft.entity.IMerchant)1 AbstractHorse (net.minecraft.entity.passive.AbstractHorse)1 Container (net.minecraft.inventory.Container)1 ContainerBeacon (net.minecraft.inventory.ContainerBeacon)1 ContainerBrewingStand (net.minecraft.inventory.ContainerBrewingStand)1 ContainerChest (net.minecraft.inventory.ContainerChest)1 ContainerDispenser (net.minecraft.inventory.ContainerDispenser)1 ContainerEnchantment (net.minecraft.inventory.ContainerEnchantment)1