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");
}
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()));
}
}
}
}
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));
}
}
Aggregations