Search in sources :

Example 26 with Enchantment

use of net.minecraft.enchantment.Enchantment in project BloodMagic by WayofTime.

the class SpellHelper method applySpecialProtection.

public static float applySpecialProtection(EntityLivingBase entity, DamageSource source, float damage) {
    ItemStack[] armour = entity.getLastActiveItems();
    if (armour == null) {
        return damage;
    }
    int total = 0;
    for (int i = 0; i < armour.length; i++) {
        ItemStack stack = armour[i];
        if (stack != null) {
            NBTTagList nbttaglist = stack.getEnchantmentTagList();
            if (nbttaglist != null) {
                for (int j = 0; j < nbttaglist.tagCount(); ++j) {
                    short short1 = nbttaglist.getCompoundTagAt(i).getShort("id");
                    short short2 = nbttaglist.getCompoundTagAt(i).getShort("lvl");
                    if (Enchantment.enchantmentsList[short1] != null) {
                        Enchantment ench = Enchantment.enchantmentsList[short1];
                        if (ench instanceof EnchantmentProtection) {
                            total += ench.calcModifierDamage(short2, source);
                        }
                    }
                }
            }
        }
    }
    if (total > 0) {
        total = (total + 1 >> +1) + rand.nextInt(total + 1 >> +1);
        if (total <= 20) {
            return damage * (25 - total) / 25;
        } else {
            float factor = (float) (0.8 + 0.2 * (1 - Math.pow(protCoeff, Math.pow((total - 20), scalCoeff))));
            return damage * (1 - factor);
        }
    }
    return damage;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) EnchantmentProtection(net.minecraft.enchantment.EnchantmentProtection)

Example 27 with Enchantment

use of net.minecraft.enchantment.Enchantment in project SilentGems by SilentChaos512.

the class ItemEnchantmentToken method clAddInformation.

// =========================
// Item and ItemSL overrides
// =========================
@Override
public void clAddInformation(ItemStack stack, World world, List list, boolean advanced) {
    LocalizationHelper loc = SilentGems.localizationHelper;
    Map<Enchantment, Integer> enchants = getEnchantments(stack);
    if (enchants.size() == 1) {
        Enchantment ench = enchants.keySet().iterator().next();
        list.add(loc.getItemSubText(itemName, "maxLevel", ench.getMaxLevel()));
        // Recipe info
        if (KeyTracker.isControlDown()) {
            list.add(loc.getItemSubText(itemName, "materials"));
            String recipeString = recipeMap.get(ench);
            if (recipeString != null && !recipeString.isEmpty()) {
                for (String str : recipeString.split(";")) {
                    list.add("  " + str);
                }
            }
        } else {
            list.add(loc.getItemSubText(itemName, "pressCtrl"));
        }
        // Debug info
        if (KeyTracker.isAltDown()) {
            list.add(TextFormatting.DARK_GRAY + ench.getRegistryName().toString());
        // list.add(TextFormatting.DARK_GRAY + "EnchID: " + ench.getEnchantmentID(ench));
        }
    }
    // Enchantment list
    for (Entry<Enchantment, Integer> entry : enchants.entrySet()) {
        Enchantment e = entry.getKey();
        String enchName = e.getTranslatedName(entry.getValue());
        String modName = Loader.instance().getIndexedModList().get(e.getRegistryName().getResourceDomain()).getName();
        list.add(loc.getItemSubText(itemName, "enchNameWithMod", enchName, modName));
        String descKey = e.getName().replaceAll(":", ".").toLowerCase() + ".desc";
        String desc = loc.getLocalizedString(descKey);
        if (!desc.equals(descKey))
            list.add(TextFormatting.ITALIC + "  " + desc);
    }
}
Also used : Enchantment(net.minecraft.enchantment.Enchantment) LocalizationHelper(net.silentchaos512.lib.util.LocalizationHelper)

Example 28 with Enchantment

use of net.minecraft.enchantment.Enchantment in project SpongeCommon by SpongePowered.

the class DamageEventHandler method createEnchantmentModifiers.

public static Optional<List<DamageFunction>> createEnchantmentModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource) {
    if (!damageSource.isDamageAbsolute()) {
        Iterable<net.minecraft.item.ItemStack> inventory = entityLivingBase.getArmorInventoryList();
        if (EnchantmentHelper.getEnchantmentModifierDamage(Lists.newArrayList(entityLivingBase.getArmorInventoryList()), damageSource) == 0) {
            return Optional.empty();
        }
        List<DamageFunction> modifiers = new ArrayList<>();
        boolean first = true;
        int totalModifier = 0;
        for (net.minecraft.item.ItemStack itemStack : inventory) {
            if (itemStack.isEmpty()) {
                continue;
            }
            final Multimap<Enchantment, Short> enchantments = LinkedHashMultimap.create();
            NBTTagList enchantmentList = itemStack.getEnchantmentTagList();
            if (enchantmentList == null) {
                continue;
            }
            for (int i = 0; i < enchantmentList.tagCount(); ++i) {
                final short enchantmentId = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID);
                final short level = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL);
                if (Enchantment.getEnchantmentByID(enchantmentId) != null) {
                    // Ok, we have an enchantment!
                    final Enchantment enchantment = Enchantment.getEnchantmentByID(enchantmentId);
                    final int temp = enchantment.calcModifierDamage(level, damageSource);
                    if (temp != 0) {
                        enchantments.put(enchantment, level);
                    }
                }
            }
            ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
            for (Map.Entry<Enchantment, Collection<Short>> enchantment : enchantments.asMap().entrySet()) {
                final DamageObject object = new DamageObject();
                int modifierTemp = 0;
                for (short level : enchantment.getValue()) {
                    modifierTemp += enchantment.getKey().calcModifierDamage(level, damageSource);
                }
                final int modifier = modifierTemp;
                object.previousDamage = totalModifier;
                if (object.previousDamage > 25) {
                    object.previousDamage = 25;
                }
                totalModifier += modifier;
                object.augment = first;
                object.ratio = modifier;
                DoubleUnaryOperator enchantmentFunction = damageIn -> {
                    if (object.augment) {
                        enchantmentDamageTracked = damageIn;
                    }
                    if (damageIn <= 0) {
                        return 0D;
                    }
                    double actualDamage = enchantmentDamageTracked;
                    if (object.previousDamage > 25) {
                        return 0D;
                    }
                    double modifierDamage = actualDamage;
                    double magicModifier;
                    if (modifier > 0 && modifier <= 20) {
                        int j = 25 - modifier;
                        magicModifier = modifierDamage * j;
                        modifierDamage = magicModifier / 25.0F;
                    }
                    return -Math.max(actualDamage - modifierDamage, 0.0D);
                };
                if (first) {
                    first = false;
                }
                // TODO: direct cause creation: bad bad bad
                DamageModifier enchantmentModifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), enchantment, snapshot, entityLivingBase)).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
                modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
            }
            if (!modifiers.isEmpty()) {
                return Optional.of(modifiers);
            }
        }
    }
    return Optional.empty();
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Item(net.minecraft.item.Item) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemArmor(net.minecraft.item.ItemArmor) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) NbtDataUtil(org.spongepowered.common.data.util.NbtDataUtil) Location(org.spongepowered.api.world.Location) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(org.spongepowered.api.world.World) Optional(java.util.Optional) MobEffects(net.minecraft.init.MobEffects) EventContext(org.spongepowered.api.event.cause.EventContext) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Iterables(com.google.common.collect.Iterables) Enchantment(net.minecraft.enchantment.Enchantment) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Multimap(com.google.common.collect.Multimap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Chunk(net.minecraft.world.chunk.Chunk) Entity(net.minecraft.entity.Entity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.util.math.BlockPos) DamageSource(net.minecraft.util.DamageSource) IBlockState(net.minecraft.block.state.IBlockState) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) EntityDamageSource(net.minecraft.util.EntityDamageSource) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) ArrayList(java.util.ArrayList) NBTTagList(net.minecraft.nbt.NBTTagList) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Collection(java.util.Collection) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) Map(java.util.Map) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 29 with Enchantment

use of net.minecraft.enchantment.Enchantment in project Galacticraft by micdoodle8.

the class ContainerEnchantmentModifier method updateEnchantmentOptions.

public void updateEnchantmentOptions(boolean validate) {
    int numoptions = slotEnchantment.size();
    slotEnchantment.clear();
    ItemStack toolstack = getSlot(0).getStack();
    if (toolstack == null) {
        percentscrolled = 0;
        return;
    }
    Item item = toolstack.getItem();
    int enchantablity = item.getItemEnchantability();
    if (enchantablity == 0 && validate) {
        percentscrolled = 0;
        return;
    }
    for (Enchantment e : Enchantment.enchantmentsList) {
        if (e == null || e.type == null || (!e.type.canEnchantItem(item) && validate)) {
            continue;
        }
        int state = 0;
        int level = -1;
        if (NEIServerUtils.stackHasEnchantment(toolstack, e.effectId)) {
            state = 2;
            level = NEIServerUtils.getEnchantmentLevel(toolstack, e.effectId);
        } else if (NEIServerUtils.doesEnchantmentConflict(NEIServerUtils.getEnchantments(toolstack), e) && validate) {
            state = 1;
        }
        slotEnchantment.add(new EnchantmentHash(e, state, level));
    }
    if (numoptions != slotEnchantment.size()) {
        percentscrolled = 0;
    }
}
Also used : Item(net.minecraft.item.Item) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) ContainerEnchantment(net.minecraft.inventory.ContainerEnchantment)

Example 30 with Enchantment

use of net.minecraft.enchantment.Enchantment in project BaseMetals by MinecraftModDevelopmentMods.

the class ShieldUpgradeRecipe method matchAndFind.

private Pair<Map<Enchantment, Integer>, ItemStack> matchAndFind(final ItemStack curItem, final Map<String, NonNullList<ItemStack>> plates) {
    final ItemStack comp = new ItemStack(curItem.getItem(), 1, curItem.getMetadata());
    Map<Enchantment, Integer> enchants = Collections.emptyMap();
    ItemStack plateMatched = null;
    final ItemStack matcher = new ItemStack(Materials.getMaterialByName(materialName).getItem(Names.SHIELD), 1, 0);
    for (final Entry<String, NonNullList<ItemStack>> ent : plates.entrySet()) {
        if (OreDictionary.containsMatch(false, ent.getValue(), comp)) {
            plateMatched = new ItemStack(Materials.getMaterialByName(ent.getKey().toLowerCase()).getItem(Names.SHIELD), 1, 0);
        }
    }
    if (OreDictionary.itemMatches(matcher, comp, false)) {
        enchants = EnchantmentHelper.getEnchantments(curItem);
    }
    return Pair.of(enchants, plateMatched);
}
Also used : NonNullList(net.minecraft.util.NonNullList) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment)

Aggregations

Enchantment (net.minecraft.enchantment.Enchantment)55 ItemStack (net.minecraft.item.ItemStack)32 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Item (net.minecraft.item.Item)9 EnchantmentData (net.minecraft.enchantment.EnchantmentData)8 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 NBTTagList (net.minecraft.nbt.NBTTagList)7 HashMap (java.util.HashMap)6 Iterator (java.util.Iterator)6 List (java.util.List)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 ResourceLocation (net.minecraft.util.ResourceLocation)4 Block (net.minecraft.block.Block)3 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)3 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)3 Entity (net.minecraft.entity.Entity)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 ItemEnchantedBook (net.minecraft.item.ItemEnchantedBook)3 ImmutableList (com.google.common.collect.ImmutableList)2