use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.
the class NbtDataUtil method getItemEnchantments.
public static List<Enchantment> getItemEnchantments(ItemStack itemStack) {
if (!itemStack.isItemEnchanted()) {
return Collections.emptyList();
}
final List<Enchantment> enchantments = Lists.newArrayList();
final NBTTagList list = itemStack.getEnchantmentTagList();
for (int i = 0; i < list.tagCount(); i++) {
final NBTTagCompound compound = list.getCompoundTagAt(i);
final short enchantmentId = compound.getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID);
final short level = compound.getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL);
final EnchantmentType enchantmentType = (EnchantmentType) net.minecraft.enchantment.Enchantment.getEnchantmentByID(enchantmentId);
if (enchantmentType == null) {
continue;
}
enchantments.add(new SpongeEnchantment(enchantmentType, level));
}
return enchantments;
}
use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.
the class StoredEnchantmentDataProcessor method getVal.
@Override
protected Optional<List<Enchantment>> getVal(ItemStack entity) {
if (!entity.hasTagCompound() || !entity.getTagCompound().hasKey(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, NbtDataUtil.TAG_LIST)) {
return Optional.empty();
}
List<Enchantment> list = Lists.newArrayList();
NBTTagList tags = entity.getTagCompound().getTagList(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, NbtDataUtil.TAG_COMPOUND);
for (int i = 0; i < tags.tagCount(); i++) {
NBTTagCompound tag = tags.getCompoundTagAt(i);
list.add(new SpongeEnchantment((EnchantmentType) net.minecraft.enchantment.Enchantment.getEnchantmentByID(tag.getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID)), tag.getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL)));
}
return Optional.of(list);
}
use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.
the class StoredEnchantmentDataProcessor method set.
@Override
protected boolean set(ItemStack entity, List<Enchantment> value) {
if (!entity.hasTagCompound()) {
entity.setTagCompound(new NBTTagCompound());
}
NBTTagList list = new NBTTagList();
for (Enchantment enchantment : value) {
NBTTagCompound tag = new NBTTagCompound();
tag.setShort(NbtDataUtil.ITEM_ENCHANTMENT_ID, (short) net.minecraft.enchantment.Enchantment.getEnchantmentID((net.minecraft.enchantment.Enchantment) enchantment.getType()));
tag.setShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL, (short) enchantment.getLevel());
list.appendTag(tag);
}
entity.getTagCompound().setTag(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, list);
return true;
}
use of org.spongepowered.api.item.enchantment.Enchantment in project RedProtect by FabioZumbi12.
the class RPVHelper7 method getInvValue.
@Override
public long getInvValue(Iterable<Inventory> inv) {
long value = 0;
for (Inventory item : inv) {
if (!item.peek().isPresent()) {
continue;
}
ItemStack stack = item.peek().get();
value += ((RedProtect.get().cfgs.getBlockCost(stack.getType().getId()) * stack.getQuantity()));
if (stack.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
for (Enchantment enchant : stack.get(Keys.ITEM_ENCHANTMENTS).get()) {
value += ((RedProtect.get().cfgs.getEnchantCost(enchant.getType().getId()) * enchant.getLevel()));
}
}
}
return value;
}
use of org.spongepowered.api.item.enchantment.Enchantment in project core by CubeEngine.
the class EnchantMatcher method applyMatchedEnchantment.
public boolean applyMatchedEnchantment(ItemStack item, String enchName, int enchStrength, boolean force) {
EnchantmentType ench = this.enchantment(enchName);
if (ench == null)
return false;
if (enchStrength == 0) {
enchStrength = ench.getMaximumLevel();
}
Enchantment enchantment = Enchantment.builder().type(ench).level(enchStrength).build();
if (force) {
EnchantmentData data = item.getOrCreate(ENCHANTMENT_DATA).get();
data.enchantments().add(enchantment);
item.offer(data);
return true;
}
try {
EnchantmentData data = item.getOrCreate(EnchantmentData.class).get();
data.enchantments().add(enchantment);
item.offer(data);
return true;
} catch (IllegalArgumentException ignored) {
return false;
}
}
Aggregations