pushing stuff to grid storage
This commit is contained in:
		@@ -17,8 +17,9 @@ import storagecraft.proxy.CommonProxy;
 | 
			
		||||
public class SC {
 | 
			
		||||
	public static class GUI {
 | 
			
		||||
		public static final int CONTROLLER = 0;
 | 
			
		||||
		public static final int GRID = 1;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public static final String ID = "storagecraft";
 | 
			
		||||
	public static final String VERSION = "1.0";
 | 
			
		||||
	public static final SimpleNetworkWrapper NETWORK = NetworkRegistry.INSTANCE.newSimpleChannel(ID);
 | 
			
		||||
@@ -32,17 +33,17 @@ public class SC {
 | 
			
		||||
	public static CommonProxy PROXY;
 | 
			
		||||
	@Instance
 | 
			
		||||
	public static SC INSTANCE;
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	@EventHandler
 | 
			
		||||
	public void preInit(FMLPreInitializationEvent e) {
 | 
			
		||||
		PROXY.preInit(e);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	@EventHandler
 | 
			
		||||
	public void init(FMLInitializationEvent e) {
 | 
			
		||||
		PROXY.init(e);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	@EventHandler
 | 
			
		||||
	public void postInit(FMLPostInitializationEvent e) {
 | 
			
		||||
		PROXY.postInit(e);
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,12 @@ package storagecraft.block;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.block.ITileEntityProvider;
 | 
			
		||||
import net.minecraft.client.renderer.texture.IIconRegister;
 | 
			
		||||
import net.minecraft.entity.player.EntityPlayer;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraft.util.IIcon;
 | 
			
		||||
import net.minecraft.world.IBlockAccess;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import storagecraft.SC;
 | 
			
		||||
import storagecraft.tile.TileGrid;
 | 
			
		||||
 | 
			
		||||
public class BlockGrid extends BlockSC implements ITileEntityProvider {
 | 
			
		||||
@@ -22,6 +24,15 @@ public class BlockGrid extends BlockSC implements ITileEntityProvider {
 | 
			
		||||
		return new TileGrid();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
 | 
			
		||||
		if (!world.isRemote) {
 | 
			
		||||
			player.openGui(SC.INSTANCE, SC.GUI.GRID, world, x, y, z);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void registerBlockIcons(IIconRegister register) {
 | 
			
		||||
		iconConnected = register.registerIcon("storagecraft:gridConnected");
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										84
									
								
								src/main/java/storagecraft/gui/GuiGrid.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								src/main/java/storagecraft/gui/GuiGrid.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
package storagecraft.gui;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.client.gui.inventory.GuiContainer;
 | 
			
		||||
import net.minecraft.inventory.Container;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
import org.lwjgl.opengl.GL11;
 | 
			
		||||
import storagecraft.SC;
 | 
			
		||||
import storagecraft.network.MessagePushToStorage;
 | 
			
		||||
import storagecraft.storage.StorageItem;
 | 
			
		||||
import storagecraft.tile.TileGrid;
 | 
			
		||||
 | 
			
		||||
public class GuiGrid extends GuiContainer {
 | 
			
		||||
	public static final ResourceLocation GRID_RESOURCE = new ResourceLocation("storagecraft:textures/gui/grid.png");
 | 
			
		||||
 | 
			
		||||
	private TileGrid grid;
 | 
			
		||||
 | 
			
		||||
	public GuiGrid(Container container, TileGrid grid) {
 | 
			
		||||
		super(container);
 | 
			
		||||
 | 
			
		||||
		this.grid = grid;
 | 
			
		||||
 | 
			
		||||
		this.xSize = 176;
 | 
			
		||||
		this.ySize = 190;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	protected void drawGuiContainerBackgroundLayer(float renderPartialTicks, int mouseX, int mouseY) {
 | 
			
		||||
		GL11.glColor3f(1.0F, 1.0F, 1.0F);
 | 
			
		||||
 | 
			
		||||
		mc.getTextureManager().bindTexture(GRID_RESOURCE);
 | 
			
		||||
 | 
			
		||||
		int x = (this.width - xSize) / 2;
 | 
			
		||||
		int y = (this.height - ySize) / 2;
 | 
			
		||||
 | 
			
		||||
		drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
 | 
			
		||||
 | 
			
		||||
		fontRendererObj.drawString("Grid", x + 7, y + 7, 4210752);
 | 
			
		||||
		fontRendererObj.drawString("Inventory", x + 7, y + 96, 4210752);
 | 
			
		||||
 | 
			
		||||
		if (grid.isConnected()) {
 | 
			
		||||
			int xx = getGridXStart();
 | 
			
		||||
			int yy = getGridYStart();
 | 
			
		||||
 | 
			
		||||
			for (int i = 0; i < grid.getController().getStorage().all().size(); ++i) {
 | 
			
		||||
				StorageItem item = grid.getController().getStorage().all().get(i);
 | 
			
		||||
 | 
			
		||||
				ItemStack stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());
 | 
			
		||||
 | 
			
		||||
				itemRender.renderItemIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
 | 
			
		||||
				itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
 | 
			
		||||
 | 
			
		||||
				xx += 18;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private int getGridXStart() {
 | 
			
		||||
		return ((this.width - xSize) / 2) + 8;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private int getGridXEnd() {
 | 
			
		||||
		return getGridXStart() + (18 * 9);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private int getGridYStart() {
 | 
			
		||||
		return ((this.height - ySize) / 2) + 20;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private int getGridYEnd() {
 | 
			
		||||
		return getGridYStart() + (18 * 4);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void mouseClicked(int mouseX, int mouseY, int clickedButton) {
 | 
			
		||||
		super.mouseClicked(mouseX, mouseY, clickedButton);
 | 
			
		||||
 | 
			
		||||
		if (clickedButton == 0) {
 | 
			
		||||
			if (mouseX > getGridXStart() && mouseX < getGridXEnd() && mouseY > getGridYStart() && mouseY < getGridYEnd()) {
 | 
			
		||||
				SC.NETWORK.sendToServer(new MessagePushToStorage(grid.getController()));
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -7,13 +7,17 @@ import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import storagecraft.SC;
 | 
			
		||||
import storagecraft.inventory.ContainerController;
 | 
			
		||||
import storagecraft.inventory.ContainerGrid;
 | 
			
		||||
import storagecraft.tile.TileController;
 | 
			
		||||
import storagecraft.tile.TileGrid;
 | 
			
		||||
 | 
			
		||||
public class GuiHandler implements IGuiHandler {
 | 
			
		||||
	private Container getContainer(int ID, EntityPlayer player) {
 | 
			
		||||
		switch (ID) {
 | 
			
		||||
			case SC.GUI.CONTROLLER:
 | 
			
		||||
				return new ContainerController(player);
 | 
			
		||||
			case SC.GUI.GRID:
 | 
			
		||||
				return new ContainerGrid(player);
 | 
			
		||||
			default:
 | 
			
		||||
				return null;
 | 
			
		||||
		}
 | 
			
		||||
@@ -31,6 +35,8 @@ public class GuiHandler implements IGuiHandler {
 | 
			
		||||
		switch (ID) {
 | 
			
		||||
			case SC.GUI.CONTROLLER:
 | 
			
		||||
				return new GuiController(getContainer(ID, player), (TileController) tile);
 | 
			
		||||
			case SC.GUI.GRID:
 | 
			
		||||
				return new GuiGrid(getContainer(ID, player), (TileGrid) tile);
 | 
			
		||||
			default:
 | 
			
		||||
				return null;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								src/main/java/storagecraft/inventory/ContainerGrid.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/main/java/storagecraft/inventory/ContainerGrid.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
package storagecraft.inventory;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.entity.player.EntityPlayer;
 | 
			
		||||
 | 
			
		||||
public class ContainerGrid extends ContainerSC {
 | 
			
		||||
	public ContainerGrid(EntityPlayer player) {
 | 
			
		||||
		super(player);
 | 
			
		||||
 | 
			
		||||
		addPlayerInventory(8, 108);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										61
									
								
								src/main/java/storagecraft/network/MessagePushToStorage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								src/main/java/storagecraft/network/MessagePushToStorage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
package storagecraft.network;
 | 
			
		||||
 | 
			
		||||
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 io.netty.buffer.ByteBuf;
 | 
			
		||||
import net.minecraft.entity.player.EntityPlayerMP;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import storagecraft.tile.TileController;
 | 
			
		||||
 | 
			
		||||
public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePushToStorage, IMessage> {
 | 
			
		||||
	private TileController controller;
 | 
			
		||||
 | 
			
		||||
	private int x;
 | 
			
		||||
	private int y;
 | 
			
		||||
	private int z;
 | 
			
		||||
 | 
			
		||||
	public MessagePushToStorage() {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public MessagePushToStorage(TileController controller) {
 | 
			
		||||
		this.controller = controller;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void fromBytes(ByteBuf buf) {
 | 
			
		||||
		x = buf.readInt();
 | 
			
		||||
		y = buf.readInt();
 | 
			
		||||
		z = buf.readInt();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void toBytes(ByteBuf buf) {
 | 
			
		||||
		buf.writeInt(controller.xCoord);
 | 
			
		||||
		buf.writeInt(controller.yCoord);
 | 
			
		||||
		buf.writeInt(controller.zCoord);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public IMessage onMessage(MessagePushToStorage message, MessageContext context) {
 | 
			
		||||
		EntityPlayerMP player = context.getServerHandler().playerEntity;
 | 
			
		||||
 | 
			
		||||
		TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z);
 | 
			
		||||
 | 
			
		||||
		if (tile instanceof TileController) {
 | 
			
		||||
			controller = (TileController) tile;
 | 
			
		||||
 | 
			
		||||
			ItemStack stack = player.inventory.getItemStack();
 | 
			
		||||
 | 
			
		||||
			if (stack != null) {
 | 
			
		||||
				controller.getStorage().push(stack);
 | 
			
		||||
 | 
			
		||||
				player.inventory.setItemStack(null);
 | 
			
		||||
				player.updateHeldItem();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -9,6 +9,7 @@ import cpw.mods.fml.relauncher.Side;
 | 
			
		||||
import storagecraft.SC;
 | 
			
		||||
import storagecraft.SCBlocks;
 | 
			
		||||
import storagecraft.gui.GuiHandler;
 | 
			
		||||
import storagecraft.network.MessagePushToStorage;
 | 
			
		||||
import storagecraft.network.MessageTileUpdate;
 | 
			
		||||
import storagecraft.tile.TileCable;
 | 
			
		||||
import storagecraft.tile.TileController;
 | 
			
		||||
@@ -17,21 +18,22 @@ import storagecraft.tile.TileGrid;
 | 
			
		||||
public class CommonProxy {
 | 
			
		||||
	public void preInit(FMLPreInitializationEvent e) {
 | 
			
		||||
		SC.NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT);
 | 
			
		||||
		
 | 
			
		||||
		SC.NETWORK.registerMessage(MessagePushToStorage.class, MessagePushToStorage.class, 1, Side.SERVER);
 | 
			
		||||
 | 
			
		||||
		NetworkRegistry.INSTANCE.registerGuiHandler(SC.INSTANCE, new GuiHandler());
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		GameRegistry.registerTileEntity(TileController.class, "controller");
 | 
			
		||||
		GameRegistry.registerTileEntity(TileCable.class, "cable");
 | 
			
		||||
		GameRegistry.registerTileEntity(TileGrid.class, "grid");
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		GameRegistry.registerBlock(SCBlocks.CONTROLLER, "controller");
 | 
			
		||||
		GameRegistry.registerBlock(SCBlocks.CABLE, "cable");
 | 
			
		||||
		GameRegistry.registerBlock(SCBlocks.GRID, "grid");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public void init(FMLInitializationEvent e) {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public void postInit(FMLPostInitializationEvent e) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										83
									
								
								src/main/java/storagecraft/storage/Storage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/main/java/storagecraft/storage/Storage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import io.netty.buffer.ByteBuf;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
public class Storage {
 | 
			
		||||
	private List<StorageItem> items = new ArrayList<StorageItem>();
 | 
			
		||||
 | 
			
		||||
	public List<StorageItem> all() {
 | 
			
		||||
		return items;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public StorageItem get(ItemStack stack) {
 | 
			
		||||
		for (StorageItem item : items) {
 | 
			
		||||
			if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
 | 
			
		||||
				return item;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean has(ItemStack stack) {
 | 
			
		||||
		return get(stack) != null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void push(ItemStack stack) {
 | 
			
		||||
		if (has(stack)) {
 | 
			
		||||
			StorageItem item = get(stack);
 | 
			
		||||
 | 
			
		||||
			item.setQuantity(item.getQuantity() + stack.stackSize);
 | 
			
		||||
		} else {
 | 
			
		||||
			items.add(new StorageItem(stack.getItem(), stack.stackSize, stack.getItemDamage()));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean take(ItemStack stack) {
 | 
			
		||||
		if (has(stack)) {
 | 
			
		||||
			StorageItem item = get(stack);
 | 
			
		||||
 | 
			
		||||
			if (item.getQuantity() < stack.stackSize) {
 | 
			
		||||
				return false;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			item.setQuantity(item.getQuantity() - stack.stackSize);
 | 
			
		||||
 | 
			
		||||
			if (item.getQuantity() == 0) {
 | 
			
		||||
				items.remove(get(stack));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void fromBytes(ByteBuf buf) {
 | 
			
		||||
		items.clear();
 | 
			
		||||
 | 
			
		||||
		int size = buf.readInt();
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < size; ++i) {
 | 
			
		||||
			Item type = Item.getItemById(buf.readInt());
 | 
			
		||||
			int quantity = buf.readInt();
 | 
			
		||||
			int meta = buf.readInt();
 | 
			
		||||
 | 
			
		||||
			items.add(new StorageItem(type, quantity, meta));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void toBytes(ByteBuf buf) {
 | 
			
		||||
		buf.writeInt(items.size());
 | 
			
		||||
 | 
			
		||||
		for (StorageItem item : items) {
 | 
			
		||||
			buf.writeInt(Item.getIdFromItem(item.getType()));
 | 
			
		||||
			buf.writeInt(item.getQuantity());
 | 
			
		||||
			buf.writeInt(item.getMeta());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								src/main/java/storagecraft/storage/StorageItem.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/main/java/storagecraft/storage/StorageItem.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
 | 
			
		||||
public class StorageItem {
 | 
			
		||||
	private Item type;
 | 
			
		||||
	private int quantity;
 | 
			
		||||
	private int meta;
 | 
			
		||||
 | 
			
		||||
	public StorageItem(Item type, int quantity, int meta) {
 | 
			
		||||
		this.type = type;
 | 
			
		||||
		this.meta = meta;
 | 
			
		||||
		this.quantity = quantity;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public StorageItem(Item type) {
 | 
			
		||||
		this.type = type;
 | 
			
		||||
		this.meta = 0;
 | 
			
		||||
		this.quantity = 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Item getType() {
 | 
			
		||||
		return type;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void setType(Item type) {
 | 
			
		||||
		this.type = type;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getQuantity() {
 | 
			
		||||
		return quantity;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void setQuantity(int quantity) {
 | 
			
		||||
		this.quantity = quantity;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getMeta() {
 | 
			
		||||
		return meta;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void setMeta(int meta) {
 | 
			
		||||
		this.meta = meta;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -5,16 +5,21 @@ import cofh.api.energy.IEnergyHandler;
 | 
			
		||||
import io.netty.buffer.ByteBuf;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.init.Items;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraftforge.common.util.ForgeDirection;
 | 
			
		||||
import storagecraft.storage.Storage;
 | 
			
		||||
 | 
			
		||||
public class TileController extends TileSC implements IEnergyHandler, INetworkTile {
 | 
			
		||||
	public static final int BASE_ENERGY_USAGE = 100;
 | 
			
		||||
 | 
			
		||||
	private Storage storage = new Storage();
 | 
			
		||||
 | 
			
		||||
	private boolean destroyed = false;
 | 
			
		||||
 | 
			
		||||
	private EnergyStorage storage = new EnergyStorage(32000);
 | 
			
		||||
	private EnergyStorage energy = new EnergyStorage(32000);
 | 
			
		||||
	private int energyUsage;
 | 
			
		||||
 | 
			
		||||
	private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
 | 
			
		||||
@@ -26,37 +31,37 @@ public class TileController extends TileSC implements IEnergyHandler, INetworkTi
 | 
			
		||||
		super.updateEntity();
 | 
			
		||||
 | 
			
		||||
		if (!destroyed) {
 | 
			
		||||
			if (!worldObj.isRemote) {
 | 
			
		||||
				ticks++;
 | 
			
		||||
			++ticks;
 | 
			
		||||
 | 
			
		||||
				if (ticks % 40 == 0) {
 | 
			
		||||
					if (!isActive()) {
 | 
			
		||||
						disconnectAll();
 | 
			
		||||
					} else {
 | 
			
		||||
						List<TileMachine> machines = new ArrayList<TileMachine>();
 | 
			
		||||
			if (!worldObj.isRemote && ticks % 40 == 0) {
 | 
			
		||||
				storage.push(new ItemStack(worldObj.rand.nextBoolean() ? Items.diamond : Items.apple, 10 + worldObj.rand.nextInt(40)));
 | 
			
		||||
 | 
			
		||||
						for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
 | 
			
		||||
							TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
 | 
			
		||||
				if (!isActive()) {
 | 
			
		||||
					disconnectAll();
 | 
			
		||||
				} else {
 | 
			
		||||
					List<TileMachine> machines = new ArrayList<TileMachine>();
 | 
			
		||||
 | 
			
		||||
							if (tile instanceof TileCable) {
 | 
			
		||||
								machines.addAll(((TileCable) tile).findMachines(this));
 | 
			
		||||
							}
 | 
			
		||||
					for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
 | 
			
		||||
						TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
 | 
			
		||||
 | 
			
		||||
						if (tile instanceof TileCable) {
 | 
			
		||||
							machines.addAll(((TileCable) tile).findMachines(this));
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						for (TileMachine machine : connectedMachines) {
 | 
			
		||||
							if (!machines.contains(machine)) {
 | 
			
		||||
								machine.onDisconnected();
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						for (TileMachine machine : machines) {
 | 
			
		||||
							if (!connectedMachines.contains(machine)) {
 | 
			
		||||
								machine.onConnected(this);
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						connectedMachines = machines;
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					for (TileMachine machine : connectedMachines) {
 | 
			
		||||
						if (!machines.contains(machine)) {
 | 
			
		||||
							machine.onDisconnected();
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					for (TileMachine machine : machines) {
 | 
			
		||||
						if (!connectedMachines.contains(machine)) {
 | 
			
		||||
							machine.onConnected(this);
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					connectedMachines = machines;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				energyUsage = BASE_ENERGY_USAGE;
 | 
			
		||||
@@ -65,7 +70,7 @@ public class TileController extends TileSC implements IEnergyHandler, INetworkTi
 | 
			
		||||
					energyUsage += machine.getEnergyUsage();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				storage.extractEnergy(energyUsage, false);
 | 
			
		||||
				energy.extractEnergy(energyUsage, false);
 | 
			
		||||
			} else {
 | 
			
		||||
				worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
 | 
			
		||||
			}
 | 
			
		||||
@@ -86,6 +91,10 @@ public class TileController extends TileSC implements IEnergyHandler, INetworkTi
 | 
			
		||||
		connectedMachines.clear();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Storage getStorage() {
 | 
			
		||||
		return storage;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public List<TileMachine> getMachines() {
 | 
			
		||||
		return connectedMachines;
 | 
			
		||||
	}
 | 
			
		||||
@@ -94,34 +103,34 @@ public class TileController extends TileSC implements IEnergyHandler, INetworkTi
 | 
			
		||||
	public void readFromNBT(NBTTagCompound nbt) {
 | 
			
		||||
		super.readFromNBT(nbt);
 | 
			
		||||
 | 
			
		||||
		storage.readFromNBT(nbt);
 | 
			
		||||
		energy.readFromNBT(nbt);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void writeToNBT(NBTTagCompound nbt) {
 | 
			
		||||
		super.writeToNBT(nbt);
 | 
			
		||||
 | 
			
		||||
		storage.writeToNBT(nbt);
 | 
			
		||||
		energy.writeToNBT(nbt);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
 | 
			
		||||
		return storage.receiveEnergy(maxReceive, simulate);
 | 
			
		||||
		return energy.receiveEnergy(maxReceive, simulate);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) {
 | 
			
		||||
		return storage.extractEnergy(maxExtract, simulate);
 | 
			
		||||
		return energy.extractEnergy(maxExtract, simulate);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int getEnergyStored(ForgeDirection from) {
 | 
			
		||||
		return storage.getEnergyStored();
 | 
			
		||||
		return energy.getEnergyStored();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int getMaxEnergyStored(ForgeDirection from) {
 | 
			
		||||
		return storage.getMaxEnergyStored();
 | 
			
		||||
		return energy.getMaxEnergyStored();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getEnergyUsage() {
 | 
			
		||||
@@ -134,18 +143,22 @@ public class TileController extends TileSC implements IEnergyHandler, INetworkTi
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean isActive() {
 | 
			
		||||
		return storage.getEnergyStored() >= getEnergyUsage();
 | 
			
		||||
		return energy.getEnergyStored() >= getEnergyUsage();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void fromBytes(ByteBuf buf) {
 | 
			
		||||
		storage.setEnergyStored(buf.readInt());
 | 
			
		||||
		energy.setEnergyStored(buf.readInt());
 | 
			
		||||
		energyUsage = buf.readInt();
 | 
			
		||||
 | 
			
		||||
		storage.fromBytes(buf);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void toBytes(ByteBuf buf) {
 | 
			
		||||
		buf.writeInt(storage.getEnergyStored());
 | 
			
		||||
		buf.writeInt(energy.getEnergyStored());
 | 
			
		||||
		buf.writeInt(energyUsage);
 | 
			
		||||
 | 
			
		||||
		storage.toBytes(buf);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,15 +4,19 @@ import io.netty.buffer.ByteBuf;
 | 
			
		||||
 | 
			
		||||
public abstract class TileMachine extends TileSC implements INetworkTile {
 | 
			
		||||
	protected boolean connected = false;
 | 
			
		||||
	private int xController, yController, zController;
 | 
			
		||||
 | 
			
		||||
	public void onConnected(TileController controller) {
 | 
			
		||||
		connected = true;
 | 
			
		||||
		this.connected = true;
 | 
			
		||||
		this.xController = controller.xCoord;
 | 
			
		||||
		this.yController = controller.yCoord;
 | 
			
		||||
		this.zController = controller.zCoord;
 | 
			
		||||
 | 
			
		||||
		worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void onDisconnected() {
 | 
			
		||||
		connected = false;
 | 
			
		||||
		this.connected = false;
 | 
			
		||||
 | 
			
		||||
		worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
 | 
			
		||||
	}
 | 
			
		||||
@@ -21,15 +25,31 @@ public abstract class TileMachine extends TileSC implements INetworkTile {
 | 
			
		||||
		return connected;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public TileController getController() {
 | 
			
		||||
		return (TileController) worldObj.getTileEntity(xController, yController, zController);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public abstract int getEnergyUsage();
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void fromBytes(ByteBuf buf) {
 | 
			
		||||
		connected = buf.readBoolean();
 | 
			
		||||
 | 
			
		||||
		if (connected) {
 | 
			
		||||
			xController = buf.readInt();
 | 
			
		||||
			yController = buf.readInt();
 | 
			
		||||
			zController = buf.readInt();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void toBytes(ByteBuf buf) {
 | 
			
		||||
		buf.writeBoolean(connected);
 | 
			
		||||
 | 
			
		||||
		if (connected) {
 | 
			
		||||
			buf.writeInt(xController);
 | 
			
		||||
			buf.writeInt(yController);
 | 
			
		||||
			buf.writeInt(zController);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								src/main/resources/assets/storagecraft/textures/gui/grid.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main/resources/assets/storagecraft/textures/gui/grid.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 1.8 KiB  | 
		Reference in New Issue
	
	Block a user