Skip to content

Commit

Permalink
feat: add support for NeoForge's ToolActions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Jun 27, 2024
1 parent dc91c5c commit df27e54
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
- Update Russian translations.
- Add support for NeoForge's `ToolActions` (should improve compatibility with modded hoes).
- Add a warning message that is displayed _once_ when trying to harvest a crop without a hoe,
where `requireHoe` is `true` in the config - this is the number one question I get in my
Discord; 'why is RightClickHarvest not working'.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.core.Direction;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
Expand Down Expand Up @@ -93,14 +92,14 @@ public static InteractionResult onBlockUse(Player player, Level level, Interacti

// Check if the block requires a hoe; if so, check if a hoe is required and if the user has one.
if (!state.is(HOE_NEVER_REQUIRED) && CONFIG.get().requireHoe) {
if (!stackInHand.is(ItemTags.HOES)) {
if (!isHoe(stackInHand)) {
if (isHarvestable(state) && player.level().isClientSide && !CONFIG.get().hasUserBeenWarnedForNotUsingHoe) {
player.displayClientMessage(
Component.translatable(
"text.rightclickharvest.use_a_hoe_warning",
Component.translatable("config.rightclickharvest.requireHoe").withStyle(s -> s.withColor(ChatFormatting.GREEN)),
Component.literal("false").withStyle(s -> s.withColor(ChatFormatting.GREEN)
)),
)),
false
);
CONFIG.get().hasUserBeenWarnedForNotUsingHoe = true;
Expand All @@ -121,7 +120,7 @@ public static InteractionResult onBlockUse(Player player, Level level, Interacti
if (state.getBlock() instanceof CocoaBlock || state.getBlock() instanceof CropBlock || state.getBlock() instanceof NetherWartBlock) {
if (isMature(state)) {
// Start radius harvesting
if (initialCall && CONFIG.get().harvestInRadius && !state.is(RADIUS_HARVEST_BLACKLIST) && stackInHand.is(ItemTags.HOES)) {
if (initialCall && CONFIG.get().harvestInRadius && !state.is(RADIUS_HARVEST_BLACKLIST) && isHoe(stackInHand)) {
int radius = 0;
boolean circle = false;
hoeInUse = true;
Expand Down Expand Up @@ -184,7 +183,7 @@ public static InteractionResult onBlockUse(Player player, Level level, Interacti
return InteractionResult.PASS;
}

private static InteractionResult completeHarvest(Level level, BlockState state, BlockPos pos, Player player, InteractionHand hand, ItemStack stackInHand, boolean hoeInUse , boolean removeReplant, Runnable setBlockAction) {
private static InteractionResult completeHarvest(Level level, BlockState state, BlockPos pos, Player player, InteractionHand hand, ItemStack stackInHand, boolean hoeInUse, boolean removeReplant, Runnable setBlockAction) {
if (!level.isClientSide) {
Block originalBlock = state.getBlock();
// Event posts are for things like claim mods
Expand Down Expand Up @@ -223,6 +222,14 @@ private static boolean isHarvestable(BlockState state) {
}
}

private static boolean isHoe(ItemStack stack) {
return stack.is(ItemTags.HOES)
|| stack.is(LOW_TIER_HOES)
|| stack.is(MID_TIER_HOES)
|| stack.is(HIGH_TIER_HOES)
|| RightClickHarvestPlatform.isHoeAccordingToPlatform(stack);
}

private static boolean isMature(BlockState state) {
if (state.getBlock() instanceof CocoaBlock) {
return state.getValue(CocoaBlock.AGE) >= CocoaBlock.MAX_AGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -20,4 +21,9 @@ public static boolean postBreakEvent(Level level, BlockPos pos, BlockState state
public static boolean postPlaceEvent(Level level, BlockPos pos, Player player) {
return false;
}

@ExpectPlatform
public static boolean isHoeAccordingToPlatform(ItemStack stack) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -21,4 +22,8 @@ public static boolean postPlaceEvent(Level level, BlockPos pos, Player player) {
// no-op, fabric doesn't have a specific place block event.
return false;
}

public static boolean isHoeAccordingToPlatform(ItemStack stack) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import io.github.jamalam360.rightclickharvest.HarvestContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.ToolActions;
import net.neoforged.neoforge.common.util.BlockSnapshot;
import net.neoforged.neoforge.event.level.BlockEvent;

Expand All @@ -27,4 +29,8 @@ public static boolean postPlaceEvent(Level level, BlockPos pos, Player player) {
);
return NeoForge.EVENT_BUS.post(placeEv).isCanceled();
}

public static boolean isHoeAccordingToPlatform(ItemStack stack) {
return stack.canPerformAction(ToolActions.HOE_TILL);
}
}

0 comments on commit df27e54

Please sign in to comment.