-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Begin implementation of cost modifiers
- Loading branch information
1 parent
f097684
commit bf6198a
Showing
14 changed files
with
313 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
shared/src/main/java/net/blay09/mods/waystones/cost/CostContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.blay09.mods.waystones.api.IWaystoneTeleportContext; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CostContext { | ||
|
||
private final Map<ResourceLocation, Object> costInstances = new HashMap<>(); | ||
private final IWaystoneTeleportContext context; | ||
|
||
public CostContext(IWaystoneTeleportContext context) { | ||
this.context = context; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T, P> void apply(CostModifier<T, P> modifier) { | ||
var costInstance = (T) costInstances.get(modifier.getCostType()); | ||
if (costInstance == null) { | ||
costInstance = CostRegistry.<T>getCostType(modifier.getCostType()).createInstance(); | ||
} | ||
costInstances.put(modifier.getCostType(), modifier.apply(costInstance, this, null)); | ||
} | ||
|
||
public float getContextValue(ResourceLocation id) { | ||
return switch (id.toString()) { | ||
case "waystones:distance" -> (float) Math.sqrt(context.getEntity().distanceToSqr(context.getDestination().getLocation())); | ||
default -> 0f; | ||
}; | ||
} | ||
|
||
public boolean matchesCondition(ResourceLocation id) { | ||
|
||
return false; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
shared/src/main/java/net/blay09/mods/waystones/cost/CostModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public interface CostModifier<TCost, TParameter> extends CostModifierFunction<TCost, TParameter> { | ||
ResourceLocation getId(); | ||
|
||
ResourceLocation getCostType(); | ||
|
||
Class<TParameter> getParameterType(); | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
shared/src/main/java/net/blay09/mods/waystones/cost/CostModifierFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
public interface CostModifierFunction<TCost, TParameter> { | ||
TCost apply(TCost costInstance, CostContext context, TParameter parameters); | ||
} |
148 changes: 148 additions & 0 deletions
148
shared/src/main/java/net/blay09/mods/waystones/cost/CostRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CostRegistry { | ||
|
||
private static final Map<ResourceLocation, CostType<?>> costTypes = new HashMap<>(); | ||
private static final Map<ResourceLocation, CostModifier<?, ?>> costModifiers = new HashMap<>(); | ||
|
||
public record IntParameter(int value) { | ||
} | ||
|
||
public record FloatParameter(float value) { | ||
} | ||
|
||
public record IdParameter(ResourceLocation value) { | ||
} | ||
|
||
public record ScaledParameters(IdParameter id, FloatParameter scale) { | ||
} | ||
|
||
public record ConditionalParameters(IdParameter id, FloatParameter scale) { | ||
} | ||
|
||
public static void registerDefaults() { | ||
final var experiencePoints = new ExperiencePointsCostType(); | ||
final var levels = new ExperienceLevelCostType(); | ||
|
||
register(experiencePoints); | ||
register(levels); | ||
|
||
registerModifier("add_levels", levels, FloatParameter.class, (cost, context, parameters) -> { | ||
cost.setLevels((int) (cost.getLevels() + parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("conditional_add_levels", levels, ConditionalParameters.class, (cost, context, parameters) -> { | ||
if (context.matchesCondition(parameters.id.value)) { | ||
cost.setLevels((int) (cost.getLevels() + parameters.scale.value)); | ||
} | ||
return cost; | ||
}); | ||
registerModifier("multiply_levels", levels, FloatParameter.class, (cost, context, parameters) -> { | ||
cost.setLevels((int) (cost.getLevels() * parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("conditional_multiply_levels", levels, ConditionalParameters.class, (cost, context, parameters) -> { | ||
if (context.matchesCondition(parameters.id.value)) { | ||
cost.setLevels((int) (cost.getLevels() * parameters.scale.value)); | ||
} | ||
return cost; | ||
}); | ||
registerModifier("scaled_add_levels", levels, ScaledParameters.class, (cost, context, parameters) -> { | ||
final var sourceValue = context.getContextValue(parameters.id.value); | ||
cost.setLevels((int) (cost.getLevels() + sourceValue * parameters.scale.value)); | ||
return cost; | ||
}); | ||
registerModifier("multiply_levels", levels, FloatParameter.class, (cost, context, parameters) -> { | ||
cost.setLevels((int) (cost.getLevels() * parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("min_levels", levels, IntParameter.class, (cost, context, parameters) -> { | ||
cost.setLevels(Math.max(cost.getLevels(), parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("max_levels", levels, IntParameter.class, (cost, context, parameters) -> { | ||
cost.setLevels(Math.min(cost.getLevels(), parameters.value)); | ||
return cost; | ||
}); | ||
|
||
registerModifier("add_xp", experiencePoints, IntParameter.class, (cost, context, parameters) -> { | ||
cost.setPoints(cost.getPoints() + parameters.value); | ||
return cost; | ||
}); | ||
registerModifier("conditional_add_xp", experiencePoints, ConditionalParameters.class, (cost, context, parameters) -> { | ||
if (context.matchesCondition(parameters.id.value)) { | ||
cost.setPoints((int) (cost.getPoints() + parameters.scale.value)); | ||
} | ||
return cost; | ||
}); | ||
registerModifier("multiply_xp", experiencePoints, FloatParameter.class, (cost, context, parameters) -> { | ||
cost.setPoints((int) (cost.getPoints() * parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("conditional_multiply_xp", experiencePoints, ConditionalParameters.class, (cost, context, parameters) -> { | ||
if (context.matchesCondition(parameters.id.value)) { | ||
cost.setPoints((int) (cost.getPoints() * parameters.scale.value)); | ||
} | ||
return cost; | ||
}); | ||
registerModifier("scaled_add_xp", experiencePoints, ScaledParameters.class, (cost, context, parameters) -> { | ||
final var sourceValue = context.getContextValue(parameters.id.value); | ||
cost.setPoints((int) (cost.getPoints() + sourceValue * parameters.scale.value)); | ||
return cost; | ||
}); | ||
registerModifier("min_xp", experiencePoints, IntParameter.class, (cost, context, parameters) -> { | ||
cost.setPoints(Math.max(cost.getPoints(), parameters.value)); | ||
return cost; | ||
}); | ||
registerModifier("max_xp", experiencePoints, IntParameter.class, (cost, context, parameters) -> { | ||
cost.setPoints(Math.min(cost.getPoints(), parameters.value)); | ||
return cost; | ||
}); | ||
} | ||
|
||
public static void register(CostType<?> costType) { | ||
costTypes.put(costType.getId(), costType); | ||
} | ||
|
||
public static void register(CostModifier<?, ?> costModifier) { | ||
costModifiers.put(costModifier.getId(), costModifier); | ||
} | ||
|
||
private static <T, P> void registerModifier(String name, CostType<T> costType, Class<P> parameterType, CostModifierFunction<T, P> function) { | ||
register(new CostModifier<T, P>() { | ||
@Override | ||
public ResourceLocation getId() { | ||
return new ResourceLocation("waystones", name); | ||
} | ||
|
||
@Override | ||
public ResourceLocation getCostType() { | ||
return costType.getId(); | ||
} | ||
|
||
@Override | ||
public Class<P> getParameterType() { | ||
return parameterType; | ||
} | ||
|
||
@Override | ||
public T apply(T cost, CostContext context, P parameters) { | ||
return function.apply(cost, context, parameters); | ||
} | ||
}); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> CostType<T> getCostType(ResourceLocation costType) { | ||
return (CostType<T>) costTypes.get(costType); | ||
} | ||
|
||
public static CostModifier<?, ?> getCostModifier(ResourceLocation costModifier) { | ||
return costModifiers.get(costModifier); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
shared/src/main/java/net/blay09/mods/waystones/cost/CostType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public interface CostType<T> { | ||
ResourceLocation getId(); | ||
T createInstance(); | ||
} |
19 changes: 19 additions & 0 deletions
19
shared/src/main/java/net/blay09/mods/waystones/cost/ExperienceLevelCostType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.blay09.mods.waystones.xp.ExperienceLevelCost; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public class ExperienceLevelCostType implements CostType<ExperienceLevelCost> { | ||
|
||
public static final ResourceLocation ID = new ResourceLocation("waystones", "experience_levels"); | ||
|
||
@Override | ||
public ResourceLocation getId() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public ExperienceLevelCost createInstance() { | ||
return new ExperienceLevelCost(0); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
shared/src/main/java/net/blay09/mods/waystones/cost/ExperiencePointsCostType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.blay09.mods.waystones.cost; | ||
|
||
import net.blay09.mods.waystones.xp.ExperiencePointsCost; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public class ExperiencePointsCostType implements CostType<ExperiencePointsCost> { | ||
|
||
public static final ResourceLocation ID = new ResourceLocation("waystones", "experience_points"); | ||
|
||
@Override | ||
public ResourceLocation getId() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public ExperiencePointsCost createInstance() { | ||
return new ExperiencePointsCost(0); | ||
} | ||
} |
Oops, something went wrong.