-
Notifications
You must be signed in to change notification settings - Fork 755
/
ModifierCondition.java
60 lines (49 loc) · 3.39 KB
/
ModifierCondition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package slimeknights.tconstruct.library.modifiers.modules.util;
import slimeknights.mantle.data.loadable.field.LoadableField;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.mantle.data.predicate.IJsonPredicate;
import slimeknights.tconstruct.library.json.IntRange;
import slimeknights.tconstruct.library.json.predicate.tool.ToolContextPredicate;
import slimeknights.tconstruct.library.json.predicate.tool.ToolStackPredicate;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import java.util.function.Function;
/** Represents common conditions for a modifier module using tool stacks, since this is reused across several modules */
public record ModifierCondition<T extends IToolContext>(IJsonPredicate<T> tool, IntRange modifierLevel) {
/** Instance matching any tool context predicate and any modifier level */
public static final ModifierCondition<IToolContext> ANY_CONTEXT = new ModifierCondition<>(ToolContextPredicate.ANY, ModifierEntry.VALID_LEVEL);
/** Instance matching any tool context predicate and any modifier level */
public static final ModifierCondition<IToolStackView> ANY_TOOL = new ModifierCondition<>(ToolStackPredicate.ANY, ModifierEntry.VALID_LEVEL);
/** Validates that the tool and modifier pass the conditions */
public boolean matches(T tool, ModifierEntry modifier) {
return this.modifierLevel.test(modifier.getLevel()) && this.tool.matches(tool);
}
/** Swaps the modifier level condition for the passed condition */
public ModifierCondition<T> with(IJsonPredicate<T> tool) {
return new ModifierCondition<T>(tool, modifierLevel);
}
/** Swaps the modifier level condition for the passed condition */
public ModifierCondition<T> with(IntRange modifierLevel) {
return new ModifierCondition<T>(this.tool, modifierLevel);
}
/* Loadable */
/** Simple interface to allow a module to use */
public interface ConditionalModule<T extends IToolContext> {
ModifierCondition<T> condition();
}
/** Loadable for tool context conditions, typically used with {@link RecordLoadable#directField(Function)} */
public static final RecordLoadable<ModifierCondition<IToolContext>> CONTEXT_LOADABLE = RecordLoadable.create(
ToolContextPredicate.LOADER.defaultField("tool", ModifierCondition::tool),
ModifierEntry.VALID_LEVEL.defaultField("modifier_level", ModifierCondition::modifierLevel),
ModifierCondition::new);
/** Loadable for tool stack conditions {@link RecordLoadable#directField(Function)} */
public static final RecordLoadable<ModifierCondition<IToolStackView>> TOOL_LOADABLE = RecordLoadable.create(
ToolStackPredicate.LOADER.defaultField("tool", ModifierCondition::tool),
ModifierEntry.VALID_LEVEL.defaultField("modifier_level", ModifierCondition::modifierLevel),
ModifierCondition::new);
/** Generic field instance used for most modules with conditions */
public static final LoadableField<ModifierCondition<IToolContext>,ConditionalModule<IToolContext>> CONTEXT_FIELD = CONTEXT_LOADABLE.directField(ConditionalModule::condition);
/** Generic field instance used for most modules with conditions */
public static final LoadableField<ModifierCondition<IToolStackView>,ConditionalModule<IToolStackView>> TOOL_FIELD = TOOL_LOADABLE.directField(ConditionalModule::condition);
}