Skip to content

Commit

Permalink
finished basic storage block origin handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JR1811 committed Sep 11, 2024
1 parent 09a1d51 commit 1bd5800
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,23 @@ protected void onStateReplaced(BlockState state, World world, BlockPos pos, Bloc
return;
}
if (world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity) {
if (!world.isClient()) {
if (!blockEntity.isEmpty()) {
ItemScatterer.spawn(world, pos, blockEntity);
}
}
if (blockEntity.isStructureOrigin()) {
getFirstConnectedBlock(world, pos).ifPresent(connectedPos ->
blockEntity.broadcastNewOriginToConnectedBlocks(world, connectedPos));
if (getFirstConnectedBlock(world, pos).isPresent()) {
BlockPos firstConnectedPos = getFirstConnectedBlock(world, pos).get();
blockEntity.broadcastNewOriginPos(world, firstConnectedPos);
if (world.getBlockEntity(firstConnectedPos) instanceof AbstractVillageContainerBlockEntity newOriginBlockEntity) {
blockEntity.moveInventory(newOriginBlockEntity);
blockEntity.moveConnectedBlocks(newOriginBlockEntity);
}
} else {
if (!world.isClient()) {
if (!blockEntity.isEmpty()) {
ItemScatterer.spawn(world, pos, blockEntity);
}
}
}
}
}
//TODO: refresh connected blocks list on structure origin
super.onStateReplaced(state, world, pos, newState, moved);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import net.minecraft.world.WorldAccess;
import org.jetbrains.annotations.Nullable;

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

public abstract class AbstractVillageContainerBlockEntity extends BlockEntity implements HandledInventory {
private BlockPos structureOriginPos;
private final HashSet<BlockPos> connectedBlocks = new HashSet<>();
private final List<BlockPos> connectedBlocks = new ArrayList<>();
private StorageCallback callback = null;

public AbstractVillageContainerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -92,7 +92,7 @@ public Optional<ItemStack> extractFromOrigin(@Nullable Direction direction) {
return extractedStack;
}

public HashSet<BlockPos> getConnectedBlocks() {
public List<BlockPos> getConnectedBlocks() {
return this.connectedBlocks;
}

Expand All @@ -111,7 +111,12 @@ public void clearConnectedBlocks() {
this.getConnectedBlocks().clear();
}

public void broadcastNewOriginToConnectedBlocks(WorldAccess world, BlockPos newOriginPos) {
public void moveConnectedBlocks(AbstractVillageContainerBlockEntity newOriginBlockEntity) {
newOriginBlockEntity.getConnectedBlocks().clear();
newOriginBlockEntity.addConnectedBlocks(this.getConnectedBlocks());
}

public void broadcastNewOriginPos(WorldAccess world, BlockPos newOriginPos) {
for (BlockPos connectedPos : getConnectedBlocks()) {
if (!(world.getBlockEntity(connectedPos) instanceof AbstractVillageContainerBlockEntity blockEntity))
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Optional;

@SuppressWarnings("unused")
public interface HandledInventory extends SidedInventory {
DefaultedList<ItemStack> getItems();

Expand Down Expand Up @@ -134,6 +135,13 @@ default void clear() {
markDirty();
}

default void moveInventory(HandledInventory external) {
for (int i = 0; i < this.getItems().size(); i++) {
external.setStack(i, this.getItems().get(i));
}
this.clear();
}

void markDirty();

default InventoryStorage getAsStorage(@Nullable Direction direction) {
Expand Down

0 comments on commit 1bd5800

Please sign in to comment.