priority system

This commit is contained in:
Raoul Van den Berge
2016-01-31 13:18:43 +01:00
parent f7a5b533e5
commit 0c7a9381ac
9 changed files with 151 additions and 11 deletions

View File

@@ -1,14 +1,21 @@
package storagecraft.gui;
import com.google.common.primitives.Ints;
import java.io.IOException;
import net.minecraft.client.gui.GuiTextField;
import storagecraft.StorageCraft;
import storagecraft.block.EnumStorageType;
import storagecraft.container.ContainerStorage;
import storagecraft.gui.sidebutton.SideButtonRedstoneMode;
import storagecraft.network.MessageStoragePriorityUpdate;
import storagecraft.tile.TileStorage;
public class GuiStorage extends GuiBase
{
private TileStorage storage;
private GuiTextField priorityField;
private int barX = 8;
private int barY = 54;
private int barWidth = 16;
@@ -25,6 +32,14 @@ public class GuiStorage extends GuiBase
public void init(int x, int y)
{
addSideButton(new SideButtonRedstoneMode(storage));
priorityField = new GuiTextField(0, fontRendererObj, x + 116 + 1, y + 54 + 1, 25, fontRendererObj.FONT_HEIGHT);
priorityField.setText(String.valueOf(storage.getPriority()));
priorityField.setEnableBackgroundDrawing(false);
priorityField.setVisible(true);
priorityField.setTextColor(16777215);
priorityField.setCanLoseFocus(false);
priorityField.setFocused(true);
}
@Override
@@ -42,6 +57,8 @@ public class GuiStorage extends GuiBase
int barHeightNew = storage.getStoredScaled(barHeight);
drawTexture(x + barX, y + barY + barHeight - barHeightNew, 179, 0 + (barHeight - barHeightNew), barWidth, barHeightNew);
priorityField.drawTextBox();
}
@Override
@@ -64,4 +81,22 @@ public class GuiStorage extends GuiBase
drawTooltip(mouseX, mouseY, t("misc.storagecraft:storage.full", storage.getStoredScaled(100)));
}
}
@Override
protected void keyTyped(char character, int keyCode) throws IOException
{
if (!checkHotbarKeys(keyCode) && priorityField.textboxKeyTyped(character, keyCode))
{
Integer result = Ints.tryParse(priorityField.getText());
if (result != null)
{
StorageCraft.NETWORK.sendToServer(new MessageStoragePriorityUpdate(storage, result));
}
}
else
{
super.keyTyped(character, keyCode);
}
}
}

View File

@@ -0,0 +1,57 @@
package storagecraft.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import storagecraft.tile.TileStorage;
public class MessageStoragePriorityUpdate extends MessageHandlerPlayerToServer<MessageStoragePriorityUpdate> implements IMessage
{
private int x;
private int y;
private int z;
private int priority;
public MessageStoragePriorityUpdate()
{
}
public MessageStoragePriorityUpdate(TileStorage storage, int priority)
{
this.x = storage.getPos().getX();
this.y = storage.getPos().getY();
this.z = storage.getPos().getZ();
this.priority = priority;
}
@Override
public void fromBytes(ByteBuf buf)
{
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
priority = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(priority);
}
@Override
public void handle(MessageStoragePriorityUpdate message, EntityPlayerMP player)
{
TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
if (tile instanceof TileStorage)
{
((TileStorage) tile).setPriority(message.priority);
}
}
}

View File

@@ -34,6 +34,7 @@ public class CommonProxy
StorageCraft.NETWORK.registerMessage(MessageDetectorAmountUpdate.class, MessageDetectorAmountUpdate.class, 7, Side.SERVER);
StorageCraft.NETWORK.registerMessage(MessageGridCraftingUpdate.class, MessageGridCraftingUpdate.class, 8, Side.CLIENT);
StorageCraft.NETWORK.registerMessage(MessageGridCraftingClear.class, MessageGridCraftingClear.class, 9, Side.SERVER);
StorageCraft.NETWORK.registerMessage(MessageStoragePriorityUpdate.class, MessageStoragePriorityUpdate.class, 10, Side.SERVER);
NetworkRegistry.INSTANCE.registerGuiHandler(StorageCraft.INSTANCE, new GuiHandler());

View File

@@ -7,7 +7,8 @@ public class CellStorage extends NBTStorage
{
public CellStorage(ItemStack cell)
{
super(cell.getTagCompound(), getCapacity(cell));
// @TODO: Priority on this stuff
super(cell.getTagCompound(), getCapacity(cell), 0);
}
public static int getCapacity(ItemStack cell)

View File

@@ -1,8 +1,7 @@
package storagecraft.storage;
import net.minecraft.item.ItemStack;
import java.util.List;
import net.minecraft.item.ItemStack;
public interface IStorage
{
@@ -13,4 +12,6 @@ public interface IStorage
public ItemStack take(ItemStack stack, int flags);
public boolean canPush(ItemStack stack);
public int getPriority();
}

View File

@@ -18,11 +18,13 @@ public class NBTStorage implements IStorage
private NBTTagCompound nbtTag;
private int capacity;
private int priority;
public NBTStorage(NBTTagCompound tag, int capacity)
public NBTStorage(NBTTagCompound tag, int capacity, int priority)
{
this.nbtTag = tag;
this.capacity = capacity;
this.priority = priority;
}
@Override
@@ -122,6 +124,12 @@ public class NBTStorage implements IStorage
return (getStored(nbtTag) + stack.stackSize) <= capacity;
}
@Override
public int getPriority()
{
return priority;
}
private StorageItem createItemFromNBT(NBTTagCompound tag)
{
return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_DAMAGE), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);

View File

@@ -4,6 +4,7 @@ import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -103,6 +104,15 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
}
}
storages.sort(new Comparator<IStorage>()
{
@Override
public int compare(IStorage s1, IStorage s2)
{
return (s1.getPriority() > s2.getPriority()) ? -1 : 1;
}
});
syncItems();
}

View File

@@ -1,5 +1,6 @@
package storagecraft.tile;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
@@ -8,8 +9,6 @@ import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
import storagecraft.util.InventoryUtils;
import java.util.List;
public class TileExternalStorage extends TileMachine implements IStorageProvider, IStorage
{
public IInventory getInventory()
@@ -119,6 +118,13 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
return InventoryUtils.canPushToInventory(inventory, stack);
}
@Override
public int getPriority()
{
// @TODO: Priority on this stuff
return 0;
}
@Override
public void addStorages(List<IStorage> storages)
{

View File

@@ -22,12 +22,15 @@ import storagecraft.util.InventoryUtils;
public class TileStorage extends TileMachine implements IStorageProvider, IStorage, IInventory, ISidedInventory
{
public static final String STORAGE_NBT = "Storage";
public static final String NBT_STORAGE = "Storage";
public static final String NBT_PRIORITY = "Priority";
private InventorySimple inventory = new InventorySimple("storage", 9);
private NBTTagCompound tag = NBTStorage.getBaseNBT();
private int priority = 0;
@SideOnly(Side.CLIENT)
private int stored;
@@ -55,9 +58,14 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
InventoryUtils.restoreInventory(this, nbt);
if (nbt.hasKey(STORAGE_NBT))
if (nbt.hasKey(NBT_STORAGE))
{
tag = nbt.getCompoundTag(STORAGE_NBT);
tag = nbt.getCompoundTag(NBT_STORAGE);
}
if (nbt.hasKey(NBT_PRIORITY))
{
priority = nbt.getInteger(NBT_PRIORITY);
}
}
@@ -68,12 +76,13 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
InventoryUtils.saveInventory(inventory, nbt);
nbt.setTag(STORAGE_NBT, tag);
nbt.setTag(NBT_STORAGE, tag);
nbt.setInteger(NBT_PRIORITY, priority);
}
public NBTStorage getStorage()
{
return new NBTStorage(tag, getType().getCapacity());
return new NBTStorage(tag, getType().getCapacity(), priority);
}
public EnumStorageType getType()
@@ -96,12 +105,23 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
return (int) ((float) getStored() / (float) getType().getCapacity() * (float) scale);
}
public int getPriority()
{
return priority;
}
public void setPriority(int priority)
{
this.priority = priority;
}
@Override
public void toBytes(ByteBuf buf)
{
super.toBytes(buf);
buf.writeInt(NBTStorage.getStored(tag));
buf.writeInt(priority);
}
@Override
@@ -110,6 +130,7 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
super.fromBytes(buf);
stored = buf.readInt();
priority = buf.readInt();
}
@Override