Skip to content

Commit

Permalink
Make code use generics better and also fix Switch Fluid Pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplemonomials committed Feb 20, 2015
1 parent 03330e1 commit b373fdb
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 60 deletions.
9 changes: 5 additions & 4 deletions src/main/java/buildcraft/additionalpipes/AdditionalPipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import buildcraft.additionalpipes.gui.GuiHandler;
import buildcraft.additionalpipes.item.ItemPipeAP;
import buildcraft.additionalpipes.network.PacketHandler;
import buildcraft.additionalpipes.pipes.APPipe;
import buildcraft.additionalpipes.pipes.PipeItemsAdvancedInsertion;
import buildcraft.additionalpipes.pipes.PipeItemsAdvancedWood;
import buildcraft.additionalpipes.pipes.PipeItemsClosed;
Expand Down Expand Up @@ -244,22 +245,22 @@ private void loadConfigs(boolean init) {

private void loadPipes() {
// Item Teleport Pipe
pipeItemsTeleport = createPipeSpecial(PipeItemsTeleport.class);
pipeItemsTeleport = createPipeSpecial((Class<? extends APPipe<?>>) PipeItemsTeleport.class);

GameRegistry.addRecipe(new ItemStack(pipeItemsTeleport, 4), new Object[] { "dgd", 'd', BuildCraftCore.diamondGearItem, 'g', Blocks.glass });
AssemblyRecipeManager.INSTANCE.addRecipe("teleportPipe", 1000, new ItemStack(pipeItemsTeleport, 8), new Object[] { new ItemStack(BuildCraftSilicon.redstoneChipset, 1, 4), new ItemStack(BuildCraftTransport.pipeItemsDiamond, 8),
new ItemStack(BuildCraftSilicon.redstoneChipset, 1, 3) });


// Liquid Teleport Pipe
pipeLiquidsTeleport = createPipeSpecial(PipeLiquidsTeleport.class);
pipeLiquidsTeleport = createPipeSpecial((Class<? extends APPipe<?>>) PipeLiquidsTeleport.class);
if(pipeItemsTeleport != null) {
GameRegistry.addRecipe(new ItemStack(pipeLiquidsTeleport), new Object[] { "w", "P", 'w', BuildCraftTransport.pipeWaterproof, 'P', pipeItemsTeleport });
}

// Power Teleport Pipe

pipePowerTeleport = createPipeSpecial(PipePowerTeleport.class);
pipePowerTeleport = createPipeSpecial((Class<? extends APPipe<?>>) PipePowerTeleport.class);
if(pipeItemsTeleport != null) {
GameRegistry.addRecipe(new ItemStack(pipePowerTeleport), new Object[] { "r", "P", 'r', Items.redstone, 'P', pipeItemsTeleport });
}
Expand Down Expand Up @@ -310,7 +311,7 @@ private static Item createPipe(Class<? extends Pipe<?>> clas)
return res;
}

private Item createPipeSpecial(Class<? extends Pipe<?>> clas)
private Item createPipeSpecial(Class<? extends APPipe<?>> clas)
{
ItemPipe item = new ItemPipeAP();
item.setUnlocalizedName(clas.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void processCommand(ICommandSender sender, String[] args) {
{
StringBuffer sb = new StringBuffer();
sb.append("Teleport pipes: ");
for(PipeTeleport pipe : TeleportManager.instance.teleportPipes) {
for(PipeTeleport<?> pipe : TeleportManager.instance.teleportPipes) {
sb.append('[');
sb.append(pipe.getClass().getSimpleName()).append(',');
sb.append(pipe.getPosition().x).append(',');
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/buildcraft/additionalpipes/GuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int
}
switch(ID) {
case PIPE_TP:
return new ContainerTeleportPipe(player, (PipeTeleport) ((TileGenericPipe) tile).pipe);
return new ContainerTeleportPipe(player, (PipeTeleport<?>) ((TileGenericPipe) tile).pipe);
case PIPE_DIST:
return new ContainerDistributionPipe((TileGenericPipe) tile);
case PIPE_WOODEN_ADV:
Expand All @@ -46,7 +46,7 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
}
switch(ID) {
case PIPE_TP:
return new GuiTeleportPipe(player, (PipeTeleport) ((TileGenericPipe) tile).pipe);
return new GuiTeleportPipe(player, (PipeTeleport<?>) ((TileGenericPipe) tile).pipe);
case PIPE_DIST:
return new GuiDistributionPipe((TileGenericPipe) tile);
case PIPE_WOODEN_ADV:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ContainerTeleportPipe extends BuildCraftContainer {
public int connectedPipes = 0;

private int ticks = 0;
private PipeTeleport pipe;
private PipeTeleport<?> pipe;
private int freq;
private byte state;
private boolean isPublic;

public ContainerTeleportPipe(EntityPlayer player, PipeTeleport pipe) {
public ContainerTeleportPipe(EntityPlayer player, PipeTeleport<?> pipe) {
super(0);
this.pipe = pipe;

Expand All @@ -34,10 +34,10 @@ public ContainerTeleportPipe(EntityPlayer player, PipeTeleport pipe) {

if(FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
{
List<PipeTeleport> connectedPipes = TeleportManager.instance.getConnectedPipes(pipe, false);
List<PipeTeleport<?>> connectedPipes = TeleportManager.instance.getConnectedPipes(pipe, false);
int[] locations = new int[connectedPipes.size() * 3];
for(int i = 0; i < connectedPipes.size() && i < 9; i++) {
PipeTeleport connectedPipe = connectedPipes.get(i);
PipeTeleport<?> connectedPipe = connectedPipes.get(i);
locations[3 * i] = connectedPipe.container.xCoord;
locations[3 * i + 1] = connectedPipe.container.yCoord;
locations[3 * i + 2] = connectedPipe.container.zCoord;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/buildcraft/additionalpipes/gui/GuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int
}
switch(ID) {
case PIPE_TP:
return new ContainerTeleportPipe(player, (PipeTeleport) ((TileGenericPipe) tile).pipe);
return new ContainerTeleportPipe(player, (PipeTeleport<?>) ((TileGenericPipe) tile).pipe);
case PIPE_DIST:
return new ContainerDistributionPipe((TileGenericPipe) tile);
case PIPE_WOODEN_ADV:
Expand All @@ -43,7 +43,7 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int
}
switch(ID) {
case PIPE_TP:
return new GuiTeleportPipe(player, (PipeTeleport) ((TileGenericPipe) tile).pipe);
return new GuiTeleportPipe(player, (PipeTeleport<?>) ((TileGenericPipe) tile).pipe);
case PIPE_DIST:
return new GuiDistributionPipe((TileGenericPipe) tile);
case PIPE_WOODEN_ADV:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public String getTooltip() {
}
}

private final PipeTeleport pipe;
private final PipeTeleport<?> pipe;
private final ContainerTeleportPipe container;
private final GuiButton[] buttons = new GuiButton[8];

public GuiTeleportPipe(EntityPlayer player, PipeTeleport pipe) {
public GuiTeleportPipe(EntityPlayer player, PipeTeleport<?> pipe) {
super(new ContainerTeleportPipe(player, pipe), null, Textures.GUI_TELEPORT);
this.pipe = pipe;
container = (ContainerTeleportPipe) inventorySlots;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public IMessage onMessage(MessageTelePipe message, MessageContext ctx)
{
TileEntity te = ctx.getServerHandler().playerEntity.worldObj.getTileEntity(message.x, message.y, message.z);
if(te instanceof TileGenericPipe) {
PipeTeleport pipe = (PipeTeleport) ((TileGenericPipe) te).pipe;
PipeTeleport<?> pipe = (PipeTeleport<?>) ((TileGenericPipe) te).pipe;
// only allow the owner to change pipe state
EntityPlayerMP entityPlayer = (EntityPlayerMP) ctx.getServerHandler().playerEntity;
if(!PipeTeleport.canPlayerModifyPipe(entityPlayer, pipe)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public IMessage onMessage(MessageTelePipeData message, MessageContext ctx)
{
TileEntity te = FMLClientHandler.instance().getClient().theWorld.getTileEntity(message.x, message.y, message.z);

PipeTeleport pipe = (PipeTeleport) ((TileGenericPipe) te).pipe;
PipeTeleport<?> pipe = (PipeTeleport<?>) ((TileGenericPipe) te).pipe;
pipe.owner = message.owner;
pipe.network = message.locations;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/buildcraft/additionalpipes/pipes/APPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public abstract class APPipe extends Pipe<PipeTransport> {
public abstract class APPipe<pipeType extends PipeTransport> extends Pipe<pipeType> {

public APPipe(PipeTransport transport, Item item) {
public APPipe(pipeType transport, Item item) {
super(transport, item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import buildcraft.transport.TravelingItem;
import buildcraft.transport.pipes.events.PipeEventItem;

public class PipeItemsAdvancedInsertion extends APPipe {
public class PipeItemsAdvancedInsertion extends APPipe<PipeTransportItems> {
private static final int ICON = 8;

public PipeItemsAdvancedInsertion(Item item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import buildcraft.transport.TravelingItem;
import cofh.api.energy.IEnergyHandler;

public class PipeItemsAdvancedWood extends APPipe implements IEnergyHandler
public class PipeItemsAdvancedWood extends APPipe<PipeTransportItems> implements IEnergyHandler
{

protected RFBattery battery = new RFBattery(640, 640, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import buildcraft.transport.PipeTransportItems;
import buildcraft.transport.pipes.events.PipeEventItem;

public class PipeItemsClosed extends APPipe implements IInventory {
public class PipeItemsClosed extends APPipe<PipeTransportItems> implements IInventory {

private ItemStack[] inventory = new ItemStack[10];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import buildcraft.transport.PipeTransportItems;
import buildcraft.transport.pipes.events.PipeEventItem;

public class PipeItemsDistributor extends APPipe {
public class PipeItemsDistributor extends APPipe<PipeTransportItems> {

public int distData[] = { 1, 1, 1, 1, 1, 1 };
public int distSide = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import buildcraft.transport.pipes.events.PipeEventItem;
import buildcraft.transport.utils.TransportUtils;

public class PipeItemsTeleport extends PipeTeleport {
public class PipeItemsTeleport extends PipeTeleport<PipeTransportItems> {
private static final int ICON = 0;

public PipeItemsTeleport(Item items) {
Expand All @@ -33,15 +33,15 @@ public void eventHandler(PipeEventItem.Entered event)
return;
}*/

List<PipeTeleport> connectedTeleportPipes = TeleportManager.instance.getConnectedPipes(this, false);
List<PipeTeleport<?>> connectedTeleportPipes = TeleportManager.instance.getConnectedPipes(this, false);
// no teleport pipes connected, use default
if(connectedTeleportPipes.size() <= 0 || (state & 0x1) == 0) {
return;
}

// output to random pipe
LinkedList<ForgeDirection> outputOrientations = new LinkedList<ForgeDirection>();
PipeTeleport otherPipe = connectedTeleportPipes.get(rand.nextInt(connectedTeleportPipes.size()));
PipeTeleport<?> otherPipe = connectedTeleportPipes.get(rand.nextInt(connectedTeleportPipes.size()));

// find possible output orientations
for(ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
import buildcraft.transport.IPipeTransportFluidsHook;
import buildcraft.transport.PipeTransportFluids;

public class PipeLiquidsTeleport extends PipeTeleport implements IPipeTransportFluidsHook {
public class PipeLiquidsTeleport extends PipeTeleport<PipeTransportFluids> implements IPipeTransportFluidsHook {
private static final int ICON = 2;

public PipeLiquidsTeleport(Item item) {
super(new PipeTransportFluids(), item);
((PipeTransportFluids) transport).flowRate = 220;
((PipeTransportFluids) transport).travelDelay = 3;
transport.flowRate = 220;
transport.travelDelay = 3;
}

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
List<PipeTeleport> pipeList = TeleportManager.instance.getConnectedPipes(this, false);
List<PipeTeleport<?>> pipeList = TeleportManager.instance.getConnectedPipes(this, false);

if(pipeList.size() == 0 || (state & 0x1) == 0) {
return 0;
Expand All @@ -52,7 +52,7 @@ public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
return used;
}

private static List<IFluidHandler> getPossibleLiquidMovements(PipeTeleport pipe) {
private static List<IFluidHandler> getPossibleLiquidMovements(PipeTeleport<?> pipe) {
List<IFluidHandler> result = new LinkedList<IFluidHandler>();

for(ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import buildcraft.additionalpipes.AdditionalPipes;
import buildcraft.transport.PipeTransportFluids;

public class PipeLiquidsWaterPump extends APPipe {
public class PipeLiquidsWaterPump extends APPipe<PipeTransportFluids> {
private static final int ICON = 24;
private static final Block water = Blocks.water;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import buildcraft.transport.TileGenericPipe;
import buildcraft.transport.pipes.PipePowerDiamond;

public class PipePowerTeleport extends PipeTeleport implements IPipeTransportPowerHook {
public class PipePowerTeleport extends PipeTeleport<PipeTransportPower> implements IPipeTransportPowerHook {
private static final int ICON = 3;

private static class PowerRequest {
Expand All @@ -48,13 +48,13 @@ public int requestEnergy(ForgeDirection from, int value ) {
return requested;
}

List<PipeTeleport> pipeList = TeleportManager.instance.getConnectedPipes(this, true);
List<PipeTeleport<?>> pipeList = TeleportManager.instance.getConnectedPipes(this, true);

if(pipeList.size() <= 0) {
return requested;
}

for(PipeTeleport pipe : pipeList) {
for(PipeTeleport<?> pipe : pipeList) {
LinkedList<ForgeDirection> possibleMovements = getRealPossibleMovements(pipe);
for(ForgeDirection orientation : possibleMovements) {
TileEntity tile = pipe.container.getTile(orientation);
Expand All @@ -72,15 +72,15 @@ public int requestEnergy(ForgeDirection from, int value ) {

@Override
public int receiveEnergy(ForgeDirection from, int energy) {
List<PipeTeleport> connectedPipes = TeleportManager.instance.getConnectedPipes(this, false);
List<PipeTeleport> sendingToList = new LinkedList<PipeTeleport>();
List<PipeTeleport<?>> connectedPipes = TeleportManager.instance.getConnectedPipes(this, false);
List<PipeTeleport<?>> sendingToList = new LinkedList<PipeTeleport<?>>();

// no connected pipes, leave!
if(connectedPipes.size() <= 0 || (state & 0x1) == 0) {
return 0;
}

for(PipeTeleport pipe : connectedPipes) {
for(PipeTeleport<?> pipe : connectedPipes) {
if(getPipesNeedsPower(pipe).size() > 0) {
sendingToList.add(pipe);
}
Expand All @@ -94,7 +94,7 @@ public int receiveEnergy(ForgeDirection from, int energy) {
// TODO proportional power relay
double powerToSend = AdditionalPipes.instance.powerLossCfg * energy / sendingToList.size();

for(PipeTeleport receiver : sendingToList) {
for(PipeTeleport<?> receiver : sendingToList) {
List<PowerRequest> needsPower = getPipesNeedsPower(receiver);

if(needsPower.size() <= 0) {
Expand All @@ -111,7 +111,7 @@ public int receiveEnergy(ForgeDirection from, int energy) {
return energy;
}

private List<PowerRequest> getPipesNeedsPower(PipeTeleport pipe) {
private List<PowerRequest> getPipesNeedsPower(PipeTeleport<?> pipe) {
LinkedList<ForgeDirection> possibleMovements = getRealPossibleMovements(pipe);
List<PowerRequest> needsPower = new LinkedList<PowerRequest>();

Expand Down Expand Up @@ -143,7 +143,7 @@ private static boolean pipeNeedsPower(TileGenericPipe tile) {
}

// returns all adjacent pipes
private static LinkedList<ForgeDirection> getRealPossibleMovements(PipeTeleport pipe) {
private static LinkedList<ForgeDirection> getRealPossibleMovements(PipeTeleport<?> pipe) {
LinkedList<ForgeDirection> result = new LinkedList<ForgeDirection>();

for(ForgeDirection orientation : ForgeDirection.VALID_DIRECTIONS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import buildcraft.transport.PipeTransport;
import buildcraft.transport.TileGenericPipe;

public class PipeSwitch extends APPipe {
public class PipeSwitch<pipeType extends PipeTransport> extends APPipe<pipeType> {

private final int textureIndex;

public PipeSwitch(PipeTransport transport, Item item, int textureIndex) {
public PipeSwitch(pipeType transport, Item item, int textureIndex) {
super(transport, item);
this.textureIndex = textureIndex;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package buildcraft.additionalpipes.pipes;

import net.minecraft.item.Item;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.transport.IPipeTile;
import buildcraft.transport.IPipeTransportFluidsHook;
import buildcraft.transport.PipeTransportFluids;


public class PipeSwitchFluids extends PipeSwitch {
public class PipeSwitchFluids extends PipeSwitch<PipeTransportFluids> implements IPipeTransportFluidsHook
{

public PipeSwitchFluids(Item item) {
super(new PipeTransportFluids(), item, 22);

transport.flowRate = 220;
transport.travelDelay = 3;
}

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
{
if (!(container.getTile(from) instanceof IPipeTile)) {
return 0;
} else {
return transport.internalTanks[from.ordinal()].fill(resource, doFill);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.item.Item;
import buildcraft.transport.PipeTransportItems;

public class PipeSwitchItems extends PipeSwitch {
public class PipeSwitchItems extends PipeSwitch<PipeTransportItems> {

public PipeSwitchItems(Item item) {
super(new PipeTransportItems(), item, 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import buildcraft.transport.PipeTransportPower;
import buildcraft.transport.pipes.PipePowerDiamond;

public class PipeSwitchPower extends PipeSwitch {
public class PipeSwitchPower extends PipeSwitch<PipeTransportPower> {

public PipeSwitchPower(Item item) {
super(new PipeTransportPower(), item, 16);
Expand Down
Loading

0 comments on commit b373fdb

Please sign in to comment.