filename:
common/src/main/java/rearth/oritech/block/blocks/storage/LargeStorageBlock.java
branch:
1.21
back to repo
package rearth.oritech.block.blocks.storage;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.base.entity.ExpandableEnergyStorageBlockEntity;
import rearth.oritech.block.entity.storage.LargeStorageBlockEntity;
import rearth.oritech.util.MultiblockMachineController;
import java.util.Objects;
import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;
public class LargeStorageBlock extends SmallStorageBlock {
public LargeStorageBlock(Settings settings) {
super(settings.luminance(value -> 2));
setDefaultState(getDefaultState().with(ASSEMBLED, false));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(ASSEMBLED);
}
@Override
public @Nullable BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new LargeStorageBlockEntity(pos, state);
}
@Override
public @Nullable BlockState getPlacementState(ItemPlacementContext ctx) {
return Objects.requireNonNull(super.getPlacementState(ctx)).with(TARGET_DIR, ctx.getHorizontalPlayerFacing().getOpposite());
}
@Override
public ItemStack getPickStack(WorldView world, BlockPos pos, BlockState state) {
return new ItemStack(this);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (!world.isClient) {
var entity = world.getBlockEntity(pos);
if (!(entity instanceof MultiblockMachineController machineEntity)) {
return ActionResult.SUCCESS;
}
var wasAssembled = state.get(ASSEMBLED);
if (!wasAssembled) {
var corePlaced = machineEntity.tryPlaceNextCore(player);
if (corePlaced) return ActionResult.SUCCESS;
}
var isAssembled = machineEntity.initMultiblock(state);
// first time created
if (isAssembled && !wasAssembled) {
// NetworkContent.MACHINE_CHANNEL.serverHandle(machineEntity).send(new NetworkContent.MachineSetupEventPacket(pos));
return ActionResult.SUCCESS;
}
if (!isAssembled) {
player.sendMessage(Text.translatable("message.oritech.machine.missing_core"));
return ActionResult.SUCCESS;
}
}
return super.onUse(state, world, pos, player, hit);
}
@Override
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (!world.isClient() && state.get(ASSEMBLED)) {
var entity = world.getBlockEntity(pos);
if (entity instanceof MultiblockMachineController machineEntity) {
machineEntity.onControllerBroken();
}
if (entity instanceof ExpandableEnergyStorageBlockEntity storageBlock) {
var stacks = storageBlock.inventory.heldStacks;
for (var heldStack : stacks) {
if (!heldStack.isEmpty()) {
var itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), heldStack);
world.spawnEntity(itemEntity);
}
}
}
}
return super.onBreak(world, pos, state, player);
}
}