SteamWar/SpigotCore
Archiviert
13
0

Add Sorting to SchematicSelector #139

Zusammengeführt
Lixfel hat 2 Commits von selector_sorting nach master 2021-12-11 12:43:04 +01:00 zusammengeführt
2 geänderte Dateien mit 70 neuen und 7 gelöschten Zeilen
Nur Änderungen aus Commit d9812dba11 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -26,6 +26,14 @@ SCHEM_SELECTOR_PUB=
SCHEM_SELECTOR_SEL_DIR=§7Ordner auswählen
SCHEM_SELECTOR_NEW_DIR=§7Neuer Ordner
SCHEM_SELECTOR_FILTER=§7Filter
SCHEM_SELECTOR_SORTING=§7Sortierung
SCHEM_SELECTOR_SORTING_CURRENT=§7Aktuell: §e{0}
SCHEM_SELECTOR_SORTING_NAME=Name
SCHEM_SELECTOR_SORTING_TYPE=Schematic-Typ
SCHEM_SELECTOR_SORTING_UPDATE=Letztes Update
SCHEM_SELECTOR_SORTING_DIRECTION=§7Richtung: §e{0}
SCHEM_SELECTOR_SORTING_ASC=Aufsteigend
SCHEM_SELECTOR_SORTING_DSC=Absteigend
SCHEM_SELECTOR_ITEM_NAME=§e{0}
SCHEM_SELECTOR_ITEM_NAME_FILTER=§7{0}

Datei anzeigen

@ -28,14 +28,13 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
public class SchematicSelector {
private static final Sorting[] all_sortings = Sorting.values();
@Getter
private final Player player;
@Getter
@ -53,6 +52,8 @@ public class SchematicSelector {
@Getter
private boolean singleDirOpen;
private boolean sdoTrigger = false;
private Sorting sorting = Sorting.NAME;
private boolean invertSorting = false;
public SchematicSelector(Player player, SelectorTarget target, Consumer<SchematicNode> callback) {
this.player = player;
@ -76,7 +77,7 @@ public class SchematicSelector {
}
private void openList(SchematicNode parent) {
List<SchematicNode> nodes = filter.isFilter()?getFilteredSchematics():getSchematicList(parent);
List<SchematicNode> nodes = applySorting(filter.isFilter()?getFilteredSchematics():getSchematicList(parent));
if(sdoTrigger) {
sdoTrigger = false;
@ -118,6 +119,17 @@ public class SchematicSelector {
inv.setItem(50, Material.CHEST, Core.MESSAGE.parse("SCHEM_SELECTOR_NEW_DIR", player), clickType -> createFolderIn(parent));
}
inv.setItem(51, Material.NAME_TAG, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER", player), clickType -> openFilter());
inv.setItem(47, sorting.mat, Core.MESSAGE.parse("SCHEM_SELECTOR_SORTING", player), List.of(
Core.MESSAGE.parse("SCHEM_SELECTOR_SORTING_CURRENT", player, sorting.parseName(player)),
Core.MESSAGE.parse("SCHEM_SELECTOR_SORTING_DIRECTION", player, Core.MESSAGE.parse(invertSorting?"SCHEM_SELECTOR_SORTING_DSC":"SCHEM_SELECTOR_SORTING_ASC", player))
), invertSorting, click -> {
if(click.isLeftClick()) {
cycleSorting();
} else {
invertSorting = !invertSorting;
}
openList(parent);
});
injectable.onListRender(this, inv, parent);
inv.open();
@ -142,6 +154,28 @@ public class SchematicSelector {
callback.accept(node);
}
private void cycleSorting() {
int next = sorting.ordinal() + 1;
if(next >= all_sortings.length) {
next = 0;
}
sorting = all_sortings[next];
}
private List<SchematicNode> applySorting(List<SchematicNode> nodes) {
if(sorting == Sorting.NAME && !invertSorting) {
return nodes;
}
Comparator<SchematicNode> comparator = sorting.comparator;
if(invertSorting) {
comparator = comparator.reversed();
}
nodes.sort(comparator);
return nodes;
}
private SWListInv.SWListEntry<SchematicNode> renderItem(SchematicNode node) {
Material m = SWItem.getMaterial(node.getItem());
@ -364,8 +398,8 @@ public class SchematicSelector {
SCHEMATIC_NODE("SCHEM_SELECTOR_SCHEMATIC_NODE", true),
SCHEMATIC_TYPE("SCHEM_SELECTOR_SCHEMATIC", false);
private String rawName;
private boolean dirs;
private final String rawName;
private final boolean dirs;
private String getName(Player player) {
return Core.MESSAGE.parse(rawName, player);
@ -426,4 +460,25 @@ public class SchematicSelector {
PRIVATE_ONLY,
PUBLIC_ONLY
}
@AllArgsConstructor
private enum Sorting {
NAME(Material.PAPER, "SCHEM_SELECTOR_SORTING_NAME", Comparator.comparing(SchematicNode::getName)),
TYPE(Material.CAULDRON, "SCHEM_SELECTOR_SORTING_TYPE", (o1, o2) -> {
if(o1.isDir() || o2.isDir()) {
return Boolean.compare(o1.isDir(), o2.isDir());
} else {
return o1.getSchemtype().name().compareTo(o2.getSchemtype().name());
}
}),
LAST_UPDATED(Material.CLOCK, "SCHEM_SELECTOR_SORTING_UPDATE", Comparator.comparing(SchematicNode::getLastUpdate));
private final Material mat;
private final String name;
private final Comparator<SchematicNode> comparator;
private String parseName(Player player) {
return Core.MESSAGE.parse(name, player);
}
}
}