Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SchnTgaiSpock committed Nov 5, 2024
1 parent ed9a7b3 commit 0674660
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public InputMatchResult match(List<ItemStack> givenItems) {
return getMatchProcedure().match(this, givenItems);
}

public InputMatchResult matchAs(MatchProcedure match, List<ItemStack> givenItems) {
return match.match(this, givenItems);
}

@Override
public abstract String toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.github.thebusybiscuit.slimefun4.api.recipes.items.RecipeOutputItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.matching.InputMatchResult;
import io.github.thebusybiscuit.slimefun4.api.recipes.matching.MatchProcedure;
import io.github.thebusybiscuit.slimefun4.api.recipes.matching.RecipeMatchResult;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

Expand Down Expand Up @@ -59,11 +60,11 @@ public static Recipe fromItemStacks(String id, ItemStack[] inputs, ItemStack out
);
}

public static Recipe fromItemStacks(ItemStack[] inputs, ItemStack output, int width, RecipeType type) {
public static Recipe fromItemStacks(ItemStack[] inputs, ItemStack output, RecipeType type, MatchProcedure match) {
return new Recipe(
Optional.empty(),
"other_recipes",
RecipeInput.fromItemStacks(inputs, type.getDefaultMatchProcedure()),
RecipeInput.fromItemStacks(inputs, match),
new RecipeOutput(List.of(new RecipeOutputItemStack(output))),
List.of(type),
Optional.empty(),
Expand All @@ -72,6 +73,10 @@ public static Recipe fromItemStacks(ItemStack[] inputs, ItemStack output, int wi
);
}

public static Recipe fromItemStacks(ItemStack[] inputs, ItemStack output, RecipeType type) {
return fromItemStacks(inputs, output, type, type.getDefaultMatchProcedure());
}


public Optional<String> getId() {
return id;
Expand Down Expand Up @@ -129,6 +134,11 @@ public RecipeMatchResult match(List<ItemStack> givenItems) {
return new RecipeMatchResult(this, result);
}

public RecipeMatchResult matchAs(MatchProcedure match, List<ItemStack> givenItems) {
InputMatchResult result = getInput().matchAs(match, givenItems);
return new RecipeMatchResult(this, result);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("Recipe { ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ItemMatchResult matchItem(ItemStack item, AbstractRecipeInputItem root) {
return result;
}
}
return new ItemMatchResult(false, root, item);
return new ItemMatchResult(false, root, item, 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class RecipeInputItem extends AbstractRecipeInputItem {

@Override
protected ItemMatchResult matchItem(ItemStack item, AbstractRecipeInputItem root) {
return new ItemMatchResult(item == null || item.getType().isAir(), this, item);
return new ItemMatchResult(item == null || item.getType().isAir(), this, item, 0);
}

@Override
Expand Down Expand Up @@ -63,11 +63,6 @@ public JsonElement serialize(JsonSerializationContext context) {
private int amount;
private int durabilityCost = 0;

public RecipeInputItem(int amount, int durabilityCost, double consumeChance) {
this.amount = amount;
this.durabilityCost = durabilityCost;
}

public RecipeInputItem(int amount, int durabilityCost) {
this.amount = amount;
this.durabilityCost = durabilityCost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public RecipeInputItemStack(ItemStack template, int durabilityCost) {
this.template = template;
}

@ParametersAreNonnullByDefault
public RecipeInputItemStack(ItemStack template) {
this(template, 0);
}

@ParametersAreNonnullByDefault
public RecipeInputItemStack(Material template, int amount, int durabilityCost) {
super(amount, durabilityCost);
Expand All @@ -36,8 +41,8 @@ public RecipeInputItemStack(Material template, int amount) {
}

@ParametersAreNonnullByDefault
public RecipeInputItemStack(ItemStack template) {
this(template, 0);
public RecipeInputItemStack(Material template) {
this(template, 1);
}

@Nonnull
Expand All @@ -46,10 +51,14 @@ public RecipeInputItemStack(ItemStack template) {

@Override
public ItemMatchResult matchItem(ItemStack item, AbstractRecipeInputItem root) {
if (item == null || item.getType().isAir()) {
return new ItemMatchResult(isEmpty(), root, item, getAmount());
} else if (item.getAmount() < getAmount()) {
return new ItemMatchResult(false, root, item, getAmount());
}
return new ItemMatchResult(
SlimefunUtils.isItemSimilar(item, template, false),
root,
item
root, item, getAmount()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ public RecipeInputSlimefunItem(String slimefunId, int amount) {
this(slimefunId, amount, 0);
}

public RecipeInputSlimefunItem(String slimefunId) {
this(slimefunId, 1);
}

public String getSlimefunId() { return slimefunId; }
public void setSlimefunId(String slimefunId) { this.slimefunId = slimefunId; }

@Override
public ItemMatchResult matchItem(ItemStack item, AbstractRecipeInputItem root) {
if (item == null || item.getType().isAir()) {
return new ItemMatchResult(isEmpty(), root, item, getAmount());
} else if (item.getAmount() < getAmount()) {
return new ItemMatchResult(false, root, item, getAmount());
}
return new ItemMatchResult(
SlimefunUtils.isItemSimilar(item, SlimefunItem.getById(slimefunId).getItem(), false),
root,
item
root, item, getAmount()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@ public RecipeInputTag(Tag<Material> tag) {
this(tag, 1);
}


public Tag<Material> getTag() { return tag; }
public void setTag(Tag<Material> tag) { this.tag = tag; }

@Override
public ItemMatchResult matchItem(ItemStack item, AbstractRecipeInputItem root) {
if (item == null || item.getType().isAir()) {
return new ItemMatchResult(isEmpty(), root, item, getAmount());
} else if (item.getAmount() < getAmount()) {
return new ItemMatchResult(false, root, item, getAmount());
}
for (Material mat : tag.getValues()) {
ItemStack template = new ItemStack(mat);
if (SlimefunUtils.isItemSimilar(item, template, true)) {
return new ItemMatchResult(
SlimefunUtils.isItemSimilar(item, template, false),
root,
item
root, item, getAmount()
);
}
}
return new ItemMatchResult(false, root, item);
return new ItemMatchResult(false, root, item, getAmount());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@ public class RecipeOutputItemStack extends RecipeOutputItem {

private ItemStack template;

public RecipeOutputItemStack(ItemStack template, double consumeChance) {
super(template.getAmount(), consumeChance);
this.template = template;
}

public RecipeOutputItemStack(ItemStack template) {
super(template.getAmount());
this.template = template;
}

public RecipeOutputItemStack(Material template, int amount, double consumeChance) {
super(amount, consumeChance);
this.template = new ItemStack(template, amount);
}

public RecipeOutputItemStack(Material template, int amount) {
super(amount);
this.template = new ItemStack(template, amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public class RecipeOutputSlimefunItem extends RecipeOutputItem {

private String slimefunId;

public RecipeOutputSlimefunItem(String slimefunId, int amount, double consumeChance) {
super(amount, consumeChance);
this.slimefunId = slimefunId;
}

public RecipeOutputSlimefunItem(String slimefunId, int amount) {
super(amount);
this.slimefunId = slimefunId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
import java.util.Collections;
import java.util.List;

import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.recipes.AbstractRecipeInput;

public class InputMatchResult {

private final AbstractRecipeInput input;
private final List<ItemMatchResult> itemMatchResults;
private final boolean itemsMatch;
private int possibleCrafts;

public InputMatchResult(AbstractRecipeInput input, List<ItemMatchResult> itemMatchResults, boolean itemsMatch) {
this.input = input;
this.itemMatchResults = itemMatchResults;
this.itemsMatch = itemsMatch;
possibleCrafts = 2147483647;
for (ItemMatchResult res : itemMatchResults) {
ItemStack item = res.getMatchedItem();
if (item == null || res.getRecipeItem().isEmpty()) {
continue;
}
possibleCrafts = Math.min(possibleCrafts, item.getAmount() / res.getConsumeAmount());
}
}

public InputMatchResult(AbstractRecipeInput input, List<ItemMatchResult> itemMatchResults) {
Expand All @@ -33,6 +44,10 @@ public boolean itemsMatch() {
public AbstractRecipeInput getInput() {
return input;
}

public int getPossibleCrafts() {
return possibleCrafts;
}

/**
* <b>IMPORTANT</b> If itemsMatch() is false, then not all match results may be present
Expand All @@ -44,5 +59,30 @@ public List<ItemMatchResult> getItemMatchResults() {
public boolean itemsMatch() {
return itemsMatch;
}

/**
* Consumes the items that were used in matching
* the recipe, based on the amount of input required
* in the recipe.
* @param amount Number of times to consume. May
* be less if there are not enough items.
* @return How many times the inputs were actually consumed
*/
public int consumeItems(int amount) {
if (possibleCrafts == 0 || amount == 0) {
return 0;
}

int amountToCraft = Math.min(possibleCrafts, amount);
for (ItemMatchResult res : itemMatchResults) {
ItemStack item = res.getMatchedItem();
if (item == null || res.getRecipeItem().isEmpty()) {
continue;
}
item.setAmount(item.getAmount() - amountToCraft * res.getConsumeAmount());
}
possibleCrafts -= amountToCraft;
return amountToCraft;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class ItemMatchResult {
private final boolean itemsMatch;
private final AbstractRecipeInputItem recipeItem;
private final @Nullable ItemStack matchedItem;
private final int consumeAmount;

public ItemMatchResult(boolean itemsMatch, AbstractRecipeInputItem recipeItem, ItemStack matchedItem) {
public ItemMatchResult(boolean itemsMatch, AbstractRecipeInputItem recipeItem, ItemStack matchedItem, int consumeAmount) {
this.itemsMatch = itemsMatch;
this.recipeItem = recipeItem;
this.matchedItem = matchedItem;
this.consumeAmount = consumeAmount;
}

/**
Expand All @@ -31,5 +33,11 @@ public ItemMatchResult(boolean itemsMatch, AbstractRecipeInputItem recipeItem, I
*/
@Nullable
public ItemStack getMatchedItem() { return matchedItem; }
/**
* @return How much of the item to consume when crafting
*/
public int getConsumeAmount() {
return consumeAmount;
}

}
Loading

0 comments on commit 0674660

Please sign in to comment.