Add search
This commit is contained in:
parent
390553c591
commit
7aba187f07
@ -8,7 +8,7 @@ yarn_mappings=1.20.6+build.3
|
||||
loader_version=0.15.11
|
||||
|
||||
# Mod Properties
|
||||
mod_version=1.3
|
||||
mod_version=1.4
|
||||
maven_group=systems.brn
|
||||
archives_base_name=Server_storage
|
||||
|
||||
|
@ -26,6 +26,7 @@ public abstract class PagedGui extends SimpleGui {
|
||||
public static final String GUI_PREVIOUS_PAGE_BLOCKED = "ewogICJ0aW1lc3RhbXAiIDogMTY0MDYxNjE5MjE0MiwKICAicHJvZmlsZUlkIiA6ICJmMjc0YzRkNjI1MDQ0ZTQxOGVmYmYwNmM3NWIyMDIxMyIsCiAgInByb2ZpbGVOYW1lIiA6ICJIeXBpZ3NlbCIsCiAgInNpZ25hdHVyZVJlcXVpcmVkIiA6IHRydWUsCiAgInRleHR1cmVzIiA6IHsKICAgICJTS0lOIiA6IHsKICAgICAgInVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS81MDgyMGY3NmUzZTA0MWM3NWY3NmQwZjMwMTIzMmJkZjQ4MzIxYjUzNGZlNmE4NTljY2I4NzNkMjk4MWE5NjIzIiwKICAgICAgIm1ldGFkYXRhIiA6IHsKICAgICAgICAibW9kZWwiIDogInNsaW0iCiAgICAgIH0KICAgIH0KICB9Cn0=";
|
||||
public static final String GUI_NEXT_PAGE = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzg2MTg1YjFkNTE5YWRlNTg1ZjE4NGMzNGYzZjNlMjBiYjY0MWRlYjg3OWU4MTM3OGU0ZWFmMjA5Mjg3In19fQ";
|
||||
public static final String GUI_NEXT_PAGE_BLOCKED = "ewogICJ0aW1lc3RhbXAiIDogMTY0MDYxNjExMDQ4OCwKICAicHJvZmlsZUlkIiA6ICIxZjEyNTNhYTVkYTQ0ZjU5YWU1YWI1NmFhZjRlNTYxNyIsCiAgInByb2ZpbGVOYW1lIiA6ICJOb3RNaUt5IiwKICAic2lnbmF0dXJlUmVxdWlyZWQiIDogdHJ1ZSwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzdlNTc3MjBhNDg3OGM4YmNhYjBlOWM5YzQ3ZDllNTUxMjhjY2Q3N2JhMzQ0NWE1NGE5MWUzZTFlMWEyNzM1NmUiLAogICAgICAibWV0YWRhdGEiIDogewogICAgICAgICJtb2RlbCIgOiAic2xpbSIKICAgICAgfQogICAgfQogIH0KfQ==";
|
||||
public static final String GUI_QUESTION_MARK = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYmM4ZWExZjUxZjI1M2ZmNTE0MmNhMTFhZTQ1MTkzYTRhZDhjM2FiNWU5YzZlZWM4YmE3YTRmY2I3YmFjNDAifX19";
|
||||
|
||||
public static final int PAGE_SIZE = 9 * 5;
|
||||
protected final Runnable closeCallback;
|
||||
@ -112,6 +113,7 @@ public abstract class PagedGui extends SimpleGui {
|
||||
return switch (id) {
|
||||
case 1 -> DisplayElement.previousPage(this);
|
||||
case 3 -> DisplayElement.nextPage(this);
|
||||
case 5 -> this.search();
|
||||
case 7 -> DisplayElement.of(
|
||||
new GuiElementBuilder(Items.STRUCTURE_VOID)
|
||||
.setName(Text.translatable(this.closeCallback != null ? "gui.back" : "mco.selectServer.close").formatted(Formatting.RED))
|
||||
@ -125,6 +127,9 @@ public abstract class PagedGui extends SimpleGui {
|
||||
};
|
||||
}
|
||||
|
||||
protected abstract DisplayElement search();
|
||||
|
||||
|
||||
public record DisplayElement(@Nullable GuiElementInterface element, @Nullable Slot slot) {
|
||||
private static final DisplayElement EMPTY = DisplayElement.of(new GuiElement(ItemStack.EMPTY, GuiElementInterface.EMPTY_CALLBACK));
|
||||
private static final DisplayElement FILLER = DisplayElement.of(
|
||||
@ -198,7 +203,7 @@ public abstract class PagedGui extends SimpleGui {
|
||||
}
|
||||
}
|
||||
|
||||
public static final void playClickSound(ServerPlayerEntity player) {
|
||||
public static void playClickSound(ServerPlayerEntity player) {
|
||||
player.playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 1, 1);
|
||||
}
|
||||
}
|
@ -158,7 +158,7 @@ public class StorageOperations {
|
||||
return getSimpleInventory(size, itemStackMap);
|
||||
}
|
||||
|
||||
private static @NotNull SimpleInventory getSimpleInventory(int size, Map<Item, Integer> itemStackMap) {
|
||||
public static @NotNull SimpleInventory getSimpleInventory(int size, Map<Item, Integer> itemStackMap) {
|
||||
SimpleInventory inv = new SimpleInventory(size);
|
||||
int invPointer = 0;
|
||||
for (Map.Entry<Item, Integer> entry : itemStackMap.entrySet()) {
|
||||
@ -173,6 +173,33 @@ public class StorageOperations {
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static SimpleInventory filterInventory(Inventory inventory, String query) {
|
||||
Map<Item, Integer> itemStackMap = new HashMap<>();
|
||||
int itemCount = 0;
|
||||
for (int slot = 0; slot < inventory.size(); slot++) {
|
||||
ItemStack stack = inventory.getStack(slot);
|
||||
ItemStack filteredStack = filterStack(stack, query);
|
||||
if (!filteredStack.isEmpty()) {
|
||||
itemCount++;
|
||||
// Count the occurrences of each item in the filtered inventory
|
||||
Item item = filteredStack.getItem();
|
||||
itemStackMap.put(item, itemStackMap.getOrDefault(item, 0) + filteredStack.getCount());
|
||||
}
|
||||
}
|
||||
return getSimpleInventory(itemCount, itemStackMap);
|
||||
}
|
||||
|
||||
public static ItemStack filterStack(ItemStack stack, String query) {
|
||||
Item item = stack.getItem();
|
||||
if (item != null) {
|
||||
String itemName = String.valueOf(item);
|
||||
if (itemName != null && query != null && !query.isEmpty() && !itemName.contains(query)) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
private static boolean canCombine(ItemStack stack1, ItemStack stack2) {
|
||||
return !stack1.isEmpty() && stack1.getItem() == stack2.getItem() && ItemStack.areItemsAndComponentsEqual(stack1, stack2);
|
||||
}
|
||||
|
@ -65,11 +65,16 @@ public class Util {
|
||||
LoreComponent lore = stack.get(DataComponentTypes.LORE);
|
||||
Text countLine = Text.literal("Amount: " + count).setStyle(Style.EMPTY.withColor(Formatting.GOLD).withItalic(true));
|
||||
|
||||
assert lore != null;
|
||||
List<Text> newLines = new ArrayList<>(lore.lines());
|
||||
newLines.addFirst(countLine);
|
||||
LoreComponent newLore = new LoreComponent(newLines);
|
||||
|
||||
LoreComponent newLore;
|
||||
if (lore == null) {
|
||||
List<Text> loreList = new ArrayList<>();
|
||||
loreList.add(countLine);
|
||||
newLore = new LoreComponent(loreList);
|
||||
} else {
|
||||
List<Text> newLines = new ArrayList<>(lore.lines());
|
||||
newLines.addFirst(countLine);
|
||||
newLore = new LoreComponent(newLines);
|
||||
}
|
||||
newStack.set(DataComponentTypes.LORE, newLore);
|
||||
return newStack;
|
||||
} else {
|
||||
|
@ -0,0 +1,26 @@
|
||||
package systems.brn.server_storage.screens;
|
||||
|
||||
import eu.pb4.sgui.api.gui.AnvilInputGui;
|
||||
import systems.brn.server_storage.lib.PagedGui;
|
||||
|
||||
public class SearchScreen extends AnvilInputGui {
|
||||
|
||||
private final PagedGui storageScreen;
|
||||
|
||||
public SearchScreen(PagedGui storageScreen, String defaultText) {
|
||||
super(storageScreen.getPlayer(), storageScreen.getLockPlayerInventory());
|
||||
this.storageScreen = storageScreen;
|
||||
storageScreen.close();
|
||||
this.setDefaultInputValue(defaultText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose() {
|
||||
super.onClose();
|
||||
storageScreen.open();
|
||||
String query = this.getInput();
|
||||
if (storageScreen instanceof StorageScreen) {
|
||||
((StorageScreen) storageScreen).doSearch(query);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,40 +5,42 @@ import eu.pb4.sgui.api.elements.GuiElementBuilder;
|
||||
import eu.pb4.sgui.api.elements.GuiElementInterface;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.screen.slot.SlotActionType;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import systems.brn.server_storage.lib.PagedGui;
|
||||
|
||||
import static systems.brn.server_storage.lib.StorageOperations.*;
|
||||
import static systems.brn.server_storage.lib.Util.addCountToLore;
|
||||
import static systems.brn.server_storage.lib.Util.removeCountFromLore;
|
||||
import static systems.brn.server_storage.lib.Util.*;
|
||||
|
||||
public class StorageScreen extends PagedGui {
|
||||
private Inventory inventory;
|
||||
private final BlockPos pos;
|
||||
private final ServerPlayerEntity player;
|
||||
private final World world;
|
||||
private String query;
|
||||
|
||||
public StorageScreen(ServerPlayerEntity player, BlockPos pos) {
|
||||
super(player, null);
|
||||
this.player = player;
|
||||
this.pos = pos;
|
||||
this.world = player.getWorld();
|
||||
|
||||
|
||||
this.setTitle(Text.literal("Networked storage system"));
|
||||
this.setLockPlayerInventory(false);
|
||||
this.updateDisplay();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void updateDisplay() {
|
||||
this.inventory = getCombinedInventoryFromChests(world, pos);
|
||||
super.updateDisplay();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected int getPageAmount() {
|
||||
int sizeX = 9;
|
||||
@ -49,7 +51,8 @@ public class StorageScreen extends PagedGui {
|
||||
@Override
|
||||
protected DisplayElement getElement(int id) {
|
||||
if (this.inventory.size() > id) {
|
||||
ItemStack itemStack = this.inventory.getStack(id);
|
||||
Inventory filteredInventory = filterInventory(this.inventory, query);
|
||||
ItemStack itemStack = filteredInventory.getStack(id);
|
||||
ItemStack newStack = addCountToLore(itemStack.getCount(), itemStack);
|
||||
GuiElementBuilder guiElement = new GuiElementBuilder(newStack);
|
||||
return DisplayElement.of(guiElement);
|
||||
@ -73,10 +76,29 @@ public class StorageScreen extends PagedGui {
|
||||
|
||||
@Override
|
||||
public boolean insertItem(ItemStack stack, int startIndex, int endIndex, boolean fromLast) {
|
||||
if(tryPutItemStackIntoChests(world, pos, stack)){
|
||||
if (tryPutItemStackIntoChests(world, pos, stack)) {
|
||||
removeFromInventory(player.getInventory(), stack, stack.getCount());
|
||||
}
|
||||
this.updateDisplay();
|
||||
return super.insertItem(stack, startIndex, endIndex, fromLast);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DisplayElement search() {
|
||||
return DisplayElement.of(
|
||||
new GuiElementBuilder(Items.PLAYER_HEAD)
|
||||
.setName(Text.translatable("itemGroup.search").formatted(Formatting.WHITE))
|
||||
.hideDefaultTooltip().noDefaults()
|
||||
.setSkullOwner(GUI_QUESTION_MARK)
|
||||
.setCallback((x, y, z) -> {
|
||||
SearchScreen searchScreen = new SearchScreen(this, "Search");
|
||||
playClickSound(this.getPlayer());
|
||||
searchScreen.open();
|
||||
})
|
||||
);
|
||||
}
|
||||
public void doSearch(String query) {
|
||||
this.query = query;
|
||||
this.updateDisplay();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user