Skip to content

Commit

Permalink
Update HasModifierPredicate to use ModifierPredicate instead of Modif…
Browse files Browse the repository at this point in the history
…ierId for its condition

Will help with requirements module later
As part of this, move common modifier level int ranges to ModifierEntry
  • Loading branch information
KnightMiner committed Apr 2, 2024
1 parent 2ebc245 commit 3171995
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 251 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@
{
"type": "tconstruct:has_modifier",
"check": "upgrades",
"level": {
"min": 1
},
"modifier": "tconstruct:writable"
"modifier": {
"type": "tconstruct:single",
"modifier": "tconstruct:writable"
}
},
{
"type": "tconstruct:has_modifier",
"check": "upgrades",
"level": {
"min": 1
},
"modifier": "tconstruct:recapitated"
"modifier": {
"type": "tconstruct:single",
"modifier": "tconstruct:recapitated"
}
},
{
"type": "tconstruct:has_modifier",
"check": "upgrades",
"level": {
"min": 1
},
"modifier": "tconstruct:harmonious"
"modifier": {
"type": "tconstruct:single",
"modifier": "tconstruct:harmonious"
}
},
{
"type": "tconstruct:has_modifier",
"check": "upgrades",
"level": {
"min": 1
},
"modifier": "tconstruct:resurrected"
"modifier": {
"type": "tconstruct:single",
"modifier": "tconstruct:resurrected"
}
},
{
"type": "tconstruct:has_modifier",
"check": "upgrades",
"level": {
"min": 1
},
"modifier": "tconstruct:gilded"
"modifier": {
"type": "tconstruct:single",
"modifier": "tconstruct:gilded"
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import slimeknights.tconstruct.gadgets.TinkerGadgets;
import slimeknights.tconstruct.library.json.predicate.tool.HasMaterialPredicate;
import slimeknights.tconstruct.library.json.predicate.tool.HasModifierPredicate;
import slimeknights.tconstruct.library.json.predicate.tool.HasModifierPredicate.ModifierCheck;
import slimeknights.tconstruct.library.json.predicate.tool.StatInRangePredicate;
import slimeknights.tconstruct.library.json.predicate.tool.StatInSetPredicate;
import slimeknights.tconstruct.library.json.predicate.tool.ToolContextPredicate;
Expand Down Expand Up @@ -181,11 +180,11 @@ protected void generate() {
builder(Items.WRITABLE_BOOK, resource("tools/upgrade_slots"), modified, FrameType.CHALLENGE, builder ->
builder.addCriterion("has_modified", InventoryChangeTrigger.TriggerInstance.hasItems(new ToolStackItemPredicate(
ToolContextPredicate.and(
new HasModifierPredicate(ModifierIds.writable, ModifierCheck.UPGRADES),
new HasModifierPredicate(ModifierIds.recapitated, ModifierCheck.UPGRADES),
new HasModifierPredicate(ModifierIds.harmonious, ModifierCheck.UPGRADES),
new HasModifierPredicate(ModifierIds.resurrected, ModifierCheck.UPGRADES),
new HasModifierPredicate(ModifierIds.gilded, ModifierCheck.UPGRADES)))))
HasModifierPredicate.hasUpgrade(ModifierIds.writable, 1),
HasModifierPredicate.hasUpgrade(ModifierIds.recapitated, 1),
HasModifierPredicate.hasUpgrade(ModifierIds.harmonious, 1),
HasModifierPredicate.hasUpgrade(ModifierIds.resurrected, 1),
HasModifierPredicate.hasUpgrade(ModifierIds.gilded, 1)))))
);

// smeltery path
Expand Down Expand Up @@ -248,7 +247,7 @@ protected void generate() {
with.accept(TinkerTools.longbow.get());
});
builder(TinkerModifiers.silkyCloth, resource("smeltery/abilities"), anvil, FrameType.CHALLENGE, builder -> {
Consumer<ModifierId> with = modifier -> builder.addCriterion(modifier.getPath(), InventoryChangeTrigger.TriggerInstance.hasItems(new ToolStackItemPredicate(new HasModifierPredicate(modifier, ModifierCheck.UPGRADES))));
Consumer<ModifierId> with = modifier -> builder.addCriterion(modifier.getPath(), InventoryChangeTrigger.TriggerInstance.hasItems(new ToolStackItemPredicate(HasModifierPredicate.hasUpgrade(modifier, 1))));
Consumer<LazyModifier> withL = modifier -> with.accept(modifier.getId());

// general
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IGenericLoader<? extends ModifierPredicate> getLoader() {
return loader;
}
});
/** Loader for block state predicates */
/** Loader for modifier predicates */
PredicateRegistry<ModifierId> LOADER = new PredicateRegistry<>("Modifier Predicate", ANY);

/** Gets an inverted condition */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,57 @@
import slimeknights.mantle.data.loadable.primitive.EnumLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.mantle.data.predicate.IJsonPredicate;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IGenericLoader;
import slimeknights.tconstruct.library.json.IntRange;
import slimeknights.tconstruct.library.json.predicate.modifier.ModifierPredicate;
import slimeknights.tconstruct.library.json.predicate.modifier.SingleModifierPredicate;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;

/**
* Predicate that checks a tool for the given modifier.
* @param modifier Modifier to check for
* @param level Range of levels to check for, use {@link #DEFAULT_RANGE} for simply checking for any level on the tool, 0 means not on the tool.
* @param level Range of levels to check for, use {@link ModifierEntry#VALID_LEVEL} for simply checking for any level on the tool, 0 means not on the tool.
* @param check Whether to check upgrades or all modifiers
*/
public record HasModifierPredicate(ModifierId modifier, IntRange level, ModifierCheck check) implements ToolContextPredicate {
/** Valid bounds of the modifier level, 0 being allowed means you can condition on a modifier not being present if you wish */
public static final IntRange MAX_RANGE = new IntRange(0, Short.MAX_VALUE);
/** Default bounds of the modifier level */
public static final IntRange DEFAULT_RANGE = MAX_RANGE.min(1);

public record HasModifierPredicate(IJsonPredicate<ModifierId> modifier, IntRange level, ModifierCheck check) implements ToolContextPredicate {
public static final RecordLoadable<HasModifierPredicate> LOADER = RecordLoadable.create(
ModifierId.PARSER.requiredField("modifier", HasModifierPredicate::modifier),
MAX_RANGE.defaultField("level", DEFAULT_RANGE, true, HasModifierPredicate::level),
ModifierPredicate.LOADER.requiredField("modifier", HasModifierPredicate::modifier),
ModifierEntry.ANY_LEVEL.defaultField("level", ModifierEntry.VALID_LEVEL, true, HasModifierPredicate::level),
new EnumLoadable<>(ModifierCheck.class).requiredField("check", HasModifierPredicate::check),
HasModifierPredicate::new);

public HasModifierPredicate(ModifierId modifier, ModifierCheck check) {
this(modifier, DEFAULT_RANGE, check);
public HasModifierPredicate(ModifierId modifier, IntRange level, ModifierCheck check) {
this(new SingleModifierPredicate(modifier), level, check);
}

@Override
public boolean matches(IToolContext tool) {
return level.test(check.getModifiers(tool).getLevel(modifier));
for (ModifierEntry entry : check.getModifiers(tool).getModifiers()) {
if (modifier.matches(entry.getId())) {
return level.test(entry.getLevel());
}
}
return level.test(0);
}

@Override
public IJsonPredicate<IToolContext> inverted() {
// if our range touches the maximum bound, then inverted just goes from min to our min-1
if (level.max() == MAX_RANGE.max()) {
return new HasModifierPredicate(modifier, new IntRange(MAX_RANGE.min(), level.min() - 1), check);
if (level.max() == ModifierEntry.ANY_LEVEL.max()) {
return new HasModifierPredicate(modifier, new IntRange(ModifierEntry.ANY_LEVEL.min(), level.min() - 1), check);
}
// if our range touches the minimum bound, then inverted just goes from our max+1 to max possible
if (level.min() == MAX_RANGE.min()) {
return new HasModifierPredicate(modifier, new IntRange(level.max() + 1, MAX_RANGE.max()), check);
if (level.min() == ModifierEntry.ANY_LEVEL.min()) {
return new HasModifierPredicate(modifier, new IntRange(level.max() + 1, ModifierEntry.ANY_LEVEL.max()), check);
}
// if we are not touching either edge, no possible range exists so use the regular inverted logic
return ToolContextPredicate.super.inverted();
}

@Override
public IGenericLoader<? extends ToolContextPredicate> getLoader() {
public RecordLoadable<? extends ToolContextPredicate> getLoader() {
return LOADER;
}

Expand All @@ -72,4 +74,27 @@ public ModifierNBT getModifiers(IToolContext tool) {

public abstract ModifierNBT getModifiers(IToolContext tool);
}


/* Constructors */

/** Creates a predicate for a tool having the given upgrade (recipe modifier) */
public static HasModifierPredicate hasUpgrade(IJsonPredicate<ModifierId> modifier, int min) {
return new HasModifierPredicate(modifier, ModifierEntry.VALID_LEVEL.min(min), ModifierCheck.UPGRADES);
}

/** Creates a predicate for a tool having the given modifier (recipe or trait) */
public static HasModifierPredicate hasModifier(IJsonPredicate<ModifierId> modifier, int min) {
return new HasModifierPredicate(modifier, ModifierEntry.VALID_LEVEL.min(min), ModifierCheck.ALL);
}

/** Creates a predicate for a tool having the given upgrade (recipe modifier) */
public static HasModifierPredicate hasUpgrade(ModifierId modifier, int min) {
return new HasModifierPredicate(modifier, ModifierEntry.ANY_LEVEL.min(min), ModifierCheck.UPGRADES);
}

/** Creates a predicate for a tool having the given modifier (recipe or trait) */
public static HasModifierPredicate hasModifier(ModifierId modifier, int min) {
return new HasModifierPredicate(modifier, ModifierEntry.ANY_LEVEL.min(min), ModifierCheck.ALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.tags.TagKey;
import slimeknights.mantle.data.loadable.primitive.IntLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.tconstruct.library.json.IntRange;
import slimeknights.tconstruct.library.modifiers.util.LazyModifier;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;

Expand All @@ -23,6 +24,10 @@ public class ModifierEntry implements Comparable<ModifierEntry> {
ModifierId.PARSER.requiredField("name", ModifierEntry::getId),
IntLoadable.FROM_ONE.defaultField("level", 1, true, ModifierEntry::getLevel),
ModifierEntry::new);
/** Range of levels for a modifier including 0 (not on the tool) */
public static final IntRange ANY_LEVEL = new IntRange(0, Short.MAX_VALUE);
/** Range of levels for a modifier on a tool */
public static final IntRange VALID_LEVEL = new IntRange(1, Short.MAX_VALUE);

/** Modifier instance */
private final LazyModifier modifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
* Represents conditions for a modifier module, since this is reused across several modules
*/
public record ModifierModuleCondition(IJsonPredicate<IToolContext> tool, IntRange modifierLevel) {
/** Range of values used for a modifier level, used to parse modifier levels in their conditions */
public static final IntRange MODIFIER_LEVEL = new IntRange(1, Short.MAX_VALUE);
/** Instance matching any tool context predicate and any modifier level */
public static final ModifierModuleCondition ANY = new ModifierModuleCondition(ToolContextPredicate.ANY, MODIFIER_LEVEL);

public static final ModifierModuleCondition ANY = new ModifierModuleCondition(ToolContextPredicate.ANY, ModifierEntry.VALID_LEVEL);
/** Loadable for modifier module conditions, typically used with {@link RecordLoadable#directField(Function)} */
public static final RecordLoadable<ModifierModuleCondition> LOADABLE = RecordLoadable.create(
ToolContextPredicate.LOADER.defaultField("tool", ModifierModuleCondition::tool),
MODIFIER_LEVEL.defaultField("modifier_level", ModifierModuleCondition::modifierLevel),
ModifierEntry.VALID_LEVEL.defaultField("modifier_level", ModifierModuleCondition::modifierLevel),
ModifierModuleCondition::new);
/** Generic field instance used for most modules with conditions */
public static final LoadableField<ModifierModuleCondition,ConditionalModifierModule> FIELD = LOADABLE.directField(ConditionalModifierModule::condition);
Expand Down Expand Up @@ -53,13 +50,13 @@ public void serializeInto(JsonObject parent) {
if (this.tool != ToolContextPredicate.ANY) {
parent.add("tool", ToolContextPredicate.LOADER.serialize(this.tool));
}
MODIFIER_LEVEL.serializeInto(parent, "modifier_level", modifierLevel);
ModifierEntry.VALID_LEVEL.serializeInto(parent, "modifier_level", modifierLevel);
}

/** Deserializes these objects from the given parent object */
public static ModifierModuleCondition deserializeFrom(JsonObject parent) {
IJsonPredicate<IToolContext> tool = ToolContextPredicate.LOADER.getOrDefault(parent, "tool");
IntRange modifierLevel = MODIFIER_LEVEL.getOrDefault(parent, "modifier_level");
IntRange modifierLevel = ModifierEntry.VALID_LEVEL.getOrDefault(parent, "modifier_level");
return new ModifierModuleCondition(tool, modifierLevel);
}

Expand Down Expand Up @@ -122,22 +119,22 @@ private T setLevels(IntRange range) {

/** Sets the modifier level range for this module */
public T levelRange(int min, int max) {
return setLevels(MODIFIER_LEVEL.range(min, max));
return setLevels(ModifierEntry.VALID_LEVEL.range(min, max));
}

/** Sets the modifier level range for this module */
public T minLevel(int min) {
return setLevels(MODIFIER_LEVEL.min(min));
return setLevels(ModifierEntry.VALID_LEVEL.min(min));
}

/** Sets the modifier level range for this module */
public T maxLevel(int max) {
return setLevels(MODIFIER_LEVEL.max(max));
return setLevels(ModifierEntry.VALID_LEVEL.max(max));
}

/** Sets the modifier level range for this module */
public T exactLevel(int value) {
return setLevels(MODIFIER_LEVEL.exactly(value));
return setLevels(ModifierEntry.VALID_LEVEL.exactly(value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import java.util.List;

import static slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition.MODIFIER_LEVEL;
import static slimeknights.tconstruct.library.modifiers.ModifierEntry.VALID_LEVEL;

/**
* Module to replace blocks with another block while walking.
Expand Down Expand Up @@ -84,7 +84,7 @@ private record BlockReplacement(IJsonPredicate<BlockState> target, BlockState st
public static final RecordLoadable<BlockReplacement> LOADABLE = RecordLoadable.create(
BlockPredicate.LOADER.defaultField("target", BlockReplacement::target),
BlockStateLoadable.DIFFERENCE.directField(BlockReplacement::state), // pulling from this object directly means the keys used are block and properties
MODIFIER_LEVEL.defaultField("modifier_level", BlockReplacement::level),
VALID_LEVEL.defaultField("modifier_level", BlockReplacement::level),
BlockReplacement::new);
}

Expand Down Expand Up @@ -117,22 +117,22 @@ private Builder replaceLevelRange(IJsonPredicate<BlockState> target, BlockState

/** Adds the given replacement only at the given level range */
public Builder replaceLevelRange(IJsonPredicate<BlockState> target, BlockState replacement, int min, int max) {
return replaceLevelRange(target, replacement, MODIFIER_LEVEL.range(min, max));
return replaceLevelRange(target, replacement, VALID_LEVEL.range(min, max));
}

/** Adds the given replacement only at the given level range */
public Builder replaceMinLevel(IJsonPredicate<BlockState> target, BlockState replacement, int max) {
return replaceLevelRange(target, replacement, MODIFIER_LEVEL.max(max));
return replaceLevelRange(target, replacement, VALID_LEVEL.max(max));
}

/** Adds the given replacement only at the given level range */
public Builder replaceMaxLevel(IJsonPredicate<BlockState> target, BlockState replacement, int min) {
return replaceLevelRange(target, replacement, MODIFIER_LEVEL.min(min));
return replaceLevelRange(target, replacement, VALID_LEVEL.min(min));
}

/** Adds the given replacement only at the given level range */
public Builder replaceAlways(IJsonPredicate<BlockState> target, BlockState replacement) {
return replaceLevelRange(target, replacement, MODIFIER_LEVEL);
return replaceLevelRange(target, replacement, VALID_LEVEL);
}

@Override
Expand Down

0 comments on commit 3171995

Please sign in to comment.