getting items from storage
This commit is contained in:
@@ -8,6 +8,7 @@ import org.lwjgl.input.Keyboard;
|
|||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
import storagecraft.SC;
|
import storagecraft.SC;
|
||||||
import storagecraft.inventory.ContainerGrid;
|
import storagecraft.inventory.ContainerGrid;
|
||||||
|
import storagecraft.network.MessagePullFromStorage;
|
||||||
import storagecraft.network.MessagePushToStorage;
|
import storagecraft.network.MessagePushToStorage;
|
||||||
import storagecraft.storage.StorageItem;
|
import storagecraft.storage.StorageItem;
|
||||||
import storagecraft.tile.TileController;
|
import storagecraft.tile.TileController;
|
||||||
@@ -56,7 +57,7 @@ public class GuiGrid extends GuiContainer {
|
|||||||
itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
|
itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((mouseX > xx && mouseX < xx + 16 && mouseY > yy && mouseY < yy + 16) || !grid.isConnected()) {
|
if ((mouseX >= xx && mouseX <= xx + 16 && mouseY >= yy && mouseY <= yy + 16) || !grid.isConnected()) {
|
||||||
int color = grid.isConnected() ? -2130706433 : 0xFF5B5B5B;
|
int color = grid.isConnected() ? -2130706433 : 0xFF5B5B5B;
|
||||||
|
|
||||||
GL11.glDisable(GL11.GL_LIGHTING);
|
GL11.glDisable(GL11.GL_LIGHTING);
|
||||||
@@ -100,12 +101,20 @@ public class GuiGrid extends GuiContainer {
|
|||||||
if (grid.isConnected()) {
|
if (grid.isConnected()) {
|
||||||
TileController controller = grid.getController();
|
TileController controller = grid.getController();
|
||||||
|
|
||||||
if (mouseX > getGridXStart() && mouseX < getGridXEnd() && mouseY > getGridYStart() && mouseY < getGridYEnd()) {
|
if (mouseX >= getGridXStart() && mouseX <= getGridXEnd() && mouseY >= getGridYStart() && mouseY <= getGridYEnd()) {
|
||||||
if (container.getPlayer().inventory.getItemStack() != null) {
|
if (container.getPlayer().inventory.getItemStack() != null) {
|
||||||
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, -1));
|
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, -1));
|
||||||
|
} else {
|
||||||
|
int slotX = ((mouseX - getGridXStart()) / 18) + 1;
|
||||||
|
int slotY = ((mouseY - getGridYStart()) / 18) + 1;
|
||||||
|
int slotId = (slotX * slotY) - 1;
|
||||||
|
|
||||||
|
SC.NETWORK.sendToServer(new MessagePullFromStorage(controller.xCoord, controller.yCoord, controller.zCoord, slotId, clickedButton == 1, Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (Object slot : container.inventorySlots) {
|
for (int i = 0; i < container.inventorySlots.size(); ++i) {
|
||||||
|
Slot slot = (Slot) container.inventorySlots.get(i);
|
||||||
|
|
||||||
if (func_146978_c(((Slot) slot).xDisplayPosition, ((Slot) slot).yDisplayPosition, 16, 16, mouseX, mouseY)) {
|
if (func_146978_c(((Slot) slot).xDisplayPosition, ((Slot) slot).yDisplayPosition, 16, 16, mouseX, mouseY)) {
|
||||||
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
|
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
|
||||||
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, ((Slot) slot).slotNumber));
|
SC.NETWORK.sendToServer(new MessagePushToStorage(controller.xCoord, controller.yCoord, controller.zCoord, ((Slot) slot).slotNumber));
|
||||||
|
@@ -0,0 +1,87 @@
|
|||||||
|
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.storage.StorageItem;
|
||||||
|
import storagecraft.tile.TileController;
|
||||||
|
|
||||||
|
public class MessagePullFromStorage implements IMessage, IMessageHandler<MessagePullFromStorage, IMessage> {
|
||||||
|
private int x;
|
||||||
|
private int y;
|
||||||
|
private int z;
|
||||||
|
private int slot;
|
||||||
|
private boolean half;
|
||||||
|
private boolean shift;
|
||||||
|
|
||||||
|
public MessagePullFromStorage() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessagePullFromStorage(int x, int y, int z, int slot, boolean half, boolean shift) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.slot = slot;
|
||||||
|
this.half = half;
|
||||||
|
this.shift = shift;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromBytes(ByteBuf buf) {
|
||||||
|
x = buf.readInt();
|
||||||
|
y = buf.readInt();
|
||||||
|
z = buf.readInt();
|
||||||
|
slot = buf.readInt();
|
||||||
|
half = buf.readBoolean();
|
||||||
|
shift = buf.readBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toBytes(ByteBuf buf) {
|
||||||
|
buf.writeInt(x);
|
||||||
|
buf.writeInt(y);
|
||||||
|
buf.writeInt(z);
|
||||||
|
buf.writeInt(slot);
|
||||||
|
buf.writeBoolean(half);
|
||||||
|
buf.writeBoolean(shift);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IMessage onMessage(MessagePullFromStorage message, MessageContext context) {
|
||||||
|
EntityPlayerMP player = context.getServerHandler().playerEntity;
|
||||||
|
|
||||||
|
TileEntity tile = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
||||||
|
|
||||||
|
if (tile instanceof TileController) {
|
||||||
|
TileController controller = (TileController) tile;
|
||||||
|
|
||||||
|
if (message.slot < controller.getStorage().all().size()) {
|
||||||
|
StorageItem item = controller.getStorage().all().get(message.slot);
|
||||||
|
|
||||||
|
int quantity = 64;
|
||||||
|
|
||||||
|
if (message.half && item.getQuantity() > 1) {
|
||||||
|
quantity = item.getQuantity() / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack stack = controller.getStorage().take(item.getType(), quantity, item.getMeta());
|
||||||
|
|
||||||
|
if (message.shift) {
|
||||||
|
// @TODO: This doesn't work
|
||||||
|
if (!player.inventory.addItemStackToInventory(stack.copy())) {
|
||||||
|
controller.getStorage().push(stack);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
player.inventory.setItemStack(stack);
|
||||||
|
player.updateHeldItem();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@@ -9,6 +9,7 @@ import cpw.mods.fml.relauncher.Side;
|
|||||||
import storagecraft.SC;
|
import storagecraft.SC;
|
||||||
import storagecraft.SCBlocks;
|
import storagecraft.SCBlocks;
|
||||||
import storagecraft.gui.GuiHandler;
|
import storagecraft.gui.GuiHandler;
|
||||||
|
import storagecraft.network.MessagePullFromStorage;
|
||||||
import storagecraft.network.MessagePushToStorage;
|
import storagecraft.network.MessagePushToStorage;
|
||||||
import storagecraft.network.MessageTileUpdate;
|
import storagecraft.network.MessageTileUpdate;
|
||||||
import storagecraft.tile.TileCable;
|
import storagecraft.tile.TileCable;
|
||||||
@@ -19,6 +20,7 @@ public class CommonProxy {
|
|||||||
public void preInit(FMLPreInitializationEvent e) {
|
public void preInit(FMLPreInitializationEvent e) {
|
||||||
SC.NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT);
|
SC.NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT);
|
||||||
SC.NETWORK.registerMessage(MessagePushToStorage.class, MessagePushToStorage.class, 1, Side.SERVER);
|
SC.NETWORK.registerMessage(MessagePushToStorage.class, MessagePushToStorage.class, 1, Side.SERVER);
|
||||||
|
SC.NETWORK.registerMessage(MessagePullFromStorage.class, MessagePullFromStorage.class, 2, Side.SERVER);
|
||||||
|
|
||||||
NetworkRegistry.INSTANCE.registerGuiHandler(SC.INSTANCE, new GuiHandler());
|
NetworkRegistry.INSTANCE.registerGuiHandler(SC.INSTANCE, new GuiHandler());
|
||||||
|
|
||||||
|
@@ -37,24 +37,24 @@ public class Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean take(ItemStack stack) {
|
public ItemStack take(Item type, int quantity, int meta) {
|
||||||
if (has(stack)) {
|
for (StorageItem item : items) {
|
||||||
StorageItem item = get(stack);
|
if (item.getType() == type && item.getMeta() == meta) {
|
||||||
|
if (item.getQuantity() < quantity) {
|
||||||
if (item.getQuantity() < stack.stackSize) {
|
quantity = item.getQuantity();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
item.setQuantity(item.getQuantity() - stack.stackSize);
|
item.setQuantity(item.getQuantity() - quantity);
|
||||||
|
|
||||||
if (item.getQuantity() == 0) {
|
if (item.getQuantity() == 0) {
|
||||||
items.remove(get(stack));
|
items.remove(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return new ItemStack(type, quantity, meta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fromBytes(ByteBuf buf) {
|
public void fromBytes(ByteBuf buf) {
|
||||||
|
Reference in New Issue
Block a user