Search in sources :

Example 1 with Identifiable

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

the class CustomInventoryTest method onPunchEntity.

@Listener
public void onPunchEntity(InteractEntityEvent.Primary event, @Root Player player) {
    if (!player.get(Keys.IS_SNEAKING).orElse(false)) {
        return;
    }
    if (event.getTargetEntity() instanceof Mule) {
        Inventory.Builder builder;
        if (((Mule) event.getTargetEntity()).getInventory().capacity() <= 2) {
            builder = Inventory.builder().of(InventoryArchetypes.HORSE);
        } else {
            builder = Inventory.builder().of(InventoryArchetypes.HORSE_WITH_CHEST);
        }
        Inventory inventory = builder.property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Custom Mule"))).withCarrier(((Horse) event.getTargetEntity())).build(this);
        int i = 1;
        for (Inventory slot : inventory.slots()) {
            slot.set(ItemStack.of(ItemTypes.APPLE, i++));
        }
        Sponge.getCauseStackManager().pushCause(player);
        player.openInventory(inventory);
        Sponge.getCauseStackManager().popCause();
        event.setCancelled(true);
    } else if (event.getTargetEntity() instanceof Llama) {
        Inventory.Builder builder;
        if (((Llama) event.getTargetEntity()).getInventory().capacity() <= 2) {
            builder = Inventory.builder().of(InventoryArchetypes.HORSE);
        } else {
            builder = Inventory.builder().of(InventoryArchetypes.HORSE_WITH_CHEST);
        }
        Inventory inventory = builder.property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Custom Llama"))).withCarrier(((Horse) event.getTargetEntity())).build(this);
        int i = 1;
        for (Inventory slot : inventory.slots()) {
            slot.set(ItemStack.of(ItemTypes.APPLE, i++));
        }
        Sponge.getCauseStackManager().pushCause(player);
        player.openInventory(inventory);
        Sponge.getCauseStackManager().popCause();
        event.setCancelled(true);
    } else if (event.getTargetEntity() instanceof Horse) {
        Inventory inventory = Inventory.builder().of(InventoryArchetypes.HORSE).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Custom Horse"))).withCarrier(((Horse) event.getTargetEntity())).build(this);
        int i = 1;
        for (Inventory slot : inventory.slots()) {
            slot.set(ItemStack.of(ItemTypes.APPLE, i++));
        }
        Sponge.getCauseStackManager().pushCause(player);
        player.openInventory(inventory);
        Sponge.getCauseStackManager().popCause();
        event.setCancelled(true);
    }
    if (event.getTargetEntity() instanceof Slime) {
        Inventory inventory = Inventory.builder().of(InventoryArchetypes.MENU_GRID).property(InventoryDimension.of(1, 9)).property(InventoryTitle.of(Text.of("Slime Content"))).property(new Identifiable()).property(new GuiIdProperty(GuiIds.DISPENSER)).build(this);
        ItemStack flard = ItemStack.of(ItemTypes.SLIME, 1);
        flard.offer(Keys.DISPLAY_NAME, Text.of("Flard?"));
        for (Inventory slot : inventory.slots()) {
            slot.set(flard);
        }
        Sponge.getCauseStackManager().pushCause(player);
        player.openInventory(inventory);
        Sponge.getCauseStackManager().popCause();
    }
}
Also used : GuiIdProperty(org.spongepowered.api.item.inventory.property.GuiIdProperty) Llama(org.spongepowered.api.entity.living.animal.Llama) Mule(org.spongepowered.api.entity.living.animal.Mule) Horse(org.spongepowered.api.entity.living.animal.Horse) Slime(org.spongepowered.api.entity.living.monster.Slime) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Identifiable(org.spongepowered.api.item.inventory.property.Identifiable) Listener(org.spongepowered.api.event.Listener)

Example 2 with Identifiable

use of org.spongepowered.api.item.inventory.property.Identifiable in project modules-extra by CubeEngine.

the class MarketSignManager method executeShowInventory.

private void executeShowInventory(MarketSignData data, Player player, boolean isOwner, Location<World> loc) {
    if (isInEditMode(loc)) {
        return;
    }
    if (isOwner || (!data.isAdminOwner() && player.hasPermission(module.perms().EDIT_PLAYER_OTHER.getId()))) {
        Inventory inventory = signInventories.get(data.getID());
        ItemStack item = data.getItem();
        if (inventory != null) {
            i18n.send(ACTION_BAR, player, NEGATIVE, "This signs inventory is being edited right now!");
        // TODO dont allow this return;
        }
        Integer size = data.getSize();
        if (size == -1) {
            size = 6;
        }
        Inventory inv = Inventory.builder().of(CHEST).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of(getOwnerName(data)))).property(InventoryDimension.PROPERTY_NAME, new InventoryDimension(9, size)).property("identifiable", new Identifiable()).build(plugin);
        signInventories.put(data.getID(), inv);
        updateSignText(data, loc);
        UUID key = UUID.randomUUID();
        signInventoryStock.put(/*TODO getProperty is always empty MinecraftInventoryAdapter inv.getProperty(IdentifiableProperty.class,
            "IdentifiableProperty").get().getValue()*/
        key, Math.min(size * 9 * item.getMaxStackQuantity(), data.getStock()));
        // TODO would be nice to just offer a 64+ item stack and split it up in inventory impl.
        // Offer stock to signinventory -->
        Integer stockToAdd = data.getStock();
        int maxStackSize = item.getMaxStackQuantity();
        while (stockToAdd > 0) {
            ItemStack addAll = item.copy();
            addAll.setQuantity(Math.min(stockToAdd, maxStackSize));
            stockToAdd -= addAll.getQuantity();
            inv.offer(addAll);
        }
        // --<
        ItemStack copy = item.copy();
        Runnable onClose = () -> {
            signInventories.remove(data.getID());
            if (data.getStock() != null) {
                int newPageStock = inv.query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(copy)).totalItems();
                Integer totalStock = data.getStock();
                Integer oldPageStock = signInventoryStock.get(key);
                oldPageStock = oldPageStock == null ? 0 : oldPageStock;
                data.setStock(totalStock - (oldPageStock - newPageStock));
                loc.offer(data);
                updateSignText(data, loc);
            }
        };
        igf.prepareInv(inv, player.getUniqueId()).blockPutInAll().blockTakeOutAll().onClose(onClose);
        if (data.getSignType() == SignType.BUY) {
            igf.notBlockPutIn(item).notBlockTakeOut(item);
        } else {
            igf.notBlockTakeOut(item);
        // TODO permission to allow putting items into sell sign
        }
        igf.submitInventory(Signmarket.class, true);
        return;
    }
    if (player.hasPermission(module.perms().INTERACT_INVENTORY.getId()) || data.isOwner(player.getUniqueId())) {
        String name = getOwnerName(data);
        Inventory inventory = Inventory.builder().of(DISPENSER).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of(name))).build(plugin);
        // middle of dispenser
        inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotPos.of(1, 1))).set(data.getItem().copy());
        igf.prepareInv(inventory, player.getUniqueId()).blockPutInAll().blockTakeOutAll().submitInventory(Signmarket.class, true);
        return;
    }
    i18n.send(ACTION_BAR, player, NEGATIVE, "You are not allowed to see the market signs inventories");
}
Also used : InventoryDimension(org.spongepowered.api.item.inventory.property.InventoryDimension) ItemStack(org.spongepowered.api.item.inventory.ItemStack) UUID(java.util.UUID) Inventory(org.spongepowered.api.item.inventory.Inventory) Identifiable(org.spongepowered.api.item.inventory.property.Identifiable)

Aggregations

Inventory (org.spongepowered.api.item.inventory.Inventory)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Identifiable (org.spongepowered.api.item.inventory.property.Identifiable)2 UUID (java.util.UUID)1 Horse (org.spongepowered.api.entity.living.animal.Horse)1 Llama (org.spongepowered.api.entity.living.animal.Llama)1 Mule (org.spongepowered.api.entity.living.animal.Mule)1 Slime (org.spongepowered.api.entity.living.monster.Slime)1 Listener (org.spongepowered.api.event.Listener)1 GuiIdProperty (org.spongepowered.api.item.inventory.property.GuiIdProperty)1 InventoryDimension (org.spongepowered.api.item.inventory.property.InventoryDimension)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1