Skip to content

Commit

Permalink
damage to player by jumping on jars
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jun 16, 2024
1 parent 2237120 commit f82050c
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 20 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 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
@@ -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 }
]
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{
"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" }
"jars=1": [
{ "model": "ukrainian_delight:block/jars/one_jar_canned_tomatoes" },
{ "model": "ukrainian_delight:block/jars/one_jar_canned_tomatoes", "y": 90 },
{ "model": "ukrainian_delight:block/jars/one_jar_canned_tomatoes", "y": 180 },
{ "model": "ukrainian_delight:block/jars/one_jar_canned_tomatoes", "y": 270 }
],
"jars=2": [
{ "model": "ukrainian_delight:block/jars/two_jar_canned_tomatoes" },
{ "model": "ukrainian_delight:block/jars/two_jar_canned_tomatoes", "y": 90 },
{ "model": "ukrainian_delight:block/jars/two_jar_canned_tomatoes", "y": 180 },
{ "model": "ukrainian_delight:block/jars/two_jar_canned_tomatoes", "y": 270 }
],
"jars=3": [
{ "model": "ukrainian_delight:block/jars/three_jar_canned_tomatoes" },
{ "model": "ukrainian_delight:block/jars/three_jar_canned_tomatoes", "y": 90 },
{ "model": "ukrainian_delight:block/jars/three_jar_canned_tomatoes", "y": 180 },
{ "model": "ukrainian_delight:block/jars/three_jar_canned_tomatoes", "y": 270 }
],
"jars=4": [
{ "model": "ukrainian_delight:block/jars/four_jar_canned_tomatoes" },
{ "model": "ukrainian_delight:block/jars/four_jar_canned_tomatoes", "y": 90 },
{ "model": "ukrainian_delight:block/jars/four_jar_canned_tomatoes", "y": 180 },
{ "model": "ukrainian_delight:block/jars/four_jar_canned_tomatoes", "y": 270 }
]
}
}
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" },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 90 },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 180 },
{ "model": "ukrainian_delight:block/jars/one_jar", "y": 270 }
],
"jars=2": [
{ "model": "ukrainian_delight:block/jars/two_jar" },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 90 },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 180 },
{ "model": "ukrainian_delight:block/jars/two_jar", "y": 270 }
],
"jars=3": [
{ "model": "ukrainian_delight:block/jars/three_jar" },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 90 },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 180 },
{ "model": "ukrainian_delight:block/jars/three_jar", "y": 270 }
],
"jars=4": [
{ "model": "ukrainian_delight:block/jars/four_jar" },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 90 },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 180 },
{ "model": "ukrainian_delight:block/jars/four_jar", "y": 270 }
]
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/ukrainian_delight/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"ukrainian_delight.advancement.lean_borscht": "The same, but with mushrooms!",
"ukrainian_delight.advancement.lean_borscht.desc": "Cook lean borscht",

"_comment": "tooltip",
"_comment": "misc",
"death.attack.glass_damage": "%s was not very careful with the glass",
"tooltip.ukrainian_delight.no_effects": "No effects",

"amplifier.none": "",
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/assets/ukrainian_delight/lang/uk_ua.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"ukrainian_delight.advancement.borscht": "Де знайти червону воду!",
"ukrainian_delight.advancement.borscht.desc": "Зваріть борщ",
"ukrainian_delight.advancement.lean_borscht": "Те саме тільки з грибами",
"ukrainian_delight.advancement.lean_borscht.desc": "Зваріть пісний борщ"
"ukrainian_delight.advancement.lean_borscht.desc": "Зваріть пісний борщ",

"_comment6": "misc",
"death.attack.glass_damage": "%s був не дуже обережний зі склом"

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exhaustion": 0.1,
"message_id": "glass_damage",
"scaling": "when_caused_by_living_non_player"
}

0 comments on commit f82050c

Please sign in to comment.