2016-01-08 00:32:03 -08:00
|
|
|
package WayofTime.bloodmagic.ritual.harvest;
|
|
|
|
|
2017-06-21 18:46:42 -07:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
2016-03-17 13:00:44 -07:00
|
|
|
import WayofTime.bloodmagic.api.BlockStack;
|
|
|
|
import WayofTime.bloodmagic.api.iface.IHarvestHandler;
|
|
|
|
import WayofTime.bloodmagic.api.registry.HarvestRegistry;
|
2016-01-08 16:14:27 -08:00
|
|
|
import net.minecraft.block.Block;
|
2017-06-21 18:46:42 -07:00
|
|
|
import net.minecraft.block.BlockCrops;
|
2016-01-08 00:32:03 -08:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2017-05-21 13:08:29 -07:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-01-08 00:32:03 -08:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.IPlantable;
|
2017-05-21 13:08:29 -07:00
|
|
|
import net.minecraftforge.fml.common.Loader;
|
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
2017-06-21 18:46:42 -07:00
|
|
|
import net.minecraftforge.fml.relauncher.ReflectionHelper;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
|
|
|
import java.util.List;
|
2016-01-08 00:32:03 -08:00
|
|
|
|
2016-01-09 01:54:25 -08:00
|
|
|
/**
|
2016-03-16 18:41:06 -04:00
|
|
|
* Harvest handler for standard plantable crops such as Wheat, Potatoes, and
|
|
|
|
* Netherwart. <br>
|
2016-01-09 01:54:25 -08:00
|
|
|
* Register a new crop for this handler with
|
|
|
|
* {@link HarvestRegistry#registerStandardCrop(Block, int)}
|
|
|
|
*/
|
2016-01-08 00:32:03 -08:00
|
|
|
public class HarvestHandlerPlantable implements IHarvestHandler
|
|
|
|
{
|
|
|
|
public HarvestHandlerPlantable()
|
|
|
|
{
|
2016-04-24 10:06:28 -07:00
|
|
|
HarvestRegistry.registerStandardCrop(Blocks.CARROTS, 7);
|
|
|
|
HarvestRegistry.registerStandardCrop(Blocks.WHEAT, 7);
|
|
|
|
HarvestRegistry.registerStandardCrop(Blocks.POTATOES, 7);
|
|
|
|
HarvestRegistry.registerStandardCrop(Blocks.BEETROOTS, 3);
|
|
|
|
HarvestRegistry.registerStandardCrop(Blocks.NETHER_WART, 3);
|
2017-05-21 13:08:29 -07:00
|
|
|
|
|
|
|
addThirdPartyCrop("actuallyadditions", "blockFlax", 7);
|
|
|
|
addThirdPartyCrop("actuallyadditions", "blockCanola", 7);
|
|
|
|
addThirdPartyCrop("actuallyadditions", "blockRice", 7);
|
|
|
|
|
|
|
|
addThirdPartyCrop("extrautils2", "redorchid", 6);
|
|
|
|
addThirdPartyCrop("extrautils2", "enderlily", 7);
|
2017-06-21 18:46:42 -07:00
|
|
|
|
|
|
|
addThirdPartyCrop("roots", "moonglow", 7);
|
|
|
|
addThirdPartyCrop("roots", "terra_moss", 7);
|
|
|
|
addThirdPartyCrop("roots", "pereskia", 7);
|
|
|
|
addThirdPartyCrop("roots", "wildroot", 7);
|
|
|
|
addThirdPartyCrop("roots", "aubergine", 7);
|
|
|
|
addThirdPartyCrop("roots", "spirit_herb", 7);
|
|
|
|
|
|
|
|
addPamCrops();
|
2016-01-08 00:32:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean harvestAndPlant(World world, BlockPos pos, BlockStack blockStack)
|
|
|
|
{
|
2016-01-09 01:54:25 -08:00
|
|
|
if (!HarvestRegistry.getStandardCrops().containsKey(blockStack.getBlock()))
|
2016-01-08 00:32:03 -08:00
|
|
|
return false;
|
|
|
|
|
2016-01-09 01:54:25 -08:00
|
|
|
int matureMeta = HarvestRegistry.getStandardCrops().get(blockStack.getBlock());
|
2016-01-08 00:32:03 -08:00
|
|
|
|
2016-03-16 18:41:06 -04:00
|
|
|
if (blockStack.getMeta() < matureMeta)
|
2016-01-08 00:32:03 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
List<ItemStack> drops = blockStack.getBlock().getDrops(world, pos, blockStack.getState(), 0);
|
|
|
|
boolean foundSeed = false;
|
|
|
|
|
2016-03-16 18:41:06 -04:00
|
|
|
for (ItemStack stack : drops)
|
|
|
|
{
|
2016-01-08 00:32:03 -08:00
|
|
|
if (stack == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (stack.getItem() instanceof IPlantable)
|
|
|
|
{
|
2017-05-21 13:08:29 -07:00
|
|
|
stack.stackSize--;
|
2016-01-08 00:32:03 -08:00
|
|
|
foundSeed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundSeed)
|
|
|
|
{
|
2016-01-08 16:14:27 -08:00
|
|
|
world.setBlockState(pos, blockStack.getBlock().getDefaultState());
|
2016-06-05 16:55:01 -07:00
|
|
|
world.playEvent(2001, pos, Block.getStateId(blockStack.getState()));
|
2016-01-08 00:32:03 -08:00
|
|
|
for (ItemStack stack : drops)
|
|
|
|
{
|
2017-05-21 13:08:29 -07:00
|
|
|
if (stack == null || stack.stackSize <= 0)
|
|
|
|
continue;
|
|
|
|
|
2016-01-08 00:32:03 -08:00
|
|
|
if (!world.isRemote)
|
|
|
|
{
|
|
|
|
EntityItem toDrop = new EntityItem(world, pos.getX(), pos.getY() + 0.5, pos.getZ(), stack);
|
2017-02-04 22:43:18 -08:00
|
|
|
world.spawnEntity(toDrop);
|
2016-01-08 00:32:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-21 13:08:29 -07:00
|
|
|
|
|
|
|
private static void addThirdPartyCrop(String modid, String regName, int matureMeta)
|
|
|
|
{
|
|
|
|
if (!Loader.isModLoaded(modid))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(modid, regName));
|
|
|
|
if (block != null && block != Blocks.AIR)
|
|
|
|
HarvestRegistry.registerStandardCrop(block, matureMeta);
|
|
|
|
}
|
2017-06-21 18:46:42 -07:00
|
|
|
|
|
|
|
private static void addPamCrops()
|
|
|
|
{
|
|
|
|
if (!Loader.isModLoaded("harvestcraft"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ClassLoader loader = HarvestHandlerPlantable.class.getClassLoader();
|
|
|
|
String className = "com.pam.harvestcraft.blocks.CropRegistry";
|
|
|
|
Class<?> registry = ReflectionHelper.getClass(loader, className);
|
|
|
|
Field names = ReflectionHelper.findField(registry, "cropNames");
|
|
|
|
Method getCrop = registry.getMethod("getCrop", String.class);
|
|
|
|
for (String name : (String[])names.get(null)) {
|
|
|
|
BlockCrops crop = (BlockCrops) getCrop.invoke(null, name);
|
|
|
|
HarvestRegistry.registerStandardCrop(crop, crop.getMaxAge());
|
|
|
|
}
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop name mapper");
|
|
|
|
return;
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; crop name lookup broke");
|
|
|
|
return;
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; crop name lookup broke");
|
|
|
|
return;
|
|
|
|
} catch (ReflectionHelper.UnableToFindClassException e) {
|
|
|
|
BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop registry");
|
|
|
|
return;
|
|
|
|
} catch (ReflectionHelper.UnableToFindFieldException e) {
|
|
|
|
BloodMagic.instance.getLogger().error("HarvestCraft integration cancelled; unable to find crop names in registry");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 00:32:03 -08:00
|
|
|
}
|