forked from BRNSystems/Server_storage
110 lines
4.5 KiB
Java
110 lines
4.5 KiB
Java
package systems.brn.server_storage.blocks;
|
|
|
|
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 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.entity.player.PlayerEntity;
|
|
import net.minecraft.item.ItemPlacementContext;
|
|
import net.minecraft.registry.Registries;
|
|
import net.minecraft.registry.Registry;
|
|
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 static systems.brn.server_storage.ServerStorage.*;
|
|
|
|
public class HardDriveContainerBlock extends ConnectedBlock 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<Block, BlockState> builder) {
|
|
super.appendProperties(builder);
|
|
builder.add(FACING);
|
|
}
|
|
|
|
@Override
|
|
public BlockState getPolymerBlockState(BlockState state) {
|
|
return this.polymerBlockState;
|
|
}
|
|
|
|
public static void register() {
|
|
var modId = id(HARD_DRIVE_CONTAINER_BLOCK_MODEL_ID);
|
|
HARD_DRIVE_CONTAINER_BLOCK = Registry.register(Registries.BLOCK, modId,
|
|
new HardDriveContainerBlock(Settings.copy(Blocks.WHITE_WOOL), modId));
|
|
UseBlockCallback.EVENT.register(HardDriveContainerBlock::onUse);
|
|
|
|
HARD_DRIVE_CONTAINER_BLOCK.setDefaultState();
|
|
|
|
HARD_DRIVE_CONTAINER_BLOCK_ENTITY = Registry.register(
|
|
Registries.BLOCK_ENTITY_TYPE,
|
|
modId,
|
|
BlockEntityType.Builder.create(HardDriveContainerBlockEntity::new, HARD_DRIVE_CONTAINER_BLOCK).build(null)
|
|
);
|
|
PolymerBlockUtils.registerBlockEntity(HARD_DRIVE_CONTAINER_BLOCK_ENTITY);
|
|
}
|
|
|
|
private static ActionResult onUse(PlayerEntity player, World world, Hand hand, BlockHitResult hitResult) {
|
|
BlockPos pos = hitResult.getBlockPos();
|
|
BlockState state = world.getBlockState(pos);
|
|
Block block = state.getBlock();
|
|
|
|
if (block instanceof HardDriveContainerBlock) {
|
|
if (!world.isClient && !player.isSpectator()) {
|
|
if (!player.isSneaking()) {
|
|
BlockEntity storageBlockEntity = world.getBlockEntity(pos);
|
|
if (storageBlockEntity instanceof HardDriveContainerBlockEntity) {
|
|
player.openHandledScreen((HardDriveContainerBlockEntity) storageBlockEntity);
|
|
}
|
|
|
|
} else {
|
|
return ActionResult.PASS;
|
|
}
|
|
}
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
return ActionResult.PASS;
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
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);
|
|
}
|
|
}
|