Skip to content

Commit

Permalink
added basic implementation of storage inventory interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
JR1811 committed Sep 8, 2024
1 parent f95dff3 commit 0f5f00e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package io.fabricatedatelier.mayor.block;

import com.mojang.serialization.MapCodec;
import io.fabricatedatelier.mayor.block.entity.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.util.ConnectedBlockUtil;
import net.fabricmc.fabric.api.transfer.v1.item.InventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
Expand Down Expand Up @@ -47,6 +56,32 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
builder.add(POSITION, Properties.WATERLOGGED);
}

@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
BlockPos originPos = getOrigin(world, pos).orElse(pos);
if (world.getBlockEntity(originPos) instanceof AbstractVillageContainerBlockEntity blockEntity && !world.isClient()) {
// extract
Optional<ItemStack> removedStack = blockEntity.extract(hit.getSide());
if (removedStack.isPresent() && !removedStack.get().isEmpty()) {
player.getInventory().offerOrDrop(removedStack.get());
return ActionResult.SUCCESS;
}
}
return super.onUse(state, world, pos, player, hit);
}

@Override
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
BlockPos originPos = getOrigin(world, pos).orElse(pos);
if (world.getBlockEntity(originPos) instanceof AbstractVillageContainerBlockEntity blockEntity && !world.isClient()) {
// inset
if (blockEntity.insert(player.getStackInHand(hand).copyAndEmpty(), hit.getSide())) {
return ItemActionResult.SUCCESS;
}
}
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
Expand All @@ -61,6 +96,9 @@ public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable Livi
if (!box.hasHoles() && box.isSquare()) {
state = state.with(POSITION, getPositionFromConnectedWalls(world, pos));
world.setBlockState(pos, state);
if (world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity) { // TODO: only if origin
box.getConnectedPosList().forEach(blockEntity::addConnectedBlocks);
}
}
super.onPlaced(world, pos, state, placer, itemStack);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fabricatedatelier.mayor.block.entity;
package io.fabricatedatelier.mayor.block;

import io.fabricatedatelier.mayor.util.HandledInventory;
import io.fabricatedatelier.mayor.util.NbtKeys;
Expand All @@ -16,7 +16,9 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;

public abstract class AbstractVillageContainerBlockEntity extends BlockEntity implements HandledInventory {
Expand All @@ -35,7 +37,12 @@ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLo
this.setStructureOriginPos(BlockPos.fromLong(nbt.getLong(NbtKeys.BLOCK_ENTITY_ORIGIN_POS)));
}

//TODO: connectedBlockList
this.connectedBlocks.clear();
NbtCompound blockPosListNbt = nbt.getCompound(NbtKeys.CONNECTED_BLOCKS);
for (String index : blockPosListNbt.getKeys()) {
BlockPos connectedPos = BlockPos.fromLong(blockPosListNbt.getLong(index));
this.connectedBlocks.add(connectedPos);
}
}

@Override
Expand All @@ -44,7 +51,13 @@ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryL
Inventories.writeNbt(nbt, this.getItems(), registryLookup);
this.getStructureOriginPos().ifPresent(originPos -> nbt.putLong(NbtKeys.BLOCK_ENTITY_ORIGIN_POS, originPos.asLong()));

//TODO: connectedBlockList
List<BlockPos> blockPosList = connectedBlocks.stream().toList();
NbtCompound blockPosListNbt = new NbtCompound();
for (int i = 0; i < blockPosList.size(); i++) {
BlockPos connectedPos = blockPosList.get(i);
blockPosListNbt.putLong(String.valueOf(i), connectedPos.asLong());
}
nbt.put(NbtKeys.CONNECTED_BLOCKS, blockPosListNbt);
}

public boolean isStructureOrigin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ public MapCodec<? extends BlockWithEntity> getCodec() {
return new LumberStorageBlockEntity(pos, state);
}

@Override
protected boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
// setBlockState is still possible, use BlockItem for that
// if (isNextToSameBlock(world, pos) || !isSupported(world, pos)) return false;
return super.canPlaceAt(state, world, pos);
}

@Override
protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
super.onStateReplaced(state, world, pos, newState, moved);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package io.fabricatedatelier.mayor.block.entity;

import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.datagen.TagProvider;
import io.fabricatedatelier.mayor.init.BlockEntities;
import io.fabricatedatelier.mayor.util.HandledInventory;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;

public class LumberStorageBlockEntity extends AbstractVillageContainerBlockEntity {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(16, ItemStack.EMPTY);

Expand All @@ -29,11 +26,4 @@ public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) {
public LumberStorageBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntities.LUMBER_STORAGE, pos, state);
}

public void modifyInventorySynced(Consumer<HandledInventory> consumer) {
consumer.accept(this);
if (this.getWorld() instanceof ServerWorld serverWorld) {
serverWorld.getChunkManager().markForUpdate(this.pos);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.fabricatedatelier.mayor.block.entity;

import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.datagen.TagProvider;
import io.fabricatedatelier.mayor.init.BlockEntities;
import net.minecraft.block.BlockState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public interface HandledInventory extends SidedInventory {
DefaultedList<ItemStack> getItems();
Expand All @@ -34,11 +35,27 @@ default boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) {
return true;
}

default boolean insert(ItemStack stack, @Nullable Direction direction) {
if (!canInsert(getItems().size(), stack, direction)) return false;
if (!tryAddingStack(stack.copy())) return false;
markDirty();
return true;
}

@Override
default boolean canExtract(int slot, ItemStack stack, Direction dir) {
return !isEmpty();
}

/**
* Extracts latest inserted item from HandledInventory.
* @param direction Interaction side for potential side filtering
* @return Removed Stack from Inventory. Is empty if stack couldn't be extracted.
*/
default Optional<ItemStack> extract(@Nullable Direction direction) {
return tryRemovingStack();
}

@Override
default boolean isEmpty() {
return size() <= 0;
Expand All @@ -58,15 +75,28 @@ default void setStack(int slot, ItemStack stack) {
default boolean tryAddingStack(ItemStack stack) {
boolean stackWasAdded = false;
for (int i = 0; i < size(); i++) {
if (!getItems().get(i).equals(ItemStack.EMPTY)) continue;
if (!getItems().get(i).equals(ItemStack.EMPTY) || !canInsert(i, stack, null)) continue;
setStack(i, stack);
stackWasAdded = true;
markDirty();
break;
}
markDirty();
return stackWasAdded;
}

default Optional<ItemStack> tryRemovingStack() {
ItemStack outputStack = ItemStack.EMPTY;
for (int i = getItems().size() - 1; i >= 0; i--) {
ItemStack stackInList = getItems().get(i);
if (stackInList.isEmpty() || !canExtract(i, stackInList, null)) continue;
outputStack = getItems().get(i).copy();
setStack(i, ItemStack.EMPTY);
markDirty();
break;
}
return outputStack.isEmpty() ? Optional.empty() : Optional.of(outputStack);
}

@Override
default ItemStack removeStack(int slot) {
return Inventories.removeStack(getItems(), slot);
Expand Down Expand Up @@ -104,11 +134,13 @@ default void clear() {
markDirty();
}

default void markDirty() {

}
void markDirty();

default InventoryStorage getAsStorage(@Nullable Direction direction) {
return InventoryStorage.of(this, direction);
}

default ItemVariant getTopItem(@Nullable Direction direction) {
return ItemVariant.of(getItems().getLast());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.fabricatedatelier.mayor.util;

import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock;
import io.fabricatedatelier.mayor.block.entity.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.state.VillageData;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
Expand Down

0 comments on commit 0f5f00e

Please sign in to comment.