Move some PartWire stuff up into superclasses
This commit is contained in:
parent
f51a371559
commit
f18af3964b
3 changed files with 126 additions and 130 deletions
|
@ -1,5 +1,6 @@
|
|||
package resonantinduction.wire.multipart;
|
||||
|
||||
import codechicken.multipart.TileMultipart;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
@ -18,6 +19,23 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
private IElectricityNetwork network;
|
||||
|
||||
public TileEntity[] adjacentConnections = null;
|
||||
public byte currentConnections = 0x00;
|
||||
|
||||
|
||||
@Override
|
||||
public void bind(TileMultipart t)
|
||||
{
|
||||
if (tile() != null && network != null)
|
||||
{
|
||||
this.getNetwork().getConductors().remove(tile());
|
||||
super.bind(t);
|
||||
this.getNetwork().getConductors().add((IConductor) tile());
|
||||
}
|
||||
else
|
||||
{
|
||||
super.bind(t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preRemove()
|
||||
|
@ -53,62 +71,111 @@ public abstract class PartConductor extends PartAdvanced implements IConductor
|
|||
this.network = network;
|
||||
}
|
||||
|
||||
public boolean connectionPrevented(TileEntity tile, ForgeDirection side)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public byte getPossibleConnections()
|
||||
{
|
||||
byte connections = 0x00;
|
||||
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
if (tileEntity instanceof INetworkProvider && !this.connectionPrevented(tileEntity, side))
|
||||
connections |= 1 << side.ordinal();
|
||||
}
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh()
|
||||
{
|
||||
if (!this.world().isRemote)
|
||||
{
|
||||
this.adjacentConnections = null;
|
||||
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
byte possibleConnections = getPossibleConnections();
|
||||
if (possibleConnections != currentConnections)
|
||||
{
|
||||
TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (tileEntity != null)
|
||||
byte or = (byte) (possibleConnections | currentConnections);
|
||||
if (or != possibleConnections) //Connections have been removed
|
||||
{
|
||||
if (tileEntity.getClass() == tile().getClass() && tileEntity instanceof INetworkProvider)
|
||||
this.getNetwork().split((IConductor) tile());
|
||||
this.setNetwork(null);
|
||||
}
|
||||
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
byte tester = (byte) (1 << side.ordinal());
|
||||
if ((possibleConnections & tester) > 0)
|
||||
{
|
||||
this.getNetwork().merge(((INetworkProvider) tileEntity).getNetwork());
|
||||
TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (tileEntity instanceof INetworkProvider)
|
||||
{
|
||||
this.getNetwork().merge(((INetworkProvider) tileEntity).getNetwork());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.getNetwork().refresh();
|
||||
currentConnections = possibleConnections;
|
||||
|
||||
}
|
||||
this.sendDescUpdate();
|
||||
this.getNetwork().refresh();
|
||||
|
||||
}
|
||||
tile().markRender();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity[] getAdjacentConnections()
|
||||
{
|
||||
/**
|
||||
* Cache the adjacentConnections.
|
||||
*/
|
||||
if (this.adjacentConnections == null)
|
||||
{
|
||||
this.adjacentConnections = new TileEntity[6];
|
||||
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
ForgeDirection side = ForgeDirection.getOrientation(i);
|
||||
TileEntity tileEntity = VectorHelper.getConnectorFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (tileEntity instanceof IConnector)
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (isCurrentlyConnected(tileEntity, side))
|
||||
{
|
||||
if (((IConnector) tileEntity).canConnect(side.getOpposite()))
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.adjacentConnections;
|
||||
}
|
||||
|
||||
public boolean isCurrentlyConnected(TileEntity tileEntity, ForgeDirection side)
|
||||
{
|
||||
if ((this.currentConnections & 1 << side.ordinal()) > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!this.canConnect(side))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnect(ForgeDirection direction)
|
||||
{
|
||||
return true;
|
||||
Vector3 connectPos = new Vector3(tile()).modifyPositionFromSide(direction);
|
||||
TileEntity connectTile = connectPos.getTileEntity(this.world());
|
||||
return !connectionPrevented(connectTile, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.Set;
|
|||
|
||||
import universalelectricity.compatibility.Compatibility;
|
||||
import universalelectricity.core.block.IConnector;
|
||||
import universalelectricity.core.block.INetworkProvider;
|
||||
import universalelectricity.core.electricity.ElectricityPack;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import universalelectricity.core.vector.VectorHelper;
|
||||
|
@ -36,56 +37,33 @@ public abstract class PartUniversalConductor extends PartConductor implements IE
|
|||
this.powerHandler.configure(0, this.buildcraftBuffer, this.buildcraftBuffer, this.buildcraftBuffer * 2);
|
||||
this.powerHandler.configurePowerPerdition(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity[] getAdjacentConnections()
|
||||
|
||||
public boolean isCurrentlyConnected(TileEntity tileEntity, ForgeDirection side)
|
||||
{
|
||||
if (this.adjacentConnections == null)
|
||||
if ((this.currentConnections & 1 << side.ordinal()) > 0)
|
||||
{
|
||||
this.adjacentConnections = new TileEntity[6];
|
||||
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
ForgeDirection side = ForgeDirection.getOrientation(i);
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (tileEntity instanceof IConnector)
|
||||
{
|
||||
if (this.canConnect(side) && ((IConnector) tileEntity).canConnect(side.getOpposite()))
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
}
|
||||
else if (Compatibility.isIndustrialCraft2Loaded() && tileEntity instanceof IEnergyTile)
|
||||
{
|
||||
if (tileEntity instanceof IEnergyAcceptor)
|
||||
{
|
||||
if (((IEnergyAcceptor) tileEntity).acceptsEnergyFrom(tile(), side.getOpposite()))
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (tileEntity instanceof IEnergyEmitter)
|
||||
{
|
||||
if (((IEnergyEmitter) tileEntity).emitsEnergyTo(tileEntity, side.getOpposite()))
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
else if (Compatibility.isBuildcraftLoaded() && tileEntity instanceof IPowerReceptor)
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.adjacentConnections;
|
||||
|
||||
if (!this.canConnect(side))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tileEntity instanceof IConnector && ((IConnector)tileEntity).canConnect(side.getOpposite()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (Compatibility.isIndustrialCraft2Loaded() && tileEntity instanceof IEnergyTile)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (Compatibility.isBuildcraftLoaded() && tileEntity instanceof IPowerReceptor)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -64,10 +64,8 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
public static IndexedCuboid6[] sides = new IndexedCuboid6[7];
|
||||
public static IndexedCuboid6[] insulatedSides = new IndexedCuboid6[7];
|
||||
public EnumWireMaterial material = EnumWireMaterial.COPPER;
|
||||
public byte currentConnections = 0x00;
|
||||
|
||||
/** Client Side Connection Check */
|
||||
public boolean isTick = false;
|
||||
private ForgeDirection testingSide;
|
||||
|
||||
static {
|
||||
|
@ -98,19 +96,9 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
this.material = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(TileMultipart t)
|
||||
public PartWire()
|
||||
{
|
||||
if (tile() != null)
|
||||
{
|
||||
this.getNetwork().getConductors().remove(tile());
|
||||
super.bind(t);
|
||||
this.getNetwork().getConductors().add((IConductor) tile());
|
||||
}
|
||||
else
|
||||
{
|
||||
super.bind(t);
|
||||
}
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -121,9 +109,7 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
return false;
|
||||
}
|
||||
|
||||
Vector3 connectPos = new Vector3(tile()).modifyPositionFromSide(direction);
|
||||
TileEntity connectTile = connectPos.getTileEntity(this.world());
|
||||
return !connectionPrevented(connectTile, direction);
|
||||
return super.canConnect(direction);
|
||||
}
|
||||
|
||||
public boolean connectionPrevented(TileEntity tile, ForgeDirection side)
|
||||
|
@ -149,24 +135,16 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
|
||||
public byte getPossibleConnections()
|
||||
{
|
||||
byte connections = 0x00;
|
||||
if (this.world().isBlockIndirectlyGettingPowered(this.x(), this.y(), this.z()))
|
||||
{
|
||||
return connections;
|
||||
return 0x00;
|
||||
}
|
||||
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
if (tileEntity instanceof INetworkProvider && !this.connectionPrevented(tileEntity, side))
|
||||
connections |= 1 << side.ordinal();
|
||||
}
|
||||
return connections;
|
||||
return super.getPossibleConnections();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh()
|
||||
{
|
||||
// this.adjacentConnections = null;
|
||||
if (!this.world().isRemote)
|
||||
{
|
||||
this.adjacentConnections = null;
|
||||
|
@ -238,30 +216,6 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
this.material = EnumWireMaterial.values()[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Furnace connection for tick wires
|
||||
*/
|
||||
@Override
|
||||
public TileEntity[] getAdjacentConnections()
|
||||
{
|
||||
super.getAdjacentConnections();
|
||||
|
||||
if (this.isTick)
|
||||
{
|
||||
for (byte i = 0; i < 6; i++)
|
||||
{
|
||||
ForgeDirection side = ForgeDirection.getOrientation(i);
|
||||
TileEntity tileEntity = VectorHelper.getTileEntityFromSide(this.world(), new Vector3(tile()), side);
|
||||
|
||||
if (tileEntity instanceof TileEntityFurnace)
|
||||
{
|
||||
this.adjacentConnections[i] = tileEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.adjacentConnections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doWork(PowerHandler workProvider)
|
||||
{
|
||||
|
@ -289,17 +243,22 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
IndexedCuboid6[] currentSides = this.isInsulated() ? insulatedSides : sides;
|
||||
if(tile() != null)
|
||||
{
|
||||
TileEntity[] connections = getAdjacentConnections();
|
||||
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
int ord = side.ordinal();
|
||||
if(connections[ord] != null || side == this.testingSide) subParts.add(currentSides[ord]);
|
||||
if(connectionMapContainsSide(currentConnections, side) || side == this.testingSide) subParts.add(currentSides[ord]);
|
||||
}
|
||||
}
|
||||
subParts.add(currentSides[6]);
|
||||
return subParts;
|
||||
}
|
||||
|
||||
public boolean connectionMapContainsSide(byte connections, ForgeDirection side)
|
||||
{
|
||||
byte tester = (byte) (1 << side.ordinal());
|
||||
return ((connections & tester) > 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Cuboid6> getCollisionBoxes()
|
||||
{
|
||||
|
@ -353,7 +312,6 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
this.setMaterialFromID(packet.readInt());
|
||||
this.dyeID = packet.readInt();
|
||||
this.isInsulated = packet.readBoolean();
|
||||
this.isTick = packet.readBoolean();
|
||||
this.currentConnections = packet.readByte();
|
||||
if (tile() != null) tile().markRender();
|
||||
}
|
||||
|
@ -364,7 +322,6 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
packet.writeInt(this.getTypeID());
|
||||
packet.writeInt(this.dyeID);
|
||||
packet.writeBoolean(this.isInsulated);
|
||||
packet.writeBoolean(this.isTick);
|
||||
packet.writeByte(this.currentConnections);
|
||||
}
|
||||
|
||||
|
@ -386,12 +343,6 @@ public class PartWire extends PartUniversalConductor implements TSlottedPart, JN
|
|||
this.isInsulated = nbt.getBoolean("isInsulated");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doesTick()
|
||||
{
|
||||
return this.isTick;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack pickItem(MovingObjectPosition hit)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue