Files
TinkersConstruct/src/main/java/tconstruct/library/tools/HarvestTool.java
T

142 lines
4.4 KiB
Java
Raw Normal View History

package tconstruct.library.tools;
import tconstruct.library.ActiveToolMod;
import tconstruct.library.TConstructRegistry;
2014-01-16 01:36:18 -05:00
import mantle.world.WorldHelper;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
2014-01-07 13:50:35 -05:00
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
/* Base class for tools that should be harvesting blocks */
public abstract class HarvestTool extends ToolCore
{
2013-12-31 11:58:36 -05:00
public HarvestTool(int baseDamage)
{
2013-12-31 11:58:36 -05:00
super(baseDamage);
}
@Override
public boolean onBlockStartBreak (ItemStack stack, int x, int y, int z, EntityPlayer player)
{
2013-08-31 05:13:31 -07:00
if (!stack.hasTagCompound())
return false;
2013-08-31 05:13:31 -07:00
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
World world = player.worldObj;
2014-01-07 13:50:35 -05:00
Block block = player.worldObj.func_147439_a(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
2014-01-07 13:50:35 -05:00
//Block block = Block.blocksList[bID];
if (block == null || block == Blocks.air)
return false;
int hlvl = MinecraftForge.getBlockHarvestLevel(block, meta, getHarvestType());
if (hlvl <= tags.getInteger("HarvestLevel"))
{
boolean cancelHarvest = false;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
if (mod.beforeBlockBreak(this, stack, x, y, z, player))
cancelHarvest = true;
}
return cancelHarvest;
}
else
{
2014-01-16 01:36:18 -05:00
WorldHelper.setBlockToAir(world, x, y, z);
if (!player.capabilities.isCreativeMode)
2014-01-30 20:34:22 -05:00
func_150894_a(stack, world, block, x, y, z, player);
if (!world.isRemote)
2014-01-07 13:50:35 -05:00
world.playAuxSFX(2001, x, y, z, block.getBlockID() + (meta << 12));
return true;
}
}
@Override
public float getStrVsBlock (ItemStack stack, Block block, int meta)
{
if (!stack.hasTagCompound())
return 1.0f;
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
if (tags.getBoolean("Broken"))
return 0.1f;
Material[] materials = getEffectiveMaterials();
for (int i = 0; i < materials.length; i++)
{
2014-01-07 15:07:07 -05:00
if (materials[i] == block.func_149688_o())
{
2013-09-17 22:39:49 -07:00
return calculateStrength(tags, block, meta);
}
}
2013-09-17 22:39:49 -07:00
if (MinecraftForge.getBlockHarvestLevel(block, meta, getHarvestType()) > 0)
{
return calculateStrength(tags, block, meta); //No issue if the harvest level is too low
2013-09-17 22:39:49 -07:00
}
return super.getStrVsBlock(stack, block, meta);
}
2013-11-10 13:57:35 -08:00
float calculateStrength (NBTTagCompound tags, Block block, int meta)
2013-09-17 22:39:49 -07:00
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;
if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}
if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}
if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}
float trueSpeed = mineSpeed / (heads * 100f);
int hlvl = MinecraftForge.getBlockHarvestLevel(block, meta, getHarvestType());
int durability = tags.getInteger("Damage");
float stonebound = tags.getFloat("Shoddy");
float bonusLog = (float) Math.log(durability / 72f + 1) * 2 * stonebound;
trueSpeed += bonusLog;
if (hlvl <= tags.getInteger("HarvestLevel"))
return trueSpeed;
return 0.1f;
}
public boolean canHarvestBlock (Block block)
{
2014-01-07 15:07:07 -05:00
if (block.func_149688_o().isToolNotRequired())
{
return true;
}
for (Material m : getEffectiveMaterials())
{
2014-01-07 15:07:07 -05:00
if (m == block.func_149688_o())
return true;
}
return false;
}
@Override
public String[] toolCategories ()
{
return new String[] { "harvest" };
}
protected abstract Material[] getEffectiveMaterials ();
protected abstract String getHarvestType ();
}