Skip to content

Commit

Permalink
Flint tilling + hoe damage #71
Browse files Browse the repository at this point in the history
  • Loading branch information
MBatt1 committed Aug 29, 2023
1 parent 1caefda commit f34af88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/main/java/net/cr24/primeval/item/FlintItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.cr24.primeval.item;

import net.minecraft.block.Block;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static net.cr24.primeval.item.tool.PrimevalHoeItem.hoeables;

public class FlintItem extends WeightedItem {
public FlintItem(Settings settings, Weight weight, Size size) {
super(settings, weight, size);
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
BlockPos pos = context.getBlockPos();
World world = context.getWorld();
Block targetBlock = world.getBlockState(pos).getBlock();
if (hoeables.containsKey(targetBlock) && world.getBlockState(pos.up()).isAir()) {
String side = world.isClient() ? "Client " : "Server ";
System.out.println(side + context.getStack().getCount());
world.setBlockState(pos, hoeables.get(targetBlock).getDefaultState());
world.playSound(context.getPlayer(), pos, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0f, 1.0f);
if (!world.isClient() && context.getWorld().getRandom().nextFloat() < 0.2) {
context.getStack().decrement(1);
return ActionResult.CONSUME;
}
return ActionResult.SUCCESS;
} else {
return super.useOnBlock(context);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/cr24/primeval/item/PrimevalItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class PrimevalItems {
public static final Item STRAW = registerItem("straw", new WeightedBlockItem(STRAW_PILE, GROUP_ITEMS(), Weight.VERY_LIGHT, Size.SMALL));
public static final Item STICK = registerItem("stick", new FirestarterItem(GROUP_ITEMS(), Weight.VERY_LIGHT, Size.SMALL));
public static final Item STRING = registerItem("string", new FirestarterItem(GROUP_ITEMS(), Weight.VERY_LIGHT, Size.SMALL));
public static final Item FLINT = registerItem("flint", new WeightedItem(GROUP_ITEMS(), Weight.LIGHT, Size.SMALL));
public static final Item FLINT = registerItem("flint", new FlintItem(GROUP_ITEMS(), Weight.LIGHT, Size.SMALL));
public static final Item ROCK = registerItem("rock", new WeightedItem(GROUP_ITEMS(), Weight.LIGHT, Size.SMALL));
public static final Item STONE_BRICK = registerItem("stone_brick", new WeightedItem(GROUP_ITEMS(), Weight.LIGHT, Size.SMALL));
public static final Item ASHES = registerItem("ashes", new WeightedItem(GROUP_ITEMS(), Weight.VERY_LIGHT, Size.SMALL));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/cr24/primeval/item/tool/PrimevalHoeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import net.cr24.primeval.item.Size;
import net.cr24.primeval.item.Weight;
import net.minecraft.block.Block;
import net.minecraft.item.HoeItem;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.ToolItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -32,7 +35,14 @@ public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
Block targetBlock = world.getBlockState(pos).getBlock();
if (hoeables.containsKey(targetBlock) && world.getBlockState(pos.up()).isAir()) {
var playerEntity = context.getPlayer();
world.setBlockState(pos, hoeables.get(targetBlock).getDefaultState());
world.playSound(playerEntity, pos, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0f, 1.0f);
if (playerEntity != null) {
context.getStack().damage(1, playerEntity, (p) -> {
p.sendToolBreakStatus(context.getHand());
});
}
return ActionResult.SUCCESS;
} else {
return super.useOnBlock(context);
Expand Down

0 comments on commit f34af88

Please sign in to comment.