-
Notifications
You must be signed in to change notification settings - Fork 755
/
SwappableSlotModule.java
124 lines (109 loc) · 5.28 KB
/
SwappableSlotModule.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package slimeknights.tconstruct.library.modifiers.modules.build;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.data.loadable.primitive.IntLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IGenericLoader;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.build.ModifierRemovalHook;
import slimeknights.tconstruct.library.modifiers.hook.build.VolatileDataModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.display.DisplayNameModifierHook;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.util.ModuleWithKey;
import slimeknights.tconstruct.library.module.HookProvider;
import slimeknights.tconstruct.library.module.ModuleHook;
import slimeknights.tconstruct.library.tools.SlotType;
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import javax.annotation.Nullable;
import java.util.List;
/**
* Module for a extra slot modifier with multiple variants based on the slot type
* @param key Persistent data key containing the slot name. If null, uses the modifier ID.
* Presently, changing this makes it incompatible with the swappable modifier recipe, this is added for future proofing.
* @param slotCount Number of slots to grant
*/
public record SwappableSlotModule(@Nullable ResourceLocation key, int slotCount) implements VolatileDataModifierHook, DisplayNameModifierHook, ModifierRemovalHook, ModifierModule, ModuleWithKey {
private static final List<ModuleHook<?>> DEFAULT_HOOKS = HookProvider.<SwappableSlotModule>defaultHooks(ModifierHooks.VOLATILE_DATA, ModifierHooks.DISPLAY_NAME, ModifierHooks.REMOVE);
/** Format key for swappable variant */
private static final String FORMAT = TConstruct.makeTranslationKey("modifier", "extra_modifier.type_format");
public static final RecordLoadable<SwappableSlotModule> LOADER = RecordLoadable.create(
ModuleWithKey.FIELD,
IntLoadable.ANY_SHORT.requiredField("slots", SwappableSlotModule::slotCount),
SwappableSlotModule::new);
public SwappableSlotModule(int slotCount) {
this(null, slotCount);
}
@Override
public Component getDisplayName(IToolStackView tool, ModifierEntry entry, Component name) {
String slotName = tool.getPersistentData().getString(getKey(entry.getModifier()));
if (!slotName.isEmpty()) {
SlotType type = SlotType.getIfPresent(slotName);
if (type != null) {
return Component.translatable(FORMAT, name.plainCopy(), type.getDisplayName()).withStyle(style -> style.withColor(type.getColor()));
}
}
return name;
}
@Override
public Integer getPriority() {
// show lower priority so they group together
return 50;
}
@Override
public void addVolatileData(ToolRebuildContext context, ModifierEntry modifier, ModDataNBT volatileData) {
String slotName = context.getPersistentData().getString(getKey(modifier.getModifier()));
if (!slotName.isEmpty()) {
SlotType type = SlotType.getIfPresent(slotName);
if (type != null) {
volatileData.addSlots(type, slotCount);
}
}
}
@Nullable
@Override
public Component onRemoved(IToolStackView tool, Modifier modifier) {
tool.getPersistentData().remove(modifier.getId());
return null;
}
@Override
public List<ModuleHook<?>> getDefaultHooks() {
return DEFAULT_HOOKS;
}
@Override
public IGenericLoader<? extends ModifierModule> getLoader() {
return LOADER;
}
/** Module to add (or remove) additional slots based on the given swappable slot type */
public record BonusSlot(@Nullable ResourceLocation key, SlotType match, SlotType bonus, int slotCount) implements VolatileDataModifierHook, ModifierModule, ModuleWithKey {
private static final List<ModuleHook<?>> DEFAULT_HOOKS = List.of(ModifierHooks.VOLATILE_DATA);
public static final RecordLoadable<BonusSlot> LOADER = RecordLoadable.create(
ModuleWithKey.FIELD,
SlotType.LOADABLE.requiredField("match", BonusSlot::match),
SlotType.LOADABLE.requiredField("bonus", BonusSlot::bonus),
IntLoadable.ANY_SHORT.requiredField("slots", BonusSlot::slotCount),
BonusSlot::new);
public BonusSlot(SlotType match, SlotType penalty, int slotCount) {
this(null, match, penalty, slotCount);
}
@Override
public void addVolatileData(ToolRebuildContext context, ModifierEntry modifier, ModDataNBT volatileData) {
String slotName = context.getPersistentData().getString(getKey(modifier.getModifier()));
if (!slotName.isEmpty() && match.getName().equals(slotName)) {
volatileData.addSlots(bonus, slotCount);
}
}
@Override
public List<ModuleHook<?>> getDefaultHooks() {
return DEFAULT_HOOKS;
}
@Override
public IGenericLoader<? extends ModifierModule> getLoader() {
return LOADER;
}
}
}