make grid sorting type / direction serverside
This commit is contained in:
@@ -28,15 +28,6 @@ import storagecraft.tile.TileGrid;
|
||||
|
||||
public class GuiGrid extends GuiBase
|
||||
{
|
||||
public static final int SORTING_DIRECTION_ASCENDING = 0;
|
||||
public static final int SORTING_DIRECTION_DESCENDING = 1;
|
||||
|
||||
public static final int SORTING_TYPE_COUNT = 0;
|
||||
public static final int SORTING_TYPE_NAME = 1;
|
||||
|
||||
public static int SORTING_DIRECTION = SORTING_DIRECTION_ASCENDING;
|
||||
public static int SORTING_TYPE = SORTING_TYPE_COUNT;
|
||||
|
||||
private ContainerGrid container;
|
||||
private TileGrid grid;
|
||||
|
||||
@@ -60,8 +51,8 @@ public class GuiGrid extends GuiBase
|
||||
{
|
||||
addSideButton(new SideButtonRedstoneMode(grid));
|
||||
|
||||
addSideButton(new SideButtonGridSortingDirection());
|
||||
addSideButton(new SideButtonGridSortingType());
|
||||
addSideButton(new SideButtonGridSortingDirection(grid));
|
||||
addSideButton(new SideButtonGridSortingType(grid));
|
||||
|
||||
searchField = new GuiTextField(0, fontRendererObj, x + 80 + 1, y + 6 + 1, 88 - 6, fontRendererObj.FONT_HEIGHT);
|
||||
searchField.setEnableBackgroundDrawing(false);
|
||||
@@ -245,46 +236,45 @@ public class GuiGrid extends GuiBase
|
||||
}
|
||||
}
|
||||
|
||||
switch (SORTING_TYPE)
|
||||
if (grid.getSortingType() == TileGrid.SORTING_TYPE_QUANTITY)
|
||||
{
|
||||
case SORTING_TYPE_COUNT:
|
||||
items.sort(new Comparator<StorageItem>()
|
||||
items.sort(new Comparator<StorageItem>()
|
||||
{
|
||||
@Override
|
||||
public int compare(StorageItem o1, StorageItem o2)
|
||||
{
|
||||
@Override
|
||||
public int compare(StorageItem o1, StorageItem o2)
|
||||
if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING)
|
||||
{
|
||||
switch (SORTING_DIRECTION)
|
||||
{
|
||||
case SORTING_DIRECTION_ASCENDING:
|
||||
return Integer.valueOf(o2.getQuantity()).compareTo(o1.getQuantity());
|
||||
case SORTING_DIRECTION_DESCENDING:
|
||||
return Integer.valueOf(o1.getQuantity()).compareTo(o2.getQuantity());
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return Integer.valueOf(o2.getQuantity()).compareTo(o1.getQuantity());
|
||||
}
|
||||
else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING)
|
||||
{
|
||||
return Integer.valueOf(o1.getQuantity()).compareTo(o2.getQuantity());
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
case SORTING_TYPE_NAME:
|
||||
items.sort(new Comparator<StorageItem>()
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (grid.getSortingType() == TileGrid.SORTING_TYPE_NAME)
|
||||
{
|
||||
items.sort(new Comparator<StorageItem>()
|
||||
{
|
||||
@Override
|
||||
public int compare(StorageItem o1, StorageItem o2)
|
||||
{
|
||||
@Override
|
||||
public int compare(StorageItem o1, StorageItem o2)
|
||||
if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING)
|
||||
{
|
||||
switch (SORTING_DIRECTION)
|
||||
{
|
||||
case SORTING_DIRECTION_ASCENDING:
|
||||
return o2.toItemStack().getDisplayName().compareTo(o1.toItemStack().getDisplayName());
|
||||
case SORTING_DIRECTION_DESCENDING:
|
||||
return o1.toItemStack().getDisplayName().compareTo(o2.toItemStack().getDisplayName());
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return o2.toItemStack().getDisplayName().compareTo(o1.toItemStack().getDisplayName());
|
||||
}
|
||||
else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING)
|
||||
{
|
||||
return o1.toItemStack().getDisplayName().compareTo(o2.toItemStack().getDisplayName());
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return items;
|
||||
|
@@ -1,11 +1,20 @@
|
||||
package storagecraft.gui.sidebutton;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import storagecraft.StorageCraft;
|
||||
import storagecraft.gui.GuiBase;
|
||||
import storagecraft.gui.GuiGrid;
|
||||
import storagecraft.network.MessageGridSortingUpdate;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
public class SideButtonGridSortingDirection extends SideButton
|
||||
{
|
||||
private TileGrid grid;
|
||||
|
||||
public SideButtonGridSortingDirection(TileGrid grid)
|
||||
{
|
||||
this.grid = grid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTooltip(GuiBase gui)
|
||||
{
|
||||
@@ -13,7 +22,7 @@ public class SideButtonGridSortingDirection extends SideButton
|
||||
|
||||
builder.append(EnumChatFormatting.YELLOW).append(gui.t("sidebutton.storagecraft:sorting.direction")).append(EnumChatFormatting.RESET).append("\n");
|
||||
|
||||
builder.append(gui.t("sidebutton.storagecraft:sorting.direction." + GuiGrid.SORTING_DIRECTION));
|
||||
builder.append(gui.t("sidebutton.storagecraft:sorting.direction." + grid.getSortingDirection()));
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
@@ -22,12 +31,23 @@ public class SideButtonGridSortingDirection extends SideButton
|
||||
public void draw(GuiBase gui, int x, int y)
|
||||
{
|
||||
gui.bindTexture("icons.png");
|
||||
gui.drawTexture(x, y + 2 - 1, GuiGrid.SORTING_DIRECTION * 16, 16, 16, 16);
|
||||
gui.drawTexture(x, y + 2 - 1, grid.getSortingDirection() * 16, 16, 16, 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed()
|
||||
{
|
||||
GuiGrid.SORTING_DIRECTION = GuiGrid.SORTING_DIRECTION == GuiGrid.SORTING_DIRECTION_ASCENDING ? GuiGrid.SORTING_DIRECTION_DESCENDING : GuiGrid.SORTING_DIRECTION_ASCENDING;
|
||||
int dir = grid.getSortingDirection();
|
||||
|
||||
if (dir == TileGrid.SORTING_DIRECTION_ASCENDING)
|
||||
{
|
||||
dir = TileGrid.SORTING_DIRECTION_DESCENDING;
|
||||
}
|
||||
else if (dir == TileGrid.SORTING_DIRECTION_DESCENDING)
|
||||
{
|
||||
dir = TileGrid.SORTING_DIRECTION_ASCENDING;
|
||||
}
|
||||
|
||||
StorageCraft.NETWORK.sendToServer(new MessageGridSortingUpdate(grid, dir, grid.getSortingType()));
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,20 @@
|
||||
package storagecraft.gui.sidebutton;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import storagecraft.StorageCraft;
|
||||
import storagecraft.gui.GuiBase;
|
||||
import storagecraft.gui.GuiGrid;
|
||||
import storagecraft.network.MessageGridSortingUpdate;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
public class SideButtonGridSortingType extends SideButton
|
||||
{
|
||||
private TileGrid grid;
|
||||
|
||||
public SideButtonGridSortingType(TileGrid grid)
|
||||
{
|
||||
this.grid = grid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTooltip(GuiBase gui)
|
||||
{
|
||||
@@ -13,7 +22,7 @@ public class SideButtonGridSortingType extends SideButton
|
||||
|
||||
builder.append(EnumChatFormatting.YELLOW).append(gui.t("sidebutton.storagecraft:sorting.type")).append(EnumChatFormatting.RESET).append("\n");
|
||||
|
||||
builder.append(gui.t("sidebutton.storagecraft:sorting.type." + GuiGrid.SORTING_TYPE));
|
||||
builder.append(gui.t("sidebutton.storagecraft:sorting.type." + grid.getSortingType()));
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
@@ -22,12 +31,23 @@ public class SideButtonGridSortingType extends SideButton
|
||||
public void draw(GuiBase gui, int x, int y)
|
||||
{
|
||||
gui.bindTexture("icons.png");
|
||||
gui.drawTexture(x, y + 2, GuiGrid.SORTING_TYPE * 16, 32, 16, 16);
|
||||
gui.drawTexture(x, y + 2 - 1, grid.getSortingType() * 16, 32, 16, 16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed()
|
||||
{
|
||||
GuiGrid.SORTING_TYPE = GuiGrid.SORTING_TYPE == GuiGrid.SORTING_TYPE_COUNT ? GuiGrid.SORTING_TYPE_NAME : GuiGrid.SORTING_TYPE_COUNT;
|
||||
int type = grid.getSortingType();
|
||||
|
||||
if (type == TileGrid.SORTING_TYPE_QUANTITY)
|
||||
{
|
||||
type = TileGrid.SORTING_TYPE_NAME;
|
||||
}
|
||||
else if (type == TileGrid.SORTING_TYPE_NAME)
|
||||
{
|
||||
type = TileGrid.SORTING_TYPE_QUANTITY;
|
||||
}
|
||||
|
||||
StorageCraft.NETWORK.sendToServer(new MessageGridSortingUpdate(grid, grid.getSortingDirection(), type));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,69 @@
|
||||
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.TileGrid;
|
||||
|
||||
public class MessageGridSortingUpdate extends MessageHandlerPlayerToServer<MessageGridSortingUpdate> implements IMessage
|
||||
{
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
private int sortingDirection;
|
||||
private int sortingType;
|
||||
|
||||
public MessageGridSortingUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
public MessageGridSortingUpdate(TileGrid grid, int sortingDirection, int sortingType)
|
||||
{
|
||||
this.x = grid.getPos().getX();
|
||||
this.y = grid.getPos().getY();
|
||||
this.z = grid.getPos().getZ();
|
||||
this.sortingDirection = sortingDirection;
|
||||
this.sortingType = sortingType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf)
|
||||
{
|
||||
x = buf.readInt();
|
||||
y = buf.readInt();
|
||||
z = buf.readInt();
|
||||
sortingDirection = buf.readInt();
|
||||
sortingType = buf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf)
|
||||
{
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
buf.writeInt(sortingDirection);
|
||||
buf.writeInt(sortingType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MessageGridSortingUpdate message, EntityPlayerMP player)
|
||||
{
|
||||
TileEntity tile = player.worldObj.getTileEntity(new BlockPos(message.x, message.y, message.z));
|
||||
|
||||
if (tile instanceof TileGrid)
|
||||
{
|
||||
if (message.sortingDirection == TileGrid.SORTING_DIRECTION_ASCENDING || message.sortingDirection == TileGrid.SORTING_DIRECTION_DESCENDING)
|
||||
{
|
||||
((TileGrid) tile).setSortingDirection(message.sortingDirection);
|
||||
}
|
||||
|
||||
if (message.sortingType == TileGrid.SORTING_TYPE_QUANTITY || message.sortingType == TileGrid.SORTING_TYPE_NAME)
|
||||
{
|
||||
((TileGrid) tile).setSortingType(message.sortingType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -37,6 +37,7 @@ public class CommonProxy
|
||||
StorageCraft.NETWORK.registerMessage(MessageGridCraftingUpdate.class, MessageGridCraftingUpdate.class, 8, Side.CLIENT);
|
||||
StorageCraft.NETWORK.registerMessage(MessageGridCraftingClear.class, MessageGridCraftingClear.class, 9, Side.SERVER);
|
||||
StorageCraft.NETWORK.registerMessage(MessagePriorityUpdate.class, MessagePriorityUpdate.class, 10, Side.SERVER);
|
||||
StorageCraft.NETWORK.registerMessage(MessageGridSortingUpdate.class, MessageGridSortingUpdate.class, 11, Side.SERVER);
|
||||
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler(StorageCraft.INSTANCE, new GuiHandler());
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -17,9 +18,21 @@ import storagecraft.util.InventoryUtils;
|
||||
|
||||
public class TileGrid extends TileMachine
|
||||
{
|
||||
public static final String NBT_SORTING_DIRECTION = "SortingDirection";
|
||||
public static final String NBT_SORTING_TYPE = "SortingType";
|
||||
|
||||
public static final int SORTING_DIRECTION_ASCENDING = 0;
|
||||
public static final int SORTING_DIRECTION_DESCENDING = 1;
|
||||
|
||||
public static final int SORTING_TYPE_QUANTITY = 0;
|
||||
public static final int SORTING_TYPE_NAME = 1;
|
||||
|
||||
private ContainerGridCrafting craftingMatrixContainer = new ContainerGridCrafting(this);
|
||||
private InventoryCrafting craftingMatrix = new InventoryCrafting(craftingMatrixContainer, 3, 3);
|
||||
private InventorySimple craftingResult = new InventorySimple("craftingResult", 1);
|
||||
private InventorySimple craftingResult = new InventorySimple("crafting_result", 1);
|
||||
|
||||
private int sortingDirection = 0;
|
||||
private int sortingType = 0;
|
||||
|
||||
@Override
|
||||
public int getEnergyUsage()
|
||||
@@ -85,12 +98,42 @@ public class TileGrid extends TileMachine
|
||||
return craftingResult;
|
||||
}
|
||||
|
||||
public int getSortingDirection()
|
||||
{
|
||||
return sortingDirection;
|
||||
}
|
||||
|
||||
public void setSortingDirection(int sortingDirection)
|
||||
{
|
||||
this.sortingDirection = sortingDirection;
|
||||
}
|
||||
|
||||
public int getSortingType()
|
||||
{
|
||||
return sortingType;
|
||||
}
|
||||
|
||||
public void setSortingType(int sortingType)
|
||||
{
|
||||
this.sortingType = sortingType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
super.readFromNBT(nbt);
|
||||
|
||||
InventoryUtils.restoreInventory(craftingMatrix, nbt);
|
||||
|
||||
if (nbt.hasKey(NBT_SORTING_DIRECTION))
|
||||
{
|
||||
sortingDirection = nbt.getInteger(NBT_SORTING_DIRECTION);
|
||||
}
|
||||
|
||||
if (nbt.hasKey(NBT_SORTING_TYPE))
|
||||
{
|
||||
sortingType = nbt.getInteger(NBT_SORTING_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -99,6 +142,27 @@ public class TileGrid extends TileMachine
|
||||
super.writeToNBT(nbt);
|
||||
|
||||
InventoryUtils.saveInventory(craftingMatrix, nbt);
|
||||
|
||||
nbt.setInteger(NBT_SORTING_DIRECTION, sortingDirection);
|
||||
nbt.setInteger(NBT_SORTING_TYPE, sortingType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf)
|
||||
{
|
||||
super.toBytes(buf);
|
||||
|
||||
buf.writeInt(sortingDirection);
|
||||
buf.writeInt(sortingType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf)
|
||||
{
|
||||
super.fromBytes(buf);
|
||||
|
||||
sortingDirection = buf.readInt();
|
||||
sortingType = buf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -49,7 +49,7 @@ sidebutton.storagecraft:sorting.direction.0=Ascending
|
||||
sidebutton.storagecraft:sorting.direction.1=Descending
|
||||
|
||||
sidebutton.storagecraft:sorting.type=Sorting Type
|
||||
sidebutton.storagecraft:sorting.type.0=Amount
|
||||
sidebutton.storagecraft:sorting.type.0=Quantity
|
||||
sidebutton.storagecraft:sorting.type.1=Name
|
||||
|
||||
sidebutton.storagecraft:mode=Mode
|
||||
|
Reference in New Issue
Block a user