Skip to content

Commit

Permalink
fully work jar placement item
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jun 14, 2024
1 parent c1c2edd commit a7be45f
Show file tree
Hide file tree
Showing 37 changed files with 1,562 additions and 344 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ loader_version=0.15.11
# rei_version=12.0.626

# Mod Properties
mod_version=1.0.0
mod_version= a0.5.0
maven_group=com.megatrex4.ukrainian_dlight
archives_base_name=ukrainian-delight

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.megatrex4.ukrainian_dlight;

import com.megatrex4.ukrainian_dlight.block.ModBlocks;
import com.megatrex4.ukrainian_dlight.item.JarsItems;
import com.megatrex4.ukrainian_dlight.item.ModItemGroups;
import com.megatrex4.ukrainian_dlight.item.ModItems;
import com.megatrex4.ukrainian_dlight.item.ToolTipHelper;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.render.RenderLayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,10 +20,12 @@ public class UkrainianDelight implements ModInitializer {
public void onInitialize() {
ModBlocks.registerModBlocks();
ModItems.registerModItems();
JarsItems.registerJarsItems();
ModItemGroups.registerItemGroups();
ToolTipHelper.registerTooltip();

BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.JAR, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.APPLE_JAM, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.TINNED_TOMATOES, RenderLayer.getCutout());


LOGGER.info("Hello Fabric world it's " + MOD_ID + "!");
Expand Down
113 changes: 43 additions & 70 deletions src/main/java/com/megatrex4/ukrainian_dlight/block/JarBlock.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
package com.megatrex4.ukrainian_dlight.block;

import com.megatrex4.ukrainian_dlight.item.JarsItems;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ShapeContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import java.util.HashMap;
import java.util.Map;

public class JarBlock extends Block {
public static final IntProperty JARS = IntProperty.of("jars", 1, 4);
private static final VoxelShape ONE_JAR_SHAPE = Block.createCuboidShape(6, 0, 6, 10, 8, 10);
private static final VoxelShape TWO_JARS_SHAPE = VoxelShapes.union(ONE_JAR_SHAPE, Block.createCuboidShape(1, 0, 3, 14, 8, 12));
private static final VoxelShape THREE_JARS_SHAPE = VoxelShapes.union(TWO_JARS_SHAPE, Block.createCuboidShape(1, 0, 1, 15, 8, 14));
private static final VoxelShape FOUR_JARS_SHAPE = VoxelShapes.union(THREE_JARS_SHAPE, Block.createCuboidShape(1, 0, 1, 15, 8, 15));
public static final IntProperty JARS = IntProperty.of("jars", 1, 4); // Adjusted range to 1-4 for handling all jars
private static final Map<Integer, VoxelShape> SHAPES = new HashMap<>();
private static final ThreadLocal<Boolean> isRemovingJar = ThreadLocal.withInitial(() -> false);

static {
SHAPES.put(1, Block.createCuboidShape(6, 0, 6, 10, 8, 10));
SHAPES.put(2, Block.createCuboidShape(1, 0, 3, 14, 8, 12));
SHAPES.put(3, Block.createCuboidShape(1, 0, 1, 15, 8, 14));
SHAPES.put(4, Block.createCuboidShape(1, 0, 1, 15, 8, 15));
}

public JarBlock() {
super(FabricBlockSettings.copyOf(Blocks.GLASS).strength(0.3F).nonOpaque().sounds(BlockSoundGroup.GLASS));
this.setDefaultState(this.stateManager.getDefaultState().with(JARS, 1));
super(FabricBlockSettings.copyOf(Blocks.GLASS).strength(0.2F).nonOpaque().sounds(BlockSoundGroup.GLASS));
this.setDefaultState(this.stateManager.getDefaultState().with(JARS, 1)); // Start with 1 jar
}

@Override
Expand All @@ -42,93 +47,61 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)

@Override
public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) {
return getShape(state);
}

@Override
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return getShape(state);
}

private VoxelShape getShape(BlockState state) {
switch (state.get(JARS)) {
case 1:
default:
return ONE_JAR_SHAPE;
case 2:
return TWO_JARS_SHAPE;
case 3:
return THREE_JARS_SHAPE;
case 4:
return FOUR_JARS_SHAPE;
}
return SHAPES.get(state.get(JARS));
}

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return this.getDefaultState().with(JARS, 1);
// Check if the player is sneaking (crouching)
return ctx.getPlayer().isSneaking() ? this.getDefaultState().with(JARS, 1) : null;
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, net.minecraft.util.hit.BlockHitResult hit) {
int currentJars = state.get(JARS);
ItemStack heldItem = player.getStackInHand(hand);

// Check if the player is using an empty hand to remove a jar
if (heldItem.isEmpty()) {
if (currentJars > 1) {
world.setBlockState(pos, state.with(JARS, currentJars - 1));
dropJarItem(world, pos, state.getBlock());
return ActionResult.SUCCESS;
} else if (currentJars == 1) {
world.breakBlock(pos, false);
dropJarItem(world, pos, state.getBlock());
return ActionResult.SUCCESS;
if (heldItem.isEmpty() && currentJars > 0) {
if (currentJars == 1) {
isRemovingJar.set(true); // Set flag to indicate the jar is being removed
world.setBlockState(pos, Blocks.AIR.getDefaultState()); // Break the block when the last jar is removed
isRemovingJar.set(false); // Reset flag after removing
} else {
world.setBlockState(pos, state.with(JARS, currentJars - 1)); // Reduce jars by 1
}
dropJarItem(world, pos, 1); // Drop 1 jar
world.playSound(null, pos, SoundEvents.BLOCK_GLASS_STEP, SoundCategory.BLOCKS, 2f, 0.7f); // Glass step sound
return ActionResult.SUCCESS;
}

// Check if the player is holding a jar item to add a jar
if (currentJars < 4 && isValidJarItem(heldItem.getItem(), state.getBlock())) {
// Check if the player is holding a jar block item to add a jar and it matches the current block
if (!heldItem.isEmpty() && currentJars < 4 && heldItem.getItem() == this.asItem()) {
world.setBlockState(pos, state.with(JARS, currentJars + 1));
if (!player.isCreative()) {
heldItem.decrement(1);
}
world.playSound(null, pos, SoundEvents.BLOCK_GLASS_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F); // Glass place sound
return ActionResult.SUCCESS;
}

return ActionResult.PASS;
}

private boolean isValidJarItem(Item item, Block block) {
if (block == ModBlocks.JAR_BLOCK && item == JarsItems.JAR) {
return true;
} else if (block == ModBlocks.APPLE_JAM_BLOCK && item == JarsItems.APPLE_JAM) {
return true;
} else if (block == ModBlocks.TINNED_TOMATOES_BLOCK && item == JarsItems.TINNED_TOMATOES) {
return true;
@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if (state.getBlock() != newState.getBlock()) {
super.onStateReplaced(state, world, pos, newState, moved);
if (!isRemovingJar.get() && state.get(JARS) > 0 && (newState.isAir() || newState.getBlock() != this)) {
dropJarItem(world, pos, state.get(JARS));
}
}
return false;
}

private void dropJarItem(World world, BlockPos pos, Block block) {
private void dropJarItem(World world, BlockPos pos, int count) {
if (!world.isClient) {
Vec3d dropPos = Vec3d.ofCenter(pos);
ItemStack jarItemStack = getJarItemStack(block);
ItemStack jarItemStack = new ItemStack(this.asItem(), count);
Block.dropStack(world, pos, jarItemStack);
}
}

private ItemStack getJarItemStack(Block block) {
if (block == ModBlocks.APPLE_JAM_BLOCK) {
return new ItemStack(JarsItems.APPLE_JAM);
} else if (block == ModBlocks.TINNED_TOMATOES_BLOCK) {
return new ItemStack(JarsItems.TINNED_TOMATOES);
}
return new ItemStack(JarsItems.JAR);
}

@Override
public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
return getJarItemStack(state.getBlock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class ModBlocks {
new Block(FabricBlockSettings.copyOf(Blocks.OAK_PLANKS).sounds(BlockSoundGroup.WOOD)));

// New Jar Blocks
public static final Block JAR_BLOCK = registerBlock("jar_block", new JarBlock());
public static final Block APPLE_JAM_BLOCK = registerBlock("apple_jar_block", new JarBlock());
public static final Block TINNED_TOMATOES_BLOCK = registerBlock("tinned_tomatoes_block", new JarBlock());
public static final Block JAR = registerBlock("jar", new JarBlock());
public static final Block APPLE_JAM = registerBlock("apple_jam", new JarBlock());
public static final Block TINNED_TOMATOES = registerBlock("canned_tomatoes", new JarBlock());

private static Block registerBlock(String name, Block block){
registerBlockItem(name, block);
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/com/megatrex4/ukrainian_dlight/item/JarBuilder.java

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/java/com/megatrex4/ukrainian_dlight/item/JarsItems.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class ModItemGroups {
.displayName(Text.translatable("itemgroup.ingredients_ukrainian_delight"))
.icon(() -> new ItemStack(ModItems.CUCUMBER))
.entries((displayContext, entries) -> {
entries.add(JarsItems.JAR);
entries.add(JarsItems.APPLE_JAM);
entries.add(JarsItems.TINNED_TOMATOES);
entries.add(ModBlocks.JAR);
entries.add(ModBlocks.APPLE_JAM);
entries.add(ModBlocks.TINNED_TOMATOES);
entries.add(ModBlocks.SALT_BLOCK);
entries.add(ModItems.SALT);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"variants": {
"jars=1": { "model": "ukrainian_delight:block/jars/one_jar_apple_jam" },
"jars=2": { "model": "ukrainian_delight:block/jars/two_jar_apple_jam" },
"jars=3": { "model": "ukrainian_delight:block/jars/three_jar_apple_jam" },
"jars=4": { "model": "ukrainian_delight:block/jars/four_jar_apple_jam" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"variants": {
"jars=1": { "model": "ukrainian_delight:block/jars/one_jar_canned_tomatoes" },
"jars=2": { "model": "ukrainian_delight:block/jars/two_jar_canned_tomatoes" },
"jars=3": { "model": "ukrainian_delight:block/jars/three_jar_canned_tomatoes" },
"jars=4": { "model": "ukrainian_delight:block/jars/four_jar_canned_tomatoes" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"variants": {
"jars=1": { "model": "ukrainian_delight:block/jars/one_jar" },
"jars=2": { "model": "ukrainian_delight:block/jars/two_jar" },
"jars=3": { "model": "ukrainian_delight:block/jars/three_jar" },
"jars=4": { "model": "ukrainian_delight:block/jars/four_jar" }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"item.ukrainian_delight.homemade_sausage": "Homemade Sausage",

"_comment": "jars",
"item.ukrainian_delight.jar": "Jar",
"block.ukrainian_delight.jar": "Jar",



Expand Down

This file was deleted.

Loading

0 comments on commit a7be45f

Please sign in to comment.