Skip to content

Commit

Permalink
refactored storage BlockEntities
Browse files Browse the repository at this point in the history
  • Loading branch information
JR1811 committed Sep 14, 2024
1 parent 42cf1ec commit d9de019
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/fabricatedatelier/mayor/MayorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public void onInitializeClient() {
Renderer.initialize();
KeyBindings.initialize();

BlockEntityRendererFactories.register(BlockEntities.LUMBER_STORAGE, context -> new LumberStorageBlockEntityRenderer<>());
BlockEntityRendererFactories.register(BlockEntities.VILLAGE_STORAGE, context -> new LumberStorageBlockEntityRenderer<>());
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package io.fabricatedatelier.mayor.api;

import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlockEntity;
import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity;
import net.minecraft.util.math.BlockPos;

/**
* If you want to listen to the {@link AbstractVillageContainerBlockEntity StorageBlockEntity}
* If you want to listen to the {@link VillageContainerBlockEntity StorageBlockEntity}
* {@link StorageCallback callbacks}, you will need to register the callback first.<br>
* In the class, where you want to listen to those signals, implement the {@link StorageCallback} interface.<br>
* Then wherever you get access to the current {@link AbstractVillageContainerBlockEntity StorageBlockEntity},
* call its {@link AbstractVillageContainerBlockEntity#registerCallback(StorageCallback) registerCallback()} method
* Then wherever you get access to the current {@link VillageContainerBlockEntity StorageBlockEntity},
* call its {@link VillageContainerBlockEntity#registerCallback(StorageCallback) registerCallback()} method
* with your current class's instance as the parameter (usually just a <code>this</code> call)
*/
public interface StorageCallback {
/**
* Runs when the origin BlockPos of an {@link AbstractVillageContainerBlockEntity} has been changed.
* Runs when the origin BlockPos of an {@link VillageContainerBlockEntity} has been changed.
* @param blockEntity the BlockEntity which changed its listed origin BlockPos
* @param oldPos the old listed entry of the origin BlockPos
* @param newPos the new listed entry of the origin BlockPos
*/
default void onOriginChanged(AbstractVillageContainerBlockEntity blockEntity, BlockPos oldPos, BlockPos newPos) {
default void onOriginChanged(VillageContainerBlockEntity blockEntity, BlockPos oldPos, BlockPos newPos) {

}

/**
* Runs when the connected blocks BlockPos list of an {@link AbstractVillageContainerBlockEntity} has been changed.
* Runs when the connected blocks BlockPos list of an {@link VillageContainerBlockEntity} has been changed.
* @param blockEntity the BlockEntity which changed its connected blocks BlockPos list
*/
default void onConnectedBlocksChanged(AbstractVillageContainerBlockEntity blockEntity) {
default void onConnectedBlocksChanged(VillageContainerBlockEntity blockEntity) {

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.fabricatedatelier.mayor.block;

import com.mojang.serialization.MapCodec;
import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity;
import io.fabricatedatelier.mayor.state.VillageData;
import io.fabricatedatelier.mayor.util.ConnectedBlockUtil;
import io.fabricatedatelier.mayor.util.MayorStateHelper;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
@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()) {
if (world.getBlockEntity(originPos) instanceof VillageContainerBlockEntity blockEntity && !world.isClient()) {
// extract
Optional<ItemStack> removedStack = blockEntity.extractFromOrigin(hit.getSide());
if (removedStack.isPresent() && !removedStack.get().isEmpty()) {
Expand All @@ -72,7 +73,7 @@ protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, Worl

if (world.isClient())
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
if (!(world.getBlockEntity(originPos) instanceof AbstractVillageContainerBlockEntity blockEntity))
if (!(world.getBlockEntity(originPos) instanceof VillageContainerBlockEntity blockEntity))
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);

if (blockEntity.canInsert(player.getStackInHand(hand).copy(), hit.getSide())) {
Expand Down Expand Up @@ -100,26 +101,25 @@ public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable Livi
world.setBlockState(pos, state);
}

if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity)) {
if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity)) {
super.onPlaced(world, pos, state, placer, itemStack);
return;
}

Optional<BlockPos> neighborPos = getFirstConnectedBlock(world, pos);
if (neighborPos.isPresent()) {
if (world.getBlockEntity(neighborPos.get()) instanceof AbstractVillageContainerBlockEntity neighborBlockEntity) {
if (neighborBlockEntity.getStructureOriginPos().isPresent()) {
blockEntity.setStructureOriginPos(neighborBlockEntity.getStructureOriginPos().get());
if (world.getBlockEntity(neighborBlockEntity.getStructureOriginPos().get()) instanceof AbstractVillageContainerBlockEntity originBlockEntity) {
var originBox = new ConnectedBlockUtil.BoundingBox(world, neighborBlockEntity.getStructureOriginPos().get(), false);
originBlockEntity.clearConnectedBlocks();
originBlockEntity.addConnectedBlocks(new ArrayList<>(originBox.getConnectedPosList()));
}
if (neighborPos.isEmpty()) {
blockEntity.setStructureOriginPos(getOrigin(world, pos).orElse(pos));
} else if (world.getBlockEntity(neighborPos.get()) instanceof VillageContainerBlockEntity neighborBlockEntity) {
if (neighborBlockEntity.getStructureOriginPos().isPresent()) {
blockEntity.setStructureOriginPos(neighborBlockEntity.getStructureOriginPos().get());
if (world.getBlockEntity(neighborBlockEntity.getStructureOriginPos().get()) instanceof VillageContainerBlockEntity originBlockEntity) {
var originBox = new ConnectedBlockUtil.BoundingBox(world, neighborBlockEntity.getStructureOriginPos().get(), false);
originBlockEntity.clearConnectedBlocks();
originBlockEntity.addConnectedBlocks(new ArrayList<>(originBox.getConnectedPosList()));
}
}
} else {
blockEntity.setStructureOriginPos(getOrigin(world, pos).orElse(pos));
}

super.onPlaced(world, pos, state, placer, itemStack);
}

Expand All @@ -144,12 +144,12 @@ protected void onStateReplaced(BlockState state, World world, BlockPos pos, Bloc
super.onStateReplaced(state, world, pos, newState, moved);
return;
}
if (world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity) {
if (world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity) {
if (blockEntity.isStructureOrigin()) {
if (getFirstConnectedBlock(world, pos).isPresent()) {
BlockPos firstConnectedPos = getFirstConnectedBlock(world, pos).get();
blockEntity.broadcastNewOriginPos(world, firstConnectedPos);
if (world.getBlockEntity(firstConnectedPos) instanceof AbstractVillageContainerBlockEntity newOriginBlockEntity) {
if (world.getBlockEntity(firstConnectedPos) instanceof VillageContainerBlockEntity newOriginBlockEntity) {
blockEntity.moveInventory(newOriginBlockEntity);
blockEntity.moveConnectedBlocks(newOriginBlockEntity);
}
Expand Down Expand Up @@ -230,12 +230,12 @@ public static HashSet<Direction> getValidConnectedDirections(WorldAccess world,


public static void setOrigin(WorldAccess world, BlockPos pos) {
if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity)) return;
if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity)) return;
blockEntity.setStructureOriginPos(pos);
}

public static Optional<BlockPos> getOrigin(BlockView world, BlockPos pos) {
if (!(world.getBlockEntity(pos) instanceof AbstractVillageContainerBlockEntity blockEntity))
if (!(world.getBlockEntity(pos) instanceof VillageContainerBlockEntity blockEntity))
return Optional.empty();
return blockEntity.getStructureOriginPos();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.mojang.serialization.MapCodec;
import io.fabricatedatelier.mayor.Mayor;
import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock;
import io.fabricatedatelier.mayor.block.entity.LumberStorageBlockEntity;
import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity;
import io.fabricatedatelier.mayor.datagen.TagProvider;
import io.fabricatedatelier.mayor.util.ConnectedBlockUtil;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
Expand All @@ -13,15 +14,11 @@
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class LumberStorageBlock extends AbstractVillageContainerBlock {


Expand All @@ -36,15 +33,7 @@ public MapCodec<? extends BlockWithEntity> getCodec() {

@Override
public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new LumberStorageBlockEntity(pos, state);
}

@Override
protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
super.onStateReplaced(state, world, pos, newState, moved);
if (!isNextToSameBlock(world, pos)) {
setOrigin(world, pos);
}
return new VillageContainerBlockEntity(pos, state, TagProvider.ItemTags.LUMBER_STORAGE_STORABLE);
}

@Override
Expand All @@ -61,28 +50,4 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player
Mayor.LOGGER.info("Has Holes: {}", boundingBox.hasHoles());
return super.onUse(state, world, pos, player, hit);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isSupported(WorldView world, BlockPos originalPos) {
BlockState stateBelow = world.getBlockState(originalPos.down());
boolean belowIsSolid = stateBelow.isSideSolidFullSquare(world, originalPos.down(), Direction.UP);
boolean hasSameBlockBelow = stateBelow.getBlock() instanceof LumberStorageBlock;
return belowIsSolid || hasSameBlockBelow;
}

private int identicalHorizontalNeighborBlocks(BlockView world, BlockPos placedPos) {
return (int) Direction.Type.HORIZONTAL.stream()
.filter(direction -> world.getBlockState(placedPos.offset(direction)).getBlock() instanceof LumberStorageBlock)
.count();
}

public boolean isNextToSameBlock(BlockView world, BlockPos placedPos) {
return identicalHorizontalNeighborBlocks(world, placedPos) > 0;
}

public static boolean wallsAreAdjacent(List<Direction> sides) {
return Direction.Type.HORIZONTAL.stream()
.allMatch(direction -> sides.stream().noneMatch(direction.getOpposite()::equals));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.fabricatedatelier.mayor.block.custom;

import com.mojang.serialization.MapCodec;
import io.fabricatedatelier.mayor.block.entity.StoneStorageBlockEntity;
import io.fabricatedatelier.mayor.block.AbstractVillageContainerBlock;
import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity;
import io.fabricatedatelier.mayor.datagen.TagProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
Expand Down Expand Up @@ -32,6 +33,6 @@ public MapCodec<? extends BlockWithEntity> getCodec() {

@Override
public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new StoneStorageBlockEntity(pos, state);
return new VillageContainerBlockEntity(pos, state, TagProvider.ItemTags.STONE_STORAGE_STORABLE);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit d9de019

Please sign in to comment.