Search in sources :

Example 1 with Hotbar

use of org.spongepowered.api.item.inventory.entity.Hotbar 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 2 with Hotbar

use of org.spongepowered.api.item.inventory.entity.Hotbar in project SpongeCommon by SpongePowered.

the class InventorySetOpsTest method onMidas.

@Listener
public void onMidas(ChangeInventoryEvent.Held event, @Root Player player) {
    // Checks if Slots are contained in the hotbar then may transform iron to gold
    Inventory hotbar = event.getTargetInventory().query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
    boolean nugget = false;
    for (SlotTransaction transaction : event.getTransactions()) {
        if (hotbar.containsInventory(transaction.getSlot())) {
            if (ItemTypes.GOLD_NUGGET.equals(transaction.getOriginal().getType())) {
                nugget = true;
            }
            if (nugget && ItemTypes.IRON_INGOT.equals(transaction.getOriginal().getType())) {
                transaction.setCustom(ItemStack.of(ItemTypes.GOLD_INGOT, transaction.getOriginal().getQuantity()));
            }
        }
    }
}
Also used : Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Inventory(org.spongepowered.api.item.inventory.Inventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) Listener(org.spongepowered.api.event.Listener)

Example 3 with Hotbar

use of org.spongepowered.api.item.inventory.entity.Hotbar in project Nucleus by NucleusPowered.

the class RepairCommand method executeCommand.

@Override
protected CommandResult executeCommand(Player pl, CommandContext args) throws Exception {
    EnumMap<ResultType, Integer> resultCount = new EnumMap<ResultType, Integer>(ResultType.class) {

        {
            put(ResultType.SUCCESS, 0);
            put(ResultType.ERROR, 0);
            put(ResultType.NO_DURABILITY, 0);
            put(ResultType.RESTRICTED, 0);
        }
    };
    EnumMap<ResultType, ItemStackSnapshot> lastItem = new EnumMap<>(ResultType.class);
    boolean checkRestrictions = !pl.hasPermission(permissions.getPermissionWithSuffix("exempt.restriction"));
    String location = "inventory";
    if (args.hasAny("a")) {
        repairInventory(pl.getInventory(), checkRestrictions, resultCount, lastItem);
    } else {
        boolean repairHotbar = args.hasAny("h");
        boolean repairEquip = args.hasAny("e");
        boolean repairOffhand = args.hasAny("o");
        boolean repairMainhand = args.hasAny("m") || !repairHotbar && !repairEquip && !repairOffhand;
        if (repairHotbar && !repairEquip && !repairOffhand && !repairMainhand) {
            location = "hotbar";
        } else if (repairEquip && !repairHotbar && !repairOffhand && !repairMainhand) {
            location = "equipment";
        } else if (repairOffhand && !repairHotbar && !repairEquip && !repairMainhand) {
            location = "offhand";
        } else if (repairMainhand && !repairHotbar && !repairEquip && !repairOffhand) {
            location = "mainhand";
        }
        // Repair item in main hand
        if (repairMainhand && pl.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
            ItemStack stack = pl.getItemInHand(HandTypes.MAIN_HAND).get();
            RepairResult result = repairStack(stack, checkRestrictions);
            resultCount.compute(result.type, (t, i) -> i += 1);
            lastItem.put(result.type, result.stack.createSnapshot());
            if (result.isSuccessful()) {
                pl.setItemInHand(HandTypes.MAIN_HAND, result.stack);
            }
        }
        // Repair item in off hand
        if (repairOffhand && pl.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
            ItemStack stack = pl.getItemInHand(HandTypes.OFF_HAND).get();
            RepairResult result = repairStack(stack, checkRestrictions);
            resultCount.compute(result.type, (t, i) -> i += 1);
            lastItem.put(result.type, result.stack.createSnapshot());
            if (result.isSuccessful()) {
                pl.setItemInHand(HandTypes.OFF_HAND, result.stack);
            }
        }
        // Repair worn equipment
        if (repairEquip) {
            repairInventory(pl.getInventory().query(EquipmentInventory.class), checkRestrictions, resultCount, lastItem);
        }
        // Repair Hotbar
        if (repairHotbar) {
            repairInventory(pl.getInventory().query(Hotbar.class), checkRestrictions, resultCount, lastItem);
        }
    }
    location = plugin.getMessageProvider().getMessageFromKey("command.repair.location." + location).orElse("inventory");
    if (resultCount.get(ResultType.SUCCESS) == 0 && resultCount.get(ResultType.ERROR) == 0 && resultCount.get(ResultType.NO_DURABILITY) == 0 && resultCount.get(ResultType.RESTRICTED) == 0) {
        throw ReturnMessageException.fromKey("command.repair.empty", pl.getName(), location);
    } else {
        // Non-repairable Message - Only used when all items processed had no durability
        if (resultCount.get(ResultType.NO_DURABILITY) > 0 && resultCount.get(ResultType.SUCCESS) == 0 && resultCount.get(ResultType.ERROR) == 0 && resultCount.get(ResultType.RESTRICTED) == 0) {
            if (resultCount.get(ResultType.NO_DURABILITY) == 1) {
                ItemStackSnapshot item = lastItem.get(ResultType.NO_DURABILITY);
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.repair.nodurability.single", item.get(Keys.DISPLAY_NAME).orElse(Text.of(item.getTranslation().get())).toBuilder().onHover(TextActions.showItem(item)).build(), Text.of(pl.getName()), Text.of(location)));
            } else {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.repair.nodurability.multiple", resultCount.get(ResultType.NO_DURABILITY).toString(), pl.getName(), location));
            }
        }
        // Success Message
        if (resultCount.get(ResultType.SUCCESS) == 1) {
            ItemStackSnapshot item = lastItem.get(ResultType.SUCCESS);
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.repair.success.single", item.get(Keys.DISPLAY_NAME).orElse(Text.of(item.getTranslation().get())).toBuilder().onHover(TextActions.showItem(item)).build(), Text.of(pl.getName()), Text.of(location)));
        } else if (resultCount.get(ResultType.SUCCESS) > 1) {
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.repair.success.multiple", resultCount.get(ResultType.SUCCESS).toString(), pl.getName(), location));
        }
        // Error Message
        if (resultCount.get(ResultType.ERROR) == 1) {
            ItemStackSnapshot item = lastItem.get(ResultType.ERROR);
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.repair.error.single", item.get(Keys.DISPLAY_NAME).orElse(Text.of(item.getTranslation().get())).toBuilder().onHover(TextActions.showItem(item)).build(), Text.of(pl.getName()), Text.of(location)));
        } else if (resultCount.get(ResultType.ERROR) > 1) {
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.repair.error.multiple", resultCount.get(ResultType.ERROR).toString(), pl.getName(), location));
        }
        // Restriction Message
        if (resultCount.get(ResultType.RESTRICTED) == 1) {
            ItemStackSnapshot item = lastItem.get(ResultType.RESTRICTED);
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.repair.restricted.single", item.get(Keys.DISPLAY_NAME).orElse(Text.of(item.getTranslation().get())).toBuilder().onHover(TextActions.showItem(item)).build(), Text.of(pl.getName()), Text.of(location)));
        } else if (resultCount.get(ResultType.RESTRICTED) > 1) {
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.repair.restricted.multiple", resultCount.get(ResultType.RESTRICTED).toString(), pl.getName(), location));
        }
        return CommandResult.successCount(resultCount.get(ResultType.SUCCESS));
    }
}
Also used : EquipmentInventory(org.spongepowered.api.item.inventory.equipment.EquipmentInventory) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Hotbar(org.spongepowered.api.item.inventory.entity.Hotbar) ItemStack(org.spongepowered.api.item.inventory.ItemStack) EnumMap(java.util.EnumMap)

Aggregations

Hotbar (org.spongepowered.api.item.inventory.entity.Hotbar)3 Listener (org.spongepowered.api.event.Listener)2 Inventory (org.spongepowered.api.item.inventory.Inventory)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 EnumMap (java.util.EnumMap)1 Sponge (org.spongepowered.api.Sponge)1 CommandException (org.spongepowered.api.command.CommandException)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandSource (org.spongepowered.api.command.CommandSource)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 Property (org.spongepowered.api.data.Property)1 Keys (org.spongepowered.api.data.key.Keys)1 DyeColors (org.spongepowered.api.data.type.DyeColors)1 Player (org.spongepowered.api.entity.living.player.Player)1 GameInitializationEvent (org.spongepowered.api.event.game.state.GameInitializationEvent)1 ItemTypes (org.spongepowered.api.item.ItemTypes)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 PlayerInventory (org.spongepowered.api.item.inventory.entity.PlayerInventory)1 EquipmentInventory (org.spongepowered.api.item.inventory.equipment.EquipmentInventory)1 SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)1