electrodynamics/src/resonantinduction/wire/part/PartAdvancedWire.java

227 lines
4.6 KiB
Java
Raw Normal View History

2013-12-21 07:23:59 +01:00
package resonantinduction.wire.part;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockColored;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemShears;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;
import resonantinduction.wire.EnumWireMaterial;
2013-12-22 10:22:46 +01:00
import universalelectricity.api.CompatibilityModule;
2013-12-21 09:38:34 +01:00
import codechicken.lib.data.MCDataInput;
import codechicken.lib.data.MCDataOutput;
2013-12-21 07:23:59 +01:00
/**
* @author Calclavia
*
*/
2013-12-22 16:49:43 +01:00
public abstract class PartAdvancedWire extends PartConductor
2013-12-21 07:23:59 +01:00
{
public static final int DEFAULT_COLOR = 16;
public int dyeID = DEFAULT_COLOR;
public EnumWireMaterial material = EnumWireMaterial.COPPER;
public boolean isInsulated = false;
2013-12-22 10:22:46 +01:00
/**
2013-12-22 10:48:15 +01:00
* INTERNAL USE.
* Can this conductor connect with an external object?
2013-12-22 10:22:46 +01:00
*/
2013-12-22 10:48:15 +01:00
@Override
public boolean canConnectTo(Object obj)
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
if (obj instanceof PartFlatWire)
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
PartFlatWire wire = (PartFlatWire) obj;
2013-12-21 07:23:59 +01:00
2013-12-22 06:24:41 +01:00
if (wire.getMaterial() == getMaterial())
2013-12-21 07:23:59 +01:00
{
2013-12-22 06:24:41 +01:00
if (this.isInsulated() && wire.isInsulated())
{
2013-12-22 16:49:43 +01:00
return this.getColor() == wire.getColor();
2013-12-22 06:24:41 +01:00
}
2013-12-21 07:23:59 +01:00
return true;
}
}
2013-12-22 10:22:46 +01:00
return CompatibilityModule.isHandler(obj);
2013-12-21 07:23:59 +01:00
}
@Override
public long getEnergyLoss()
{
/**
* TODO: FIX THIS!
*/
return (int) (this.getMaterial().resistance * 1000);
}
@Override
public long getEnergyCapacitance()
{
return this.getMaterial().maxAmps;
}
2013-12-22 16:49:43 +01:00
/**
* Material Methods
*/
2013-12-21 07:23:59 +01:00
public EnumWireMaterial getMaterial()
{
2013-12-22 16:49:43 +01:00
return this.material;
2013-12-21 07:23:59 +01:00
}
2013-12-22 16:49:43 +01:00
public void setMaterial(EnumWireMaterial material)
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
this.material = material;
2013-12-21 07:23:59 +01:00
}
2013-12-22 16:49:43 +01:00
public void setMaterial(int id)
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
this.setMaterial(EnumWireMaterial.values()[id]);
2013-12-21 07:23:59 +01:00
}
2013-12-22 16:49:43 +01:00
public int getMaterialID()
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
return this.material.ordinal();
2013-12-21 07:23:59 +01:00
}
2013-12-22 16:49:43 +01:00
/**
* Insulation Methods
*/
2013-12-21 07:23:59 +01:00
public void setInsulated(boolean insulated)
{
2013-12-22 16:49:43 +01:00
this.isInsulated = insulated;
this.dyeID = DEFAULT_COLOR;
2013-12-21 07:23:59 +01:00
}
public void setInsulated(int dyeColour)
{
isInsulated = true;
dyeID = dyeColour;
tile().notifyPartChange(this);
}
2013-12-22 16:49:43 +01:00
public boolean isInsulated()
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
return this.isInsulated;
2013-12-21 07:23:59 +01:00
}
2013-12-22 16:49:43 +01:00
/**
* Wire Coloring Methods
*/
public int getColor()
2013-12-21 07:23:59 +01:00
{
2013-12-22 16:49:43 +01:00
return this.isInsulated ? this.dyeID : -1;
}
public void setColor(int dye)
{
this.dyeID = dye;
2013-12-21 07:23:59 +01:00
}
/**
* Changes the wire's color.
*/
@Override
public boolean activate(EntityPlayer player, MovingObjectPosition part, ItemStack item)
{
if (item != null)
{
if (item.itemID == Item.dyePowder.itemID && isInsulated())
{
2013-12-22 16:49:43 +01:00
setColor(item.getItemDamage());
2013-12-21 07:23:59 +01:00
return true;
}
else if (item.itemID == Block.cloth.blockID)
{
if (isInsulated() && !world().isRemote)
{
tile().dropItems(Collections.singletonList(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(dyeID))));
}
setInsulated(BlockColored.getDyeFromBlock(item.getItemDamage()));
player.inventory.decrStackSize(player.inventory.currentItem, 1);
return true;
}
else if ((item.itemID == Item.shears.itemID || item.getItem() instanceof ItemShears) && isInsulated())
{
if (!world().isRemote)
{
tile().dropItems(Collections.singletonList(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(dyeID))));
}
setInsulated(false);
return true;
}
}
2013-12-22 05:06:29 +01:00
2013-12-21 07:23:59 +01:00
return false;
}
protected ItemStack getItem()
{
return EnumWireMaterial.values()[getMaterialID()].getWire();
}
@Override
public Iterable<ItemStack> getDrops()
{
List<ItemStack> drops = new ArrayList<ItemStack>();
drops.add(getItem());
if (this.isInsulated)
{
drops.add(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(dyeID)));
}
return drops;
}
@Override
public ItemStack pickItem(MovingObjectPosition hit)
{
return getItem();
}
@Override
public void readDesc(MCDataInput packet)
{
2013-12-22 16:49:43 +01:00
this.setMaterial(packet.readByte());
2013-12-21 07:23:59 +01:00
this.dyeID = packet.readByte();
this.isInsulated = packet.readBoolean();
}
@Override
public void writeDesc(MCDataOutput packet)
{
packet.writeByte((byte) this.getMaterialID());
packet.writeByte((byte) this.dyeID);
packet.writeBoolean(this.isInsulated);
}
@Override
public void save(NBTTagCompound nbt)
{
super.save(nbt);
nbt.setInteger("typeID", getMaterialID());
nbt.setBoolean("isInsulated", isInsulated);
nbt.setInteger("dyeID", dyeID);
}
@Override
public void load(NBTTagCompound nbt)
{
super.load(nbt);
2013-12-22 16:49:43 +01:00
setMaterial(nbt.getInteger("typeID"));
this.isInsulated = nbt.getBoolean("isInsulated");
this.dyeID = nbt.getInteger("dyeID");
2013-12-21 07:23:59 +01:00
}
}