Java Code Examples for net.minecraft.enchantment.EnchantmentHelper#getEnchantmentLevel()

The following examples show how to use net.minecraft.enchantment.EnchantmentHelper#getEnchantmentLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoArmorModule.java    From seppuku with GNU General Public License v3.0 6 votes vote down vote up
private int findArmorSlot(EntityEquipmentSlot type) {
    int slot = -1;
    float damage = 0;

    for (int i = 9; i < 45; i++) {
        final ItemStack s = Minecraft.getMinecraft().player.inventoryContainer.getSlot(i).getStack();
        if (s != null && s.getItem() != Items.AIR) {

            if (s.getItem() instanceof ItemArmor) {
                final ItemArmor armor = (ItemArmor) s.getItem();
                if (armor.armorType == type) {
                    final float currentDamage = (armor.damageReduceAmount + EnchantmentHelper.getEnchantmentLevel(Enchantments.PROTECTION, s));

                    final boolean cursed = this.curse.getValue() ? (EnchantmentHelper.hasBindingCurse(s)) : false;

                    if (currentDamage > damage && !cursed) {
                        damage = currentDamage;
                        slot = i;
                    }
                }
            }
        }
    }

    return slot;
}
 
Example 2
Source File: ItemUnicornDagger.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SubscribeEvent
public static void onAttackEntity(AttackEntityEvent event) {
	if (event.getTarget().canBeAttackedWithItem() && !event.getTarget().hitByEntity(event.getEntity()) && event.getTarget() instanceof EntityLivingBase && event.getTarget().hurtResistantTime <= 0) {
		EntityLivingBase attacker = event.getEntityLiving();
		EntityLivingBase target = (EntityLivingBase) event.getTarget();

		if (attacker.getHeldItemMainhand() == ItemStack.EMPTY)
			return;
		if (attacker.getHeldItemMainhand().getItem() != ModItems.UNICORN_DAGGER)
			return;

		float damage = 1 + EnchantmentHelper.getEnchantmentLevel(Enchantments.KNOCKBACK, attacker.getHeldItemMainhand());
		float attackCD = attacker instanceof EntityPlayer ? ((EntityPlayer) attacker).getCooledAttackStrength(0.5F) : 1;
		damage *= (0.2f + attackCD * attackCD * 0.8f);

		target.attackEntityFrom(DamageSource.causeIndirectMagicDamage(attacker, null), damage);
		target.hurtResistantTime = 0;
	}
}
 
Example 3
Source File: ModEnchantments.java    From Et-Futurum with The Unlicense 6 votes vote down vote up
public static void onLivingUpdate(EntityLivingBase entity) {
	if (entity.worldObj.isRemote)
		return;
	if (!EtFuturum.enableFrostWalker)
		return;

	ItemStack boots = entity.getEquipmentInSlot(1);
	int level = 0;
	if ((level = EnchantmentHelper.getEnchantmentLevel(frostWalker.effectId, boots)) > 0)
		if (entity.onGround) {
			int x = (int) entity.posX;
			int y = (int) entity.posY;
			int z = (int) entity.posZ;

			int radius = 1 + level;

			for (int i = -radius; i <= radius; i++)
				for (int j = -radius; j <= radius; j++) {
					Block block = entity.worldObj.getBlock(x + i, y - 1, z + j);
					if (block == Blocks.water || block == Blocks.flowing_water)
						entity.worldObj.setBlock(x + i, y - 1, z + j, ModBlocks.frosted_ice);
				}
		}
}
 
Example 4
Source File: AutoToolModule.java    From seppuku with GNU General Public License v3.0 6 votes vote down vote up
private int getToolHotbar(BlockPos pos) {
    int index = -1;

    float speed = 1.0f;

    for (int i = 0; i <= 9; i++) {
        final ItemStack stack = Minecraft.getMinecraft().player.inventory.getStackInSlot(i);
        if (stack != null && stack != ItemStack.EMPTY) {
            final float digSpeed = EnchantmentHelper.getEnchantmentLevel(Enchantments.EFFICIENCY, stack);
            final float destroySpeed = stack.getDestroySpeed(Minecraft.getMinecraft().world.getBlockState(pos));

            if ((digSpeed + destroySpeed) > speed) {
                speed = (digSpeed + destroySpeed);
                index = i;
            }
        }
    }

    return index;
}
 
Example 5
Source File: OpenBlock.java    From OpenModsLib with MIT License 6 votes vote down vote up
protected void handleNormalDrops(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, @Nonnull ItemStack stack) {
	harvesters.set(player);
	final int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);

	boolean addNormalDrops = true;

	if (te instanceof ICustomHarvestDrops) {
		final ICustomHarvestDrops dropper = (ICustomHarvestDrops)te;
		final List<ItemStack> drops = Lists.newArrayList();
		dropper.addHarvestDrops(player, drops, state, fortune, false);

		ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, fortune, 1.0f, false, player);
		for (ItemStack drop : drops)
			spawnAsEntity(world, pos, drop);

		addNormalDrops = !dropper.suppressBlockHarvestDrops();
	}

	if (addNormalDrops)
		dropBlockAsItem(world, pos, state, fortune);

	harvesters.set(null);
}
 
Example 6
Source File: BlockYuba.java    From TofuCraftReload with MIT License 6 votes vote down vote up
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, @Nullable ItemStack stack)
{
    player.addStat(StatList.getBlockStats(this), 1);
    player.addExhaustion(0.025F);

    if (this.canSilkHarvest(worldIn, pos, state, player) && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) > 0)
    {
        this.dropYuba(worldIn, pos, state);
    }
    else
    {
        if (stack != null && OreDictionary.containsMatch(false, OreDictionary.getOres("stickWood"), new ItemStack(Items.STICK)))
        {
            this.dropYuba(worldIn, pos, state);
        }
    }
}
 
Example 7
Source File: ScrapingDisabledWarning.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SubscribeEvent
public static void addInformation(ItemTooltipEvent ev)
{
    if (!ConfigManager.SERVER.enableScraping.get() && EnchantmentHelper.getEnchantmentLevel(SurvivalistMod.SCRAPING.get(), ev.getItemStack()) > 0)
    {
        List<ITextComponent> list = ev.getToolTip();
        /*int lastScraping = -1;
        for (int i = 0; i < list.size(); i++)
        {
            if (list.get(i).getFormattedText().startsWith(I18n.format("enchantment.survivalist.scraping")))
            {
                lastScraping = i;
            }
        }
        if (lastScraping >= 0)
        {
            list.add(lastScraping + 1, "" + TextFormatting.DARK_GRAY + TextFormatting.ITALIC + I18n.format("tooltip.survivalist.scraping.disabled"));
        }*/
        list.add(new TranslationTextComponent("tooltip.survivalist.scraping.disabled").func_240701_a_(TextFormatting.DARK_GRAY, TextFormatting.ITALIC));
    }
}
 
Example 8
Source File: InfusionEnchantmentRecipe.java    From Chisel-2 with GNU General Public License v2.0 5 votes vote down vote up
public int calcInstability(ItemStack recipeInput) {
	int i = 0;
	Map map1 = EnchantmentHelper.getEnchantments(recipeInput);
	Iterator iterator = map1.keySet().iterator();
       while (iterator.hasNext())
       {
       	int j1 = ((Integer)iterator.next()).intValue();
       	i += EnchantmentHelper.getEnchantmentLevel(j1, recipeInput);
       }
	return (i/2) + instability;
}
 
Example 9
Source File: ItemMorphShovel.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Override
public boolean onBlockDestroyed(ItemStack stack, World world, Block block, int x, int y, int z, EntityLivingBase player) {
    if(EnchantmentHelper.getEnchantmentLevel(DarkEnchantments.impact.effectId, stack) <= 0)
        return super.onBlockDestroyed(stack, world, block, x, y, z, player);
    if(!player.worldObj.isRemote) {
        int meta = world.getBlockMetadata(x, y, z);
        if(ForgeHooks.isToolEffective(stack, block, meta)) {
            for(int aa = -1; aa <= 1; ++aa) {
                for(int bb = -1; bb <= 1; ++bb) {
                    int xx = 0;
                    int yy = 0;
                    int zz = 0;
                    if(this.side <= 1) {
                        xx = aa;
                        zz = bb;
                    } else if(this.side <= 3) {
                        xx = aa;
                        yy = bb;
                    } else {
                        zz = aa;
                        yy = bb;
                    }

                    if(!(player instanceof EntityPlayer) || world.canMineBlock((EntityPlayer)player, x + xx, y + yy, z + zz)) {
                        Block bl = world.getBlock(x + xx, y + yy, z + zz);
                        meta = world.getBlockMetadata(x + xx, y + yy, z + zz);
                        if(bl.getBlockHardness(world, x + xx, y + yy, z + zz) >= 0.0F && ForgeHooks.isToolEffective(stack, bl, meta)) {
                            stack.damageItem(1, player);
                            BlockUtils.harvestBlock(world, (EntityPlayer)player, x + xx, y + yy, z + zz, true, 2);
                        }
                    }
                }
            }
        }
        else
            return super.onBlockDestroyed(stack, world, block, x, y, z, player);
    }

    return super.onBlockDestroyed(stack, world, block, x, y, z, player);
}
 
Example 10
Source File: ItemBreakingTracker.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void onItemBroken(PlayerEntity player, ItemStack stack)
{
    int scrappingLevel = EnchantmentHelper.getEnchantmentLevel(SurvivalistMod.SCRAPING.get(), stack);

    if (player.getClass().getName().equals("com.rwtema.extrautils2.fakeplayer.XUFakePlayer"))
        return;

    boolean fortune = rnd.nextDouble() > 0.9 / (1 + scrappingLevel);

    ItemStack ret = null;

    for (Triple<ItemStack, ItemStack, ItemStack> scraping : scrapingRegistry)
    {
        ItemStack source = scraping.getLeft();

        if (source.getItem() != stack.getItem())
            continue;

        ItemStack good = scraping.getMiddle();
        ItemStack bad = scraping.getRight();

        ret = fortune ? good.copy() : bad.copy();

        break;
    }

    if (ret != null)
    {
        SurvivalistMod.LOGGER.debug("Item broke (" + stack + ") and the player got " + ret + " in return!");

        SurvivalistMod.channel.sendTo(new ScrapingMessage(stack, ret), ((ServerPlayerEntity) player).connection.netManager, NetworkDirection.PLAY_TO_CLIENT);

        ItemHandlerHelper.giveItemToPlayer(player, ret);
    }
}
 
Example 11
Source File: ItemKotachi.java    From Sakura_mod with MIT License 4 votes vote down vote up
@Override
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {
    int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.SWEEPING, stack);

    if (k > 0) {
        //big sweep!!!
        entityLiving.swingArm(entityLiving.getActiveHand());

        float f = (float) entityLiving.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
        f += EnchantmentHelper.getModifierForCreature(stack, entityLiving.getCreatureAttribute()) / 1.5F;

        float f3 = EnchantmentHelper.getSweepingDamageRatio(entityLiving) * f;

        float sweepingRatio = EnchantmentHelper.getSweepingDamageRatio(entityLiving);

        for (Entity entitylivingbase : worldIn.getEntitiesWithinAABB(Entity.class, entityLiving.getEntityBoundingBox().grow(1.4D + sweepingRatio * 1.2D, 0.3D + sweepingRatio * 0.15D, 1.4D + sweepingRatio * 1.2D))) {
            if (entitylivingbase != entityLiving && !entityLiving.isOnSameTeam(entitylivingbase)) {
                if (entitylivingbase instanceof EntityLivingBase) {
                    if (entitylivingbase instanceof EntityPlayer && ((EntityPlayer) entitylivingbase).isActiveItemStackBlocking()) {
                        //disable shield
                        ((EntityPlayer) entitylivingbase).disableShield(false);
                    }
                    ((EntityLivingBase) entitylivingbase).knockBack(entityLiving, 0.4F + 0.1F * EnchantmentHelper.getSweepingDamageRatio(entityLiving), MathHelper.sin(entityLiving.rotationYaw * 0.017453292F), (-MathHelper.cos(entityLiving.rotationYaw * 0.017453292F)));
                    entitylivingbase.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) entityLiving), f3);
                }

                if (entitylivingbase instanceof MultiPartEntityPart) {
                    entitylivingbase.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) entityLiving), f3);
                }
            }
        }

        worldIn.playSound(null, entityLiving.posX, entityLiving.posY, entityLiving.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_SWEEP, entityLiving.getSoundCategory(), 1.0F, 1.0F);

        if (worldIn instanceof WorldServer) {
            double d0 = (-MathHelper.sin(entityLiving.rotationYaw * 0.017453292F));
            double d1 = MathHelper.cos(entityLiving.rotationYaw * 0.017453292F);
            ((WorldServer) worldIn).spawnParticle(EnumParticleTypes.SWEEP_ATTACK, entityLiving.posX + d0, entityLiving.posY + entityLiving.height * 0.5D, entityLiving.posZ + d1, 0, d0, 0.0D, d1, 0.0D);
        }

        stack.damageItem(2, entityLiving);

        ((EntityPlayer) entityLiving).getCooldownTracker().setCooldown(this, 16);
    }

}
 
Example 12
Source File: InfusionEnchantmentRecipe.java    From AdvancedMod with GNU General Public License v3.0 4 votes vote down vote up
public int calcXP(ItemStack recipeInput) {
	return recipeXP * (1+EnchantmentHelper.getEnchantmentLevel(enchantment.effectId, recipeInput));
}
 
Example 13
Source File: BlockLogNatural.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
{
	if(world.isRemote)
		return true;

	//get our item parameters
	ItemStack stack = player.getHeldItemMainhand();
	int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);
	int maxCut = 0;
	if(stack.getItem() instanceof ItemAxe)
	{
		maxCut = ((ItemAxe)stack.getItem()).maxTreeSize;
	}
	else return false;

	//create the map of our tree
	BlockPosList tree = BlockLogNatural.getTreeForCut(world, pos);
	int count = tree.size();

	//if the tree has too many blocks then prevent chopping
	if(count > maxCut)
	{
		player.sendMessage(new TextComponentTranslation(Core.translate("gui.axe.treetoobig")));
		return false;
	}
	else if(count > stack.getMaxDamage() - stack.getItemDamage())
	{
		player.sendMessage(new TextComponentTranslation(Core.translate("gui.axe.needsrepair")));
		return false;
	}
	else
	{
		for(BlockPos p : tree)
		{
			IBlockState s = world.getBlockState(p);
			this.onBlockHarvested(world, pos, s, player);
			world.setBlockToAir(p);
			s.getBlock().dropBlockAsItem(world, p, s, fortune);
		}
	}
	stack.damageItem(count-1, player);

	return true;
}
 
Example 14
Source File: ItemMorphShovel.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 4 votes vote down vote up
public int getWarp(ItemStack itemstack, EntityPlayer player) {
    if(EnchantmentHelper.getEnchantmentLevel(DarkEnchantments.voidtouched.effectId, itemstack) > 0)
        return 1;
    else
        return 0;
}
 
Example 15
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean addToolDamage(ItemStack stack, int amount, EntityLivingBase living1, EntityLivingBase living2)
{
    //System.out.println("addToolDamage(): living1: " + living1 + " living2: " + living2 + " remote: " + living2.worldObj.isRemote);
    if (this.isToolBroken(stack))
    {
        return false;
    }

    if (amount > 0)
    {
        int unbreakingLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.getEnchantmentByLocation("unbreaking"), stack);
        int amountNegated = 0;

        for (int i = 0; unbreakingLevel > 0 && i < amount; i++)
        {
            if (itemRand.nextInt(amount + 1) > 0)
            {
                amountNegated++;
            }
        }

        amount -= amountNegated;

        if (amount <= 0)
        {
            return false;
        }
    }

    int damage = this.getDamage(stack);
    damage = Math.min(damage + amount, this.material.getMaxUses());
    this.setDamage(stack, damage);

    // Tool just broke
    if (damage == this.material.getMaxUses())
    {
        living1.renderBrokenItemStack(stack);
    }

    return true;
}
 
Example 16
Source File: BlockLogNatural2.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
{
	if(world.isRemote)
		return true;

	//get our item parameters
	ItemStack stack = player.getHeldItemMainhand();
	int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);
	int maxCut = 0;
	if(stack.getItem() instanceof ItemAxe)
	{
		maxCut = ((ItemAxe)stack.getItem()).maxTreeSize;
	}
	else return false;

	//create the map of our tree
	BlockPosList tree = BlockLogNatural.getTreeForCut(world, pos);
	int count = tree.size();

	//if the tree has too many blocks then prevent chopping
	if(count > maxCut)
	{
		player.sendMessage(new TextComponentTranslation(Core.translate("gui.axe.treetoobig")));
		return false;
	}
	else if(count > stack.getMaxDamage() - stack.getItemDamage())
	{
		player.sendMessage(new TextComponentTranslation(Core.translate("gui.axe.needsrepair")));
		return false;
	}
	else
	{
		for(BlockPos p : tree)
		{
			IBlockState s = world.getBlockState(p);
			this.onBlockHarvested(world, pos, s, player);
			world.setBlockToAir(p);
			s.getBlock().dropBlockAsItem(world, p, s, fortune);
		}
	}
	stack.damageItem(count-1, player);

	return true;
}
 
Example 17
Source File: InfusionEnchantmentRecipe.java    From AdvancedMod with GNU General Public License v3.0 4 votes vote down vote up
/**
     * Used to check if a recipe matches current crafting inventory
     * @param player 
     */
	public boolean matches(ArrayList<ItemStack> input, ItemStack central, World world, EntityPlayer player) {
		if (research.length()>0 && !ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), research)) {
    		return false;
    	}
		
		if (!enchantment.canApply(central) || !central.getItem().isItemTool(central)) {
			return false;
		}
				
		Map map1 = EnchantmentHelper.getEnchantments(central);
		Iterator iterator = map1.keySet().iterator();
        while (iterator.hasNext())
        {
        	int j1 = ((Integer)iterator.next()).intValue();
            Enchantment ench = Enchantment.enchantmentsList[j1];
            if (j1 == enchantment.effectId &&
            		EnchantmentHelper.getEnchantmentLevel(j1, central)>=ench.getMaxLevel())
            	return false;
            if (enchantment.effectId != ench.effectId && 
            	(!enchantment.canApplyTogether(ench) ||
            	!ench.canApplyTogether(enchantment))) {
            	return false;
            }
        }
		
		ItemStack i2 = null;
		
		ArrayList<ItemStack> ii = new ArrayList<ItemStack>();
		for (ItemStack is:input) {
			ii.add(is.copy());
		}
		
		for (ItemStack comp:components) {
			boolean b=false;
			for (int a=0;a<ii.size();a++) {
				 i2 = ii.get(a).copy();
				if (comp.getItemDamage()==OreDictionary.WILDCARD_VALUE) {
					i2.setItemDamage(OreDictionary.WILDCARD_VALUE);
				}
				if (areItemStacksEqual(i2, comp,true)) {
					ii.remove(a);
					b=true;
					break;
				}
			}
			if (!b) return false;
		}
//		System.out.println(ii.size());
		return ii.size()==0?true:false;
    }
 
Example 18
Source File: ItemVoidPickaxe.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean addToolDamage(ItemStack stack, int amount, EntityLivingBase living1, EntityLivingBase living2)
{
    //System.out.println("addToolDamage(): living1: " + living1 + " living2: " + living2 + " remote: " + living2.worldObj.isRemote);
    if (this.isToolBroken(stack))
    {
        return false;
    }

    if (amount > 0)
    {
        int unbreakingLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.getEnchantmentByLocation("unbreaking"), stack);
        int amountNegated = 0;

        for (int i = 0; unbreakingLevel > 0 && i < amount; i++)
        {
            if (itemRand.nextInt(amount + 1) > 0)
            {
                amountNegated++;
            }
        }

        amount -= amountNegated;

        if (amount <= 0)
        {
            return false;
        }
    }

    int damage = this.getDamage(stack);
    damage = Math.min(damage + amount, this.material.getMaxUses());

    if (living1.getEntityWorld().isRemote == false)
    {
        this.setDamage(stack, damage);
    }

    // Tool just broke
    if (damage == this.material.getMaxUses())
    {
        //System.out.printf("tool broke @ %s\n", living1.getEntityWorld().isRemote ? "client" : "server");
        living1.renderBrokenItemStack(stack);
    }

    return true;
}