Skip to content

Commit

Permalink
Implement sorting of items in the terminal
Browse files Browse the repository at this point in the history
Currently you can sort by Alphabetical, amount, and ID
  • Loading branch information
SeanOMik committed Jun 29, 2021
1 parent 351dc77 commit e568d27
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 72 deletions.
61 changes: 6 additions & 55 deletions src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,60 +102,9 @@ public static boolean doesPlayerHaveSystem(UUID uuid) {
public static Map<UUID, List<ESSystem>> getAllSystems() {
Map<UUID, List<ESSystem>> allSystems = new HashMap<>();

for (String playerUUID : getConfig().getConfigurationSection("players").getKeys(false)) {
List<ESSystem> playersSystems = new ArrayList<>();
for (String systemUUID : getConfig().getConfigurationSection("players." + playerUUID + ".systems").getKeys(false)) {
String systemPath = "players." + playerUUID + ".systems." + systemUUID + ".";
List<ESDrive> drives = new ArrayList<>();

if (getConfig().contains(systemPath + "drives")) {
for (String driveUUID : getConfig().getConfigurationSection(systemPath + "drives").getKeys(false)) {

Map<ItemStack, Integer> items = new HashMap();
if (getConfig().contains(systemPath + "drives." + driveUUID + ".items")) {
try {
JSONParser jsonParser = new JSONParser();
JSONArray itemJsonArray = (JSONArray) jsonParser.parse(getConfig().getString(systemPath + "drives." + driveUUID + ".items"));

for (int i = 0; i < itemJsonArray.size(); i++) {
JSONObject itemObject = (JSONObject) itemJsonArray.get(i);

Map.Entry<ItemStack, Integer> item = ItemSerialization.deserializeItem((String) itemObject.get("itemYAML"));

items.put(item.getKey(), item.getValue());
}
} catch (ParseException | InvalidConfigurationException e) {
e.printStackTrace();
}
}

int size = getConfig().getInt(systemPath + "drives." + driveUUID + ".size");

drives.add(new ESDrive(size, items));
}
}

List<UUID> trustedUUIDs = new ArrayList<>();
if (getConfig().contains(systemPath + "trustedUUIDs")) {
try {
JSONArray trustedJson = (JSONArray) new JSONParser().parse(getConfig().getString(systemPath + "trustedUUIDs"));
for (int i = 0; i < trustedJson.size(); i++) {
JSONObject object = (JSONObject) trustedJson.get(i);

trustedUUIDs.add(UUID.fromString((String) object.get("UUID")));
}
} catch (ParseException e) {
e.printStackTrace();
}
}

boolean isPublic = getConfig().getBoolean(systemPath + "public");

Location loc = Utils.convertStringToLocation(getConfig().getString(systemPath + "loc"));
playersSystems.add(new ESSystem(UUID.fromString(playerUUID), UUID.fromString(systemUUID), loc, drives, trustedUUIDs, isPublic));
}

allSystems.put(UUID.fromString(playerUUID), playersSystems);
for (String playerUUIDStr : getConfig().getConfigurationSection("players").getKeys(false)) {
UUID playerUUID = UUID.fromString(playerUUIDStr);
allSystems.put(playerUUID, getPlayersSystems(playerUUID));
}

return allSystems;
Expand Down Expand Up @@ -209,9 +158,10 @@ public static List<ESSystem> getPlayersSystems(UUID uuid) {
}

boolean isPublic = getConfig().getBoolean(systemPath + "public");
ESSystem.SortOrder sortOrder = ESSystem.SortOrder.valueOf(getConfig().getString(systemPath + "sortOrder"));

Location loc = Utils.convertStringToLocation(getConfig().getString(systemPath + "loc"));
systems.add(new ESSystem(uuid, UUID.fromString(systemUUID), loc, drives, trustedUUIDs, isPublic));
systems.add(new ESSystem(uuid, UUID.fromString(systemUUID), loc, drives, trustedUUIDs, isPublic, sortOrder));
}

return systems;
Expand All @@ -222,6 +172,7 @@ public static void savePlayerSystem(ESSystem esSystem) {

getConfig().set(systemPath + "loc", Utils.convertLocationToString(esSystem.getLocation()));
getConfig().set(systemPath + "public", esSystem.isPublic());
getConfig().set(systemPath + "sortOrder", esSystem.getSortOrder().toString());

try {
JSONArray jsonArray = new JSONArray();
Expand Down
60 changes: 57 additions & 3 deletions src/main/java/net/seanomik/energeticstorage/gui/ESTerminalGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -88,6 +89,40 @@ private void initializeItems(Player player, ESSystem openSystem) {
items = openSearches.get(player.getUniqueId());
}

List<ItemStack> sortedKeys = new ArrayList<>(items.keySet());

switch (openSystem.getSortOrder()) {
case ALPHABETICAL:
sortedKeys.sort((i1, i2) -> {
ItemMeta im1 = i1.getItemMeta();
ItemMeta im2 = i2.getItemMeta();

String name1 = im1 == null ? "" : im1.getDisplayName();
if (name1.isEmpty()) {
name1 = i1.getType().name();
}

String name2 = im2 == null ? "" : im2.getDisplayName();
if (name2.isEmpty()) {
name2 = i2.getType().name();
}

return name1.compareTo(name2);
});


break;
case AMOUNT:
Map<ItemStack, Integer> finalItems = items;
sortedKeys.sort((i1, i2) -> {
return finalItems.get(i2).compareTo(finalItems.get(i1));
});
break;
case ID:
sortedKeys.sort(Comparator.comparing(ItemStack::getType));
break;
}

for (int i = 10; i < 44; i++) {
// Ignore the borders
if (i == 18 || i == 27 || i == 36 || i == 17 || i == 26 || i == 35) {
Expand All @@ -102,8 +137,8 @@ private void initializeItems(Player player, ESSystem openSystem) {
int itemIndex = i - (10 + lineIndex * 2) + pageIndex * 28; // The start of a new line is + 2 boxes, with each page showing 28 items.
if (itemIndex < items.size()) {
try {
ItemStack item = (ItemStack) items.keySet().toArray()[itemIndex];
int amount = (int) items.values().toArray()[itemIndex];
ItemStack item = sortedKeys.get(itemIndex);
int amount = items.get(item);

ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasLore()) {
Expand Down Expand Up @@ -133,7 +168,6 @@ private void initializeItems(Player player, ESSystem openSystem) {
inv.clear(i);
}


inv.setItem(45, createGuiItem(Material.IRON_BARS, "Security"));

// Create the lore for the drives
Expand Down Expand Up @@ -169,6 +203,8 @@ private void initializeItems(Player player, ESSystem openSystem) {
lore.add(ChatColor.BLUE + "Filled Items: " + spaceColor + filledSpace + ChatColor.BLUE + "/" + ChatColor.GREEN + maxSpace);
lore.add(ChatColor.BLUE + "Filled Types: " + itemsColor + filledTypes + ChatColor.BLUE + "/" + ChatColor.GREEN + maxTypes);
inv.setItem(46, createGuiItem(Material.CHEST, "Drives", lore));

inv.setItem(47, createGuiItem(Material.HOPPER, "Sort by " + openSystem.getSortOrder().toDisplayString()));
}
}

Expand Down Expand Up @@ -338,6 +374,24 @@ public void onInventoryClick(InventoryClickEvent event) {
Reference.ES_SYSTEM_SECURITY_GUI.openInventory(player, openSystem);
} else if (slot == 46) { // Drives
Reference.ES_DRIVE_GUI.openInventory(player, openSystem);
} else if (slot == 47) { // Sort method
ESSystem.SortOrder sortOrder = openSystem.getSortOrder();

// Change sort order
switch (sortOrder) {
case ID:
sortOrder = ESSystem.SortOrder.ALPHABETICAL;
break;
case AMOUNT:
sortOrder = ESSystem.SortOrder.ID;
break;
case ALPHABETICAL:
sortOrder = ESSystem.SortOrder.AMOUNT;
break;
}

openSystem.setSortOrder(sortOrder);
initializeItems(player, openSystem);
} else {
switch (clickType) {
case SHIFT_IN:
Expand Down
55 changes: 54 additions & 1 deletion src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
import net.seanomik.energeticstorage.utils.Reference;
import net.seanomik.energeticstorage.utils.Utils;
import org.apache.commons.text.StringEscapeUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.*;

public class ESDrive implements Cloneable {
public class ESDrive implements Cloneable, ConfigurationSerializable {
private UUID uuid;
private int size;
private Map<ItemStack, Integer> items = new HashMap<>(); // Item, amount
Expand All @@ -25,6 +31,12 @@ public ESDrive(int size) {
this.size = size;
}

protected ESDrive(UUID uuid, int size, Map<ItemStack, Integer> items) {
this.uuid = uuid;
this.size = size;
this.items = items;
}

public ESDrive(int size, Map<ItemStack, Integer> items) {
this(size);
uuid = UUID.randomUUID();
Expand Down Expand Up @@ -181,4 +193,45 @@ public ItemStack getDriveItem() {

return null;
}

// @TODO: Implement (has not been tested)
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> result = new LinkedHashMap();
result.put("uuid", uuid);
result.put("size", size);

if (!items.isEmpty()) {
List<Object> itemsSerialized = new ArrayList<>();
for (Map.Entry<ItemStack, Integer> entry : items.entrySet()) {
Map<String, Object> itemSerialized = new LinkedHashMap<>();
itemSerialized.put("amount", entry.getValue());
itemSerialized.put("item", entry.getKey().serialize());
itemsSerialized.add(itemSerialized);
}
result.put("items", itemsSerialized);
}

return result;
}

// @TODO: Implement (has not been tested)
@NotNull
public static ESDrive deserialize(@NotNull Map<String, Object> args) {
UUID uuid = (UUID) args.get("uuid");
int size = ((Number)args.get("size")).intValue();
Map<ItemStack, Integer> items = new HashMap<>();

if (args.containsKey("items")) {
Object raw = args.get("items");
if (raw instanceof Map) {
Map<?, ?> map = (Map)raw;

items.put(ItemStack.deserialize((Map<String, Object>) map.get("item")), ((Number)map.get("amount")).intValue());
}
}

return new ESDrive(uuid, size, items);
}
}
Loading

0 comments on commit e568d27

Please sign in to comment.