From 7f8d4b8eb3e8b370e8697c3c81a34c51b397a024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Thu, 27 Jun 2024 08:14:58 +0200 Subject: [PATCH] Fix 2 --- .../brn/server_storage/ServerStorage.java | 16 ++-- .../HardDriveContainerBlockEntity.java | 59 ++++++++++++- .../blocks/HardDriveContainerBlock.java | 35 ++++++-- .../server_storage/blocks/StorageBlock.java | 29 ++++++- .../server_storage/items/HardDriveItem.java | 39 +++++++++ .../server_storage/items/SimpleBlockItem.java | 2 +- .../brn/server_storage/items/SimpleItem.java | 26 +++++- .../brn/server_storage/items/TieredItem.java | 31 ------- .../server_storage/lib/ConnectedChests.java | 23 ++++- .../server_storage/lib/DriveComponents.java | 42 ++++++++++ .../brn/server_storage/lib/HardDriveSlot.java | 24 ++++++ .../DriveContainerScreenHandler.java | 84 +++++++++++++++++++ .../blockstates/drive_container.json | 22 ++++- .../serverstorage/blockstates/storage.json | 21 ++++- .../blocks/drive_container.json | 0 .../blocks/storage.json | 0 .../{recipes => recipe}/diamond_drive.json | 0 .../{recipes => recipe}/diamond_head.json | 0 .../{recipes => recipe}/diamond_platter.json | 0 .../{recipes => recipe}/drive_casing.json | 0 .../{recipes => recipe}/drive_container.json | 0 .../{recipes => recipe}/golden_drive.json | 0 .../{recipes => recipe}/golden_head.json | 0 .../{recipes => recipe}/golden_platter.json | 0 .../{recipes => recipe}/iron_drive.json | 0 .../{recipes => recipe}/iron_head.json | 0 .../{recipes => recipe}/iron_platter.json | 0 .../{recipes => recipe}/netherite_drive.json | 0 .../{recipes => recipe}/netherite_head.json | 0 .../netherite_platter.json | 0 .../{recipes => recipe}/storage.json | 0 31 files changed, 400 insertions(+), 53 deletions(-) create mode 100644 src/main/java/systems/brn/server_storage/items/HardDriveItem.java delete mode 100644 src/main/java/systems/brn/server_storage/items/TieredItem.java create mode 100644 src/main/java/systems/brn/server_storage/lib/DriveComponents.java create mode 100644 src/main/java/systems/brn/server_storage/lib/HardDriveSlot.java create mode 100644 src/main/java/systems/brn/server_storage/screenhandlers/DriveContainerScreenHandler.java rename src/main/resources/data/serverstorage/{loot_tables => loot_table}/blocks/drive_container.json (100%) rename src/main/resources/data/serverstorage/{loot_tables => loot_table}/blocks/storage.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/diamond_drive.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/diamond_head.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/diamond_platter.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/drive_casing.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/drive_container.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/golden_drive.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/golden_head.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/golden_platter.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/iron_drive.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/iron_head.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/iron_platter.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/netherite_drive.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/netherite_head.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/netherite_platter.json (100%) rename src/main/resources/data/serverstorage/{recipes => recipe}/storage.json (100%) diff --git a/src/main/java/systems/brn/server_storage/ServerStorage.java b/src/main/java/systems/brn/server_storage/ServerStorage.java index 651bbe4..b5f7377 100644 --- a/src/main/java/systems/brn/server_storage/ServerStorage.java +++ b/src/main/java/systems/brn/server_storage/ServerStorage.java @@ -5,15 +5,16 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.item.Item; import net.minecraft.util.Identifier; import net.minecraft.world.GameRules; import systems.brn.server_storage.blockentities.HardDriveContainerBlockEntity; import systems.brn.server_storage.blockentities.StorageBlockEntity; import systems.brn.server_storage.blocks.HardDriveContainerBlock; import systems.brn.server_storage.blocks.StorageBlock; +import systems.brn.server_storage.items.HardDriveItem; import systems.brn.server_storage.items.SimpleBlockItem; import systems.brn.server_storage.items.SimpleItem; -import systems.brn.server_storage.items.TieredItem; import java.util.Arrays; import java.util.List; @@ -32,6 +33,11 @@ public class ServerStorage implements ModInitializer { public static StorageBlock STORAGE_BLOCK; public static BlockEntityType STORAGE_BLOCK_ENTITY; + public static Item DRIVE_CASING; + public static List PLATTERS; + public static List DRIVES; + public static List HEADS; + public static final GameRules.Key ServerStorage_Crafting_Enable = GameRuleRegistry.register("enableserverstoragecrafting", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(false)); @@ -49,10 +55,10 @@ public class ServerStorage implements ModInitializer { HardDriveContainerBlock.register(); SimpleBlockItem.register(HARD_DRIVE_CONTAINER_BLOCK); - TieredItem.register("drive"); - TieredItem.register("head"); - TieredItem.register("platter"); - SimpleItem.register("drive_casing"); + HEADS = SimpleItem.register("head", tiers); + PLATTERS = SimpleItem.register("platter", tiers); + DRIVE_CASING = SimpleItem.register("drive_casing"); + DRIVES = HardDriveItem.register(tiers); PolymerResourcePackUtils.addModAssets(MOD_ID); PolymerResourcePackUtils.markAsRequired(); diff --git a/src/main/java/systems/brn/server_storage/blockentities/HardDriveContainerBlockEntity.java b/src/main/java/systems/brn/server_storage/blockentities/HardDriveContainerBlockEntity.java index 19d1ae3..cb35b82 100644 --- a/src/main/java/systems/brn/server_storage/blockentities/HardDriveContainerBlockEntity.java +++ b/src/main/java/systems/brn/server_storage/blockentities/HardDriveContainerBlockEntity.java @@ -1,13 +1,68 @@ package systems.brn.server_storage.blockentities; import net.minecraft.block.BlockState; -import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.LootableContainerBlockEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventories; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.RegistryWrapper; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.text.Text; +import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; +import systems.brn.server_storage.screenhandlers.DriveContainerScreenHandler; import static systems.brn.server_storage.ServerStorage.HARD_DRIVE_CONTAINER_BLOCK_ENTITY; -public class HardDriveContainerBlockEntity extends BlockEntity { +public class HardDriveContainerBlockEntity extends LootableContainerBlockEntity { + private DefaultedList inventory; + private static final int INVENTORY_SIZE = 9; + public HardDriveContainerBlockEntity(BlockPos pos, BlockState state) { super(HARD_DRIVE_CONTAINER_BLOCK_ENTITY, pos, state); + this.inventory = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY); + } + + @Override + protected Text getContainerName() { + return Text.translatable("block.serverstorage.drive_container"); + } + + @Override + protected DefaultedList getHeldStacks() { + return inventory; + } + + @Override + protected void setHeldStacks(DefaultedList inventory) { + this.inventory = inventory; + } + + @Override + protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) { + return new DriveContainerScreenHandler(syncId, playerInventory, this); + } + + @Override + public int size() { + return INVENTORY_SIZE; + } + + @Override + protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { + super.readNbt(nbt, registryLookup); + this.inventory = DefaultedList.ofSize(this.size(), ItemStack.EMPTY); + if (!this.readLootTable(nbt)) { + Inventories.readNbt(nbt, this.inventory, registryLookup); + } + } + + @Override + protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { + super.writeNbt(nbt, registryLookup); + if (!this.writeLootTable(nbt)) { + Inventories.writeNbt(nbt, this.inventory, registryLookup); + } } } diff --git a/src/main/java/systems/brn/server_storage/blocks/HardDriveContainerBlock.java b/src/main/java/systems/brn/server_storage/blocks/HardDriveContainerBlock.java index 3a28fba..d56c7c1 100644 --- a/src/main/java/systems/brn/server_storage/blocks/HardDriveContainerBlock.java +++ b/src/main/java/systems/brn/server_storage/blocks/HardDriveContainerBlock.java @@ -11,33 +11,50 @@ import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemPlacementContext; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; -import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.DirectionProperty; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; +import net.minecraft.util.ItemScatterer; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; import systems.brn.server_storage.blockentities.HardDriveContainerBlockEntity; -import systems.brn.server_storage.screens.DriveScreen; import static systems.brn.server_storage.ServerStorage.*; public class HardDriveContainerBlock extends SimplePolymerBlock implements PolymerTexturedBlock, BlockEntityProvider { final Identifier identifier; + public static final DirectionProperty FACING = FacingBlock.FACING; + private final BlockState polymerBlockState; public HardDriveContainerBlock(AbstractBlock.Settings settings, Identifier identifier) { super(settings, Blocks.NOTE_BLOCK); this.identifier = identifier; + this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH)); + this.polymerBlockState = PolymerBlockResourceUtils.requestBlock(BlockModelType.FULL_BLOCK, PolymerBlockModel.of(identifier.withPath("block/" + identifier.getPath()))); + } + + @Override + public BlockState getPlacementState(ItemPlacementContext ctx) { + return this.getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing().getOpposite()); + } + + @Override + protected void appendProperties(StateManager.Builder builder) { + builder.add(FACING); } @Override public BlockState getPolymerBlockState(BlockState state) { - return PolymerBlockResourceUtils.requestBlock(BlockModelType.FULL_BLOCK, PolymerBlockModel.of(identifier)); + return this.polymerBlockState; } public static void register() { @@ -62,8 +79,10 @@ public class HardDriveContainerBlock extends SimplePolymerBlock implements Polym if (block instanceof HardDriveContainerBlock) { if (!world.isClient && !player.isSpectator()) { if (!player.isSneaking()) { - DriveScreen driveScreen = new DriveScreen((ServerPlayerEntity) player, pos); - driveScreen.open(); + BlockEntity storageBlockEntity = world.getBlockEntity(pos); + if (storageBlockEntity instanceof HardDriveContainerBlockEntity) { + player.openHandledScreen((HardDriveContainerBlockEntity) storageBlockEntity); + } } else { return ActionResult.PASS; @@ -79,4 +98,10 @@ public class HardDriveContainerBlock extends SimplePolymerBlock implements Polym public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return new HardDriveContainerBlockEntity(pos, state); } + + @Override + protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { + ItemScatterer.onStateReplaced(state, newState, world, pos); + super.onStateReplaced(state, world, pos, newState, moved); + } } 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 7729e5e..72b3c69 100644 --- a/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java +++ b/src/main/java/systems/brn/server_storage/blocks/StorageBlock.java @@ -4,18 +4,23 @@ import eu.pb4.polymer.blocks.api.BlockModelType; import eu.pb4.polymer.blocks.api.PolymerBlockModel; import eu.pb4.polymer.blocks.api.PolymerBlockResourceUtils; import eu.pb4.polymer.blocks.api.PolymerTexturedBlock; +import eu.pb4.polymer.core.api.block.PolymerBlockUtils; import eu.pb4.polymer.core.api.block.SimplePolymerBlock; import net.fabricmc.fabric.api.event.player.UseBlockCallback; import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.component.DataComponentTypes; import net.minecraft.component.type.WrittenBookContentComponent; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemPlacementContext; 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.state.StateManager; +import net.minecraft.state.property.DirectionProperty; import net.minecraft.text.RawFilteredPair; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; @@ -23,6 +28,7 @@ import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; import systems.brn.server_storage.blockentities.StorageBlockEntity; @@ -35,15 +41,29 @@ import static systems.brn.server_storage.lib.Util.generateBookContent; public class StorageBlock extends SimplePolymerBlock implements PolymerTexturedBlock, BlockEntityProvider { final Identifier identifier; + public static final DirectionProperty FACING = FacingBlock.FACING; + private final BlockState polymerBlockState; public StorageBlock(Settings settings, Identifier identifier) { super(settings, Blocks.NOTE_BLOCK); this.identifier = identifier; + this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH)); + this.polymerBlockState = PolymerBlockResourceUtils.requestBlock(BlockModelType.FULL_BLOCK, PolymerBlockModel.of(identifier.withPath("block/" + identifier.getPath()))); + } + + @Override + public BlockState getPlacementState(ItemPlacementContext ctx) { + return this.getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing().getOpposite()); + } + + @Override + protected void appendProperties(StateManager.Builder builder) { + builder.add(FACING); } @Override public BlockState getPolymerBlockState(BlockState state) { - return PolymerBlockResourceUtils.requestBlock(BlockModelType.FULL_BLOCK, PolymerBlockModel.of(identifier)); + return this.polymerBlockState; } public static void register() { @@ -51,6 +71,13 @@ public class StorageBlock extends SimplePolymerBlock implements PolymerTexturedB STORAGE_BLOCK = Registry.register(Registries.BLOCK, modId, new StorageBlock(AbstractBlock.Settings.copy(Blocks.WHITE_WOOL), modId)); UseBlockCallback.EVENT.register(StorageBlock::onUse); + + STORAGE_BLOCK_ENTITY = Registry.register( + Registries.BLOCK_ENTITY_TYPE, + modId, + BlockEntityType.Builder.create(StorageBlockEntity::new, STORAGE_BLOCK).build(null) + ); + PolymerBlockUtils.registerBlockEntity(STORAGE_BLOCK_ENTITY); } private static ActionResult onUse(PlayerEntity player, World world, Hand hand, BlockHitResult hitResult) { diff --git a/src/main/java/systems/brn/server_storage/items/HardDriveItem.java b/src/main/java/systems/brn/server_storage/items/HardDriveItem.java new file mode 100644 index 0000000..aa097cf --- /dev/null +++ b/src/main/java/systems/brn/server_storage/items/HardDriveItem.java @@ -0,0 +1,39 @@ +package systems.brn.server_storage.items; + +import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroups; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.util.Identifier; +import systems.brn.server_storage.lib.DriveComponents; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static systems.brn.server_storage.ServerStorage.id; + +public class HardDriveItem extends SimpleItem { + public HardDriveItem(Settings settings, Identifier identifier) { + super(settings, identifier); + } + + public static List register(List tiers){ + ArrayList items = new ArrayList<>(); + int numberItems = 128; + for (String tier : tiers) { + Identifier identifier = id(tier + "_drive"); + Item item = Registry.register(Registries.ITEM, identifier, new SimpleItem(new Settings() + .maxCount(1) + .component(DriveComponents.MAX_ITEMS, numberItems) + .component(DriveComponents.USED_ITEMS, 0) + .component(DriveComponents.ITEMSTACK_MAP, new HashMap<>()) + , identifier)); + ItemGroupEvents.modifyEntriesEvent(ItemGroups.FUNCTIONAL).register(content -> content.add(item)); + items.add(item); + numberItems *= 2; + } + return items; + } +} diff --git a/src/main/java/systems/brn/server_storage/items/SimpleBlockItem.java b/src/main/java/systems/brn/server_storage/items/SimpleBlockItem.java index a7ec692..197e497 100644 --- a/src/main/java/systems/brn/server_storage/items/SimpleBlockItem.java +++ b/src/main/java/systems/brn/server_storage/items/SimpleBlockItem.java @@ -23,7 +23,7 @@ public class SimpleBlockItem extends PolymerBlockItem implements PolymerItem { public SimpleBlockItem(Item.Settings settings, Block block, Identifier identifier) { super(block, settings, Items.BARRIER); - this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier); + this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier.withPath("item/" + identifier.getPath())); } @Override diff --git a/src/main/java/systems/brn/server_storage/items/SimpleItem.java b/src/main/java/systems/brn/server_storage/items/SimpleItem.java index 8e5114d..24c4fff 100644 --- a/src/main/java/systems/brn/server_storage/items/SimpleItem.java +++ b/src/main/java/systems/brn/server_storage/items/SimpleItem.java @@ -15,6 +15,9 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; + import static systems.brn.server_storage.ServerStorage.id; public class SimpleItem extends SimplePolymerItem implements PolymerItem { @@ -22,7 +25,7 @@ public class SimpleItem extends SimplePolymerItem implements PolymerItem { public SimpleItem(Settings settings, Identifier identifier) { super(settings, Items.BARRIER); - this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier); + this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier.withPath("item/" + identifier.getPath())); } @Override @@ -35,9 +38,26 @@ public class SimpleItem extends SimplePolymerItem implements PolymerItem { return this.polymerModel.value(); } - public static void register(String name) { + public static Item register(String name){ + return register(name, 64); + } + + public static Item register(String name, int maxCount) { Identifier identifier = id(name); - Item item = Registry.register(Registries.ITEM, identifier, new SimpleItem(new Settings(), identifier)); + Item item = Registry.register(Registries.ITEM, identifier, new SimpleItem(new Settings().maxCount(maxCount), identifier)); ItemGroupEvents.modifyEntriesEvent(ItemGroups.FUNCTIONAL).register(content -> content.add(item)); + return item; + } + + public static List register(String name, List tiers) { + return register(name, tiers, 64); + } + + public static List register(String name, List tiers, int maxCount) { + ArrayList items = new ArrayList<>(); + for (String tier : tiers) { + items.add(SimpleItem.register(tier + "_" + name, maxCount)); + } + return items; } } diff --git a/src/main/java/systems/brn/server_storage/items/TieredItem.java b/src/main/java/systems/brn/server_storage/items/TieredItem.java deleted file mode 100644 index 7597db3..0000000 --- a/src/main/java/systems/brn/server_storage/items/TieredItem.java +++ /dev/null @@ -1,31 +0,0 @@ -package systems.brn.server_storage.items; - -import eu.pb4.polymer.core.api.item.PolymerItem; -import eu.pb4.polymer.resourcepack.api.PolymerModelData; -import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils; -import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; -import net.minecraft.item.Item; -import net.minecraft.item.ItemGroups; -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.util.Identifier; -import org.jetbrains.annotations.Nullable; - -import static systems.brn.server_storage.ServerStorage.id; -import static systems.brn.server_storage.ServerStorage.tiers; - -public class TieredItem extends SimpleItem implements PolymerItem { - - public TieredItem(Item.Settings settings, Identifier identifier) { - super(settings, identifier); - } - - public static void register(String name) { - for (String tier : tiers) { - SimpleItem.register(tier + "_" + name); - } - } -} diff --git a/src/main/java/systems/brn/server_storage/lib/ConnectedChests.java b/src/main/java/systems/brn/server_storage/lib/ConnectedChests.java index 7430c41..70eacac 100644 --- a/src/main/java/systems/brn/server_storage/lib/ConnectedChests.java +++ b/src/main/java/systems/brn/server_storage/lib/ConnectedChests.java @@ -7,7 +7,9 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.World; +import systems.brn.server_storage.blockentities.HardDriveContainerBlockEntity; import systems.brn.server_storage.blockentities.StorageBlockEntity; +import systems.brn.server_storage.items.HardDriveItem; import java.util.*; @@ -16,6 +18,8 @@ import static systems.brn.server_storage.lib.StorageOperations.*; public class ConnectedChests { public final List inventories; public final List hoppers; + public final List driveContainers; + public final List drives; public final Inventory inventory; public final int containerCount; @@ -26,12 +30,16 @@ public class ConnectedChests { public ConnectedChests(World world, BlockPos startPos, boolean sortAlphabetically, String searchString, boolean allInventories) { List inventories = new ArrayList<>(); List hoppers = new ArrayList<>(); + List driveContainers = new ArrayList<>(); + List drives = new ArrayList<>(); Set visited = new HashSet<>(); - getConnectedChestsHelper(world, startPos, inventories, hoppers, visited, allInventories); + getConnectedChestsHelper(world, startPos, inventories, hoppers, driveContainers, drives, visited, allInventories); this.inventories = inventories; this.hoppers = hoppers; + this.driveContainers = driveContainers; + this.drives = drives; this.containerCount = inventories.size(); this.inventory = getCombinedInventory(sortAlphabetically, searchString); @@ -61,7 +69,7 @@ public class ConnectedChests { blockEntity instanceof ShulkerBoxBlockEntity; } - private static void getConnectedChestsHelper(World world, BlockPos pos, List inventories, List hoppers, Set visited, Boolean allContainers) { + private static void getConnectedChestsHelper(World world, BlockPos pos, List inventories, List hoppers, List driveContainers, List drives, Set visited, Boolean allContainers) { if (visited.contains(pos)) { return; } @@ -69,6 +77,15 @@ public class ConnectedChests { BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof HardDriveContainerBlockEntity) { + driveContainers.add((Inventory) blockEntity); + //loop over all items in inventory + for (int slot = 0; slot < inventories.size(); slot++) { + ItemStack slotStack = ((Inventory) blockEntity).getStack(slot); + //drives.add(new HardDriveItem(slotStack, (HardDriveContainerBlockEntity) blockEntity)); + } + } + if (isEnabledHopper(blockEntity)) { hoppers.add((Inventory) blockEntity); } @@ -80,7 +97,7 @@ public class ConnectedChests { for (Direction direction : Direction.values()) { BlockPos nextPos = pos.offset(direction); - getConnectedChestsHelper(world, nextPos, inventories, hoppers, visited, allContainers); + getConnectedChestsHelper(world, nextPos, inventories, hoppers, driveContainers, drives, visited, allContainers); } } } diff --git a/src/main/java/systems/brn/server_storage/lib/DriveComponents.java b/src/main/java/systems/brn/server_storage/lib/DriveComponents.java new file mode 100644 index 0000000..4351788 --- /dev/null +++ b/src/main/java/systems/brn/server_storage/lib/DriveComponents.java @@ -0,0 +1,42 @@ +package systems.brn.server_storage.lib; + +import com.mojang.serialization.Codec; +import eu.pb4.polymer.core.api.other.PolymerComponent; +import net.minecraft.component.ComponentType; +import net.minecraft.item.ItemStack; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; +import net.minecraft.util.dynamic.Codecs; + +import java.util.Map; +import java.util.function.UnaryOperator; + + +public class DriveComponents { + public static final Codec> ITEMSTACK_MAP_CODEC = Codec.unboundedMap(ItemStack.CODEC, Codec.INT); + + public static final ComponentType MAX_ITEMS = register( + "max_items", builder -> builder.codec(Codecs.POSITIVE_INT).packetCodec(PacketCodecs.VAR_INT) + ); + + public static final ComponentType USED_ITEMS = register( + "used_items", builder -> builder.codec(Codecs.NONNEGATIVE_INT).packetCodec(PacketCodecs.VAR_INT) + ); + + public static final ComponentType> ITEMSTACK_MAP = register( + "itemstack_map", + builder -> builder.codec(ITEMSTACK_MAP_CODEC) // No packetCodec needed for this example + ); + + + private static ComponentType register(String id, UnaryOperator> builderOperator) { + ComponentType componentType = Registry.register( + Registries.DATA_COMPONENT_TYPE, + id, + builderOperator.apply(ComponentType.builder()).build() + ); + PolymerComponent.registerDataComponent(componentType); + return componentType; + } +} diff --git a/src/main/java/systems/brn/server_storage/lib/HardDriveSlot.java b/src/main/java/systems/brn/server_storage/lib/HardDriveSlot.java new file mode 100644 index 0000000..c38ba4f --- /dev/null +++ b/src/main/java/systems/brn/server_storage/lib/HardDriveSlot.java @@ -0,0 +1,24 @@ +package systems.brn.server_storage.lib; + +import net.minecraft.inventory.Inventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.slot.Slot; + +import static systems.brn.server_storage.ServerStorage.DRIVES; + +public class HardDriveSlot extends Slot { + public HardDriveSlot(Inventory inventory, int index, int x, int y) { + super(inventory, index, x, y); + } + + public static boolean isHardDrive(Item item){ + return DRIVES.contains(item); + } + + @Override + public boolean canInsert(ItemStack stack) { + return isHardDrive(stack.getItem()); + } + +} diff --git a/src/main/java/systems/brn/server_storage/screenhandlers/DriveContainerScreenHandler.java b/src/main/java/systems/brn/server_storage/screenhandlers/DriveContainerScreenHandler.java new file mode 100644 index 0000000..ba4faa4 --- /dev/null +++ b/src/main/java/systems/brn/server_storage/screenhandlers/DriveContainerScreenHandler.java @@ -0,0 +1,84 @@ +package systems.brn.server_storage.screenhandlers; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.Slot; +import systems.brn.server_storage.lib.HardDriveSlot; + +public class DriveContainerScreenHandler extends ScreenHandler { + private static final int CONTAINER_SIZE = 9; + private static final int INVENTORY_START = 9; + private static final int INVENTORY_END = 36; + private static final int HOTBAR_START = 36; + private static final int HOTBAR_END = 45; + private final Inventory inventory; + + public DriveContainerScreenHandler(int syncId, PlayerInventory playerInventory, Inventory inventory) { + super(ScreenHandlerType.GENERIC_3X3, syncId); + checkSize(inventory, 9); + this.inventory = inventory; + inventory.onOpen(playerInventory.player); + + for(int i = 0; i < 3; ++i) { + for(int j = 0; j < 3; ++j) { + this.addSlot(new HardDriveSlot(inventory, j + i * 3, 62 + j * 18, 17 + i * 18)); + } + } + + for(int i = 0; i < 3; ++i) { + for(int j = 0; j < 9; ++j) { + this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + + for(int i = 0; i < 9; ++i) { + this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); + } + } + + @Override + public boolean canUse(PlayerEntity player) { + return this.inventory.canPlayerUse(player); + } + + @Override + public ItemStack quickMove(PlayerEntity player, int slot) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot2 = this.slots.get(slot); + if (slot2.hasStack()) { + ItemStack itemStack2 = slot2.getStack(); + itemStack = itemStack2.copy(); + if (slot < 9) { + if (!this.insertItem(itemStack2, 9, 45, true)) { + return ItemStack.EMPTY; + } + } else if (!this.insertItem(itemStack2, 0, 9, false)) { + return ItemStack.EMPTY; + } + + if (itemStack2.isEmpty()) { + slot2.setStack(ItemStack.EMPTY); + } else { + slot2.markDirty(); + } + + if (itemStack2.getCount() == itemStack.getCount()) { + return ItemStack.EMPTY; + } + + slot2.onTakeItem(player, itemStack2); + } + + return itemStack; + } + + @Override + public void onClosed(PlayerEntity player) { + super.onClosed(player); + this.inventory.onClose(player); + } +} diff --git a/src/main/resources/assets/serverstorage/blockstates/drive_container.json b/src/main/resources/assets/serverstorage/blockstates/drive_container.json index d3beb3b..b7cc7ad 100644 --- a/src/main/resources/assets/serverstorage/blockstates/drive_container.json +++ b/src/main/resources/assets/serverstorage/blockstates/drive_container.json @@ -1,7 +1,27 @@ { "variants": { - "": { + "facing=down": { + "model": "serverstorage:block/drive_container", + "x": 180 + }, + "facing=east": { + "model": "serverstorage:block/drive_container", + "y": 90 + }, + "facing=north": { "model": "serverstorage:block/drive_container" + }, + "facing=south": { + "model": "serverstorage:block/drive_container", + "y": 180 + }, + "facing=up": { + "model": "serverstorage:block/drive_container", + "x": 90 + }, + "facing=west": { + "model": "serverstorage:block/drive_container", + "y": 270 } } } \ No newline at end of file diff --git a/src/main/resources/assets/serverstorage/blockstates/storage.json b/src/main/resources/assets/serverstorage/blockstates/storage.json index 29a26f6..671916b 100644 --- a/src/main/resources/assets/serverstorage/blockstates/storage.json +++ b/src/main/resources/assets/serverstorage/blockstates/storage.json @@ -1,7 +1,26 @@ { "variants": { - "": { + "facing=down": { + "model": "serverstorage:block/storage", + "x": 180 + }, + "facing=east": { + "model": "serverstorage:block/storage", + "y": 90 + }, + "facing=north": { "model": "serverstorage:block/storage" + }, + "facing=south": { + "model": "serverstorage:block/storage", + "y": 180 + }, + "facing=up": { + "model": "serverstorage:block/storage" + }, + "facing=west": { + "model": "serverstorage:block/storage", + "y": 270 } } } \ No newline at end of file diff --git a/src/main/resources/data/serverstorage/loot_tables/blocks/drive_container.json b/src/main/resources/data/serverstorage/loot_table/blocks/drive_container.json similarity index 100% rename from src/main/resources/data/serverstorage/loot_tables/blocks/drive_container.json rename to src/main/resources/data/serverstorage/loot_table/blocks/drive_container.json diff --git a/src/main/resources/data/serverstorage/loot_tables/blocks/storage.json b/src/main/resources/data/serverstorage/loot_table/blocks/storage.json similarity index 100% rename from src/main/resources/data/serverstorage/loot_tables/blocks/storage.json rename to src/main/resources/data/serverstorage/loot_table/blocks/storage.json diff --git a/src/main/resources/data/serverstorage/recipes/diamond_drive.json b/src/main/resources/data/serverstorage/recipe/diamond_drive.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/diamond_drive.json rename to src/main/resources/data/serverstorage/recipe/diamond_drive.json diff --git a/src/main/resources/data/serverstorage/recipes/diamond_head.json b/src/main/resources/data/serverstorage/recipe/diamond_head.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/diamond_head.json rename to src/main/resources/data/serverstorage/recipe/diamond_head.json diff --git a/src/main/resources/data/serverstorage/recipes/diamond_platter.json b/src/main/resources/data/serverstorage/recipe/diamond_platter.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/diamond_platter.json rename to src/main/resources/data/serverstorage/recipe/diamond_platter.json diff --git a/src/main/resources/data/serverstorage/recipes/drive_casing.json b/src/main/resources/data/serverstorage/recipe/drive_casing.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/drive_casing.json rename to src/main/resources/data/serverstorage/recipe/drive_casing.json diff --git a/src/main/resources/data/serverstorage/recipes/drive_container.json b/src/main/resources/data/serverstorage/recipe/drive_container.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/drive_container.json rename to src/main/resources/data/serverstorage/recipe/drive_container.json diff --git a/src/main/resources/data/serverstorage/recipes/golden_drive.json b/src/main/resources/data/serverstorage/recipe/golden_drive.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/golden_drive.json rename to src/main/resources/data/serverstorage/recipe/golden_drive.json diff --git a/src/main/resources/data/serverstorage/recipes/golden_head.json b/src/main/resources/data/serverstorage/recipe/golden_head.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/golden_head.json rename to src/main/resources/data/serverstorage/recipe/golden_head.json diff --git a/src/main/resources/data/serverstorage/recipes/golden_platter.json b/src/main/resources/data/serverstorage/recipe/golden_platter.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/golden_platter.json rename to src/main/resources/data/serverstorage/recipe/golden_platter.json diff --git a/src/main/resources/data/serverstorage/recipes/iron_drive.json b/src/main/resources/data/serverstorage/recipe/iron_drive.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/iron_drive.json rename to src/main/resources/data/serverstorage/recipe/iron_drive.json diff --git a/src/main/resources/data/serverstorage/recipes/iron_head.json b/src/main/resources/data/serverstorage/recipe/iron_head.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/iron_head.json rename to src/main/resources/data/serverstorage/recipe/iron_head.json diff --git a/src/main/resources/data/serverstorage/recipes/iron_platter.json b/src/main/resources/data/serverstorage/recipe/iron_platter.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/iron_platter.json rename to src/main/resources/data/serverstorage/recipe/iron_platter.json diff --git a/src/main/resources/data/serverstorage/recipes/netherite_drive.json b/src/main/resources/data/serverstorage/recipe/netherite_drive.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/netherite_drive.json rename to src/main/resources/data/serverstorage/recipe/netherite_drive.json diff --git a/src/main/resources/data/serverstorage/recipes/netherite_head.json b/src/main/resources/data/serverstorage/recipe/netherite_head.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/netherite_head.json rename to src/main/resources/data/serverstorage/recipe/netherite_head.json diff --git a/src/main/resources/data/serverstorage/recipes/netherite_platter.json b/src/main/resources/data/serverstorage/recipe/netherite_platter.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/netherite_platter.json rename to src/main/resources/data/serverstorage/recipe/netherite_platter.json diff --git a/src/main/resources/data/serverstorage/recipes/storage.json b/src/main/resources/data/serverstorage/recipe/storage.json similarity index 100% rename from src/main/resources/data/serverstorage/recipes/storage.json rename to src/main/resources/data/serverstorage/recipe/storage.json