Add right click and store all

This commit is contained in:
Bruno Rybársky 2024-05-26 10:48:45 +02:00
parent a5cd5adb09
commit a821bb0e51
4 changed files with 49 additions and 14 deletions

@ -8,7 +8,7 @@ yarn_mappings=1.20.6+build.3
loader_version=0.15.11 loader_version=0.15.11
# Mod Properties # Mod Properties
mod_version=1.8 mod_version=1.9
maven_group=systems.brn maven_group=systems.brn
archives_base_name=Server_storage archives_base_name=Server_storage

@ -30,7 +30,7 @@ public abstract class PagedGui extends SimpleGui {
public static final String GUI_REFRESH = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDc1ZDNkYjAzZGMyMWU1NjNiMDM0MTk3ZGE0MzViNzllY2ZlZjRiOGUyZWNkYjczMGUzNzBjMzE2NjI5ZDM2ZiJ9fX0="; public static final String GUI_REFRESH = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDc1ZDNkYjAzZGMyMWU1NjNiMDM0MTk3ZGE0MzViNzllY2ZlZjRiOGUyZWNkYjczMGUzNzBjMzE2NjI5ZDM2ZiJ9fX0=";
public static final String GUI_A = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGU0MTc0ODEyMTYyNmYyMmFlMTZhNGM2NjRjNzMwMWE5ZjhlYTU5MWJmNGQyOTg4ODk1NzY4MmE5ZmRhZiJ9fX0="; public static final String GUI_A = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGU0MTc0ODEyMTYyNmYyMmFlMTZhNGM2NjRjNzMwMWE5ZjhlYTU5MWJmNGQyOTg4ODk1NzY4MmE5ZmRhZiJ9fX0=";
public static final String GUI_1 = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2E1MTZmYmFlMTYwNThmMjUxYWVmOWE2OGQzMDc4NTQ5ZjQ4ZjZkNWI2ODNmMTljZjVhMTc0NTIxN2Q3MmNjIn19fQ=="; public static final String GUI_1 = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvY2E1MTZmYmFlMTYwNThmMjUxYWVmOWE2OGQzMDc4NTQ5ZjQ4ZjZkNWI2ODNmMTljZjVhMTc0NTIxN2Q3MmNjIn19fQ==";
public static final String GUI_STORE_ALL = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWFkNmM4MWY4OTlhNzg1ZWNmMjZiZTFkYzQ4ZWFlMmJjZmU3NzdhODYyMzkwZjU3ODVlOTViZDgzYmQxNGQifX19";
public static final int PAGE_SIZE = 9 * 5; public static final int PAGE_SIZE = 9 * 5;
protected final Runnable closeCallback; protected final Runnable closeCallback;
@ -128,6 +128,7 @@ public abstract class PagedGui extends SimpleGui {
this.updateDisplay(); this.updateDisplay();
}) })
); );
case 5 -> this.storeAll();
case 6 -> DisplayElement.nextPage(this); case 6 -> DisplayElement.nextPage(this);
case 8 -> DisplayElement.of( case 8 -> DisplayElement.of(
new GuiElementBuilder(Items.STRUCTURE_VOID) new GuiElementBuilder(Items.STRUCTURE_VOID)
@ -146,6 +147,8 @@ public abstract class PagedGui extends SimpleGui {
protected abstract DisplayElement sorting(); protected abstract DisplayElement sorting();
protected abstract DisplayElement storeAll();
public record DisplayElement(@Nullable GuiElementInterface element, @Nullable Slot slot) { 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 EMPTY = DisplayElement.of(new GuiElement(ItemStack.EMPTY, GuiElementInterface.EMPTY_CALLBACK));

@ -278,7 +278,7 @@ public class StorageOperations {
maxInsert += remainingSpace; maxInsert += remainingSpace;
// If the maximum insertion count is greater than or equal to the item count, return the item count // If the maximum insertion count is greater than or equal to the item count, return the item count
if (maxInsert >= itemStack.getCount()) { if (maxInsert >= itemStack.getCount()) {
return Math.min(itemStack.getCount(), remainingSpace); return Math.min(itemStack.getCount(), maxInsert);
} }
} }
} }
@ -286,7 +286,7 @@ public class StorageOperations {
return maxInsert; // Return the maximum insertion count return maxInsert; // Return the maximum insertion count
} }
private static boolean canCombine(ItemStack stack1, ItemStack stack2) { public static boolean canCombine(ItemStack stack1, ItemStack stack2) {
return !stack1.isEmpty() && stack1.getItem() == stack2.getItem() && ItemStack.areItemsAndComponentsEqual(stack1, stack2); return !stack1.isEmpty() && stack1.getItem() == stack2.getItem() && ItemStack.areItemsAndComponentsEqual(stack1, stack2);
} }
} }

@ -85,19 +85,34 @@ public class StorageScreen extends PagedGui {
if (clickedElement != null) { if (clickedElement != null) {
ItemStack clickedItem = clickedElement.getItemStack(); ItemStack clickedItem = clickedElement.getItemStack();
ItemStack noLoreStack = removeCountFromLore(clickedItem); ItemStack noLoreStack = removeCountFromLore(clickedItem);
if (noLoreStack.getCount() > noLoreStack.getMaxCount() && !type.shift) { if (type.isRight) {
noLoreStack.setCount(noLoreStack.getMaxCount()); if (!type.shift) {
noLoreStack.setCount(Math.min(noLoreStack.getMaxCount(), noLoreStack.getCount() / 2)); //half stack
} else {
for (int i = 0; i < player.getInventory().main.size(); i++) {
ItemStack stack = player.getInventory().main.get(i);
if (ItemStack.areItemsAndComponentsEqual(stack, noLoreStack)) {
insertItem(stack, 0, this.getVirtualSize(), false);
}
}
}
} else if (type.isLeft) {
if (noLoreStack.getCount() > noLoreStack.getMaxCount() && !type.shift) {
noLoreStack.setCount(noLoreStack.getMaxCount()); // if not shift, get one stack
}
} }
int maxToInsert = canInsertItemIntoPlayerInventory(player, noLoreStack); if (!(type.isRight && type.shift)) {
int insertCount = Math.min(maxToInsert, noLoreStack.getCount()); int insertCount = canInsertItemIntoPlayerInventory(player, noLoreStack);
ItemStack insertingStack = noLoreStack.copy(); ItemStack insertingStack = noLoreStack.copy();
insertingStack.setCount(insertCount); insertingStack.setCount(insertCount);
if (canRemoveFromInventory(inventory, noLoreStack) && insertCount > 0) { if (canRemoveFromInventory(inventory, noLoreStack) && insertCount > 0) {
player.getInventory().insertStack(insertingStack.copy()); player.getInventory().insertStack(insertingStack.copy());
removeItemStackFromChests(inventories, insertingStack); removeItemStackFromChests(inventories, insertingStack);
this.updateDisplay();
}
} }
} }
this.updateDisplay();
return false; return false;
} }
@ -140,6 +155,23 @@ public class StorageScreen extends PagedGui {
); );
} }
@Override
protected DisplayElement storeAll() {
return DisplayElement.of(
new GuiElementBuilder(Items.PLAYER_HEAD)
.setName(Text.translatable("gui.all").formatted(Formatting.WHITE))
.hideDefaultTooltip().noDefaults()
.setSkullOwner(GUI_STORE_ALL)
.setCallback((x, y, z) -> {
playClickSound(this.player);
for (int i = 0; i < player.getInventory().main.size(); i++) {
ItemStack stack = player.getInventory().main.get(i);
insertItem(stack, 0, this.getVirtualSize(), false);
}
})
);
}
public void doSearch(String query) { public void doSearch(String query) {
this.query = query; this.query = query;
this.page = 0; this.page = 0;