use of gregtech.api.enchants.EnchantmentData in project GregTech by GregTechCE.
the class ToolMetaItem method onCreated.
@Override
public void onCreated(ItemStack stack, World world, EntityPlayer player) {
T metaToolValueItem = getItem(stack);
if (metaToolValueItem != null) {
IToolStats toolStats = metaToolValueItem.getToolStats();
toolStats.onToolCrafted(stack, player);
ArrayList<EnchantmentData> enchantments = new ArrayList<>(toolStats.getEnchantments(stack));
SolidMaterial material = getPrimaryMaterial(stack);
if (material == null) {
return;
}
for (EnchantmentData enchantmentData : material.toolEnchantments) {
Optional<EnchantmentData> sameEnchantment = enchantments.stream().filter(it -> it.enchantment == enchantmentData.enchantment).findAny();
if (sameEnchantment.isPresent()) {
enchantments.remove(sameEnchantment.get());
int level = Math.min(enchantmentData.level + sameEnchantment.get().level, enchantmentData.enchantment.getMaxLevel());
enchantments.add(new EnchantmentData(enchantmentData.enchantment, level));
} else {
enchantments.add(enchantmentData);
}
}
for (EnchantmentData enchantmentData : enchantments) {
stack.addEnchantment(enchantmentData.enchantment, enchantmentData.level);
}
}
}
Aggregations