From cb676877078a3a1369763754a0cf4000d09898fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Fri, 24 May 2024 19:59:43 +0200 Subject: [PATCH] Add books --- gradle.properties | 2 +- .../server_storage/blocks/StorageBlock.java | 28 +++++++++- .../server_storage/lib/StorageOperations.java | 2 +- .../systems/brn/server_storage/lib/Util.java | 56 +++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/main/java/systems/brn/server_storage/lib/Util.java diff --git a/gradle.properties b/gradle.properties index 2a9773a..f0adf34 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ yarn_mappings=1.20.6+build.3 loader_version=0.15.11 # Mod Properties -mod_version=1.0 +mod_version=1.1 maven_group=systems.brn archives_base_name=Server_storage diff --git a/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java b/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java index 6568d3e..a3fc91a 100644 --- a/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java +++ b/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java @@ -9,10 +9,17 @@ import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.WrittenBookContentComponent; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.RawFilteredPair; +import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; @@ -22,7 +29,11 @@ import net.minecraft.world.World; import systems.brn.server_storage.ServerStorage; import systems.brn.server_storage.screens.StorageScreen; +import java.util.List; + import static systems.brn.server_storage.ServerStorage.STORAGE_BLOCK; +import static systems.brn.server_storage.lib.StorageOperations.getCombinedInventoryFromChests; +import static systems.brn.server_storage.lib.Util.generateBookContent; public class StorageBlock extends Block implements PolymerTexturedBlock { @@ -51,8 +62,21 @@ public class StorageBlock extends Block implements PolymerTexturedBlock { Block block = state.getBlock(); if (block instanceof StorageBlock) { - if (!world.isClient) { - new StorageScreen((ServerPlayerEntity) player, pos).open(); + if (!world.isClient && !player.isSpectator()) { + if (player.isSneaking() && player.getStackInHand(hand).getItem() == Items.WRITTEN_BOOK) { + ItemStack book = player.getStackInHand(hand); + Inventory inventory = getCombinedInventoryFromChests(world, pos); + List> generatedContent = generateBookContent(inventory); + book.set(DataComponentTypes.WRITTEN_BOOK_CONTENT, new WrittenBookContentComponent( + RawFilteredPair.of("Item Listing"), + player.getGameProfile().getName(), + 0, + generatedContent, + false + )); + } else { + new StorageScreen((ServerPlayerEntity) player, pos).open(); + } } return ActionResult.SUCCESS; } diff --git a/src/main/java/systems/brn/server_storage/lib/StorageOperations.java b/src/main/java/systems/brn/server_storage/lib/StorageOperations.java index b1c12b9..c6cd568 100644 --- a/src/main/java/systems/brn/server_storage/lib/StorageOperations.java +++ b/src/main/java/systems/brn/server_storage/lib/StorageOperations.java @@ -167,7 +167,7 @@ public class StorageOperations { // Create a new ItemStack with the item type and the correct count ItemStack stack = new ItemStack(item, count); // Populate the inventory with the item stack - inv.setStack(invPointer, stack); + inv.heldStacks.set(invPointer, stack); invPointer++; } return inv; diff --git a/src/main/java/systems/brn/server_storage/lib/Util.java b/src/main/java/systems/brn/server_storage/lib/Util.java new file mode 100644 index 0000000..22678d5 --- /dev/null +++ b/src/main/java/systems/brn/server_storage/lib/Util.java @@ -0,0 +1,56 @@ +package systems.brn.server_storage.lib; + +import net.minecraft.inventory.Inventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.text.RawFilteredPair; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; + +public class Util { + + public static List> generateBookContent(Inventory inventory) { + List pages = new ArrayList<>(); + for (int i = 0; i < inventory.size(); i++) { + ItemStack slotStack = inventory.getStack(i); + if (!slotStack.isEmpty()) { + Item itemName = slotStack.getItem(); + pages.add(String.valueOf(itemName) + " " + slotStack.getCount()); + } + } + return stringToBookContent(String.join(System.lineSeparator(), pages)); + } + + public static List> stringToBookContent(String string) { + List> pages = new ArrayList<>(); + List lines = new ArrayList<>(); + String[] words = string.split("\\s+"); + + StringBuilder currentLine = new StringBuilder(); + for (String word : words) { + if (currentLine.length() + word.length() + 1 <= 21) { // Max line length in Minecraft + currentLine.append(word).append(" "); + } else { + lines.add(currentLine.toString().trim()); + currentLine = new StringBuilder(word + " "); + } + } + lines.add(currentLine.toString().trim()); + + List pageContent = new ArrayList<>(); + for (int i = 0; i < lines.size(); i++) { + if (i % 14 == 0 && i != 0) { // Max lines per page in Minecraft + pages.add(RawFilteredPair.of(Text.literal(String.join("\n", pageContent)))); + pageContent.clear(); + } + pageContent.add(lines.get(i)); + } + if (!pageContent.isEmpty()) { + pages.add(RawFilteredPair.of(Text.literal(String.join("\n", pageContent)))); + } + + return pages; + } +}