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

296 lines
6.1 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;
2013-12-23 04:26:39 +01:00
import net.minecraft.entity.Entity;
2013-12-21 07:23:59 +01:00
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-23 05:38:55 +01:00
import universalelectricity.api.UniversalElectricity;
import universalelectricity.api.energy.IConductor;
import calclavia.lib.prefab.CustomDamageSource;
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
{
2013-12-23 03:38:08 +01:00
public static final int DEFAULT_COLOR = 15;
2013-12-22 17:55:08 +01:00
public int color = DEFAULT_COLOR;
2013-12-21 07:23:59 +01:00
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 17:55:08 +01:00
if (this.getMaterial() == wire.getMaterial())
2013-12-21 07:23:59 +01:00
{
2013-12-22 06:24:41 +01:00
if (this.isInsulated() && wire.isInsulated())
{
2013-12-23 03:38:08 +01:00
return this.getColor() == wire.getColor() || (this.getColor() == DEFAULT_COLOR || wire.getColor() == DEFAULT_COLOR);
2013-12-22 06:24:41 +01:00
}
2013-12-21 07:23:59 +01:00
return true;
}
}
2013-12-23 05:38:55 +01:00
else if (!(obj instanceof IConductor))
2013-12-23 03:38:08 +01:00
{
return CompatibilityModule.isHandler(obj);
}
2013-12-21 07:23:59 +01:00
2013-12-23 03:38:08 +01:00
return false;
2013-12-21 07:23:59 +01:00
}
2013-12-23 04:26:39 +01:00
@Override
public void onEntityCollision(Entity entity)
{
if (!this.isInsulated() && this.getNetwork().getLastBuffer() > 0)
{
entity.attackEntityFrom(CustomDamageSource.electrocution, this.getNetwork().getLastBuffer());
}
}
@Override
2013-12-23 05:38:55 +01:00
public float getResistance()
{
2013-12-23 05:38:55 +01:00
return this.getMaterial().resistance;
}
@Override
public long getCurrentCapacity()
{
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;
2013-12-22 17:55:08 +01:00
this.color = DEFAULT_COLOR;
if (!this.world().isRemote)
{
tile().notifyPartChange(this);
this.sendInsulationUpdate();
}
2013-12-21 07:23:59 +01:00
}
public void setInsulated(int dyeColour)
{
2013-12-22 17:55:08 +01:00
this.isInsulated = true;
this.color = dyeColour;
if (!this.world().isRemote)
{
tile().notifyPartChange(this);
this.sendInsulationUpdate();
this.sendColorUpdate();
}
2013-12-21 07:23:59 +01:00
}
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 17:55:08 +01:00
public void sendInsulationUpdate()
{
tile().getWriteStream(this).writeByte(1).writeBoolean(this.isInsulated);
}
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 17:55:08 +01:00
return this.isInsulated ? this.color : -1;
2013-12-22 16:49:43 +01:00
}
public void setColor(int dye)
{
2013-12-22 17:55:08 +01:00
if (this.isInsulated)
{
this.color = dye;
if (!this.world().isRemote)
{
tile().notifyPartChange(this);
this.sendColorUpdate();
}
}
}
public void sendColorUpdate()
{
tile().getWriteStream(this).writeByte(2).writeInt(this.color);
2013-12-21 07:23:59 +01:00
}
/**
* Changes the wire's color.
*/
@Override
2013-12-22 17:55:08 +01:00
public boolean activate(EntityPlayer player, MovingObjectPosition part, ItemStack itemStack)
2013-12-21 07:23:59 +01:00
{
2013-12-22 17:55:08 +01:00
if (itemStack != null)
2013-12-21 07:23:59 +01:00
{
2013-12-22 17:55:08 +01:00
if (itemStack.itemID == Item.dyePowder.itemID && this.isInsulated())
2013-12-21 07:23:59 +01:00
{
2013-12-22 17:55:08 +01:00
this.setColor(itemStack.getItemDamage());
2013-12-21 07:23:59 +01:00
return true;
}
2013-12-22 17:55:08 +01:00
else if (itemStack.itemID == Block.cloth.blockID)
2013-12-21 07:23:59 +01:00
{
2013-12-22 17:55:08 +01:00
if (this.isInsulated())
2013-12-21 07:23:59 +01:00
{
2013-12-22 17:55:08 +01:00
if (!world().isRemote)
{
tile().dropItems(Collections.singletonList(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(color))));
}
2013-12-21 07:23:59 +01:00
2013-12-22 17:55:08 +01:00
this.setInsulated(false);
return true;
}
else
{
this.setInsulated(BlockColored.getDyeFromBlock(itemStack.getItemDamage()));
player.inventory.decrStackSize(player.inventory.currentItem, 1);
return true;
}
2013-12-21 07:23:59 +01:00
}
2013-12-22 17:55:08 +01:00
else if ((itemStack.itemID == Item.shears.itemID || itemStack.getItem() instanceof ItemShears) && isInsulated())
2013-12-21 07:23:59 +01:00
{
if (!world().isRemote)
{
2013-12-22 17:55:08 +01:00
tile().dropItems(Collections.singletonList(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(color))));
2013-12-21 07:23:59 +01:00
}
2013-12-22 17:55:08 +01:00
this.setInsulated(false);
2013-12-21 07:23:59 +01:00
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)
{
2013-12-22 17:55:08 +01:00
drops.add(new ItemStack(Block.cloth, 1, BlockColored.getBlockFromDye(color)));
2013-12-21 07:23:59 +01:00
}
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-22 17:55:08 +01:00
this.color = packet.readByte();
2013-12-21 07:23:59 +01:00
this.isInsulated = packet.readBoolean();
}
@Override
public void writeDesc(MCDataOutput packet)
{
packet.writeByte((byte) this.getMaterialID());
2013-12-22 17:55:08 +01:00
packet.writeByte((byte) this.color);
2013-12-21 07:23:59 +01:00
packet.writeBoolean(this.isInsulated);
}
2013-12-22 17:55:08 +01:00
public void read(MCDataInput packet, int packetID)
{
switch (packetID)
{
case 1:
this.isInsulated = packet.readBoolean();
this.tile().markRender();
break;
case 2:
this.color = packet.readInt();
this.tile().markRender();
break;
}
}
2013-12-21 07:23:59 +01:00
@Override
public void save(NBTTagCompound nbt)
{
super.save(nbt);
nbt.setInteger("typeID", getMaterialID());
nbt.setBoolean("isInsulated", isInsulated);
2013-12-22 17:55:08 +01:00
nbt.setInteger("dyeID", color);
2013-12-21 07:23:59 +01:00
}
@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");
2013-12-22 17:55:08 +01:00
this.color = nbt.getInteger("dyeID");
2013-12-21 07:23:59 +01:00
}
}