Skip to content

Commit

Permalink
Merge pull request #1 from SKZGx/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
MEGATREX4 authored Jun 16, 2024
2 parents 2c67832 + a0288a3 commit 491a2e9
Show file tree
Hide file tree
Showing 27 changed files with 246 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@

import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UkrainianDelight implements ModInitializer {
public static final String MOD_ID = "ukrainian_delight";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static final RegistryKey<DamageType> GLASS_DAMAGE = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("ukrainian_delight", "glass_damage"));

@Override
public void onInitialize() {
RegistryKey<DamageType> GLASS_DAMAGE;

FoodBlocks.registerFoodBlocks();
ModBlock.registerModBlocks();
ModItems.registerModItems();
Expand All @@ -27,7 +35,7 @@ public void onInitialize() {

BlockRenderLayerMap.INSTANCE.putBlock(FoodBlocks.JAR, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(FoodBlocks.APPLE_JAM, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(FoodBlocks.CANNED_TOMATOES, RenderLayer.getCutout());
BlockRenderLayerMap.INSTANCE.putBlock(FoodBlocks.JARRED_TOMATOES, RenderLayer.getCutout());


LOGGER.info("Hello Fabric world it's " + MOD_ID + "!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.megatrex4.ukrainian_dlight.block;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.item.FoodBlockItem;
import com.megatrex4.ukrainian_dlight.item.ModFoodComponents;
import com.megatrex4.ukrainian_dlight.item.FoodBlockComponents;
import com.megatrex4.ukrainian_dlight.item.FoodBlockItem; // Import the FoodBlockItem class
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.FoodComponent;
Expand All @@ -13,11 +13,10 @@

public class FoodBlocks {


// New Jar Blocks
public static final Block JAR = registerBlock("jar", new JarBlock(), null);
public static final Block APPLE_JAM = registerBlock("apple_jam", new JarBlock(), ModFoodComponents.APPLE_JAM);
public static final Block CANNED_TOMATOES = registerBlock("canned_tomatoes", new JarBlock(), ModFoodComponents.CANNED_TOMATOES);
public static final Block APPLE_JAM = registerBlock("apple_jam", new JarBlock(), FoodBlockComponents.APPLE_JAM);
public static final Block JARRED_TOMATOES = registerBlock("jarred_tomatoes", new JarBlock(), FoodBlockComponents.JARRED_TOMATOES);

private static Block registerBlock(String name, Block block, FoodComponent foodComponent){
registerBlockItem(name, block, foodComponent);
Expand Down
63 changes: 57 additions & 6 deletions src/main/java/com/megatrex4/ukrainian_dlight/block/JarBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ShapeContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.BlockStateParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
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.DirectionProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

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

import static com.megatrex4.ukrainian_dlight.UkrainianDelight.GLASS_DAMAGE;

public class JarBlock extends Block {
public static final IntProperty JARS = IntProperty.of("jars", 1, 4); // Adjusted range to 1-4 for handling all jars
public static final IntProperty JARS = IntProperty.of("jars", 1, 4);
public static final DirectionProperty FACING = DirectionProperty.of("facing", Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST);
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));
Expand All @@ -37,12 +53,12 @@ public class JarBlock extends Block {

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

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(JARS);
builder.add(JARS, FACING);
}

@Override
Expand All @@ -52,12 +68,12 @@ public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
// Check if the player is sneaking (crouching)
return ctx.getPlayer().isSneaking() ? this.getDefaultState().with(JARS, 1) : null;
Direction facing = ctx.getHorizontalPlayerFacing().getOpposite();
return ctx.getPlayer().isSneaking() ? this.getDefaultState().with(JARS, 1).with(FACING, facing) : null; // Set the facing direction based on player placement
}

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

Expand Down Expand Up @@ -104,4 +120,39 @@ private void dropJarItem(World world, BlockPos pos, int count) {
Block.dropStack(world, pos, jarItemStack);
}
}

@Override
public void onLandedUpon(World world, BlockState state, BlockPos pos, Entity entity, float fallDistance) {
this.tryBreakJar(world, state, pos, entity, 0.3); // 30% chance
super.onLandedUpon(world, state, pos, entity, fallDistance);

}

private void tryBreakJar(World world, BlockState state, BlockPos pos, Entity entity, double breakChance) {
if (this.breaksJar(world, entity)) {
if (!world.isClient && world.random.nextDouble() < breakChance) {
this.breakJar(world, pos, state);
entity.damage(world.getDamageSources().create(GLASS_DAMAGE), 2.0F);
}
}
}


private void breakJar(World world, BlockPos pos, BlockState state) {
world.playSound(null, pos, SoundEvents.BLOCK_GLASS_BREAK, SoundCategory.BLOCKS, 0.7F, 0.9F + world.random.nextFloat() * 0.2F);
int currentJars = state.get(JARS);
if (currentJars <= 1) {
isRemovingJar.set(true); // Set flag to avoid dropping items
world.breakBlock(pos, false);
isRemovingJar.set(false); // Reset flag after breaking
} else {
world.setBlockState(pos, state.with(JARS, currentJars - 1), 2);
world.syncWorldEvent(2001, pos, Block.getRawIdFromState(state));
world.addParticle(new BlockStateParticleEffect(ParticleTypes.BLOCK, Blocks.GLASS.getDefaultState()), pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, 0.0, 0.0, 0.0); // Glass break particles
}
}

private boolean breaksJar(World world, Entity entity) {
return entity instanceof PlayerEntity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.megatrex4.ukrainian_dlight.item;

import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.FoodComponent;

public class FoodBlockComponents {
public static final FoodComponent APPLE_JAM = new FoodComponent.Builder().hunger(4).saturationModifier(0.3f).build();
public static final FoodComponent JARRED_TOMATOES = new FoodComponent.Builder().hunger(6).saturationModifier(0.6f)
.statusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 3*20, 3), 1.0f).build();

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
package com.megatrex4.ukrainian_dlight.item;

import com.megatrex4.ukrainian_dlight.util.StatusEffectUtil;
import com.mojang.datafixers.util.Pair;
import net.minecraft.block.Block;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.BlockItem;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;

import java.time.Duration;
import java.util.List;

public class FoodBlockItem extends BlockItem {
private final FoodComponent foodComponent;

public FoodBlockItem(Block block, FoodComponent foodComponent) {
super(block, new Item.Settings().food(foodComponent));
this.foodComponent = foodComponent;
}

@Override
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

// Add food component tooltip
if (foodComponent != null) {
// Check for status effects and add them to the tooltip
if (!foodComponent.getStatusEffects().isEmpty()) {
for (Pair<StatusEffectInstance, Float> effect : foodComponent.getStatusEffects()) {
StatusEffectInstance instance = effect.getFirst();
String effectName = instance.getEffectType().getName().getString();
int duration = effect.getFirst().getDuration() / 20;
int amplifier = effect.getFirst().getAmplifier() + 1;
String amplifierString = StatusEffectUtil.formatAmplifier(amplifier);
String durationString = formatDuration(duration);
tooltip.add(Text.translatable(effectName).append(Text.of(amplifierString)).append(Text.of(" (" + durationString + ") ")).formatted(Formatting.BLUE));
}
}
}
}

private static String formatDuration(int duration) {
int minutes = duration / 60;
int seconds = duration % 60;

return String.format("%d:%02d", minutes, seconds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class ModFoodComponents {
public static final StatusEffect COMFORT = EffectsRegistry.COMFORT.get();
public static final StatusEffect NOURISHMENT = EffectsRegistry.NOURISHMENT.get();

public static final FoodComponent APPLE_JAM = new FoodComponent.Builder().hunger(4).saturationModifier(0.3f).build();
public static final FoodComponent CANNED_TOMATOES = new FoodComponent.Builder().hunger(6).saturationModifier(0.6f).build();

// MEALS
public static final FoodComponent VARENYK = new FoodComponent.Builder()
.hunger(6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ModItemGroups {
.entries((displayContext, entries) -> {
entries.add(FoodBlocks.JAR);
entries.add(FoodBlocks.APPLE_JAM);
entries.add(FoodBlocks.CANNED_TOMATOES);
entries.add(FoodBlocks.JARRED_TOMATOES);
entries.add(ModBlock.SALT_BLOCK);
entries.add(ModItems.SALT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,21 @@ public ToolTipHelper(Settings settings) {
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

FoodComponent foodComponent = this.getFoodComponent();

if (foodComponent != null && !foodComponent.getStatusEffects().isEmpty()) {
addFoodEffectTooltip(stack, tooltip, foodComponent.getStatusEffects());
}
}

public static void addFoodEffectTooltip(ItemStack itemStack, List<Text> tooltip, List<Pair<StatusEffectInstance, Float>> effects) {
if (effects.isEmpty()) {
tooltip.add(Text.translatable("tooltip.ukrainian_delight.no_effects").formatted(Formatting.GRAY));
} else {
for (Pair<StatusEffectInstance, Float> pair : effects) {
StatusEffectInstance effect = pair.getFirst();
String name = effect.getEffectType().getTranslationKey();
int duration = effect.getDuration() / 20; // Convert ticks to seconds
int amplifier = effect.getAmplifier() + 1;
String amplifierString = StatusEffectUtil.formatAmplifier(amplifier);
String durationString = formatDuration(duration);

Text tooltipText = Text.translatable(name).formatted(Formatting.BLUE).append(Text.of(amplifierString))
.append(Text.of(" (" + durationString + ") "));

tooltip.add(tooltipText);
FoodComponent foodComponent = stack.getItem().getFoodComponent();

// Add food component tooltip
if (foodComponent != null) {
// Check for status effects and add them to the tooltip
if (!foodComponent.getStatusEffects().isEmpty()) {
for (Pair<StatusEffectInstance, Float> effect : foodComponent.getStatusEffects()) {
StatusEffectInstance instance = effect.getFirst();
String effectName = instance.getEffectType().getTranslationKey();
int duration = instance.getDuration() / 20;
int amplifier = instance.getAmplifier() + 1;
String amplifierString = StatusEffectUtil.formatAmplifier(amplifier);
String durationString = formatDuration(duration);
tooltip.add(Text.translatable(effectName).append(Text.of(amplifierString)).append(Text.of(" (" + durationString + ") ")).formatted(Formatting.BLUE));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{
"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" }
"jars=1": [
{ "model": "ukrainian_delight:block/jars/one_jar_apple_jam" },
{ "model": "ukrainian_delight:block/jars/one_jar_apple_jam", "y": 90 },
{ "model": "ukrainian_delight:block/jars/one_jar_apple_jam", "y": 180 },
{ "model": "ukrainian_delight:block/jars/one_jar_apple_jam", "y": 270 }
],
"jars=2": [
{ "model": "ukrainian_delight:block/jars/two_jar_apple_jam" },
{ "model": "ukrainian_delight:block/jars/two_jar_apple_jam", "y": 90 },
{ "model": "ukrainian_delight:block/jars/two_jar_apple_jam", "y": 180 },
{ "model": "ukrainian_delight:block/jars/two_jar_apple_jam", "y": 270 }
],
"jars=3": [
{ "model": "ukrainian_delight:block/jars/three_jar_apple_jam" },
{ "model": "ukrainian_delight:block/jars/three_jar_apple_jam", "y": 90 },
{ "model": "ukrainian_delight:block/jars/three_jar_apple_jam", "y": 180 },
{ "model": "ukrainian_delight:block/jars/three_jar_apple_jam", "y": 270 }
],
"jars=4": [
{ "model": "ukrainian_delight:block/jars/four_jar_apple_jam" },
{ "model": "ukrainian_delight:block/jars/four_jar_apple_jam", "y": 90 },
{ "model": "ukrainian_delight:block/jars/four_jar_apple_jam", "y": 180 },
{ "model": "ukrainian_delight:block/jars/four_jar_apple_jam", "y": 270 }
]
}
}

This file was deleted.

28 changes: 24 additions & 4 deletions src/main/resources/assets/ukrainian_delight/blockstates/jar.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{
"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" }
"jars=1": [
{ "model": "ukrainian_delight:block/jars/one_jar", "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 90, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 180, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 270, "particles": "minecraft:block/glass" }
],
"jars=2": [
{ "model": "ukrainian_delight:block/jars/two_jar", "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 90, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 180, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 270, "particles": "minecraft:block/glass" }
],
"jars=3": [
{ "model": "ukrainian_delight:block/jars/three_jar", "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 90, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 180, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 270, "particles": "minecraft:block/glass" }
],
"jars=4": [
{ "model": "ukrainian_delight:block/jars/four_jar", "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 90, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 180, "particles": "minecraft:block/glass" },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 270, "particles": "minecraft:block/glass" }
]
}
}
Loading

0 comments on commit 491a2e9

Please sign in to comment.