Skip to content

Commit

Permalink
Make all tool heads repair with the same quality
Browse files Browse the repository at this point in the history
Means for instance on a sledge hammer, the hammer head and the large plate both grant equivalent repair if they are the only instance of that material. Before the large plates would only repair half their materials value
Note the values may still be different due to material stats, this just removes the additional penalty
  • Loading branch information
KnightMiner committed May 19, 2024
1 parent 6f511a4 commit 30f9366
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
public float getRepairAmount(IToolStackView tool, MaterialId material) {
return 0;
}

@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
return 0;
}
});

/** Hook for adding raw unconditional stats to a tool */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
return this.material.equals(material);
}

@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
return this.material.equals(material) ? 1 : 0;
}

@Override
public float getRepairAmount(IToolStackView tool, MaterialId material) {
return this.material.equals(material) ? repairAmount : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ public interface MaterialRepairToolHook {
*/
float getRepairAmount(IToolStackView tool, MaterialId material);

/**
* Gets the factor to multiply an attempted repair with this material, used for part swapping repair notably.
* @param tool Tool instance
* @param material Material used to repair
* @return Repair factor, 1 is the default
*/
float getRepairFactor(IToolStackView tool, MaterialId material);


/** Gets the repair stat for the given tool */
static boolean canRepairWith(IToolStackView tool, MaterialId material) {
Expand All @@ -43,11 +35,6 @@ static float repairAmount(IToolStackView tool, MaterialId material) {
return tool.getHook(ToolHooks.MATERIAL_REPAIR).getRepairAmount(tool, material);
}

/** Gets the repair stat for the given tool */
static float repairFactor(IToolStackView tool, MaterialId material) {
return tool.getHook(ToolHooks.MATERIAL_REPAIR).getRepairFactor(tool, material);
}

/** Merger that takes the largest option from all nested modules */
record MaxMerger(Collection<MaterialRepairToolHook> hooks) implements MaterialRepairToolHook {
@Override
Expand All @@ -68,14 +55,5 @@ public float getRepairAmount(IToolStackView tool, MaterialId material) {
}
return amount;
}

@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
float factor = 0;
for (MaterialRepairToolHook hook : hooks) {
factor = Math.max(factor, hook.getRepairFactor(tool, material));
}
return factor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.IntToDoubleFunction;
import java.util.stream.IntStream;

/** Module for building tool stats using materials */
Expand All @@ -43,7 +42,6 @@ public class MaterialStatsModule implements ToolStatsHook, ToolTraitHook, ToolMa
@Getter @VisibleForTesting
final float[] scales;
private int[] repairIndices;
private float maxRepairScale = 0;

public MaterialStatsModule(List<MaterialStatsId> statTypes, float[] scales) {
this.statTypes = statTypes;
Expand Down Expand Up @@ -74,14 +72,6 @@ private int[] getRepairIndices() {
return repairIndices;
}

/** Gets the largest weight of all repair materials */
private float maxRepairScale() {
if (maxRepairScale == 0) {
maxRepairScale = (float)IntStream.of(getRepairIndices()).mapToDouble(i -> scales[i]).max().orElse(1f);
}
return maxRepairScale;
}

@Override
public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
for (int part : getRepairIndices()) {
Expand All @@ -92,24 +82,15 @@ public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
return false;
}

/** Shared logic for both repair value functions */
private float getRepair(IToolStackView tool, MaterialId material, IntToDoubleFunction mapper) {
return (float)(IntStream.of(getRepairIndices())
.filter(i -> tool.getMaterial(i).matches(material))
.mapToDouble(mapper)
.reduce(0, Double::max)
/ maxRepairScale());
}

@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
return getRepair(tool, material, i -> scales[i]);
}

@Override
public float getRepairAmount(IToolStackView tool, MaterialId material) {
ResourceLocation toolId = tool.getDefinition().getId();
return getRepair(tool, material, i -> MaterialRepairModule.getDurability(toolId, material, statTypes.get(i)) * scales[i]);
for (int i : getRepairIndices()) {
if (tool.getMaterial(i).matches(material)) {
return MaterialRepairModule.getDurability(toolId, material, statTypes.get(i));
}
}
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationContainer;
import slimeknights.tconstruct.library.recipe.tinkerstation.ITinkerStationRecipe;
import slimeknights.tconstruct.library.tools.definition.module.material.MaterialRepairModule;
import slimeknights.tconstruct.library.tools.definition.module.material.MaterialRepairToolHook;
import slimeknights.tconstruct.library.tools.definition.module.material.ToolPartsHook;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.item.IModifiable;
Expand Down Expand Up @@ -133,7 +132,7 @@ public RecipeResult<ItemStack> getValidatedResult(ITinkerStationContainer inv) {
// ensure there is a change in the part or we are repairing the tool, note we compare variants so you could swap oak head for birch head
MaterialVariant toolVariant = tool.getMaterial(index);
boolean didChange = !toolVariant.sameVariant(partVariant);
int repairDurability = MaterialRepairModule.getDurability(null, partVariant.getId(), part.getStatType());
float repairDurability = MaterialRepairModule.getDurability(null, partVariant.getId(), part.getStatType());
if (!didChange && (tool.getDamage() == 0 || repairDurability == 0)) {
return RecipeResult.pass();
}
Expand Down Expand Up @@ -184,18 +183,18 @@ public RecipeResult<ItemStack> getValidatedResult(ITinkerStationContainer inv) {
// must have a registered recipe
int cost = MaterialCastingLookup.getItemCost(part);
if (cost > 0) {
// apply tool repair factor and modifier repair boost, note this works because the material has been swapped already
float factor = cost / MaterialRecipe.INGOTS_PER_REPAIR * MaterialRepairToolHook.repairFactor(tool, partVariant.getId());
if (factor > 0) {
// takes 3 ingots for a full repair, however count the head cost in the repair amount
repairDurability *= cost / MaterialRecipe.INGOTS_PER_REPAIR;
if (repairDurability > 0) {
for (ModifierEntry entry : tool.getModifierList()) {
factor = entry.getHook(ModifierHooks.REPAIR_FACTOR).getRepairFactor(tool, entry, factor);
if (factor <= 0) {
repairDurability = entry.getHook(ModifierHooks.REPAIR_FACTOR).getRepairFactor(tool, entry, repairDurability);
if (repairDurability <= 0) {
break;
}
}
}
if (factor > 0) {
ToolDamageUtil.repair(tool, (int)(repairDurability * factor));
if (repairDurability > 0) {
ToolDamageUtil.repair(tool, (int)repairDurability);
}
}
}
Expand Down

0 comments on commit 30f9366

Please sign in to comment.