Add books

This commit is contained in:
Bruno Rybársky 2024-05-24 19:59:43 +02:00
parent 3301cf2d8b
commit cb67687707
4 changed files with 84 additions and 4 deletions

@ -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

@ -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,9 +62,22 @@ public class StorageBlock extends Block implements PolymerTexturedBlock {
Block block = state.getBlock();
if (block instanceof StorageBlock) {
if (!world.isClient) {
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<RawFilteredPair<Text>> 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;
}
return ActionResult.PASS;

@ -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;

@ -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<RawFilteredPair<Text>> generateBookContent(Inventory inventory) {
List<String> 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<RawFilteredPair<Text>> stringToBookContent(String string) {
List<RawFilteredPair<Text>> pages = new ArrayList<>();
List<String> 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<String> 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;
}
}