Skip to content

Commit

Permalink
Make modifier recipes support larger stack sizes
Browse files Browse the repository at this point in the history
Backport of 6cb4189 (per request)
  • Loading branch information
KnightMiner committed Mar 16, 2022
1 parent ea5f03d commit 30ee6e8
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class AbstractModifierRecipe implements ITinkerStationRecipe, ID
private final ResourceLocation id;
/** Ingredient representing the required tool, typically a tag */
protected final Ingredient toolRequirement;
/** Max size of the tool for this modifier. If the tool size is smaller, the stack will reduce by less */
protected final int maxToolSize;
/** Modifiers that must match for this recipe */
protected final ModifierMatch requirements;
/** Error message to display if the requirements do not match */
Expand All @@ -61,12 +63,13 @@ public abstract class AbstractModifierRecipe implements ITinkerStationRecipe, ID
@Nullable
private final SlotCount slots;

/** @deprecated Use {@link #AbstractModifierRecipe(ResourceLocation, Ingredient, ModifierMatch, String, ModifierEntry, int, SlotCount)} */
/** @deprecated Use {@link #AbstractModifierRecipe(ResourceLocation, Ingredient, int, ModifierMatch, String, ModifierEntry, int, SlotCount)} */
@Deprecated
protected AbstractModifierRecipe(ResourceLocation id, Ingredient toolRequirement, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, int upgradeSlots, int abilitySlots) {
this.id = id;
this.toolRequirement = toolRequirement;
this.maxToolSize = ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE;
this.requirements = requirements;
this.requirementsError = requirementsError;
this.result = result;
Expand All @@ -83,8 +86,14 @@ protected AbstractModifierRecipe(ResourceLocation id, Ingredient toolRequirement

protected AbstractModifierRecipe(ResourceLocation id, Ingredient toolRequirement, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
this(id, toolRequirement, ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE, requirements, requirementsError, result, maxLevel, slots);
}

protected AbstractModifierRecipe(ResourceLocation id, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
this.id = id;
this.toolRequirement = toolRequirement;
this.maxToolSize = maxToolSize;
this.requirements = requirements;
this.requirementsError = requirementsError;
this.result = result;
Expand All @@ -93,7 +102,7 @@ protected AbstractModifierRecipe(ResourceLocation id, Ingredient toolRequirement
ModifierRecipeLookup.addRequirements(toolRequirement, result, requirements, requirementsError);
}

@Override
@Override
public abstract ValidatedResult getValidatedResult(ITinkerStationInventory inv);

/** @deprecated */
Expand All @@ -102,6 +111,10 @@ public ItemStack getRecipeOutput() {
return ItemStack.EMPTY;
}

@Override
public int shrinkToolSlotBy() {
return maxToolSize;
}

/* JEI display */
/** Cache of input items shared between result and input */
Expand Down Expand Up @@ -263,17 +276,31 @@ protected ModifierEntry readResult(JsonObject json) {
* Reads any remaining data from the modifier recipe
* @return Full recipe instance
*/
public T read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, ModifierMatch requirements,
public T read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
int upgradeSlots = SlotCount.get(slots, SlotType.UPGRADE);
int abilitySlots = SlotCount.get(slots, SlotType.ABILITY);
return read(id, json, toolRequirement, requirements, requirementsError, result, maxLevel, upgradeSlots, abilitySlots);
return read(id, json, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
}

/**
* Reads any remaining data from the modifier recipe
* @return Full recipe instance
*/
public T read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
return read(id, buffer, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
}

/** @deprecated use {@link #read(ResourceLocation, JsonObject, Ingredient, int, ModifierMatch, String, ModifierEntry, int, SlotCount)} */
@Deprecated
public T read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
int upgradeSlots = SlotCount.get(slots, SlotType.UPGRADE);
int abilitySlots = SlotCount.get(slots, SlotType.ABILITY);
return read(id, json, toolRequirement, requirements, requirementsError, result, maxLevel, upgradeSlots, abilitySlots);
}

/** @deprecated use {@link #read(ResourceLocation, PacketBuffer, Ingredient, int, ModifierMatch, String, ModifierEntry, int, SlotCount)} */
@Deprecated
public T read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
int upgradeSlots = SlotCount.get(slots, SlotType.UPGRADE);
Expand All @@ -294,6 +321,7 @@ public abstract T read(ResourceLocation id, PacketBuffer buffer, Ingredient tool
@Override
public final T read(ResourceLocation id, JsonObject json) {
Ingredient toolRequirement = Ingredient.deserialize(json.get("tools"));
int maxToolSize = JSONUtils.getInt(json, "max_tool_size", ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE);
ModifierMatch requirements = ModifierMatch.ALWAYS;
String requirementsError = "";
if (json.has("requirements")) {
Expand Down Expand Up @@ -322,24 +350,26 @@ public final T read(ResourceLocation id, JsonObject json) {
TConstruct.LOG.warn("Using deprecated modifier recipe key ability_slots for recipe " + id);
}
}
return read(id, json, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
return read(id, json, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
}

@Override
protected final T readSafe(ResourceLocation id, PacketBuffer buffer) {
Ingredient toolRequirement = Ingredient.read(buffer);
int maxToolSize = buffer.readVarInt();
ModifierMatch requirements = ModifierMatch.read(buffer);
String requirementsError = buffer.readString(Short.MAX_VALUE);
ModifierEntry result = ModifierEntry.read(buffer);
int maxLevel = buffer.readVarInt();
SlotCount slots = SlotCount.read(buffer);
return read(id, buffer, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
return read(id, buffer, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
}

/** Writes relevant packet data. When overriding, call super first for consistency with {@link #read(ResourceLocation, PacketBuffer)} */
@Override
protected void writeSafe(PacketBuffer buffer, T recipe) {
recipe.toolRequirement.write(buffer);
buffer.writeVarInt(recipe.maxToolSize);
recipe.requirements.write(buffer);
buffer.writeString(recipe.requirementsError);
recipe.result.write(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.recipe.modifiers.ModifierMatch;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe;
import slimeknights.tconstruct.library.tools.SlotType;
import slimeknights.tconstruct.tools.TinkerModifiers;

Expand All @@ -31,6 +32,7 @@ public abstract class AbstractModifierRecipeBuilder<T extends AbstractModifierRe
// shared
protected final ModifierEntry result;
protected Ingredient tools = Ingredient.EMPTY;
protected int maxToolSize = ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE;
protected SlotType slotType;
protected int slots;
protected int maxLevel = 0;
Expand All @@ -54,7 +56,18 @@ public T includeUnarmed() {
* @return Builder instance
*/
public T setTools(Ingredient tools) {
return setTools(tools, ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE);
}

/**
* Sets the list of tools this modifier can be applied to
* @param tools Modifier tools list
* @param maxSize Max stack size this recipe applies to
* @return Builder instance
*/
public T setTools(Ingredient tools, int maxSize) {
this.tools = tools;
this.maxToolSize = maxSize;
return (T) this;
}

Expand Down Expand Up @@ -183,6 +196,9 @@ private void writeCommon(JsonObject json, @Nullable Boolean unarmed) {
ingredient = CompoundIngredient.from(ingredient, Ingredient.fromTag(TinkerTags.Items.CHESTPLATES));
}
json.add("tools", ingredient.serialize());
if (maxToolSize != ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE) {
json.addProperty("max_tool_size", maxToolSize);
}
if (slotType != null && slots > 0) {
JsonObject slotJson = new JsonObject();
slotJson.addProperty(slotType.getName(), slots);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import slimeknights.tconstruct.library.recipe.modifiers.ModifierRecipeLookup;
import slimeknights.tconstruct.library.recipe.tinkerstation.IMutableTinkerStationInventory;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationInventory;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe;
import slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult;
import slimeknights.tconstruct.library.tools.SlotType.SlotCount;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
Expand Down Expand Up @@ -56,7 +57,11 @@ public IncrementalModifierRecipe(ResourceLocation id, Ingredient input, int amou
}

public IncrementalModifierRecipe(ResourceLocation id, Ingredient input, int amountPerInput, int neededPerLevel, Ingredient toolRequirement, ModifierMatch requirements, String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots, ItemStack leftover) {
super(id, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
this(id, toolRequirement, amountPerInput, neededPerLevel, toolRequirement, ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE, requirements, requirementsError, result, maxLevel, slots, leftover);
}

public IncrementalModifierRecipe(ResourceLocation id, Ingredient input, int amountPerInput, int neededPerLevel, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements, String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots, ItemStack leftover) {
super(id, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
this.input = input;
this.amountPerInput = amountPerInput;
this.neededPerLevel = neededPerLevel;
Expand All @@ -76,7 +81,8 @@ public boolean matches(ITinkerStationInventory inv, World world) {

@Override
public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
ToolStack tool = ToolStack.from(inv.getTinkerableStack());
ItemStack tinkerable = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(tinkerable);

// if the tool lacks the modifier, treat current as maxLevel, means we will add a new level
Modifier modifier = result.getModifier();
Expand Down Expand Up @@ -118,7 +124,7 @@ public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
}

// successfully added the modifier
return ValidatedResult.success(tool.createStack());
return ValidatedResult.success(tool.createStack(Math.min(tinkerable.getCount(), shrinkToolSlotBy())));
}

/**
Expand Down Expand Up @@ -284,7 +290,7 @@ public static ItemStack deseralizeResultItem(JsonObject parent, String name) {

public static class Serializer extends AbstractModifierRecipe.Serializer<IncrementalModifierRecipe> {
@Override
public IncrementalModifierRecipe read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, ModifierMatch requirements,
public IncrementalModifierRecipe read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
Ingredient input = Ingredient.deserialize(JsonHelper.getElement(json, "input"));
int amountPerInput = JSONUtils.getInt(json, "amount_per_item", 1);
Expand All @@ -299,17 +305,17 @@ public IncrementalModifierRecipe read(ResourceLocation id, JsonObject json, Ingr
if (amountPerInput > 1 && json.has("leftover")) {
leftover = deseralizeResultItem(json, "leftover");
}
return new IncrementalModifierRecipe(id, input, amountPerInput, neededPerLevel, toolRequirement, requirements, requirementsError, result, maxLevel, slots, leftover);
return new IncrementalModifierRecipe(id, input, amountPerInput, neededPerLevel, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots, leftover);
}

@Override
public IncrementalModifierRecipe read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, ModifierMatch requirements,
public IncrementalModifierRecipe read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
Ingredient input = Ingredient.read(buffer);
int amountPerInput = buffer.readVarInt();
int neededPerLevel = buffer.readVarInt();
ItemStack leftover = buffer.readItemStack();
return new IncrementalModifierRecipe(id, input, amountPerInput, neededPerLevel, toolRequirement, requirements, requirementsError, result, maxLevel, slots, leftover);
return new IncrementalModifierRecipe(id, input, amountPerInput, neededPerLevel, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots, leftover);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import slimeknights.tconstruct.library.recipe.modifiers.ModifierRecipeLookup;
import slimeknights.tconstruct.library.recipe.tinkerstation.IMutableTinkerStationInventory;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationInventory;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe;
import slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult;
import slimeknights.tconstruct.library.tools.SlotType.SlotCount;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
Expand Down Expand Up @@ -50,7 +51,11 @@ public ModifierRecipe(ResourceLocation id, List<SizedIngredient> inputs, Ingredi
}

public ModifierRecipe(ResourceLocation id, List<SizedIngredient> inputs, Ingredient toolRequirement, ModifierMatch requirements, String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
super(id, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
this(id, inputs, toolRequirement, ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE, requirements, requirementsError, result, maxLevel, slots);
}

public ModifierRecipe(ResourceLocation id, List<SizedIngredient> inputs, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements, String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
super(id, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
this.inputs = inputs;
// add all inputs to the modifier listing
for (SizedIngredient ingredient : inputs) {
Expand Down Expand Up @@ -130,7 +135,8 @@ public boolean matches(ITinkerStationInventory inv, World world) {
*/
@Override
public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
ToolStack tool = ToolStack.from(inv.getTinkerableStack());
ItemStack tinkerable = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(tinkerable);

// common errors
ValidatedResult commonError = validatePrerequisites(tool);
Expand All @@ -155,7 +161,7 @@ public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
return toolValidation;
}

return ValidatedResult.success(tool.createStack());
return ValidatedResult.success(tool.createStack(Math.min(tinkerable.getCount(), shrinkToolSlotBy())));
}

/**
Expand Down Expand Up @@ -196,21 +202,21 @@ protected void addIngredients(Consumer<List<ItemStack>> builder) {

public static class Serializer extends AbstractModifierRecipe.Serializer<ModifierRecipe> {
@Override
public ModifierRecipe read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, ModifierMatch requirements,
public ModifierRecipe read(ResourceLocation id, JsonObject json, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
List<SizedIngredient> ingredients = JsonHelper.parseList(json, "inputs", SizedIngredient::deserialize);
return new ModifierRecipe(id, ingredients, toolRequirement, requirements, requirementsError, result, maxLevel, slots);
return new ModifierRecipe(id, ingredients, toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
}

@Override
public ModifierRecipe read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, ModifierMatch requirements,
public ModifierRecipe read(ResourceLocation id, PacketBuffer buffer, Ingredient toolRequirement, int maxToolSize, ModifierMatch requirements,
String requirementsError, ModifierEntry result, int maxLevel, @Nullable SlotCount slots) {
int size = buffer.readVarInt();
ImmutableList.Builder<SizedIngredient> builder = ImmutableList.builder();
for (int i = 0; i < size; i++) {
builder.add(SizedIngredient.read(buffer));
}
return new ModifierRecipe(id, builder.build(), toolRequirement, requirements, requirementsError, result, maxLevel, slots);
return new ModifierRecipe(id, builder.build(), toolRequirement, maxToolSize, requirements, requirementsError, result, maxLevel, slots);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public boolean matches(ITinkerStationInventory inv, World world) {

@Override
public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
ToolStack tool = ToolStack.from(inv.getTinkerableStack());
ItemStack tinkerable = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(tinkerable);
OverslimeModifier overslime = TinkerModifiers.overslime.get();
// if the tool lacks true overslime, add overslime
if (tool.getUpgrades().getLevel(TinkerModifiers.overslime.get()) == 0) {
Expand All @@ -83,7 +84,7 @@ public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
// see how much value is available, update overslime to the max possible
int available = IncrementalModifierRecipe.getAvailableAmount(inv, ingredient, restoreAmount);
overslime.addOverslime(tool, available);
return ValidatedResult.success(tool.createStack());
return ValidatedResult.success(tool.createStack(Math.min(tinkerable.getCount(), shrinkToolSlotBy())));
}

/**
Expand Down

0 comments on commit 30ee6e8

Please sign in to comment.