Skip to content

Commit

Permalink
Add generic return type to the stat module builder
Browse files Browse the repository at this point in the history
Forgot that the speciifc modifier module types are needed for the specific hook builder
  • Loading branch information
KnightMiner committed Jan 1, 2024
1 parent f53c803 commit a138802
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.GsonHelper;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;

/**
Expand Down Expand Up @@ -79,17 +78,17 @@ public static LevelingValue eachLevel(float eachLevel) {
}

/** Trait to mix into a builder using leveling values */
public interface Builder {
public interface Builder<M> {
/** Creates an instance with a flat value and a leveling value*/
ModifierModule amount(float flat, float eachLevel);
M amount(float flat, float eachLevel);

/** Creates an instance with a value that ignores level */
default ModifierModule flat(float flat) {
default M flat(float flat) {
return amount(flat, 0);
}

/** Creates an instance with a value that increases each level */
default ModifierModule eachLevel(float eachLevel) {
default M eachLevel(float eachLevel) {
return amount(0, eachLevel);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.network.FriendlyByteBuf;
import slimeknights.tconstruct.library.json.LevelingValue;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;

Expand Down Expand Up @@ -88,18 +87,18 @@ interface FallbackFormula {

/** Builder for a module containing a modifier formula */
@RequiredArgsConstructor
abstract class Builder<T extends Builder<T>> extends ModifierModuleCondition.Builder<T> implements LevelingValue.Builder {
abstract class Builder<T extends Builder<T,M>,M> extends ModifierModuleCondition.Builder<T> implements LevelingValue.Builder<M> {
/** Variables to use for post fix formulas */
private final String[] variables;
/** Fallback formula for simple leveling */
@Getter(AccessLevel.PROTECTED)
private final FallbackFormula formula;

/** Builds the module given the formula */
protected abstract ModifierModule build(ModifierFormula formula);
protected abstract M build(ModifierFormula formula);

@Override
public ModifierModule amount(float flat, float leveling) {
public M amount(float flat, float leveling) {
return build(new SimpleLevelingFormula(new LevelingValue(flat, leveling), getFormula()));
}

Expand All @@ -115,7 +114,7 @@ protected FormulaBuilder() {
}

/** Builds the module given the formula */
public ModifierModule build() {
public M build() {
return Builder.this.build(buildFormula());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package slimeknights.tconstruct.library.modifiers.modules;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation;
import slimeknights.tconstruct.library.json.LevelingValue;

/**
* Shared builder logic for {@link slimeknights.tconstruct.library.modifiers.modules.behavior.AttributeModule} and {@link slimeknights.tconstruct.library.modifiers.modules.combat.MeleeAttributeModule}
* @param <T> Builder type
* @param <M> Module return type
*/
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class AttributeModuleBuilder<T extends AttributeModuleBuilder<T,M>, M> extends ModifierModuleCondition.Builder<T> implements LevelingValue.Builder<M> {
protected final Attribute attribute;
protected final Operation operation;
protected String unique;

/**
* Sets the unique string directly
*/
@SuppressWarnings("unchecked")
public T unique(String unique) {
this.unique = unique;
return (T)this;
}

/**
* Sets the unique string using a resource location
*/
public T uniqueFrom(ResourceLocation id) {
return unique(id.getNamespace() + ".modifier." + id.getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public Builder builder() {
}

/** Builder for this module */
public class Builder extends ModifierFormula.Builder<Builder> {
public class Builder extends ModifierFormula.Builder<Builder,T> {
private Builder() {
super(variables, fallbackFormula);
}

@Override
protected ModifierModule build(ModifierFormula formula) {
protected T build(ModifierFormula formula) {
return constructor.apply(formula, condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public static Builder block(Block block) {
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder {
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder<CoverGroundWalkerModule> {
private final BlockState state;

@Override
public ModifierModule amount(float flat, float eachLevel) {
public CoverGroundWalkerModule amount(float flat, float eachLevel) {
return new CoverGroundWalkerModule(state, new LevelingValue(flat, eachLevel), condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ public static Builder source(IJsonPredicate<DamageSource> source) {
@Setter
@Accessors(fluent = true)
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder {
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder<ProtectionModule> {
private final IJsonPredicate<DamageSource> source;
private IJsonPredicate<LivingEntity> entity = LivingEntityPredicate.ANY;
private Enchantment subtract;

@Override
public ModifierModule amount(float flat, float eachLevel) {
public ProtectionModule amount(float flat, float eachLevel) {
return new ProtectionModule(source, entity, new LevelingValue(flat, eachLevel), subtract, condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static Builder builder() {
return new Builder();
}

public static class Builder implements LevelingValue.Builder {
public static class Builder implements LevelingValue.Builder<ReplaceBlockWalkerModule> {
private final ImmutableList.Builder<BlockReplacement> replacements = ImmutableList.builder();
@Setter
@Accessors(fluent = true)
Expand Down Expand Up @@ -205,7 +205,7 @@ public Builder replaceAlways(IJsonPredicate<BlockState> target, BlockState repla
}

@Override
public ModifierModule amount(float flat, float eachLevel) {
public ReplaceBlockWalkerModule amount(float flat, float eachLevel) {
List<BlockReplacement> replacements = this.replacements.build();
if (replacements.isEmpty()) {
throw new IllegalStateException("Must have at least 1 replacement");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ public static Builder builder(ToolAction action, SoundEvent sound) {
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder {
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder<ToolActionWalkerTransformModule> {
private final ToolAction action;
private final SoundEvent sound;

@Override
public ModifierModule amount(float flat, float eachLevel) {
public ToolActionWalkerTransformModule amount(float flat, float eachLevel) {
return new ToolActionWalkerTransformModule(action, sound, new LevelingValue(flat, eachLevel), condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import slimeknights.tconstruct.library.modifiers.ModifierHook;
import slimeknights.tconstruct.library.modifiers.TinkerHooks;
import slimeknights.tconstruct.library.modifiers.hook.behavior.AttributesModifierHook;
import slimeknights.tconstruct.library.modifiers.modules.AttributeModuleBuilder;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition;
import slimeknights.tconstruct.library.modifiers.modules.combat.MeleeAttributeModule;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;

import java.util.Collection;
Expand Down Expand Up @@ -143,7 +143,7 @@ public static Builder builder(Attribute attribute, Operation operation) {
return new Builder(attribute, operation);
}

public static class Builder extends MeleeAttributeModule.Builder<Builder> {
public static class Builder extends AttributeModuleBuilder<Builder,AttributeModule> {
private EquipmentSlot[] slots = EquipmentSlot.values();

protected Builder(Attribute attribute, Operation operation) {
Expand All @@ -157,7 +157,7 @@ public Builder slots(EquipmentSlot... slots) {
}

@Override
public ModifierModule amount(float flat, float eachLevel) {
public AttributeModule amount(float flat, float eachLevel) {
if (unique == null) {
throw new IllegalStateException("Must set unique for attributes");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public static Builder multiplyAll(INumericToolStat<?> stat) {
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder {
public static class Builder extends ModifierModuleCondition.Builder<Builder> implements LevelingValue.Builder<StatBoostModule> {
private final INumericToolStat<?> stat;
private final BoostType operation;

@Override
public ModifierModule amount(float flat, float eachLevel) {
public StatBoostModule amount(float flat, float eachLevel) {
return new StatBoostModule(stat, operation, new LevelingValue(flat, eachLevel), condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Builder target(IJsonPredicate<LivingEntity> target) {
}

/** Builder class */
public static class Builder extends ModifierFormula.Builder<Builder> {
public static class Builder extends ModifierFormula.Builder<Builder,ConditionalMeleeDamageModule> {
private final IJsonPredicate<LivingEntity> target;
@Setter
@Accessors(fluent = true)
Expand All @@ -169,7 +169,7 @@ public Builder percent() {
}

@Override
protected ModifierModule build(ModifierFormula formula) {
protected ConditionalMeleeDamageModule build(ModifierFormula formula) {
return new ConditionalMeleeDamageModule(target, attacker, formula, percent, condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static Builder builder() {
}

/** Builder class */
public static class Builder extends ModifierFormula.Builder<Builder> {
public static class Builder extends ModifierFormula.Builder<Builder,KnockbackModule> {
@Setter
@Accessors(fluent = true)
private IJsonPredicate<LivingEntity> entity = LivingEntityPredicate.ANY;
Expand All @@ -111,7 +111,7 @@ private Builder() {
}

@Override
protected ModifierModule build(ModifierFormula formula) {
protected KnockbackModule build(ModifierFormula formula) {
return new KnockbackModule(entity, formula, condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package slimeknights.tconstruct.library.modifiers.modules.combat;

import com.google.gson.JsonObject;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
Expand All @@ -19,6 +16,7 @@
import slimeknights.tconstruct.library.modifiers.ModifierHook;
import slimeknights.tconstruct.library.modifiers.TinkerHooks;
import slimeknights.tconstruct.library.modifiers.hook.combat.MeleeHitModifierHook;
import slimeknights.tconstruct.library.modifiers.modules.AttributeModuleBuilder;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition;
import slimeknights.tconstruct.library.tools.context.ToolAttackContext;
Expand Down Expand Up @@ -133,30 +131,17 @@ public void toNetwork(MeleeAttributeModule object, FriendlyByteBuf buffer) {


/** Creates a new builder instance */
public static Builder<?> builder(Attribute attribute, Operation operation) {
return new Builder<>(attribute, operation);
public static Builder builder(Attribute attribute, Operation operation) {
return new Builder(attribute, operation);
}

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public static class Builder<T extends Builder<T>> extends ModifierModuleCondition.Builder<T> implements LevelingValue.Builder {
protected final Attribute attribute;
protected final Operation operation;
protected String unique;

/** Sets the unique string directly */
@SuppressWarnings("unchecked")
public T unique(String unique) {
this.unique = unique;
return (T) this;
}

/** Sets the unique string using a resource location */
public T uniqueFrom(ResourceLocation id) {
return unique(id.getNamespace() + ".modifier." + id.getPath());
public static class Builder extends AttributeModuleBuilder<Builder,MeleeAttributeModule> {
private Builder(Attribute attribute, Operation operation) {
super(attribute, operation);
}

@Override
public ModifierModule amount(float flat, float eachLevel) {
public MeleeAttributeModule amount(float flat, float eachLevel) {
if (unique == null) {
throw new IllegalStateException("Must set unique for attributes");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition;
import slimeknights.tconstruct.library.modifiers.modules.behavior.ConditionalStatTooltip;
import slimeknights.tconstruct.library.modifiers.modules.combat.ConditionalMeleeDamageModule;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.stat.INumericToolStat;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
Expand Down Expand Up @@ -141,7 +140,7 @@ public static Builder blocks(IJsonPredicate<BlockState> blocks) {
}

/** Builder class */
public static class Builder extends ModifierFormula.Builder<ConditionalMeleeDamageModule.Builder> {
public static class Builder extends ModifierFormula.Builder<Builder,ConditionalMiningSpeedModule> {
private final IJsonPredicate<BlockState> blocks;
@Setter
@Accessors(fluent = true)
Expand Down Expand Up @@ -172,7 +171,7 @@ public Builder allowIneffective() {
}

@Override
protected ModifierModule build(ModifierFormula formula) {
protected ConditionalMiningSpeedModule build(ModifierFormula formula) {
return new ConditionalMiningSpeedModule(blocks, holder, requireEffective, formula, percent, condition);
}
}
Expand Down

0 comments on commit a138802

Please sign in to comment.