-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress #7 Implement Culture Jar GUI and base block entity machanics.
- Loading branch information
Showing
16 changed files
with
973 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 151 additions & 1 deletion
152
src/main/java/growthcraft/cellar/block/CultureJarBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,154 @@ | ||
package growthcraft.cellar.block; | ||
|
||
public class CultureJarBlock { | ||
import growthcraft.cellar.GrowthcraftCellar; | ||
import growthcraft.cellar.block.entity.CultureJarBlockEntity; | ||
import growthcraft.cellar.init.GrowthcraftCellarBlockEntities; | ||
import growthcraft.core.utils.BlockPropertiesUtils; | ||
import growthcraft.lib.utils.BlockStateUtils; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.context.BlockPlaceContext; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.LevelAccessor; | ||
import net.minecraft.world.level.block.*; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.state.StateDefinition; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
import net.minecraft.world.level.block.state.properties.BooleanProperty; | ||
import net.minecraft.world.level.block.state.properties.DirectionProperty; | ||
import net.minecraft.world.level.material.PushReaction; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import net.minecraftforge.common.capabilities.ForgeCapabilities; | ||
import net.minecraftforge.fluids.FluidUtil; | ||
import net.minecraftforge.network.NetworkHooks; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class CultureJarBlock extends BaseEntityBlock { | ||
|
||
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; | ||
public static final BooleanProperty LIT = BooleanProperty.create("lit"); | ||
|
||
private static final VoxelShape VOXEL_SHAPE = Block.box( | ||
5.0D, 0.0D, 5.0D, | ||
11.0D, 8.0D, 11.0D | ||
); | ||
|
||
public CultureJarBlock() { | ||
this(getInitProperties()); | ||
} | ||
|
||
public CultureJarBlock(Properties properties) { | ||
super(properties); | ||
this.registerDefaultState( | ||
this.stateDefinition.any() | ||
.setValue(FACING, Direction.NORTH) | ||
.setValue(LIT, false) | ||
); | ||
} | ||
|
||
private static Properties getInitProperties() { | ||
Properties properties = Properties.copy(Blocks.GLASS); | ||
properties.strength(0.5F); | ||
properties.noOcclusion(); | ||
properties.isValidSpawn(BlockPropertiesUtils::never); | ||
properties.isRedstoneConductor(BlockPropertiesUtils::never); | ||
properties.isSuffocating(BlockPropertiesUtils::never); | ||
properties.isViewBlocking(BlockPropertiesUtils::never); | ||
return properties; | ||
} | ||
|
||
@Override | ||
public RenderShape getRenderShape(BlockState blockState) { | ||
return RenderShape.MODEL; | ||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState blockState, BlockGetter level, BlockPos blockPos, CollisionContext context) { | ||
return VOXEL_SHAPE; | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) { | ||
super.createBlockStateDefinition(stateBuilder.add(FACING).add(LIT)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
return GrowthcraftCellarBlockEntities.CULTURE_JAR_BLOCK_ENTITY.get().create(blockPos, blockState); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockState getStateForPlacement(BlockPlaceContext context) { | ||
// TODO: Is block below heated? | ||
return this.defaultBlockState() | ||
.setValue(FACING, context.getHorizontalDirection().getOpposite()) | ||
.setValue(LIT, BlockStateUtils.isHeated(context.getLevel(), context.getClickedPos())); | ||
} | ||
|
||
@Override | ||
public PushReaction getPistonPushReaction(BlockState p_60584_) { | ||
return PushReaction.DESTROY; | ||
} | ||
|
||
@Override | ||
public BlockState rotate(BlockState state, LevelAccessor level, BlockPos pos, Rotation rotation) { | ||
return state.setValue(FACING, rotation.rotate(state.getValue(FACING))).setValue(LIT, state.getValue(LIT)); | ||
} | ||
|
||
@Override | ||
public BlockState mirror(BlockState state, Mirror mirror) { | ||
return state.rotate(mirror.getRotation(state.getValue(FACING))).setValue(LIT, state.getValue(LIT)); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) { | ||
return createTickerHelper( | ||
blockEntityType, | ||
GrowthcraftCellarBlockEntities.CULTURE_JAR_BLOCK_ENTITY.get(), | ||
(worldLevel, pos, blockState, blockEntity) -> (blockEntity).tick() | ||
); | ||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) { | ||
if (!level.isClientSide && player.isCrouching()) { | ||
try { | ||
// Play sound | ||
level.playSound(player, blockPos, SoundEvents.BARREL_OPEN, SoundSource.BLOCKS, 1.0F, 1.0F); | ||
CultureJarBlockEntity blockEntity = (CultureJarBlockEntity) level.getBlockEntity(blockPos); | ||
NetworkHooks.openScreen(((ServerPlayer) player), blockEntity, blockPos); | ||
} catch (Exception ex) { | ||
GrowthcraftCellar.LOGGER.error(String.format("%s unable to open CultureJarBlockEntity GUI at %s.", player.getDisplayName().getString(), blockPos)); | ||
GrowthcraftCellar.LOGGER.error(ex.getMessage()); | ||
GrowthcraftCellar.LOGGER.error(ex.fillInStackTrace()); | ||
} | ||
return InteractionResult.SUCCESS; | ||
} | ||
|
||
if (!level.isClientSide) { | ||
if ( | ||
FluidUtil.interactWithFluidHandler(player, interactionHand, level, blockPos, blockHitResult.getDirection()) | ||
|| player.getItemInHand(interactionHand).getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).isPresent() | ||
) { | ||
return InteractionResult.SUCCESS; | ||
} | ||
} | ||
|
||
return InteractionResult.SUCCESS; | ||
} | ||
} |
Oops, something went wrong.