Search in sources :

Example 1 with Enchantment

use of net.minecraft.enchantment.Enchantment in project NetherEx by LogicTechCorp.

the class Trade method getRandomTrade.

public MerchantRecipe getRandomTrade(Random rand) {
    ItemStack outputStack = getItemToSell().copy();
    ItemStack inputA = getItemToBuy().copy();
    ItemStack inputB = getSecondItemToBuy().copy();
    outputStack.setCount(rand.nextInt((outputAmount.getMax() - outputAmount.getMin()) + 1) + outputAmount.getMin());
    inputA.setCount(rand.nextInt((inputAAmount.getMax() - inputAAmount.getMin()) + 1) + inputAAmount.getMin());
    inputB.setCount(rand.nextInt((inputBAmount.getMax() - inputBAmount.getMin()) + 1) + inputBAmount.getMin());
    if (outputEnchantments.size() > 0) {
        for (TradeOffer.Enchantment outputEnchantment : outputEnchantments) {
            Enchantment enchantment = Enchantment.getEnchantmentByLocation(outputEnchantment.getName());
            TradeOffer.Amount enchantmentAmount = outputEnchantment.getAmount();
            if (enchantment != null) {
                if (outputStack.getItem() instanceof ItemEnchantedBook) {
                    ((ItemEnchantedBook) outputStack.getItem()).addEnchantment(outputStack, new EnchantmentData(enchantment, rand.nextInt((enchantmentAmount.getMax() - enchantmentAmount.getMin()) + 1) + enchantmentAmount.getMin()));
                } else {
                    outputStack.addEnchantment(enchantment, rand.nextInt((enchantmentAmount.getMax() - enchantmentAmount.getMin()) + 1) + enchantmentAmount.getMin());
                }
            }
        }
    }
    return new MerchantRecipe(inputA, inputB, outputStack, 0, rand.nextInt((offerAmount.getMax() - offerAmount.getMin()) + 1) + offerAmount.getMin());
}
Also used : MerchantRecipe(net.minecraft.village.MerchantRecipe) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) ItemEnchantedBook(net.minecraft.item.ItemEnchantedBook) EnchantmentData(net.minecraft.enchantment.EnchantmentData)

Example 2 with Enchantment

use of net.minecraft.enchantment.Enchantment in project RFToolsDimensions by McJty.

the class ModCrafting method createEnchantedItem.

public static ItemStack createEnchantedItem(Item item, Enchantment enchantment, int amount) {
    ItemStack stack = new ItemStack(item);
    Map<Enchantment, Integer> enchant = new HashMap<>();
    enchant.put(enchantment, amount);
    EnchantmentHelper.setEnchantments(enchant, stack);
    return stack;
}
Also used : HashMap(java.util.HashMap) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment)

Example 3 with Enchantment

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

the class DamageEventHandler method createAttackEnchantmentFunction.

public static List<DamageFunction> createAttackEnchantmentFunction(net.minecraft.item.ItemStack heldItem, EnumCreatureAttribute creatureAttribute, float attackStrength) {
    final Multimap<Enchantment, Integer> enchantments = LinkedHashMultimap.create();
    final List<DamageFunction> damageModifierFunctions = new ArrayList<>();
    if (!heldItem.isEmpty()) {
        NBTTagList nbttaglist = heldItem.getEnchantmentTagList();
        if (nbttaglist.hasNoTags()) {
            return ImmutableList.of();
        }
        for (int i = 0; i < nbttaglist.tagCount(); ++i) {
            int j = nbttaglist.getCompoundTagAt(i).getShort("id");
            int enchantmentLevel = nbttaglist.getCompoundTagAt(i).getShort("lvl");
            final Enchantment enchantment = Enchantment.getEnchantmentByID(j);
            if (enchantment != null) {
                enchantments.put(enchantment, enchantmentLevel);
            }
        }
        if (enchantments.isEmpty()) {
            return ImmutableList.of();
        }
        ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(heldItem);
        for (Map.Entry<Enchantment, Collection<Integer>> enchantment : enchantments.asMap().entrySet()) {
            final DamageModifier enchantmentModifier = DamageModifier.builder().type(DamageModifierTypes.WEAPON_ENCHANTMENT).cause(Cause.of(EventContext.empty(), snapshot, enchantment)).build();
            DoubleUnaryOperator enchantmentFunction = (damage) -> {
                double totalDamage = 0;
                for (int level : enchantment.getValue()) {
                    totalDamage += (double) enchantment.getKey().calcDamageByCreature(level, creatureAttribute) * attackStrength;
                }
                return totalDamage;
            };
            damageModifierFunctions.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
        }
    }
    return damageModifierFunctions;
}
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) Enchantment(net.minecraft.enchantment.Enchantment) Map(java.util.Map) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 4 with Enchantment

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

the class MixinEntityVillagerListEnchantedBookForEmeralds method apply.

@Override
public TradeOffer apply(Random random) {
    checkNotNull(random, "Random cannot be null!");
    Enchantment enchantment = Enchantment.REGISTRY.getRandomObject(random);
    int enchantmentLevel = MathHelper.getInt(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    ItemStack itemstack = ItemEnchantedBook.getEnchantedItemStack(new EnchantmentData(enchantment, enchantmentLevel));
    int emeraldCount = 2 + random.nextInt(5 + enchantmentLevel * 10) + 3 * enchantmentLevel;
    if (enchantment.isTreasureEnchantment()) {
        emeraldCount *= 2;
    }
    if (emeraldCount > 64) {
        emeraldCount = 64;
    }
    return (TradeOffer) new MerchantRecipe(new ItemStack(Items.BOOK), new ItemStack(Items.EMERALD, emeraldCount), itemstack);
}
Also used : MerchantRecipe(net.minecraft.village.MerchantRecipe) Enchantment(net.minecraft.enchantment.Enchantment) ItemStack(net.minecraft.item.ItemStack) EnchantmentData(net.minecraft.enchantment.EnchantmentData) TradeOffer(org.spongepowered.api.item.merchant.TradeOffer)

Example 5 with Enchantment

use of net.minecraft.enchantment.Enchantment in project Wurst-MC-1.12 by Wurst-Imperium.

the class EnchantCmd method call.

@Override
public void call(String[] args) throws CmdException {
    if (!WMinecraft.getPlayer().capabilities.isCreativeMode)
        throw new CmdError("Creative mode only.");
    if (args.length == 0) {
        ItemStack currentItem = WMinecraft.getPlayer().inventory.getCurrentItem();
        if (currentItem == null)
            throw new CmdError("There is no item in your hand.");
        for (Enchantment enchantment : Enchantment.REGISTRY) try {
            if (enchantment == WEnchantments.SILK_TOUCH)
                continue;
            currentItem.addEnchantment(enchantment, 127);
        } catch (Exception e) {
        }
    } else if (args[0].equals("all")) {
        int items = 0;
        for (int i = 0; i < 40; i++) {
            ItemStack currentItem = WMinecraft.getPlayer().inventory.getStackInSlot(i);
            if (currentItem == null)
                continue;
            items++;
            for (Enchantment enchantment : Enchantment.REGISTRY) try {
                if (enchantment == WEnchantments.SILK_TOUCH)
                    continue;
                currentItem.addEnchantment(enchantment, 127);
            } catch (Exception e) {
            }
        }
        if (items == 1)
            ChatUtils.message("Enchanted 1 item.");
        else
            ChatUtils.message("Enchanted " + items + " items.");
    } else
        throw new CmdSyntaxError();
}
Also used : ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment)

Aggregations

Enchantment (net.minecraft.enchantment.Enchantment)51 ItemStack (net.minecraft.item.ItemStack)28 ArrayList (java.util.ArrayList)8 Item (net.minecraft.item.Item)8 Map (java.util.Map)6 EnchantmentData (net.minecraft.enchantment.EnchantmentData)6 NBTTagList (net.minecraft.nbt.NBTTagList)6 HashMap (java.util.HashMap)5 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)5 Iterator (java.util.Iterator)4 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)3 EntityLivingBase (net.minecraft.entity.EntityLivingBase)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables (com.google.common.collect.Iterables)2 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)2 Lists (com.google.common.collect.Lists)2 Multimap (com.google.common.collect.Multimap)2 Collection (java.util.Collection)2 List (java.util.List)2