Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort button to storage scanner #2031

Open
wants to merge 5 commits into
base: 1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
*.ipr
*.iws
eclipse/
.vscode/
.settings/
.project
.classpath
*.launch
out/
bin/
.idea/
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dependencies {
deobfCompile "cofh:RedstoneFlux:${redstoneflux_version}"
deobfCompile "net.darkhax.tesla:Tesla-${tesla_version}"
//deobfCompile "pl.asie.charset:charset:0.5.0.164"
compile "team.chisel.ctm:CTM:MC1.12-0.2.3.12"
compile "team.chisel.ctm:CTM:MC1.12.2-1.0.1.30"

deobfCompile "com.github.mcjty:intwheel:${intwheel_version}"
if (!project.hasProperty("singleproject")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import mcjty.lib.varia.Logging;
import mcjty.rftools.setup.CommandHandler;
import mcjty.rftools.RFTools;
import mcjty.rftools.blocks.storage.sorters.CountItemSorter;
import mcjty.rftools.blocks.storage.sorters.ItemSorter;
import mcjty.rftools.blocks.storage.sorters.NameItemSorter;
import mcjty.rftools.craftinggrid.GuiCraftingGrid;
import mcjty.rftools.setup.GuiProxy;
import mcjty.rftools.network.RFToolsMessages;
Expand Down Expand Up @@ -45,6 +48,8 @@ public class GuiStorageScanner extends GenericGuiContainer<StorageScannerTileEnt
private static final ResourceLocation iconLocation = new ResourceLocation(RFTools.MODID, "textures/gui/storagescanner.png");
private static final ResourceLocation guielements = new ResourceLocation(RFTools.MODID, "textures/gui/guielements.png");

private static final ItemSorter[] itemSorters = {new CountItemSorter(), new NameItemSorter()};

private WidgetList storageList;
private WidgetList itemList;
private ToggleButton openViewButton;
Expand All @@ -55,6 +60,7 @@ public class GuiStorageScanner extends GenericGuiContainer<StorageScannerTileEnt
private Button bottomButton;
private Button removeButton;
private TextField searchField;
private ImageChoiceLabel sortMode;
private ImageChoiceLabel exportToStarred;
private Panel storagePanel;
private Panel itemPanel;
Expand Down Expand Up @@ -155,11 +161,20 @@ public void initGui() {
fromServer_foundInventories.clear();
startSearch(newText);
});
sortMode = new ImageChoiceLabel(mc, this)
.setTooltips("Control how items are sorted", "in the view")
.setDesiredWidth(16)
.setDesiredHeight(16)
.addChoiceEvent((parent, newChoice) -> updateSortMode());
for (ItemSorter sorter : itemSorters) {
sortMode.addChoice(sorter.getName(), sorter.getTooltip(), guielements, sorter.getU(), sorter.getV());
}
Panel searchPanel = new Panel(mc, this)
.setLayoutHint(new PositionalLayout.PositionalHint(8, 142, 256 - 11, 18))
.setLayout(new HorizontalLayout()).setDesiredHeight(18)
.addChild(new Label(mc, this).setText("Search:"))
.addChild(searchField);
.addChild(searchField)
.addChild(sortMode);

Slider radiusSlider = new Slider(mc, this)
.setHorizontal()
Expand Down Expand Up @@ -272,6 +287,14 @@ private void toggleView() {
.put(PARAM_VIEW, openViewButton.isPressed())
.build());
}

private void updateSortMode() {
tileEntity.setSortMode(sortMode.getCurrentChoice());
sendServerCommand(RFToolsMessages.INSTANCE, StorageScannerTileEntity.CMD_UPDATESORTMODE,
TypedMap.builder()
.put(PARAM_SORTMODE, sortMode.getCurrentChoice())
.build());
}

@Override
protected void mouseClicked(int x, int y, int button) throws IOException {
Expand Down Expand Up @@ -412,8 +435,9 @@ private void updateContentsList() {
int numcolumns = openViewButton.isPressed() ? 5 : 9;
int spacing = 3;

// Collections.sort(fromServer_inventory, (o1, o2) -> o1.stackSize == o2.stackSize ? 0 : o1.stackSize < o2.stackSize ? -1 : 1);
Collections.sort(fromServer_inventory, Comparator.comparing(ItemStack::getDisplayName));
ItemSorter sorter = getCurrentSorter();
Comparator<Pair<ItemStack, Integer>> comparator = sorter.getComparator();
Collections.sort(fromServer_inventory, (l, r) -> comparator.compare(Pair.of(l, 0), Pair.of(r, 0)));

String filterText = searchField.getText().toLowerCase();
Predicate<ItemStack> matcher = StorageScannerTileEntity.getMatcher(filterText);
Expand All @@ -425,6 +449,22 @@ private void updateContentsList() {
}
}
}

private ItemSorter getCurrentSorter() {
String sortName = sortMode.getCurrentChoice();
sortMode.clear();

for (ItemSorter sorter : itemSorters) {
sortMode.addChoice(sorter.getName(), sorter.getTooltip(), guielements, sorter.getU(), sorter.getV());
}

int sort = sortMode.findChoice(sortName);
if (sort == -1) {
sort = 0;
}
sortMode.setCurrentChoice(sort);
return itemSorters[sort];
}

private Pair<Panel, Integer> addItemToList(ItemStack item, WidgetList itemList, Pair<Panel, Integer> currentPos, int numcolumns, int spacing) {
Panel panel = currentPos.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public class StorageScannerTileEntity extends GenericEnergyReceiverTileEntity im
public static final String CMD_REMOVE = "scanner.remove";
public static final String CMD_TOGGLEROUTABLE = "scanner.toggleRoutable";
public static final String CMD_SETVIEW = "scanner.setView";
public static final String CMD_UPDATESORTMODE = "scanner.updateSortMode";

public static final Key<Integer> PARAM_INDEX = new Key<>("index", Type.INTEGER);
public static final Key<BlockPos> PARAM_POS = new Key<>("pos", Type.BLOCKPOS);
public static final Key<Boolean> PARAM_VIEW = new Key<>("view", Type.BOOLEAN);
public static final Key<String> PARAM_SORTMODE = new Key<>("sortmode", Type.STRING);

public static final String ACTION_CLEARGRID = "clearGrid";

Expand All @@ -74,6 +76,8 @@ public class StorageScannerTileEntity extends GenericEnergyReceiverTileEntity im
// Client side data returned by CMD_SCANNER_INFO
public static long rfReceived = 0;
public static boolean exportToCurrentReceived = false;

private String sortMode = "";

@Override
public IAction[] getActions() {
Expand Down Expand Up @@ -479,7 +483,7 @@ private static Predicate<ItemStack> makeSearchPredicate(String split) {
return s -> s.getDisplayName().toLowerCase().contains(split);
}
}

public int getRadius() {
return radius;
}
Expand Down Expand Up @@ -1030,6 +1034,7 @@ public void readRestorableFromNBT(NBTTagCompound tagCompound) {
readBufferFromNBT(tagCompound, inventoryHelper);
radius = tagCompound.getInteger("radius");
exportToCurrent = tagCompound.getBoolean("exportC");
sortMode = tagCompound.getString("sortMode");
if (tagCompound.hasKey("wideview")) {
openWideView = tagCompound.getBoolean("wideview");
} else {
Expand Down Expand Up @@ -1069,6 +1074,7 @@ public void writeRestorableToNBT(NBTTagCompound tagCompound) {
tagCompound.setInteger("radius", radius);
tagCompound.setBoolean("exportC", exportToCurrent);
tagCompound.setBoolean("wideview", openWideView);
tagCompound.setString("sortMode", sortMode);
tagCompound.setTag("grid", craftingGrid.writeToNBT());
}

Expand Down Expand Up @@ -1107,6 +1113,9 @@ public boolean execute(EntityPlayerMP playerMP, String command, TypedMap params)
} else if (CMD_SETVIEW.equals(command)) {
setOpenWideView(params.get(PARAM_VIEW));
return true;
} else if (CMD_UPDATESORTMODE.equals(command)) {
setSortMode(params.get(PARAM_SORTMODE));
return true;
}
return false;
}
Expand Down Expand Up @@ -1225,4 +1234,13 @@ public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direct
public int[] getSlotsForFace(EnumFacing side) {
return SLOTS;
}

public String getSortMode() {
return sortMode;
}

public void setSortMode(String sortMode) {
this.sortMode = sortMode;
markDirty();
}
}