Skip to content

Commit

Permalink
Merge pull request #169 from Hojosa/cork
Browse files Browse the repository at this point in the history
Cork Tree additions
  • Loading branch information
Alatyami authored Nov 22, 2024
2 parents 7675d5d + 2b4cbad commit 2429f77
Show file tree
Hide file tree
Showing 19 changed files with 1,266 additions and 139 deletions.
110 changes: 110 additions & 0 deletions src/main/java/growthcraft/cellar/block/CorkCoasterBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package growthcraft.cellar.block;

import growthcraft.cellar.block.entity.CorkCoasterBlockEntity;
import growthcraft.cellar.init.GrowthcraftCellarBlockEntities;
import growthcraft.core.utils.BlockPropertiesUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
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.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

public class CorkCoasterBlock extends BaseEntityBlock {
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final BooleanProperty ITEM = BooleanProperty.create("item");
private static VoxelShape SHAPE = Block.box(1.0D, 0.0D, 1.0D, 15.0D, 2.0D, 15.0D);
private static VoxelShape SHAPE_WITH_BOTTLE = Shapes.or(Block.box(5.0D, 0.0D, 5.0D, 11.0D, 16.0D, 11.0D), SHAPE);

public CorkCoasterBlock() {
this(getInitProperties());
}

protected CorkCoasterBlock(Properties pProperties) {
super(pProperties);
}

private static Properties getInitProperties() {
Properties properties = BlockBehaviour.Properties.copy(Blocks.OAK_PLANKS);
properties.strength(1.5F);
properties.noOcclusion();
properties.isValidSpawn(BlockPropertiesUtils::never);
properties.isRedstoneConductor(BlockPropertiesUtils::never);
properties.isViewBlocking(BlockPropertiesUtils::never);
return properties;
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return state.getValue(ITEM).booleanValue() ? SHAPE_WITH_BOTTLE : SHAPE;
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return defaultBlockState()
.setValue(FACING, context.getHorizontalDirection().getOpposite())
.setValue(ITEM, Boolean.FALSE);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder.add(FACING).add(ITEM));
}

@Override
public BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}

@Override
public BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}

@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return GrowthcraftCellarBlockEntities.CORK_COASTER_BLOCK_ENTITY.get().create(pPos, pState);
}

@Override
public RenderShape getRenderShape(BlockState blockState) {
return RenderShape.MODEL;
}

@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
int slot = 0;
CorkCoasterBlockEntity blockEntity = (CorkCoasterBlockEntity) pLevel.getBlockEntity(pPos);
ItemStack itemInHand = pPlayer.getItemInHand(pHand);
ItemStack slotStack = blockEntity.getItem(slot);
if ((!itemInHand.isEmpty() && blockEntity.canPlaceItem(slot, itemInHand)) || !slotStack.isEmpty() && (itemInHand.isEmpty() || blockEntity.canPlaceItem(slot, itemInHand))) {
blockEntity.setItem(slot, itemInHand);
pPlayer.setItemInHand(pHand, slotStack);

pState = pState.setValue(ITEM, !blockEntity.isEmpty());
pLevel.setBlock(pPos, pState, UPDATE_ALL);
return InteractionResult.SUCCESS;
}
return super.use(pState, pLevel, pPos, pPlayer, pHand, pHit);
}
}
62 changes: 62 additions & 0 deletions src/main/java/growthcraft/cellar/block/CorkLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package growthcraft.cellar.block;

import org.jetbrains.annotations.Nullable;

import growthcraft.cellar.init.GrowthcraftCellarBlocks;
import growthcraft.cellar.init.GrowthcraftCellarItems;
import growthcraft.lib.block.GrowthcraftLogBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraftforge.common.ToolAction;
import net.minecraftforge.common.ToolActions;
import oshi.driver.windows.perfmon.SystemInformation.ContextSwitchProperty;

public class CorkLog extends GrowthcraftLogBlock{
public static final BooleanProperty REGROW = BooleanProperty.create("regrow");

@Override
public void randomTick(BlockState pState, ServerLevel pLevel, BlockPos pPos, RandomSource pRandom) {
if (pRandom.nextInt((int)(25.0F / 2) + 1) == 0){
pLevel.setBlockAndUpdate(pPos, GrowthcraftCellarBlocks.CORK_WOOD_LOG.get().defaultBlockState().setValue(AXIS, pState.getValue(AXIS)).setValue(REGROW, pState.getValue(REGROW)));
}
}

@Override
public boolean isRandomlyTicking(BlockState pState) {
return pState.is(GrowthcraftCellarBlocks.CORK_WOOD_LOG_STRIPPED.get()) && pState.getValue(REGROW);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> blockStateBuilder) {
super.createBlockStateDefinition(blockStateBuilder.add(REGROW));
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return this.defaultBlockState().setValue(REGROW, false);
}

@Override
public @Nullable BlockState getToolModifiedState(BlockState state, UseOnContext context, ToolAction toolAction, boolean simulate) {
if (context.getItemInHand().canPerformAction(ToolActions.AXE_STRIP)) {
if(state.is(GrowthcraftCellarBlocks.CORK_WOOD_LOG.get())) {
System.out.println("hello " + context.getClickedPos());
System.out.println("hello2 " + context.getClickedPos().relative(context.getClickedFace(), 1));
popResource(context.getLevel(), context.getClickedPos().relative(context.getClickedFace(), 1), new ItemStack(GrowthcraftCellarItems.CORK_BARK.get()));
return GrowthcraftCellarBlocks.CORK_WOOD_LOG_STRIPPED.get().defaultBlockState().setValue(AXIS, state.getValue(AXIS)).setValue(REGROW, state.getValue(REGROW));
}
if(state.is(GrowthcraftCellarBlocks.CORK_WOOD.get())) {
return GrowthcraftCellarBlocks.CORK_WOOD_STRIPPED.get().defaultBlockState().setValue(AXIS, state.getValue(AXIS));
}
}
return super.getToolModifiedState(state, context, toolAction, simulate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package growthcraft.cellar.block.entity;

import java.util.Objects;

import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import growthcraft.cellar.init.GrowthcraftCellarBlockEntities;
import growthcraft.cellar.init.GrowthcraftCellarItems;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class CorkCoasterBlockEntity extends BlockEntity implements Container{

private static final String ITEMS_TAG = "Inventory";
protected final ItemStackHandler itemHandler;
private LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();

public CorkCoasterBlockEntity(BlockPos blockPos, BlockState blockState) {
this(GrowthcraftCellarBlockEntities.CORK_COASTER_BLOCK_ENTITY.get(), blockPos, blockState);
}

public CorkCoasterBlockEntity(BlockEntityType<?> pType, BlockPos pPos, BlockState pBlockState) {
super(pType, pPos, pBlockState);
itemHandler = new ItemStackHandler(1) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
setChanged();
Objects.requireNonNull(level).sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_ALL);
}
};
}

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return stack.is(GrowthcraftCellarItems.POTION_WINE.get());
}


@Override
public void onLoad() {
super.onLoad();
lazyItemHandler = LazyOptional.of(() -> itemHandler);
}

@Override
public void load(CompoundTag tag) {
if(tag.contains(ITEMS_TAG)) {
itemHandler.deserializeNBT(tag.getCompound(ITEMS_TAG));
}
super.load(tag);
}

@Override
public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put(ITEMS_TAG, itemHandler.serializeNBT());
}

@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
this.load(pkt.getTag());
}

@Nullable
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}

@Override
public @NotNull CompoundTag getUpdateTag() {
return this.saveWithoutMetadata();
}

@Override
public void handleUpdateTag(CompoundTag tag) {
this.load(tag);
}

@Override
public void clearContent() {
for (int i = 0; i < getContainerSize(); i++) {
itemHandler.setStackInSlot(i, ItemStack.EMPTY);
}
}

@Override
public int getContainerSize() {
return itemHandler.getSlots();
}

@Override
public boolean isEmpty() {
for (int i = 0; i < getContainerSize(); i++) {
if (!itemHandler.getStackInSlot(i).isEmpty()) return false;
}
return true;
}

@Override
public ItemStack getItem(int pSlot) {
return pSlot < getContainerSize() ? itemHandler.getStackInSlot(pSlot) : ItemStack.EMPTY;
}

@Override
public ItemStack removeItem(int pSlot, int pAmount) {
return pSlot < getContainerSize() ? itemHandler.getStackInSlot(pSlot).split(pAmount) : ItemStack.EMPTY;
}

@Override
public ItemStack removeItemNoUpdate(int pSlot) {
if (pSlot < getContainerSize()) {
ItemStack stack = itemHandler.getStackInSlot(pSlot);
itemHandler.setStackInSlot(pSlot, ItemStack.EMPTY);
return stack;
}
return ItemStack.EMPTY;
}

@Override
public void setItem(int pSlot, ItemStack pStack) {
if (pSlot < getContainerSize()) {
itemHandler.setStackInSlot(pSlot, pStack);
this.setChanged();
}
}

@Override
public boolean stillValid(Player pPlayer) {
BlockPos pos = getBlockPos();
return Objects.requireNonNull(level).getBlockEntity(pos) == this && pPlayer.distanceToSqr(Vec3.atCenterOf(pos)) <= 64;
}

@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
if(cap == ForgeCapabilities.ITEM_HANDLER) {
return lazyItemHandler.cast();
}
return super.getCapability(cap, side);
}

@Override
public void invalidateCaps() {
super.invalidateCaps();
lazyItemHandler.invalidate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package growthcraft.cellar.block.entity.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;

import growthcraft.cellar.block.entity.CorkCoasterBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;

public class CorkCoasterBlockEntityRenderer implements BlockEntityRenderer<CorkCoasterBlockEntity> {

@Override
public void render(CorkCoasterBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource multiBufferSource, int light, int overlay) {
if(!blockEntity.isEmpty()) {
ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
ItemStack item = blockEntity.getItem(0);
poseStack.pushPose();

poseStack.translate(0.5F, 0.8F, 0.5F);
poseStack.mulPose(Axis.YP.rotationDegrees(switch (blockEntity.getBlockState().getValue(BlockStateProperties.HORIZONTAL_FACING)) {
case SOUTH -> 0;
case EAST -> 90;
default -> 180;
case WEST -> 270;
}));

poseStack.translate(0, -0.1, 0);
// poseStack.mulPose(Axis.ZP.rotationDegrees(135));
itemRenderer.renderStatic(item, ItemDisplayContext.FIXED, 250, overlay, poseStack, multiBufferSource,blockEntity.getLevel(), 1);
poseStack.popPose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public class GrowthcraftCellarBlockEntities {
).build(null)
);

public static final RegistryObject<BlockEntityType<CorkCoasterBlockEntity>> CORK_COASTER_BLOCK_ENTITY = BLOCK_ENTITIES.register(
Reference.UnlocalizedName.CORK_COASTER,
() -> BlockEntityType.Builder.of(
CorkCoasterBlockEntity::new,
GrowthcraftCellarBlocks.CORK_COASTER.get()
).build(null)
);

public GrowthcraftCellarBlockEntities() {
/* Disable automatic default public constructor */
}
Expand Down
Loading

0 comments on commit 2429f77

Please sign in to comment.