Skip to content

Commit

Permalink
Remove IAgriProduct
Browse files Browse the repository at this point in the history
The idea sounded good at the start but wound up being too restrictive.
  • Loading branch information
RlonRyan committed Jun 12, 2017
1 parent ae90f69 commit 185f370
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface IAgriCrop extends IAgriSeedProvider, IAgriSeedAcceptor, IAgriFe
* Converts this crop to a crosscrop or a regular crop
*
* @param status true for crosscrop, false for regular crop
*
* @return if the cross crop was successfully set.
*/
boolean setCrossCrop(boolean status);

Expand All @@ -78,14 +80,6 @@ default boolean isFertile(AgriSeed seed) {
*/
boolean isMature();

default boolean spawn() {
return false;
}

default boolean spread() {
return false;
}

Optional<IAgriSoil> getSoil();

// =========================================================================
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*/
package com.infinityraider.agricraft.api.v1.misc;

import com.infinityraider.agricraft.api.v1.util.MethodResult;
import net.minecraft.entity.player.EntityPlayer;

/**
Expand All @@ -23,6 +24,6 @@ public interface IAgriHarvestable {
* harvested by automation.
* @return if the harvest was successful.
*/
boolean onHarvest(EntityPlayer player);
MethodResult onHarvest(EntityPlayer player);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.infinityraider.agricraft.api.v1.plant;

import com.infinityraider.agricraft.api.v1.misc.IAgriHarvestProduct;
import com.infinityraider.agricraft.api.v1.crop.IAgriCrop;
import com.infinityraider.agricraft.api.v1.misc.IAgriRegisterable;
import com.infinityraider.agricraft.api.v1.render.RenderMethod;
import com.infinityraider.agricraft.api.v1.requirement.IGrowthRequirement;
import com.infinityraider.agricraft.api.v1.stat.IAgriStat;
import com.infinityraider.agricraft.api.v1.util.FuzzyStack;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nonnull;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -201,15 +204,32 @@ default boolean isAggressive() {
IGrowthRequirement getGrowthRequirement();

/**
* Retrieves a list of all possible products of this plant. This should be a
* unmodifiable view of a constant list. Notice this is used as the master
* list of products for AgriCraft, in that it will be used for both
* harvesting drops, and for display in both the Seed Journal and JEI (if
* the product is not hidden).
* Retrieves all possible products of harvesting this plant. Note, this
* function is to be used for informational purposes only, i.e. for display
* in the seed journal and JEI. As such, this method is not required as to
* provide the complete set of all items that this plant could actually drop
* on a harvest event, but rather is required to list all the products that
* should show up in the journal. Consequently, if you wish for your plant
* to produce a hidden easter-egg style product, it should not be listed
* here!
*
* @return A list containing all the possible products of the plant.
* @param products a consumer for collecting all the possible plant products
* that should be listed.
*/
List<IAgriHarvestProduct> getProducts();
void getPossibleProducts(@Nonnull Consumer<ItemStack> products);

/**
* Retrieves the products that would be produced upon harvesting the given
* plant, with the given stat, at the given position in the given world.
* This method will always be called on harvest events, including any time
* that the containing crop is broken. As such, it is important as to check the actual passed growth value of the plant, given that the plant is not garnteed as to be mature when this method is called.
*
* @param products a consumer for collecting all the possible plant harvest products that should be dropped.
* @param crop the crop instance that contains this plant.
* @param stat the stats associated with this instance of the plant.
* @param rand a random for use in rng.
*/
void getHarvestProducts(@Nonnull Consumer<ItemStack> products, @Nonnull IAgriCrop crop, @Nonnull IAgriStat stat, @Nonnull Random rand);

// =========================================================================
// IAgriPlant Rendering Interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.infinityraider.infinitylib.render.block.BlockRendererRegistry;
import com.infinityraider.infinitylib.utility.RegisterHelper;
import java.util.Set;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.registry.GameRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IRecipeWrapper getRecipeWrapper(@Nonnull IAgriPlant recipe) {

@Override
public boolean isRecipeValid(@Nonnull IAgriPlant recipe) {
return recipe.getProducts() != null;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*/
package com.infinityraider.agricraft.compat.jei.produce;

import com.infinityraider.agricraft.api.v1.misc.IAgriHarvestProduct;
import com.infinityraider.agricraft.api.v1.plant.IAgriPlant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.BlankRecipeWrapper;
import net.minecraft.init.Blocks;
Expand All @@ -23,19 +21,25 @@ public class ProduceRecipeWrapper extends BlankRecipeWrapper {
private final List<ItemStack> output;

public ProduceRecipeWrapper(IAgriPlant recipe) {
input = new ArrayList();
output = recipe.getProducts().stream()
.map(IAgriHarvestProduct::toLabeledStack)
.collect(Collectors.toList());

// Allocate Lists
input = new ArrayList<>();
output = new ArrayList<>();

// Fill Output List
recipe.getPossibleProducts(output::add);

// Add Seed to Input List
input.add(recipe.getSeed());

// Add Required Soil to Input List
input.add(recipe.getGrowthRequirement().getSoils().stream()
.flatMap(s -> s.getVarients().stream())
.findFirst()
.map(s -> s.toStack())
.orElse(new ItemStack(Blocks.FARMLAND))
);

// Add Representative Growth Requirement Stack to Input List
recipe.getGrowthRequirement().getConditionStack()
.map(b -> b.toStack())
.ifPresent(input::add);
Expand Down
42 changes: 19 additions & 23 deletions src/main/java/com/infinityraider/agricraft/core/JsonPlant.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

import com.agricraft.agricore.core.AgriCore;
import com.agricraft.agricore.plant.AgriPlant;
import com.agricraft.agricore.plant.AgriProduct;
import com.agricraft.agricore.util.TypeHelper;
import com.infinityraider.agricraft.api.v1.misc.IAgriHarvestProduct;
import com.infinityraider.agricraft.api.v1.crop.IAgriCrop;
import com.infinityraider.agricraft.api.v1.plant.IAgriPlant;
import com.infinityraider.agricraft.api.v1.render.RenderMethod;
import com.infinityraider.agricraft.api.v1.requirement.BlockCondition;
import com.infinityraider.agricraft.api.v1.requirement.IGrowthReqBuilder;
import com.infinityraider.agricraft.api.v1.requirement.IGrowthRequirement;
import com.infinityraider.agricraft.api.v1.stat.IAgriStat;
import com.infinityraider.agricraft.api.v1.util.BlockRange;
import com.infinityraider.agricraft.api.v1.util.FuzzyStack;
import com.infinityraider.agricraft.impl.v1.AgriHarvestProduct;
import com.infinityraider.agricraft.farming.PlantStats;
import com.infinityraider.agricraft.farming.growthrequirement.GrowthReqBuilder;
import com.infinityraider.agricraft.init.AgriItems;
Expand All @@ -29,8 +27,9 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.ItemStack;
Expand All @@ -46,13 +45,11 @@ public class JsonPlant implements IAgriPlant {
private final AgriPlant plant;

private final List<FuzzyStack> seedItems;
private final List<IAgriHarvestProduct> products;
private final IGrowthRequirement growthRequirement;

public JsonPlant(AgriPlant plant) {
this.plant = Objects.requireNonNull(plant, "A JSONPlant may not consist of a null AgriPlant! Why would you even try that!?");
this.seedItems = initSeedItemsListJSON(plant);
this.products = initProductListJSON(plant);
this.growthRequirement = initGrowthRequirementJSON(plant);
}

Expand Down Expand Up @@ -150,8 +147,21 @@ public int getGrowthStages() {
}

@Override
public List<IAgriHarvestProduct> getProducts() {
return Collections.unmodifiableList(this.products);
public void getPossibleProducts(Consumer<ItemStack> products) {
this.plant.getProducts().getAll().stream()
.map(p -> p.toStack(FuzzyStack.class))
.filter(Optional::isPresent)
.map(p -> p.get().toStack())
.forEach(products);
}

@Override
public void getHarvestProducts(Consumer<ItemStack> products, IAgriCrop crop, IAgriStat stat, Random rand) {
this.plant.getProducts().getRandom(rand).stream()
.map(p -> p.toStack(FuzzyStack.class))
.filter(Optional::isPresent)
.map(p -> p.get().toStack())
.forEach(products);
}

@Override
Expand Down Expand Up @@ -205,20 +215,6 @@ public List<BakedQuad> getPlantQuads(IExtendedBlockState state, int growthStage,
return Collections.emptyList();
}

public static final List<IAgriHarvestProduct> initProductListJSON(AgriPlant plant) {
return plant.getProducts().getAll().stream()
.map(JsonPlant::convertProductJSON)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}

public static final Optional<IAgriHarvestProduct> convertProductJSON(AgriProduct product) {
return product
.toStack(FuzzyStack.class)
.map(stack -> new AgriHarvestProduct(stack.getItem(), stack.getTagCompound(), stack.getMeta(), product.getMin(), product.getMax(), product.getChance(), false));
}

public static final List<FuzzyStack> initSeedItemsListJSON(AgriPlant plant) {
final List<FuzzyStack> seeds = new ArrayList<>(plant.getSeedItems().size());
plant.getSeedItems().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.agricraft.agricore.core.AgriCore;
import com.infinityraider.agricraft.api.v1.AgriApi;
import com.infinityraider.agricraft.api.v1.misc.IAgriHarvestProduct;
import com.infinityraider.agricraft.api.v1.mutation.IAgriMutation;
import com.infinityraider.agricraft.api.v1.plant.IAgriPlant;
import com.infinityraider.agricraft.gui.component.BasicComponents;
Expand All @@ -11,6 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -129,11 +129,10 @@ private void addSeed(List<GuiComponent> components) {
}

private void addFruits(List<GuiComponent> components) {
int x = 30;
for (IAgriHarvestProduct product : discoveredSeeds.get(page).getProducts()) {
components.add(BasicComponents.getStackComponentFramed(product.toLabeledStack(), x, 102));
x += 24;
}
final AtomicInteger x = new AtomicInteger(30);
discoveredSeeds.get(page).getPossibleProducts(
p -> components.add(BasicComponents.getStackComponentFramed(p, x.getAndAdd(24), 102))
);
}

private void addSeeds(List<GuiComponent> components) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import com.infinityraider.agricraft.api.v1.AgriApi;
import com.infinityraider.agricraft.api.v1.items.IAgriClipperItem;
import com.infinityraider.agricraft.api.v1.items.IAgriTrowelItem;
import com.infinityraider.agricraft.api.v1.misc.IAgriHarvestProduct;
import com.infinityraider.agricraft.api.v1.seed.AgriSeed;
import com.infinityraider.agricraft.reference.AgriCraftConfig;
import com.infinityraider.agricraft.utility.StackHelper;
import com.mojang.realmsclient.gui.ChatFormatting;
import java.text.DecimalFormat;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
Expand Down Expand Up @@ -52,20 +50,20 @@ public void addSeedStatsTooltip(ItemTooltipEvent event) {
}
}

@SubscribeEvent
public void addProductInfo(ItemTooltipEvent event) {
if (StackHelper.hasTag(event.getItemStack())) {
final NBTTagCompound tag = StackHelper.getTag(event.getItemStack());
if (tag.hasKey(IAgriHarvestProduct.PRODUCT_MARKER_TAG)) {
final int minAmount = tag.getInteger(IAgriHarvestProduct.PRODUCT_MIN_TAG);
final int maxAmount = tag.getInteger(IAgriHarvestProduct.PRODUCT_MAX_TAG);
final double chance = tag.getDouble(IAgriHarvestProduct.PRODUCT_CHANCE_TAG);
event.getToolTip().add(ChatFormatting.GRAY + "Chance: " + DecimalFormat.getPercentInstance().format(chance));
event.getToolTip().add(ChatFormatting.GRAY + "Min. Amount: " + minAmount);
event.getToolTip().add(ChatFormatting.GRAY + "Max. Amount: " + maxAmount);
}
}
}
// @SubscribeEvent
// public void addProductInfo(ItemTooltipEvent event) {
// if (StackHelper.hasTag(event.getItemStack())) {
// final NBTTagCompound tag = StackHelper.getTag(event.getItemStack());
// if (tag.hasKey(IAgriHarvestProduct.PRODUCT_MARKER_TAG)) {
// final int minAmount = tag.getInteger(IAgriHarvestProduct.PRODUCT_MIN_TAG);
// final int maxAmount = tag.getInteger(IAgriHarvestProduct.PRODUCT_MAX_TAG);
// final double chance = tag.getDouble(IAgriHarvestProduct.PRODUCT_CHANCE_TAG);
// event.getToolTip().add(ChatFormatting.GRAY + "Chance: " + DecimalFormat.getPercentInstance().format(chance));
// event.getToolTip().add(ChatFormatting.GRAY + "Min. Amount: " + minAmount);
// event.getToolTip().add(ChatFormatting.GRAY + "Max. Amount: " + maxAmount);
// }
// }
// }

@SubscribeEvent
public void addNbtInfo(ItemTooltipEvent event) {
Expand Down
Loading

0 comments on commit 185f370

Please sign in to comment.