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

197 lines
5.8 KiB
Java
Raw Normal View History

package tconstruct.library.tools;
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.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
2014-02-05 10:28:54 -05:00
import tconstruct.library.ActiveToolMod;
import tconstruct.library.TConstructRegistry;
/* 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 float getDigSpeed (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-02-05 08:28:53 -05:00
if (materials[i] == block.getMaterial())
{
2013-09-17 22:39:49 -07:00
return calculateStrength(tags, block, meta);
}
}
2014-02-01 15:17:30 -05:00
if (block.getHarvestLevel(meta) > 0)
2013-09-17 22:39:49 -07:00
{
2014-02-16 14:23:50 -05:00
return calculateStrength(tags, block, meta); // No issue if the
// harvest level is
// too low
2013-09-17 22:39:49 -07:00
}
return super.getDigSpeed(stack, block, meta);
}
2013-11-10 13:57:35 -08:00
public 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);
2014-02-01 15:17:30 -05:00
int hlvl = block.getHarvestLevel(meta);
2013-09-17 22:39:49 -07:00
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;
}
@Override
2014-03-19 19:38:35 +03:00
public boolean func_150897_b (Block block)
{
2014-02-05 08:28:53 -05:00
if (block.getMaterial().isToolNotRequired())
{
return true;
}
for (Material m : getEffectiveMaterials())
{
2014-02-05 08:28:53 -05:00
if (m == block.getMaterial())
return true;
}
return false;
}
@Override
public boolean canHarvestBlock (Block block, ItemStack itemStack)
{
return func_150897_b(block);
}
@Override
2014-06-30 22:26:00 -08:00
public String[] getTraits ()
{
return new String[] { "harvest" };
}
protected abstract Material[] getEffectiveMaterials ();
protected abstract String getHarvestType ();
//Right-click
@Override
public boolean onItemUse (ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float clickX, float clickY, float clickZ)
{
/*if (world.isRemote)
return true;*/
boolean used = false;
int hotbarSlot = player.inventory.currentItem;
int itemSlot = hotbarSlot == 0 ? 8 : hotbarSlot + 1;
ItemStack nearbyStack = null;
if (hotbarSlot < 8)
{
nearbyStack = player.inventory.getStackInSlot(itemSlot);
if (nearbyStack != null)
{
Item item = nearbyStack.getItem();
if (item instanceof ItemBlock)
{
int posX = x;
int posY = y;
int posZ = z;
int playerPosX = (int) Math.floor(player.posX);
int playerPosY = (int) Math.floor(player.posY);
int playerPosZ = (int) Math.floor(player.posZ);
if (side == 0)
{
--posY;
}
if (side == 1)
{
++posY;
}
if (side == 2)
{
--posZ;
}
if (side == 3)
{
++posZ;
}
if (side == 4)
{
--posX;
}
if (side == 5)
{
++posX;
}
if (posX == playerPosX && (posY == playerPosY || posY == playerPosY + 1 || posY == playerPosY - 1) && posZ == playerPosZ)
{
return false;
}
used = item.onItemUse(nearbyStack, player, world, x, y, z, side, clickX, clickY, clickZ);
if (nearbyStack.stackSize < 1)
{
nearbyStack = null;
player.inventory.setInventorySlotContents(itemSlot, null);
}
}
}
}
/*
if (used) //Update client
{
Packet103SetSlot packet = new Packet103SetSlot(player.openContainer.windowId, itemSlot, nearbyStack);
((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet);
}
*/
return used;
}
}