From bcd0948f2a7de0daafa9458e2976f67de55b65cd Mon Sep 17 00:00:00 2001 From: YannickMG Date: Sun, 30 Mar 2025 23:04:46 -0400 Subject: [PATCH] Allow Scythe to harvest IC2 crops --- dependencies.gradle | 4 ++ .../api/harvesting/CropHarvestHandler.java | 12 +++++ .../api/harvesting/CropHarvestHandlers.java | 18 +++++++ .../java/tconstruct/items/tools/Scythe.java | 49 +++++++++++++++++++ .../plugins/ic2/Ic2CropHarvestHandler.java | 22 +++++++++ .../tconstruct/plugins/ic2/TinkerIC2.java | 3 ++ 6 files changed, 108 insertions(+) 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/plugins/ic2/Ic2CropHarvestHandler.java diff --git a/dependencies.gradle b/dependencies.gradle index b27a907c..06d5a97a 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -15,4 +15,8 @@ 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") + +// devOnlyNonPublishable("net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev") + } 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/items/tools/Scythe.java b/src/main/java/tconstruct/items/tools/Scythe.java index c04edab8..cb386fef 100644 --- a/src/main/java/tconstruct/items/tools/Scythe.java +++ b/src/main/java/tconstruct/items/tools/Scythe.java @@ -24,6 +24,8 @@ 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; @@ -292,4 +294,51 @@ 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) { + return false; + } + if (shouldHarvestInAoe(world, x, y, z)) { + harvestInAoe(stack, player, world, x, y, z); + return true; + } + return false; + } + + private boolean shouldHarvestInAoe(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"))) { + harvestCrop(stack, player, world, x + i, y + j, z + k); + } + } + + } + } + } + + private static void harvestCrop(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;