mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-27 15:33:06 +00:00
Client state syncing framework in place.
This commit is contained in:
1 parent
91370e20e7
commit
22f826b1a2
16 files changed
+1115
-157
No files matched your search
@@ -12,6 +12,7 @@ import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.AxisAlignedBB;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
@@ -28,10 +29,18 @@ import crazypants.enderio.ModObject;
|
||||
import crazypants.enderio.gui.IAdvancedTooltipProvider;
|
||||
import crazypants.enderio.gui.TooltipAddera;
|
||||
import crazypants.enderio.machine.IoMode;
|
||||
import crazypants.enderio.machine.capbank.network.CapBankClientNetwork;
|
||||
import crazypants.enderio.machine.capbank.network.ClientNetworkManager;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketClientStateRequest;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketClientStateResponse;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketNetworkIdRequest;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketNetworkIdResponse;
|
||||
import crazypants.enderio.machine.power.PowerDisplayUtil;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.power.PowerHandlerUtil;
|
||||
import crazypants.enderio.tool.ToolUtil;
|
||||
import crazypants.enderio.waila.IWailaInfoProvider;
|
||||
import crazypants.vecmath.Vector3d;
|
||||
|
||||
public class BlockCapBank extends BlockEio implements IGuiHandler, IAdvancedTooltipProvider, IWailaInfoProvider {
|
||||
|
||||
@@ -39,6 +48,11 @@ public class BlockCapBank extends BlockEio implements IGuiHandler, IAdvancedTool
|
||||
|
||||
public static BlockCapBank create() {
|
||||
|
||||
PacketHandler.INSTANCE.registerMessage(PacketClientStateResponse.class, PacketClientStateResponse.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketClientStateRequest.class, PacketClientStateRequest.class, PacketHandler.nextID(), Side.SERVER);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketNetworkIdRequest.class, PacketNetworkIdRequest.class, PacketHandler.nextID(), Side.SERVER);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketNetworkIdResponse.class, PacketNetworkIdResponse.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
|
||||
BlockCapBank res = new BlockCapBank();
|
||||
res.init();
|
||||
return res;
|
||||
@@ -152,19 +166,19 @@ public class BlockCapBank extends BlockEio implements IGuiHandler, IAdvancedTool
|
||||
|
||||
@Override
|
||||
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
// TileEntity te = world.getTileEntity(x, y, z);
|
||||
// if(te instanceof TileCapBank) {
|
||||
// return new ContainerCapBank(player, player.inventory, ((TileCapBank) te).getController());
|
||||
// }
|
||||
TileEntity te = world.getTileEntity(x, y, z);
|
||||
if(te instanceof TileCapBank) {
|
||||
return new ContainerCapBank(player, player.inventory, (TileCapBank) te);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
// TileEntity te = world.getTileEntity(x, y, z);
|
||||
// if(te instanceof TileCapBank) {
|
||||
// return new GuiCapBank(player, player.inventory, ((TileCapBank) te).getController());
|
||||
// }
|
||||
TileEntity te = world.getTileEntity(x, y, z);
|
||||
if(te instanceof TileCapBank) {
|
||||
return new GuiCapBank(player, player.inventory, (TileCapBank) te);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -338,24 +352,13 @@ public class BlockCapBank extends BlockEio implements IGuiHandler, IAdvancedTool
|
||||
return;
|
||||
}
|
||||
TileEntity te = world.getTileEntity(x, y, z);
|
||||
// if(te instanceof TileCapacitorBank) {
|
||||
// TileCapacitorBank cb = (TileCapacitorBank) te;
|
||||
// cb.addEnergy(PowerHandlerUtil.getStoredEnergyForItem(stack));
|
||||
// if(player instanceof EntityPlayer) {
|
||||
// for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
// BlockCoord bc = new BlockCoord(x, y, z);
|
||||
// bc = bc.getLocation(dir);
|
||||
// te = world.getTileEntity(bc.x, bc.y, bc.z);
|
||||
// if(te instanceof TileCapacitorBank) {
|
||||
// if(((TileCapacitorBank)te).isMaxSize()) {
|
||||
// ((EntityPlayer)player).addChatComponentMessage(new ChatComponentText("Capacitor bank is at maximum size"));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
if(!(te instanceof TileCapBank)) {
|
||||
return;
|
||||
}
|
||||
|
||||
TileCapBank cb = (TileCapBank) te;
|
||||
cb.addEnergy(PowerHandlerUtil.getStoredEnergyForItem(stack));
|
||||
|
||||
world.markBlockForUpdate(x, y, z);
|
||||
}
|
||||
|
||||
@@ -378,30 +381,32 @@ public class BlockCapBank extends BlockEio implements IGuiHandler, IAdvancedTool
|
||||
world.removeTileEntity(x, y, z);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// @SideOnly(Side.CLIENT)
|
||||
// public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, int z) {
|
||||
// TileEntity te = world.getTileEntity(x, y, z);
|
||||
// if(!(te instanceof TileCapacitorBank)) {
|
||||
// return super.getSelectedBoundingBoxFromPool(world, x, y, z);
|
||||
// }
|
||||
// TileCapacitorBank tr = (TileCapacitorBank) te;
|
||||
// if(!tr.isMultiblock()) {
|
||||
// return super.getSelectedBoundingBoxFromPool(world, x, y, z);
|
||||
// }
|
||||
//
|
||||
// Vector3d min = new Vector3d(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
|
||||
// Vector3d max = new Vector3d(-Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);
|
||||
// for (BlockCoord bc : tr.multiblock) {
|
||||
// min.x = Math.min(min.x, bc.x);
|
||||
// max.x = Math.max(max.x, bc.x + 1);
|
||||
// min.y = Math.min(min.y, bc.y);
|
||||
// max.y = Math.max(max.y, bc.y + 1);
|
||||
// min.z = Math.min(min.z, bc.z);
|
||||
// max.z = Math.max(max.z, bc.z + 1);
|
||||
// }
|
||||
// return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
// }
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, int z) {
|
||||
TileEntity te = world.getTileEntity(x, y, z);
|
||||
if(!(te instanceof TileCapBank)) {
|
||||
return super.getSelectedBoundingBoxFromPool(world, x, y, z);
|
||||
}
|
||||
TileCapBank tr = (TileCapBank) te;
|
||||
CapBankClientNetwork network = ClientNetworkManager.getInstance().getNetwork(tr);
|
||||
|
||||
if(!tr.getType().isMultiblock() || network == null) {
|
||||
return super.getSelectedBoundingBoxFromPool(world, x, y, z);
|
||||
}
|
||||
|
||||
Vector3d min = new Vector3d(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
|
||||
Vector3d max = new Vector3d(-Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);
|
||||
for (TileCapBank bc : network.getMembers()) {
|
||||
min.x = Math.min(min.x, bc.xCoord);
|
||||
max.x = Math.max(max.x, bc.xCoord + 1);
|
||||
min.y = Math.min(min.y, bc.yCoord);
|
||||
max.y = Math.max(max.y, bc.yCoord + 1);
|
||||
min.z = Math.min(min.z, bc.zCoord);
|
||||
max.z = Math.max(max.z, bc.zCoord + 1);
|
||||
}
|
||||
return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasComparatorInputOverride() {
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package crazypants.enderio.machine.capbank;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class CapBankNetwork {
|
||||
|
||||
protected final List<TileCapBank> capBanks = new ArrayList<TileCapBank>();
|
||||
|
||||
private int ioCap;
|
||||
|
||||
private long timeAtLastApply;
|
||||
|
||||
public void init(TileCapBank cap, Collection<TileCapBank> neighbours, World world) {
|
||||
if(world.isRemote) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
for (TileCapBank con : neighbours) {
|
||||
CapBankNetwork network = con.getNetwork();
|
||||
if(network != null) {
|
||||
network.destroyNetwork();
|
||||
}
|
||||
}
|
||||
setNetwork(world, cap);
|
||||
notifyNetworkOfUpdate();
|
||||
}
|
||||
|
||||
protected void setNetwork(World world, TileCapBank cap) {
|
||||
if(cap != null && cap.setNetwork(this)) {
|
||||
addCapBank(cap);
|
||||
Collection<TileCapBank> neighbours = NetworkManager.getNeigbours(cap);
|
||||
for (TileCapBank neighbour : neighbours) {
|
||||
if(neighbour.getNetwork() == null) {
|
||||
setNetwork(world, neighbour);
|
||||
} else if(neighbour.getNetwork() != this) {
|
||||
neighbour.getNetwork().destroyNetwork();
|
||||
setNetwork(world, neighbour);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void destroyNetwork() {
|
||||
for (TileCapBank cb : capBanks) {
|
||||
cb.setNetwork(null);
|
||||
}
|
||||
capBanks.clear();
|
||||
}
|
||||
|
||||
public void addCapBank(TileCapBank cap) {
|
||||
if(!capBanks.contains(cap)) {
|
||||
capBanks.add(cap);
|
||||
ioCap += cap.getType().getMaxIO();
|
||||
}
|
||||
}
|
||||
|
||||
public int getMaxEnergyRecieved() {
|
||||
return ioCap;
|
||||
}
|
||||
|
||||
public void onUpdateEntity(TileCapBank tileCapBank) {
|
||||
World world = tileCapBank.getWorldObj();
|
||||
if(world == null) {
|
||||
return;
|
||||
}
|
||||
if(world.isRemote) {
|
||||
return;
|
||||
}
|
||||
long curTime = world.getTotalWorldTime();
|
||||
if(curTime != timeAtLastApply) {
|
||||
timeAtLastApply = curTime;
|
||||
powerManager.applyRecievedPower();
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyNetworkOfUpdate() {
|
||||
for (TileCapBank cb : capBanks) {
|
||||
cb.getWorldObj().markBlockForUpdate(cb.xCoord, cb.yCoord, cb.zCoord);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
package crazypants.enderio.machine.capbank;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
|
||||
public class ContainerCapBank {
|
||||
public class ContainerCapBank extends Container {
|
||||
|
||||
public ContainerCapBank(EntityPlayer player, InventoryPlayer inventory, CapBankNetwork controller) {
|
||||
// TODO Auto-generated constructor stub
|
||||
public ContainerCapBank(Entity player, InventoryPlayer inventory, TileCapBank cb) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer p_75145_1_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,396 @@
|
||||
package crazypants.enderio.machine.capbank;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiTextField;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
|
||||
public class GuiCapBank {
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.gui.IconButtonEIO;
|
||||
import crazypants.enderio.gui.IconEIO;
|
||||
import crazypants.enderio.gui.RedstoneModeButton;
|
||||
import crazypants.enderio.machine.GuiMachineBase;
|
||||
import crazypants.enderio.machine.GuiOverlayIoConfig;
|
||||
import crazypants.enderio.machine.IRedstoneModeControlable;
|
||||
import crazypants.enderio.machine.IoMode;
|
||||
import crazypants.enderio.machine.RedstoneControlMode;
|
||||
import crazypants.enderio.machine.capbank.network.CapBankClientNetwork;
|
||||
import crazypants.enderio.machine.capbank.network.ClientNetworkManager;
|
||||
import crazypants.enderio.machine.capbank.network.NetworkClientState;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketClientStateRequest;
|
||||
import crazypants.enderio.machine.power.PowerDisplayUtil;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.gui.GuiContainerBase;
|
||||
import crazypants.gui.GuiToolTip;
|
||||
import crazypants.render.RenderUtil;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.Lang;
|
||||
import crazypants.vecmath.VecmathUtil;
|
||||
|
||||
public class GuiCapBank extends GuiContainerBase {
|
||||
|
||||
private static final NetworkClientState EMPTY_STATE = new NetworkClientState();
|
||||
|
||||
protected static final int INPUT_BUTTON_ID = 18;
|
||||
protected static final int OUTPUT_BUTTON_ID = 37;
|
||||
|
||||
protected static final int CONFIG_ID = 377996104;
|
||||
|
||||
private static final int POWER_X = 11 + 18;
|
||||
private static final int POWER_Y = 9;
|
||||
private static final int POWER_WIDTH = 10;
|
||||
private static final int POWER_HEIGHT = 68;
|
||||
protected static final int BOTTOM_POWER_Y = POWER_Y + POWER_HEIGHT;
|
||||
|
||||
private int inputX = 78 + 24;
|
||||
private int inputY = 18;
|
||||
|
||||
private int outputX = 78 + 24;
|
||||
private int outputY = 36;
|
||||
|
||||
private int rightMargin = 8 + 24;
|
||||
|
||||
private final TileCapBank capBank;
|
||||
|
||||
private RedstoneModeButton inputRsButton;
|
||||
private RedstoneModeButton outputRsButton;
|
||||
|
||||
private GuiTextField maxInputTF;
|
||||
private GuiTextField maxOutputTF;
|
||||
|
||||
private GuiOverlayIoConfig configOverlay;
|
||||
private IconButtonEIO configB;
|
||||
|
||||
private NetworkClientState state = EMPTY_STATE;
|
||||
|
||||
public GuiCapBank(Entity player, InventoryPlayer playerInv, TileCapBank te) {
|
||||
super(new ContainerCapBank(player, playerInv, te));
|
||||
capBank = te;
|
||||
|
||||
updateState();
|
||||
|
||||
xSize = 176 + 42;
|
||||
|
||||
addToolTip(new GuiToolTip(new Rectangle(5, POWER_Y, POWER_WIDTH, POWER_HEIGHT), "") {
|
||||
|
||||
@Override
|
||||
protected void updateText() {
|
||||
text.clear();
|
||||
text.add(PowerDisplayUtil.formatPower(state.getEnergyStored()) + " " + PowerDisplayUtil.ofStr());
|
||||
text.add(PowerDisplayUtil.formatPower(state.getMaxEnergyStored()) + " " + PowerDisplayUtil.abrevation());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
int x = xSize - rightMargin - GuiMachineBase.BUTTON_SIZE - 21;
|
||||
int y = inputY;
|
||||
inputRsButton = new RedstoneModeButton(this, -1, x, y, new IRedstoneModeControlable() {
|
||||
|
||||
@Override
|
||||
public void setRedstoneControlMode(RedstoneControlMode mode) {
|
||||
// capBank.setInputControlMode(mode);
|
||||
// PacketHandler.INSTANCE.sendToServer(new PacketClientState(capBank));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedstoneControlMode getRedstoneControlMode() {
|
||||
return RedstoneControlMode.IGNORE;//capBank.getInputControlMode();
|
||||
}
|
||||
});
|
||||
inputRsButton.setTooltipKey("enderio.gui.capBank.inputRs");
|
||||
|
||||
y += 18;
|
||||
outputRsButton = new RedstoneModeButton(this, -1, x, y, new IRedstoneModeControlable() {
|
||||
|
||||
@Override
|
||||
public void setRedstoneControlMode(RedstoneControlMode mode) {
|
||||
// capBank.setOutputControlMode(mode);
|
||||
// PacketHandler.INSTANCE.sendToServer(new PacketClientState(capBank));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedstoneControlMode getRedstoneControlMode() {
|
||||
//return capBank.getOutputControlMode();
|
||||
return RedstoneControlMode.IGNORE;
|
||||
}
|
||||
});
|
||||
outputRsButton.setTooltipKey("enderio.gui.capBank.outputRs");
|
||||
|
||||
y += 20;
|
||||
configB = new IconButtonEIO(this, CONFIG_ID, x, y, IconEIO.IO_CONFIG_UP);
|
||||
configB.setToolTip(Lang.localize("gui.machine.ioMode.overlay.tooltip"));
|
||||
|
||||
List<BlockCoord> coords = new ArrayList<BlockCoord>();
|
||||
CapBankClientNetwork network = ClientNetworkManager.getInstance().getNetwork(capBank);
|
||||
if(network != null) {
|
||||
for (TileCapBank cb : network.getMembers()) {
|
||||
coords.add(cb.getLocation());
|
||||
}
|
||||
}
|
||||
if(coords.isEmpty()) {
|
||||
coords.add(te.getLocation());
|
||||
}
|
||||
|
||||
configOverlay = new GuiOverlayIoConfig(coords) {
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean visible) {
|
||||
super.setVisible(visible);
|
||||
configB.setIcon(visible ? IconEIO.IO_CONFIG_DOWN : IconEIO.IO_CONFIG_UP);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLabelForMode(IoMode mode) {
|
||||
if(mode == IoMode.PUSH) {
|
||||
return Lang.localize("gui.capBank.outputMode");
|
||||
} else if(mode == IoMode.PULL) {
|
||||
return Lang.localize("gui.capBank.inputMode");
|
||||
}
|
||||
return super.getLabelForMode(mode);
|
||||
}
|
||||
|
||||
};
|
||||
addOverlay(configOverlay);
|
||||
|
||||
public GuiCapBank(EntityPlayer player, InventoryPlayer inventory, CapBankNetwork controller) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void actionPerformed(GuiButton b) {
|
||||
super.actionPerformed(b);
|
||||
if(b.id == CONFIG_ID) {
|
||||
boolean vis = !configOverlay.isVisible();
|
||||
configOverlay.setVisible(vis);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initGui() {
|
||||
super.initGui();
|
||||
|
||||
configB.onGuiInit();
|
||||
|
||||
int x = guiLeft + xSize - rightMargin - GuiMachineBase.BUTTON_SIZE;
|
||||
int y = guiTop + inputY;
|
||||
|
||||
FontRenderer fontRenderer = getFontRenderer();
|
||||
|
||||
inputRsButton.onGuiInit();
|
||||
outputRsButton.onGuiInit();
|
||||
|
||||
x = guiLeft + inputX;
|
||||
y = guiTop + inputY;
|
||||
maxInputTF = new GuiTextField(fontRenderer, x, y, 68, 16);
|
||||
maxInputTF.setCanLoseFocus(true);
|
||||
maxInputTF.setMaxStringLength(10);
|
||||
maxInputTF.setFocused(false);
|
||||
maxInputTF.setText(PowerDisplayUtil.formatPower(getMaxInput()));
|
||||
|
||||
x = guiLeft + outputX;
|
||||
y = guiTop + outputY;
|
||||
maxOutputTF = new GuiTextField(fontRenderer, x, y, 68, 16);
|
||||
maxOutputTF.setCanLoseFocus(true);
|
||||
maxOutputTF.setMaxStringLength(10);
|
||||
maxOutputTF.setFocused(true);
|
||||
maxOutputTF.setText(PowerDisplayUtil.formatPower(getMaxOutput()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void keyTyped(char par1, int par2) {
|
||||
super.keyTyped(par1, par2);
|
||||
if(par1 == 'e') {
|
||||
super.keyTyped(par1, 1);
|
||||
}
|
||||
maxInputTF.textboxKeyTyped(par1, par2);
|
||||
maxOutputTF.textboxKeyTyped(par1, par2);
|
||||
updateInputOutput();
|
||||
}
|
||||
|
||||
private void updateInputOutput() {
|
||||
int input = parsePower(maxInputTF);
|
||||
if(input >= 0 && getMaxInput() != input) {
|
||||
setMaxInput(input);
|
||||
}
|
||||
int output = parsePower(maxOutputTF);
|
||||
if(output >= 0 && getMaxOutput() != output) {
|
||||
setMaxOutput(output);
|
||||
}
|
||||
}
|
||||
|
||||
private void setMaxOutput(int output) {
|
||||
if(output != getMaxOutput()) {
|
||||
// capBank.setMaxOutput(output);
|
||||
// maxOutputTF.setText(PowerDisplayUtil.formatPower(capBank.getMaxOutput()));
|
||||
// PacketHandler.INSTANCE.sendToServer(new PacketClientState(capBank));
|
||||
}
|
||||
}
|
||||
|
||||
private void setMaxInput(int input) {
|
||||
if(input != getMaxInput()) {
|
||||
// capBank.setMaxInput(input);
|
||||
// maxInputTF.setText(PowerDisplayUtil.formatPower(capBank.getMaxInput()));
|
||||
// PacketHandler.INSTANCE.sendToServer(new PacketClientState(capBank));
|
||||
}
|
||||
}
|
||||
|
||||
private int parsePower(GuiTextField tf) {
|
||||
String txt = tf.getText();
|
||||
try {
|
||||
Integer power = PowerDisplayUtil.parsePower(txt);
|
||||
if(power == null) {
|
||||
return -1;
|
||||
}
|
||||
return power.intValue();
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseClicked(int par1, int par2, int par3) {
|
||||
super.mouseClicked(par1, par2, par3);
|
||||
maxInputTF.mouseClicked(par1, par2, par3);
|
||||
maxOutputTF.mouseClicked(par1, par2, par3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateScreen() {
|
||||
maxInputTF.updateCursorCounter();
|
||||
maxOutputTF.updateCursorCounter();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {
|
||||
|
||||
requestStateUpdate();
|
||||
|
||||
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
RenderUtil.bindTexture("enderio:textures/gui/capacitorBank.png");
|
||||
int sx = (width - xSize) / 2;
|
||||
int sy = (height - ySize) / 2;
|
||||
|
||||
drawTexturedModalRect(sx, sy, 0, 0, xSize - 21, ySize);
|
||||
|
||||
int i1 = getEnergyStoredScaled(POWER_HEIGHT);
|
||||
drawTexturedModalRect(sx + POWER_X, sy + BOTTOM_POWER_Y - i1, 176 + 21, 0, POWER_WIDTH, i1);
|
||||
|
||||
for (int i = 0; i < buttonList.size(); ++i) {
|
||||
GuiButton guibutton = (GuiButton) buttonList.get(i);
|
||||
guibutton.drawButton(mc, 0, 0);
|
||||
}
|
||||
|
||||
maxInputTF.drawTextBox();
|
||||
maxOutputTF.drawTextBox();
|
||||
|
||||
int midX = sx + xSize / 2;
|
||||
|
||||
String str = Lang.localize("gui.capBank.maxIo") + " " + PowerDisplayUtil.formatPower(getMaxIO()) +
|
||||
" " + PowerDisplayUtil.abrevation() + PowerDisplayUtil.perTickStr();
|
||||
FontRenderer fontRenderer = getFontRenderer();
|
||||
int swid = fontRenderer.getStringWidth(str);
|
||||
int x = midX - swid / 2;
|
||||
int y = guiTop + 5;
|
||||
|
||||
drawString(fontRenderer, str, x, y, -1);
|
||||
|
||||
str = Lang.localize("gui.capBank.maxInput") + ":";
|
||||
swid = fontRenderer.getStringWidth(str);
|
||||
x = guiLeft + inputX - swid - 3;
|
||||
y = guiTop + inputY + 2;
|
||||
drawString(fontRenderer, str, x, y, -1);
|
||||
|
||||
str = Lang.localize("gui.capBank.maxOutput") + ":";
|
||||
swid = fontRenderer.getStringWidth(str);
|
||||
x = guiLeft + outputX - swid - 3;
|
||||
y = guiTop + outputY + 2;
|
||||
drawString(fontRenderer, str, x, y, -1);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawHoveringText(List par1List, int par2, int par3, FontRenderer font) {
|
||||
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
|
||||
GL11.glPushAttrib(GL11.GL_LIGHTING_BIT);
|
||||
super.drawHoveringText(par1List, par2 + 24, par3, font);
|
||||
GL11.glPopAttrib();
|
||||
GL11.glPopAttrib();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGuiLeft() {
|
||||
return guiLeft + 24;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGuiTop() {
|
||||
return guiTop;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getXSize() {
|
||||
return xSize - 42;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOverlayOffsetX() {
|
||||
return 21;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FontRenderer getFontRenderer() {
|
||||
return Minecraft.getMinecraft().fontRenderer;
|
||||
}
|
||||
|
||||
private int getMaxOutput() {
|
||||
return state.getMaxOutput();
|
||||
}
|
||||
|
||||
private int getMaxInput() {
|
||||
return state.getMaxInput();
|
||||
}
|
||||
|
||||
private int getMaxIO() {
|
||||
return state.getMaxIO();
|
||||
}
|
||||
|
||||
private int getEnergyStoredScaled(int scale) {
|
||||
return (int) VecmathUtil.clamp(Math.round(scale * getEnergyStoredRatio()), 0, scale);
|
||||
}
|
||||
|
||||
private double getEnergyStoredRatio() {
|
||||
return (double) state.getEnergyStored() / state.getMaxEnergyStored();
|
||||
}
|
||||
|
||||
private void requestStateUpdate() {
|
||||
if(EnderIO.proxy.getTickCount() % 2 == 0) {
|
||||
PacketHandler.INSTANCE.sendToServer(new PacketClientStateRequest(capBank));
|
||||
}
|
||||
updateState();
|
||||
}
|
||||
|
||||
private void updateState() {
|
||||
|
||||
NetworkClientState prevState = state;
|
||||
//
|
||||
|
||||
//System.out.println("GuiCapBank.updateState: " + capBank.getNetworkId());
|
||||
state = ClientNetworkManager.getInstance().getClientState(capBank.getNetworkId());
|
||||
if(state == null) {
|
||||
state = EMPTY_STATE;
|
||||
}
|
||||
|
||||
if(prevState == EMPTY_STATE && maxInputTF != null) {
|
||||
maxInputTF.setText(PowerDisplayUtil.formatPower(getMaxInput()));
|
||||
maxOutputTF.setText(PowerDisplayUtil.formatPower(getMaxInput()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,10 +12,17 @@ import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import crazypants.enderio.TileEntityEio;
|
||||
import crazypants.enderio.conduit.IConduitBundle;
|
||||
import crazypants.enderio.machine.IIoConfigurable;
|
||||
import crazypants.enderio.machine.IoMode;
|
||||
import crazypants.enderio.machine.capbank.network.CapBankNetwork;
|
||||
import crazypants.enderio.machine.capbank.network.ClientNetworkManager;
|
||||
import crazypants.enderio.machine.capbank.network.NetworkUtil;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketNetworkIdRequest;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.enderio.power.IInternalPowerReceptor;
|
||||
import crazypants.enderio.power.IPowerInterface;
|
||||
import crazypants.enderio.power.PowerHandlerUtil;
|
||||
@@ -31,6 +38,9 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
private CapBankNetwork network;
|
||||
|
||||
//Client side refernce to look up network state
|
||||
private int networkId = -1;
|
||||
|
||||
@Override
|
||||
public BlockCoord getLocation() {
|
||||
return new BlockCoord(this);
|
||||
@@ -45,6 +55,19 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
//---------- Multiblock
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void setNetworkId(int networkId) {
|
||||
this.networkId = networkId;
|
||||
if(networkId != -1) {
|
||||
ClientNetworkManager.getInstance().addToNetwork(networkId, this);
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public int getNetworkId() {
|
||||
return networkId;
|
||||
}
|
||||
|
||||
public CapBankNetwork getNetwork() {
|
||||
return network;
|
||||
}
|
||||
@@ -61,7 +84,14 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
@Override
|
||||
public void onChunkUnload() {
|
||||
CapBankNetwork network = getNetwork();
|
||||
if(network != null) {
|
||||
network.destroyNetwork();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
super.invalidate();
|
||||
if(network != null) {
|
||||
network.destroyNetwork();
|
||||
}
|
||||
@@ -70,6 +100,9 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
if(worldObj.isRemote) {
|
||||
if(networkId == -1) {
|
||||
PacketHandler.INSTANCE.sendToServer(new PacketNetworkIdRequest(this));
|
||||
}
|
||||
return;
|
||||
}
|
||||
updateNetwork(worldObj);
|
||||
@@ -78,7 +111,7 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
private void updateNetwork(World world) {
|
||||
if(getNetwork() == null) {
|
||||
NetworkManager.ensureValidNetwork(this);
|
||||
NetworkUtil.ensureValidNetwork(this);
|
||||
if(getNetwork() != null && !world.isRemote) {
|
||||
// world.notifyBlocksOfNeighborChange(bundle.getEntity().xCoord, bundle.getEntity().yCoord, bundle.getEntity().zCoord,
|
||||
// bundle.getEntity().getBlockType());
|
||||
@@ -146,6 +179,9 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
// }
|
||||
// render = true;
|
||||
// updateBlock();
|
||||
if(worldObj != null) {
|
||||
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -172,6 +208,14 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
//----------- Power
|
||||
|
||||
public void addEnergy(int energy) {
|
||||
if(network == null) {
|
||||
setEnergyStored(getEnergyStored() + energy);
|
||||
} else {
|
||||
network.addEnergy(energy);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnergyStored(int stored) {
|
||||
energyStored = MathHelper.clamp_int(stored, 0, getMaxEnergyStored());
|
||||
@@ -189,11 +233,11 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyRecieved(ForgeDirection dir) {
|
||||
CapBankNetwork con = getNetwork();
|
||||
if(con == null) {
|
||||
getType().getMaxEnergyStored();
|
||||
CapBankNetwork network = getNetwork();
|
||||
if(network == null) {
|
||||
return getType().getMaxIO();
|
||||
}
|
||||
return con.getMaxEnergyRecieved();
|
||||
return network.getMaxEnergyRecieved();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -203,7 +247,14 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
|
||||
return PowerHandlerUtil.recieveInternal(this, maxReceive, from, simulate);
|
||||
if(network == null) {
|
||||
return 0;
|
||||
}
|
||||
IoMode mode = getIoMode(from);
|
||||
if(mode == IoMode.DISABLED || mode == IoMode.PUSH) {
|
||||
return 0;
|
||||
}
|
||||
return network.recieveEnergy(maxReceive, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -306,6 +357,7 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
protected void writeCustomNBT(NBTTagCompound nbtRoot) {
|
||||
|
||||
nbtRoot.setString("type", getType().getUid());
|
||||
nbtRoot.setInteger("energyStored", energyStored);
|
||||
|
||||
//face modes
|
||||
if(faceModes != null) {
|
||||
@@ -320,6 +372,7 @@ public class TileCapBank extends TileEntityEio implements IInternalPowerReceptor
|
||||
protected void readCustomNBT(NBTTagCompound nbtRoot) {
|
||||
|
||||
type = CapBankType.getTypeFromUID(nbtRoot.getString("type"));
|
||||
energyStored = nbtRoot.getInteger("energyStored");
|
||||
|
||||
if(nbtRoot.hasKey("hasFaces")) {
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package crazypants.enderio.machine.capbank.network;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
|
||||
public class CapBankClientNetwork {
|
||||
|
||||
private final int id;
|
||||
private final List<TileCapBank> members = new ArrayList<TileCapBank>();
|
||||
private NetworkClientState state;
|
||||
|
||||
public CapBankClientNetwork(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setState(NetworkClientState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public void addMember(TileCapBank capBank) {
|
||||
members.add(capBank);
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
for (TileCapBank cb : members) {
|
||||
cb.setNetworkId(-1);
|
||||
}
|
||||
}
|
||||
|
||||
public NetworkClientState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public List<TileCapBank> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package crazypants.enderio.machine.capbank.network;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
import crazypants.enderio.machine.capbank.packet.PacketClientStateResponse;
|
||||
|
||||
public class CapBankNetwork {
|
||||
|
||||
protected final List<TileCapBank> capBanks = new ArrayList<TileCapBank>();
|
||||
|
||||
private final int id;
|
||||
|
||||
private int maxIO;
|
||||
|
||||
private long timeAtLastApply;
|
||||
|
||||
private long energyStored;
|
||||
|
||||
private long maxEnergyStored;
|
||||
|
||||
public CapBankNetwork(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void init(TileCapBank cap, Collection<TileCapBank> neighbours, World world) {
|
||||
if(world.isRemote) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
for (TileCapBank con : neighbours) {
|
||||
CapBankNetwork network = con.getNetwork();
|
||||
if(network != null) {
|
||||
network.destroyNetwork();
|
||||
}
|
||||
}
|
||||
setNetwork(world, cap);
|
||||
notifyNetworkOfUpdate();
|
||||
|
||||
EnderIO.packetPipeline.sendToAllAround(new PacketClientStateResponse(this), cap);
|
||||
|
||||
}
|
||||
|
||||
protected void setNetwork(World world, TileCapBank cap) {
|
||||
if(cap != null && cap.setNetwork(this)) {
|
||||
addCapBank(cap);
|
||||
Collection<TileCapBank> neighbours = NetworkUtil.getNeigbours(cap);
|
||||
for (TileCapBank neighbour : neighbours) {
|
||||
if(neighbour.getNetwork() == null) {
|
||||
setNetwork(world, neighbour);
|
||||
} else if(neighbour.getNetwork() != this) {
|
||||
neighbour.getNetwork().destroyNetwork();
|
||||
setNetwork(world, neighbour);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void destroyNetwork() {
|
||||
distributeEnergy();
|
||||
TileCapBank cap = null;
|
||||
for (TileCapBank cb : capBanks) {
|
||||
cb.setNetwork(null);
|
||||
if(cap == null) {
|
||||
cap = cb;
|
||||
}
|
||||
}
|
||||
capBanks.clear();
|
||||
if(cap != null) {
|
||||
EnderIO.packetPipeline.INSTANCE.sendToAll(new PacketClientStateResponse(this, true));
|
||||
}
|
||||
}
|
||||
|
||||
public void addCapBank(TileCapBank cap) {
|
||||
if(!capBanks.contains(cap)) {
|
||||
capBanks.add(cap);
|
||||
maxIO += cap.getType().getMaxIO();
|
||||
energyStored += cap.getEnergyStored();
|
||||
maxEnergyStored += cap.getMaxEnergyStored();
|
||||
}
|
||||
}
|
||||
|
||||
public void onUpdateEntity(TileCapBank tileCapBank) {
|
||||
World world = tileCapBank.getWorldObj();
|
||||
if(world == null) {
|
||||
return;
|
||||
}
|
||||
if(world.isRemote) {
|
||||
return;
|
||||
}
|
||||
long curTime = world.getTotalWorldTime();
|
||||
if(curTime != timeAtLastApply) {
|
||||
timeAtLastApply = curTime;
|
||||
doNetworkTick();
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyNetworkOfUpdate() {
|
||||
for (TileCapBank cb : capBanks) {
|
||||
cb.getWorldObj().markBlockForUpdate(cb.xCoord, cb.yCoord, cb.zCoord);
|
||||
}
|
||||
}
|
||||
|
||||
private void doNetworkTick() {
|
||||
|
||||
|
||||
distributeEnergy();
|
||||
}
|
||||
|
||||
public int recieveEnergy(int maxReceive, boolean simulate) {
|
||||
if(maxReceive <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
long spaceAvailable = maxEnergyStored - energyStored;
|
||||
if(spaceAvailable > Integer.MAX_VALUE) {
|
||||
spaceAvailable = Integer.MAX_VALUE;
|
||||
}
|
||||
int res = Math.min(maxReceive, (int) spaceAvailable);
|
||||
if(!simulate) {
|
||||
addEnergy(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public void addEnergy(int energy) {
|
||||
energyStored += energy;
|
||||
if(energyStored > maxEnergyStored) {
|
||||
energyStored = maxEnergyStored;
|
||||
} else if(energyStored < 0) {
|
||||
energyStored = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public NetworkClientState getClientState() {
|
||||
return new NetworkClientState(this);
|
||||
}
|
||||
|
||||
public int getMaxEnergyRecieved() {
|
||||
//TODO
|
||||
return maxIO;
|
||||
}
|
||||
|
||||
public int getMaxEnergySent() {
|
||||
//TODO
|
||||
return maxIO;
|
||||
}
|
||||
|
||||
public long getEnergyStored() {
|
||||
return energyStored;
|
||||
}
|
||||
|
||||
public long getMaxEnergyStored() {
|
||||
return maxEnergyStored;
|
||||
}
|
||||
|
||||
public int getMaxIO() {
|
||||
return maxIO;
|
||||
}
|
||||
|
||||
private void distributeEnergy() {
|
||||
if(capBanks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int energyPerCapBank = (int) (energyStored / capBanks.size());
|
||||
int remaining = (int) (energyStored % capBanks.size());
|
||||
for (TileCapBank cb : capBanks) {
|
||||
cb.setEnergyStored(energyPerCapBank);
|
||||
}
|
||||
TileCapBank cb = capBanks.get(0);
|
||||
cb.setEnergyStored(cb.getEnergyStored() + remaining);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package crazypants.enderio.machine.capbank.network;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
|
||||
public class ClientNetworkManager {
|
||||
|
||||
private static final ClientNetworkManager instance = new ClientNetworkManager();
|
||||
|
||||
public static ClientNetworkManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Map<Integer, CapBankClientNetwork> networks = new HashMap<Integer, CapBankClientNetwork>();
|
||||
|
||||
ClientNetworkManager() {
|
||||
}
|
||||
|
||||
public void destroyNetwork(int id) {
|
||||
System.out.println("ClientNetworkManager.destroyNetwork: " + id);
|
||||
CapBankClientNetwork res = networks.get(id);
|
||||
if(res != null) {
|
||||
res.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateState(int id, NetworkClientState state) {
|
||||
CapBankClientNetwork network = getOrCreateNetwork(id);
|
||||
network.setState(state);
|
||||
}
|
||||
|
||||
public NetworkClientState getClientState(int id) {
|
||||
CapBankClientNetwork res = networks.get(id);
|
||||
if(res == null) {
|
||||
return null;
|
||||
}
|
||||
return res.getState();
|
||||
}
|
||||
|
||||
private CapBankClientNetwork getOrCreateNetwork(int id) {
|
||||
CapBankClientNetwork res = networks.get(id);
|
||||
if(res == null) {
|
||||
res = new CapBankClientNetwork(id);
|
||||
networks.put(id, res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public void addToNetwork(int id, TileCapBank tileCapBank) {
|
||||
CapBankClientNetwork network = getOrCreateNetwork(id);
|
||||
network.addMember(tileCapBank);
|
||||
|
||||
}
|
||||
|
||||
public CapBankClientNetwork getNetwork(TileCapBank tr) {
|
||||
if(tr == null) {
|
||||
return null;
|
||||
}
|
||||
return networks.get(tr.getNetworkId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package crazypants.enderio.machine.capbank.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class NetworkClientState {
|
||||
|
||||
private final long energyStored;
|
||||
private final long maxEnergyStored;
|
||||
private final int maxIO;
|
||||
private final int maxInput;
|
||||
private final int maxOutput;
|
||||
|
||||
public NetworkClientState() {
|
||||
this(0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
public NetworkClientState(long energyStored, long maxEnergyStored, int maxIO, int maxInput, int maxOutput) {
|
||||
this.energyStored = energyStored;
|
||||
this.maxEnergyStored = maxEnergyStored;
|
||||
this.maxIO = maxIO;
|
||||
this.maxInput = maxInput;
|
||||
this.maxOutput = maxOutput;
|
||||
}
|
||||
|
||||
public NetworkClientState(CapBankNetwork network) {
|
||||
energyStored = network.getEnergyStored();
|
||||
maxEnergyStored = network.getMaxEnergyStored();
|
||||
maxIO = network.getMaxIO();
|
||||
maxInput = network.getMaxEnergyRecieved();
|
||||
maxOutput = network.getMaxEnergySent();
|
||||
}
|
||||
|
||||
public void writeToBuf(ByteBuf buf) {
|
||||
buf.writeLong(energyStored);
|
||||
buf.writeLong(maxEnergyStored);
|
||||
buf.writeInt(maxIO);
|
||||
buf.writeInt(maxInput);
|
||||
buf.writeInt(maxOutput);
|
||||
}
|
||||
|
||||
public static NetworkClientState readFromBuf(ByteBuf buf) {
|
||||
return new NetworkClientState(buf.readLong(), buf.readLong(), buf.readInt(), buf.readInt(), buf.readInt());
|
||||
}
|
||||
|
||||
public long getEnergyStored() {
|
||||
return energyStored;
|
||||
}
|
||||
|
||||
public long getMaxEnergyStored() {
|
||||
return maxEnergyStored;
|
||||
}
|
||||
|
||||
public int getMaxOutput() {
|
||||
return maxOutput;
|
||||
}
|
||||
|
||||
public int getMaxInput() {
|
||||
return maxInput;
|
||||
}
|
||||
|
||||
public int getMaxIO() {
|
||||
return maxIO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NetworkClientState [energyStored=" + energyStored + ", maxEnergyStored=" + maxEnergyStored + ", maxIO=" + maxIO + ", maxInput=" + maxInput
|
||||
+ ", maxOutput=" + maxOutput + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+7
-3
@@ -1,16 +1,20 @@
|
||||
package crazypants.enderio.machine.capbank;
|
||||
package crazypants.enderio.machine.capbank.network;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
import crazypants.util.BlockCoord;
|
||||
|
||||
public class NetworkManager {
|
||||
public class NetworkUtil {
|
||||
|
||||
private static AtomicInteger nextID = new AtomicInteger(0);
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static void ensureValidNetwork(TileCapBank cap) {
|
||||
@@ -19,7 +23,7 @@ public class NetworkManager {
|
||||
if(reuseNetwork(cap, neighbours, world)) {
|
||||
return;
|
||||
}
|
||||
CapBankNetwork network = new CapBankNetwork();
|
||||
CapBankNetwork network = new CapBankNetwork(nextID.getAndIncrement());
|
||||
network.init(cap, neighbours, world);
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package crazypants.enderio.machine.capbank.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
|
||||
public class PacketClientStateRequest implements IMessage, IMessageHandler<PacketClientStateRequest, PacketClientStateResponse> {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
|
||||
public PacketClientStateRequest() {
|
||||
}
|
||||
|
||||
public PacketClientStateRequest(TileCapBank capBank) {
|
||||
x = capBank.xCoord;
|
||||
y = capBank.yCoord;
|
||||
z = capBank.zCoord;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
x = buf.readInt();
|
||||
y = buf.readInt();
|
||||
z = buf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PacketClientStateResponse onMessage(PacketClientStateRequest message, MessageContext ctx) {
|
||||
EntityPlayer player = ctx.getServerHandler().playerEntity;
|
||||
TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
||||
if(te instanceof TileCapBank) {
|
||||
return new PacketClientStateResponse(((TileCapBank) te).getNetwork());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package crazypants.enderio.machine.capbank.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import crazypants.enderio.machine.capbank.network.CapBankNetwork;
|
||||
import crazypants.enderio.machine.capbank.network.ClientNetworkManager;
|
||||
import crazypants.enderio.machine.capbank.network.NetworkClientState;
|
||||
|
||||
public class PacketClientStateResponse implements IMessage, IMessageHandler<PacketClientStateResponse, IMessage> {
|
||||
|
||||
private int id;
|
||||
private NetworkClientState state;
|
||||
|
||||
public PacketClientStateResponse() {
|
||||
}
|
||||
|
||||
public PacketClientStateResponse(CapBankNetwork network) {
|
||||
this(network, false);
|
||||
}
|
||||
|
||||
public PacketClientStateResponse(CapBankNetwork network, boolean remove) {
|
||||
id = network.getId();
|
||||
if(!remove) {
|
||||
state = network.getClientState();
|
||||
} else {
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(id);
|
||||
buf.writeBoolean(state != null);
|
||||
if(state != null) {
|
||||
state.writeToBuf(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
id = buf.readInt();
|
||||
boolean hasState = buf.readBoolean();
|
||||
if(hasState) {
|
||||
state = NetworkClientState.readFromBuf(buf);
|
||||
} else {
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketClientStateResponse message, MessageContext ctx) {
|
||||
if(message.state != null) {
|
||||
ClientNetworkManager.getInstance().updateState(message.id, message.state);
|
||||
} else {
|
||||
ClientNetworkManager.getInstance().destroyNetwork(message.id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package crazypants.enderio.machine.capbank.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
|
||||
public class PacketNetworkIdRequest implements IMessage, IMessageHandler<PacketNetworkIdRequest, PacketNetworkIdResponse> {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
|
||||
public PacketNetworkIdRequest() {
|
||||
}
|
||||
|
||||
public PacketNetworkIdRequest(TileCapBank capBank) {
|
||||
x = capBank.xCoord;
|
||||
y = capBank.yCoord;
|
||||
z = capBank.zCoord;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
x = buf.readInt();
|
||||
y = buf.readInt();
|
||||
z = buf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PacketNetworkIdResponse onMessage(PacketNetworkIdRequest message, MessageContext ctx) {
|
||||
EntityPlayer player = ctx.getServerHandler().playerEntity;
|
||||
TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
||||
if(te instanceof TileCapBank) {
|
||||
TileCapBank capBank = (TileCapBank) te;
|
||||
if(capBank.getNetwork() != null) {
|
||||
return new PacketNetworkIdResponse((TileCapBank) te);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PacketNetworkIdRequest [x=" + x + ", y=" + y + ", z=" + z + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package crazypants.enderio.machine.capbank.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.machine.capbank.TileCapBank;
|
||||
|
||||
public class PacketNetworkIdResponse implements IMessage, IMessageHandler<PacketNetworkIdResponse, IMessage> {
|
||||
|
||||
private int id;
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
|
||||
public PacketNetworkIdResponse() {
|
||||
}
|
||||
|
||||
PacketNetworkIdResponse(TileCapBank capBank) {
|
||||
if(capBank != null && capBank.getNetwork() != null) {
|
||||
id = capBank.getNetwork().getId();
|
||||
} else {
|
||||
id = -1;
|
||||
}
|
||||
x = capBank.xCoord;
|
||||
y = capBank.yCoord;
|
||||
z = capBank.zCoord;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(id);
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
id = buf.readInt();
|
||||
x = buf.readInt();
|
||||
y = buf.readInt();
|
||||
z = buf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketNetworkIdResponse message, MessageContext ctx) {
|
||||
World w = EnderIO.proxy.getClientWorld();
|
||||
if(w != null) {
|
||||
TileEntity te = w.getTileEntity(message.x, message.y, message.z);
|
||||
if(te instanceof TileCapBank) {
|
||||
((TileCapBank) te).setNetworkId(message.id);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -67,7 +67,6 @@ public class BlockCapacitorBank extends BlockEio implements IGuiHandler, IAdvanc
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
//super.init();
|
||||
|
||||
GameRegistry.registerBlock(this, BlockItemCapacitorBank.class, name);
|
||||
if(teClass != null) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.text.NumberFormat;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import cofh.api.energy.IEnergyContainerItem;
|
||||
import crazypants.enderio.config.Config;
|
||||
import crazypants.util.Lang;
|
||||
|
||||
public class PowerDisplayUtil {
|
||||
@@ -64,13 +63,20 @@ public class PowerDisplayUtil {
|
||||
return formatPower(amount) + "/" + formatPower(capacity) + " " + PowerDisplayUtil.abrevation();
|
||||
}
|
||||
|
||||
public static String formatPower(long amount) {
|
||||
String str = INT_NF.format(amount);
|
||||
if(REPLACE_NBSP) {
|
||||
str = str.replace(NBSP, ' ');
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
public static String formatPower(int powerRF) {
|
||||
String str = INT_NF.format(powerRF);
|
||||
if(REPLACE_NBSP) {
|
||||
str = str.replace(NBSP, ' ');
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user