Skip to content

Commit

Permalink
Progress #7 Implement Culture Jar GUI and base block entity machanics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alatyami committed Apr 30, 2023
1 parent 9d6102d commit 86059ee
Show file tree
Hide file tree
Showing 16 changed files with 973 additions and 31 deletions.
13 changes: 8 additions & 5 deletions src/main/java/growthcraft/cellar/GrowthcraftCellar.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package growthcraft.cellar;

import growthcraft.cellar.init.GrowthcraftCellarBlocks;
import growthcraft.cellar.init.GrowthcraftCellarFluids;
import growthcraft.cellar.init.GrowthcraftCellarItems;
import growthcraft.cellar.init.GrowthcraftCellarRecipes;
import growthcraft.cellar.init.*;
import growthcraft.cellar.init.client.GrowthcraftCellarBlockRenderers;
import growthcraft.cellar.init.config.GrowthcraftCellarConfig;
import growthcraft.cellar.lib.networking.GrowthcraftCellarMessages;
import growthcraft.cellar.screen.CultureJarScreen;
import growthcraft.cellar.shared.Reference;
import growthcraft.core.init.GrowthcraftCreativeModeTabs;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.CreativeModeTabEvent;
Expand Down Expand Up @@ -39,8 +39,10 @@ public GrowthcraftCellar() {
// Blocks, Items, Fluids, Block Entities, Containers
GrowthcraftCellarBlocks.BLOCKS.register(modEventBus);
GrowthcraftCellarItems.ITEMS.register(modEventBus);
GrowthcraftCellarBlockEntities.BLOCK_ENTITIES.register(modEventBus);
GrowthcraftCellarFluids.FLUID_TYPES.register(modEventBus);
GrowthcraftCellarFluids.FLUIDS.register(modEventBus);
GrowthcraftCellarMenus.MENUS.register(modEventBus);

GrowthcraftCellarRecipes.register(modEventBus);

Expand All @@ -50,10 +52,11 @@ public GrowthcraftCellar() {

private void clientSetupEvent(final FMLClientSetupEvent event) {
GrowthcraftCellarBlockRenderers.setRenderLayers();
MenuScreens.register(GrowthcraftCellarMenus.CULTURE_JAR_MENU.get(), CultureJarScreen::new);
}

private void setup(final FMLCommonSetupEvent event) {
// Do Nothing for now ...
GrowthcraftCellarMessages.register();
}

public void buildCreativeTabContents(CreativeModeTabEvent.BuildContents event) {
Expand Down
152 changes: 151 additions & 1 deletion src/main/java/growthcraft/cellar/block/CultureJarBlock.java
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;
}
}
Loading

0 comments on commit 86059ee

Please sign in to comment.