From d37062770328213a44624bea640243111d8f09ec Mon Sep 17 00:00:00 2001 From: Yannick Marcotte-Gourde Date: Wed, 2 Apr 2025 13:07:47 -0400 Subject: [PATCH] Allow the Scythe to harvest crops in an AOE (#159) --- dependencies.gradle | 11 +++- src/main/java/tconstruct/TConstruct.java | 5 ++ .../api/harvesting/CropHarvestHandler.java | 12 +++++ .../api/harvesting/CropHarvestHandlers.java | 18 +++++++ .../VanillaCropsHarvestHandler.java | 32 +++++++++++ .../java/tconstruct/items/tools/Scythe.java | 54 +++++++++++++++++++ .../plugins/ic2/Ic2CropHarvestHandler.java | 22 ++++++++ .../tconstruct/plugins/ic2/TinkerIC2.java | 3 ++ .../natura/NaturaCropHarvestHandler.java | 32 +++++++++++ .../plugins/natura/TinkerNatura.java | 22 ++++++++ .../tconstruct/util/config/PHConstruct.java | 9 ++++ 11 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 src/main/java/tconstruct/api/harvesting/CropHarvestHandler.java create mode 100644 src/main/java/tconstruct/api/harvesting/CropHarvestHandlers.java create mode 100644 src/main/java/tconstruct/api/harvesting/VanillaCropsHarvestHandler.java create mode 100644 src/main/java/tconstruct/plugins/ic2/Ic2CropHarvestHandler.java create mode 100644 src/main/java/tconstruct/plugins/natura/NaturaCropHarvestHandler.java create mode 100644 src/main/java/tconstruct/plugins/natura/TinkerNatura.java diff --git a/dependencies.gradle b/dependencies.gradle index 16615d30..dbc96eaa 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -4,7 +4,7 @@ dependencies { compileOnlyApi('com.github.GTNewHorizons:Angelica:1.0.0-beta40:api') api("com.github.GTNewHorizons:Mantle:0.5.0:dev") api("com.github.GTNewHorizons:ForgeMultipart:1.6.2:dev") - implementation("com.github.GTNewHorizons:NotEnoughItems:2.7.31-GTNH:dev") + implementation("com.github.GTNewHorizons:NotEnoughItems:2.7.34-GTNH:dev") compileOnlyApi("curse.maven:cofh-core-69162:2388751") compileOnly("com.github.GTNewHorizons:inventory-tweaks:1.7.1:api") compileOnly("com.github.GTNewHorizons:waila:1.8.4:api") @@ -15,4 +15,13 @@ dependencies { compileOnly("curse.maven:notenoughkeys-224614:2276280") compileOnly("thaumcraft:Thaumcraft:1.7.10-4.2.3.5:dev") compileOnly("com.github.GTNewHorizons:Mobs-Info:0.5.2-GTNH:dev") + compileOnlyApi("net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev") + compileOnlyApi("com.github.GTNewHorizons:Natura:2.7.6:dev") + + // For testing scythe crop harvesting + // devOnlyNonPublishable("net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev") + // devOnlyNonPublishable("com.github.GTNewHorizons:harvestcraft:1.3.1-GTNH:dev") + // devOnlyNonPublishable(deobfCurse("pams-harvest-the-nether-231262:2241397")) + // devOnlyNonPublishable("com.github.GTNewHorizons:Natura:2.7.6:dev") + } diff --git a/src/main/java/tconstruct/TConstruct.java b/src/main/java/tconstruct/TConstruct.java index 51b2c156..788b0f26 100644 --- a/src/main/java/tconstruct/TConstruct.java +++ b/src/main/java/tconstruct/TConstruct.java @@ -36,6 +36,8 @@ import mantle.pulsar.control.PulseManager; import tconstruct.achievements.AchievementEvents; import tconstruct.achievements.TAchievements; import tconstruct.api.TConstructAPI; +import tconstruct.api.harvesting.CropHarvestHandlers; +import tconstruct.api.harvesting.VanillaCropsHarvestHandler; import tconstruct.armor.TinkerArmor; import tconstruct.armor.player.TPlayerHandler; import tconstruct.armor.player.TPlayerStats; @@ -59,6 +61,7 @@ import tconstruct.plugins.imc.TinkerBuildCraft; import tconstruct.plugins.imc.TinkerMystcraft; import tconstruct.plugins.imc.TinkerRfTools; import tconstruct.plugins.mfr.TinkerMFR; +import tconstruct.plugins.natura.TinkerNatura; import tconstruct.plugins.te4.TinkerTE4; import tconstruct.plugins.te4.TinkersThermalFoundation; import tconstruct.plugins.ubc.TinkerUBC; @@ -172,6 +175,7 @@ public class TConstruct { pulsar.registerPulse(new TinkerUBC()); pulsar.registerPulse(new TinkerGears()); pulsar.registerPulse(new TinkerRfTools()); + pulsar.registerPulse(new TinkerNatura()); TConstructRegistry.materialTab = new TConstructCreativeTab("TConstructMaterials"); TConstructRegistry.toolTab = new TConstructCreativeTab("TConstructTools"); @@ -227,6 +231,7 @@ public class TConstruct { GameRegistry.registerWorldGenerator(new SlimeIslandGen(TinkerWorld.slimePool, 2), 2); pulsar.init(event); + CropHarvestHandlers.registerCropHarvestHandler(new VanillaCropsHarvestHandler()); } @EventHandler diff --git a/src/main/java/tconstruct/api/harvesting/CropHarvestHandler.java b/src/main/java/tconstruct/api/harvesting/CropHarvestHandler.java new file mode 100644 index 00000000..607cbe5b --- /dev/null +++ b/src/main/java/tconstruct/api/harvesting/CropHarvestHandler.java @@ -0,0 +1,12 @@ +package tconstruct.api.harvesting; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +public interface CropHarvestHandler { + + boolean couldHarvest(World world, int x, int y, int z); + + boolean tryHarvest(ItemStack stack, EntityPlayer player, World world, int x, int y, int z); +} diff --git a/src/main/java/tconstruct/api/harvesting/CropHarvestHandlers.java b/src/main/java/tconstruct/api/harvesting/CropHarvestHandlers.java new file mode 100644 index 00000000..9e03f088 --- /dev/null +++ b/src/main/java/tconstruct/api/harvesting/CropHarvestHandlers.java @@ -0,0 +1,18 @@ +package tconstruct.api.harvesting; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class CropHarvestHandlers { + + private static List cropHarvestHandlers = new ArrayList<>(); + + public static void registerCropHarvestHandler(CropHarvestHandler handler) { + cropHarvestHandlers.add(handler); + } + + public static List getCropHarvestHandlers() { + return Collections.unmodifiableList(cropHarvestHandlers); + } +} diff --git a/src/main/java/tconstruct/api/harvesting/VanillaCropsHarvestHandler.java b/src/main/java/tconstruct/api/harvesting/VanillaCropsHarvestHandler.java new file mode 100644 index 00000000..1609db8b --- /dev/null +++ b/src/main/java/tconstruct/api/harvesting/VanillaCropsHarvestHandler.java @@ -0,0 +1,32 @@ +package tconstruct.api.harvesting; + +import net.minecraft.block.BlockCrops; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +public class VanillaCropsHarvestHandler implements CropHarvestHandler { + + @Override + public boolean couldHarvest(World world, int x, int y, int z) { + return world.getBlock(x, y, z) instanceof BlockCrops; + } + + @Override + public boolean tryHarvest(ItemStack stack, EntityPlayer player, World world, int x, int y, int z) { + int blockMetadata = world.getBlockMetadata(x, y, z); + boolean shouldHarvest = couldHarvest(world, x, y, z) && blockMetadata >= 7; + if (shouldHarvest) { + world.getBlock(x, y, z).dropBlockAsItem( + world, + x, + y, + z, + world.getBlockMetadata(x, y, z), + EnchantmentHelper.getFortuneModifier(player)); + world.setBlockMetadataWithNotify(x, y, z, 0, 2); + } + return shouldHarvest; + } +} diff --git a/src/main/java/tconstruct/items/tools/Scythe.java b/src/main/java/tconstruct/items/tools/Scythe.java index c04edab8..03af0055 100644 --- a/src/main/java/tconstruct/items/tools/Scythe.java +++ b/src/main/java/tconstruct/items/tools/Scythe.java @@ -24,11 +24,14 @@ import net.minecraft.world.World; import net.minecraftforge.common.IShearable; import cpw.mods.fml.client.FMLClientHandler; +import tconstruct.api.harvesting.CropHarvestHandler; +import tconstruct.api.harvesting.CropHarvestHandlers; import tconstruct.library.ActiveToolMod; import tconstruct.library.TConstructRegistry; import tconstruct.library.tools.AbilityHelper; import tconstruct.library.tools.Weapon; import tconstruct.tools.TinkerTools; +import tconstruct.util.config.PHConstruct; public class Scythe extends Weapon { @@ -292,4 +295,55 @@ public class Scythe extends Weapon { } return true; } + + @Override + public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, + float hitX, float hitY, float hitZ) { + if (world.isRemote || !PHConstruct.scytheAoeHarvest) { + return false; + } + if (canPlayerHarvestCrop(world, x, y, z)) { + if (player.isSneaking()) { + tryHarvestCrop(stack, player, world, x, y, z); + } else { + harvestInAoe(stack, player, world, x, y, z); + } + return true; + } + return false; + } + + private boolean canPlayerHarvestCrop(World world, int x, int y, int z) { + for (CropHarvestHandler handler : CropHarvestHandlers.getCropHarvestHandlers()) { + if (handler.couldHarvest(world, x, y, z)) { + return true; + } + } + return false; + } + + private void harvestInAoe(ItemStack stack, EntityPlayer player, World world, int x, int y, int z) { + for (int i = -2; i < 3; i++) { + for (int j = -2; j < 3; j++) { + for (int k = -2; k < 3; k++) { + if (!(stack.getTagCompound().getCompoundTag("InfiTool").getBoolean("Broken"))) { + tryHarvestCrop(stack, player, world, x + i, y + j, z + k); + } + } + + } + } + } + + private static void tryHarvestCrop(ItemStack stack, EntityPlayer player, World world, int x, int y, int z) { + for (CropHarvestHandler handler : CropHarvestHandlers.getCropHarvestHandlers()) { + if (handler.couldHarvest(world, x, y, z)) { + boolean harvestSuccessful = handler.tryHarvest(stack, player, world, x, y, z); + if (harvestSuccessful && !player.capabilities.isCreativeMode) { + AbilityHelper.damageTool(stack, 1, null, false); + } + return; + } + } + } } diff --git a/src/main/java/tconstruct/plugins/ic2/Ic2CropHarvestHandler.java b/src/main/java/tconstruct/plugins/ic2/Ic2CropHarvestHandler.java new file mode 100644 index 00000000..86d7dffb --- /dev/null +++ b/src/main/java/tconstruct/plugins/ic2/Ic2CropHarvestHandler.java @@ -0,0 +1,22 @@ +package tconstruct.plugins.ic2; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +import ic2.api.crops.ICropTile; +import tconstruct.api.harvesting.CropHarvestHandler; + +public class Ic2CropHarvestHandler implements CropHarvestHandler { + + @Override + public boolean couldHarvest(World world, int x, int y, int z) { + return world.getTileEntity(x, y, z) instanceof ICropTile; + } + + @Override + public boolean tryHarvest(ItemStack stack, EntityPlayer player, World world, int x, int y, int z) { + return world.getTileEntity(x, y, z) instanceof ICropTile crop && crop.getCrop().canBeHarvested(crop) + && crop.harvest(true); + } +} diff --git a/src/main/java/tconstruct/plugins/ic2/TinkerIC2.java b/src/main/java/tconstruct/plugins/ic2/TinkerIC2.java index c730d72e..2a67070c 100644 --- a/src/main/java/tconstruct/plugins/ic2/TinkerIC2.java +++ b/src/main/java/tconstruct/plugins/ic2/TinkerIC2.java @@ -11,6 +11,7 @@ import cpw.mods.fml.common.registry.GameRegistry.ObjectHolder; import mantle.pulsar.pulse.Handler; import mantle.pulsar.pulse.Pulse; import tconstruct.TConstruct; +import tconstruct.api.harvesting.CropHarvestHandlers; import tconstruct.library.TConstructRegistry; import tconstruct.library.crafting.LiquidCasting; @@ -28,6 +29,8 @@ public class TinkerIC2 { public void init(FMLInitializationEvent event) { TConstruct.logger.info("IC2 detected. Preparing for shenanigans."); + CropHarvestHandlers.registerCropHarvestHandler(new Ic2CropHarvestHandler()); + Fluid fluidUUM = FluidRegistry.getFluid(IC2_UUM_FLUIDNAME); if (fluidUUM == null) return; diff --git a/src/main/java/tconstruct/plugins/natura/NaturaCropHarvestHandler.java b/src/main/java/tconstruct/plugins/natura/NaturaCropHarvestHandler.java new file mode 100644 index 00000000..d2d7aebc --- /dev/null +++ b/src/main/java/tconstruct/plugins/natura/NaturaCropHarvestHandler.java @@ -0,0 +1,32 @@ +package tconstruct.plugins.natura; + +import net.minecraft.block.Block; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +import mods.natura.blocks.crops.CropBlock; +import tconstruct.api.harvesting.CropHarvestHandler; + +public class NaturaCropHarvestHandler implements CropHarvestHandler { + + @Override + public boolean couldHarvest(World world, int x, int y, int z) { + return world.getBlock(x, y, z) instanceof CropBlock; + } + + @Override + public boolean tryHarvest(ItemStack stack, EntityPlayer player, World world, int x, int y, int z) { + Block block = world.getBlock(x, y, z); + if (block instanceof CropBlock crop) { + int meta = world.getBlockMetadata(x, y, z); + if (meta == crop.getMaxGrowth(meta)) { + crop.dropBlockAsItem(world, x, y, z, meta, EnchantmentHelper.getFortuneModifier(player)); + world.setBlockMetadataWithNotify(x, y, z, crop.getStartGrowth(meta), 2); + return true; + } + } + return false; + } +} diff --git a/src/main/java/tconstruct/plugins/natura/TinkerNatura.java b/src/main/java/tconstruct/plugins/natura/TinkerNatura.java new file mode 100644 index 00000000..8dca4817 --- /dev/null +++ b/src/main/java/tconstruct/plugins/natura/TinkerNatura.java @@ -0,0 +1,22 @@ +package tconstruct.plugins.natura; + +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.registry.GameRegistry; +import mantle.pulsar.pulse.Handler; +import mantle.pulsar.pulse.Pulse; +import tconstruct.TConstruct; +import tconstruct.api.harvesting.CropHarvestHandlers; + +@GameRegistry.ObjectHolder(TConstruct.modID) +@Pulse( + id = "Tinkers Natura Compatibility", + description = "Tinkers Construct compatibility for Natura", + modsRequired = "Natura", + forced = true) +public class TinkerNatura { + + @Handler + public void init(FMLInitializationEvent event) { + CropHarvestHandlers.registerCropHarvestHandler(new NaturaCropHarvestHandler()); + } +} diff --git a/src/main/java/tconstruct/util/config/PHConstruct.java b/src/main/java/tconstruct/util/config/PHConstruct.java index 74154d60..56e9d414 100644 --- a/src/main/java/tconstruct/util/config/PHConstruct.java +++ b/src/main/java/tconstruct/util/config/PHConstruct.java @@ -321,6 +321,12 @@ public class PHConstruct { "AluminumBrass", "For pack maintainers. Defines the LiquidType used to create metal casts.").getString(); + scytheAoeHarvest = config.get( + "NewFeatures", + "Scythe AOE harvest", + true, + "Can the Scythe harvest crops in an AOE on right click?.").getBoolean(); + /* Save the configuration file only if it has changed */ if (config.hasChanged()) config.save(); @@ -517,4 +523,7 @@ public class PHConstruct { // Crossmod interactions public static String metalCastFluidTypeName; + + // New features + public static boolean scytheAoeHarvest; }