Allow the Scythe to harvest crops in an AOE (#159)

This commit is contained in:
Yannick Marcotte-Gourde authored and GitHub committed 2025-04-02 12:07:47 -05:00
1 parent eb0ac72963
commit d370627703
11 files changed
+219 -1

No files matched your search

+5
View File
@@ -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
@@ -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);
}
@@ -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<CropHarvestHandler> cropHarvestHandlers = new ArrayList<>();
public static void registerCropHarvestHandler(CropHarvestHandler handler) {
cropHarvestHandlers.add(handler);
}
public static List<CropHarvestHandler> getCropHarvestHandlers() {
return Collections.unmodifiableList(cropHarvestHandlers);
}
}
@@ -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;
}
}
@@ -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;
}
}
}
}
@@ -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);
}
}
@@ -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;
@@ -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;
}
}
@@ -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());
}
}
@@ -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;
}