use of org.spongepowered.api.item.enchantment.EnchantmentType in project SpongeCommon by SpongePowered.
the class NbtDataUtil method setItemEnchantments.
public static void setItemEnchantments(ItemStack itemStack, List<Enchantment> value) {
final NBTTagCompound compound;
if (itemStack.getTagCompound() == null) {
compound = new NBTTagCompound();
itemStack.setTagCompound(compound);
} else {
compound = itemStack.getTagCompound();
}
if (value.isEmpty()) {
// if there's no enchantments, remove the tag that says there's enchantments
compound.removeTag(NbtDataUtil.ITEM_ENCHANTMENT_LIST);
return;
}
final Map<EnchantmentType, Integer> valueMap = Maps.newLinkedHashMap();
for (Enchantment enchantment : value) {
// convert ItemEnchantment to map
valueMap.put(enchantment.getType(), enchantment.getLevel());
}
// construct the enchantment list
final NBTTagList newList = new NBTTagList();
for (Map.Entry<EnchantmentType, Integer> entry : valueMap.entrySet()) {
final NBTTagCompound enchantmentCompound = new NBTTagCompound();
enchantmentCompound.setShort(NbtDataUtil.ITEM_ENCHANTMENT_ID, (short) net.minecraft.enchantment.Enchantment.getEnchantmentID((net.minecraft.enchantment.Enchantment) entry.getKey()));
enchantmentCompound.setShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL, entry.getValue().shortValue());
newList.appendTag(enchantmentCompound);
}
compound.setTag(NbtDataUtil.ITEM_ENCHANTMENT_LIST, newList);
}
use of org.spongepowered.api.item.enchantment.EnchantmentType in project SpongeCommon by SpongePowered.
the class EnchantmentTest method onGameInitialization.
@Listener
public void onGameInitialization(final GameInitializationEvent event) {
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(GenericArguments.onlyOne(GenericArguments.catalogedElement(Text.of("enchantment"), EnchantmentType.class)), GenericArguments.onlyOne(GenericArguments.integer(Text.of("level")))).executor((src, args) -> {
if (!(src instanceof Player)) {
throw new CommandException(Text.of(TextColors.RED, "You must be a player to use this command!"));
}
final Player player = (Player) src;
final ItemStack itemStack = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty());
if (!itemStack.supports(Keys.ITEM_ENCHANTMENTS)) {
throw new CommandException(Text.of(TextColors.RED, "This item does not support item enchantments."));
}
final EnchantmentType type = args.<EnchantmentType>getOne("enchantment").orElse(EnchantmentTypes.BINDING_CURSE);
final int level = args.<Integer>getOne("level").orElse(1);
final Enchantment newEnchantment = Enchantment.builder().type(type).level(level).build();
final List<Enchantment> enchantments = itemStack.get(Keys.ITEM_ENCHANTMENTS).orElse(new ArrayList<>());
enchantments.add(newEnchantment);
itemStack.offer(Keys.ITEM_ENCHANTMENTS, enchantments);
player.setItemInHand(HandTypes.MAIN_HAND, itemStack);
player.sendMessage(Text.of(TextColors.GOLD, "You have successfully added the enchantment " + type.getName() + " with a level of " + level + "."));
return CommandResult.success();
}).build(), "spongeenchant");
Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
if (!(src instanceof Player)) {
throw new CommandException(Text.of(TextColors.RED, "You must be a player to use this command!"));
}
final Player player = (Player) src;
final ItemStack itemStack = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty());
if (!itemStack.supports(Keys.ITEM_ENCHANTMENTS)) {
throw new CommandException(Text.of(TextColors.RED, "This item does not support item enchantments."));
}
final List<Enchantment> enchantments = itemStack.get(Keys.ITEM_ENCHANTMENTS).orElse(ImmutableList.of());
if (enchantments.isEmpty()) {
src.sendMessage(Text.of(TextColors.RED, "This item has no enchantments!"));
}
enchantments.forEach(enchantment -> {
final EnchantmentType type = enchantment.getType();
src.sendMessage(Text.of(TextColors.GOLD, "============================="));
src.sendMessage(Text.of(TextColors.GOLD, "Type: ", TextColors.GRAY, type.getName()));
src.sendMessage(Text.of(TextColors.GOLD, "Type ID: ", TextColors.GRAY, type.getId()));
src.sendMessage(Text.of(TextColors.GOLD, "Translation: ", TextColors.GRAY, enchantment.getType().getTranslation()));
src.sendMessage(Text.of(TextColors.GOLD, "Level: ", TextColors.GRAY, enchantment.getLevel()));
src.sendMessage(Text.of(TextColors.GOLD, "Maximum level: ", TextColors.GRAY, type.getMaximumLevel()));
src.sendMessage(Text.of(TextColors.GOLD, "Minimum level: ", TextColors.GRAY, type.getMinimumLevel()));
src.sendMessage(Text.of(TextColors.GOLD, "Weight: ", TextColors.GRAY, type.getWeight()));
src.sendMessage(Text.of(TextColors.GOLD, "Curse: ", TextColors.GRAY, type.isCurse()));
src.sendMessage(Text.of(TextColors.GOLD, "Treasure: ", TextColors.GRAY, type.isTreasure()));
});
return CommandResult.success();
}).build(), "spongeenchantinfo");
}
Aggregations