-
Notifications
You must be signed in to change notification settings - Fork 754
/
TankCapacityModule.java
70 lines (61 loc) · 3.39 KB
/
TankCapacityModule.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
package slimeknights.tconstruct.library.modifiers.modules.fluid;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.data.loadable.Loadables;
import slimeknights.mantle.data.loadable.field.LoadableField;
import slimeknights.mantle.data.loadable.primitive.BooleanLoadable;
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.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.build.VolatileDataModifierHook;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.module.HookProvider;
import slimeknights.tconstruct.library.module.ModuleHook;
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import java.util.List;
/** Module letting a tool change tank capacity */
@Getter
@RequiredArgsConstructor
public class TankCapacityModule implements ModifierModule, VolatileDataModifierHook {
private static final List<ModuleHook<?>> DEFAULT_HOOKS = HookProvider.<TankCapacityModule>defaultHooks(ModifierHooks.VOLATILE_DATA);
/** Default key for capacity */
public static final ResourceLocation DEFAULT_CAPACITY_KEY = TConstruct.getResource("tank_capacity");
/* Fields */
protected static final LoadableField<ResourceLocation,TankCapacityModule> CAPACITY_KEY_FIELD = Loadables.RESOURCE_LOCATION.defaultField("capacity_key", DEFAULT_CAPACITY_KEY, TankCapacityModule::getCapacityKey);
protected static final LoadableField<Integer,TankCapacityModule> CAPACITY_FIELD = IntLoadable.FROM_ZERO.requiredField("capacity", TankCapacityModule::getCapacity);
protected static final LoadableField<Boolean,TankCapacityModule> SCALE_CAPACITY_FIELD = BooleanLoadable.INSTANCE.requiredField("scale_capacity", TankCapacityModule::isScaleCapacity);
/** Loader instance */
public static final RecordLoadable<TankCapacityModule> LOADER = RecordLoadable.create(CAPACITY_KEY_FIELD, CAPACITY_FIELD, SCALE_CAPACITY_FIELD, TankCapacityModule::new);
/** Volatile NBT integer indicating the tank's max capacity */
private final ResourceLocation capacityKey;
/** Max capacity added by this module */
private final int capacity;
/** If true, capacity scales with level */
private final boolean scaleCapacity;
public TankCapacityModule(int capacity, boolean scaleCapacity) {
this(DEFAULT_CAPACITY_KEY, capacity, scaleCapacity);
}
/** Gets the full capacity of the tank */
public int getCapacity(IToolStackView tool) {
return tool.getVolatileData().getInt(getCapacityKey());
}
@Override
public void addVolatileData(ToolRebuildContext context, ModifierEntry modifier, ModDataNBT volatileData) {
ResourceLocation key = getCapacityKey();
volatileData.putInt(key, capacity * modifier.getLevel() + volatileData.getInt(key));
}
@Override
public List<ModuleHook<?>> getDefaultHooks() {
return DEFAULT_HOOKS;
}
@Override
public IGenericLoader<? extends TankCapacityModule> getLoader() {
return LOADER;
}
}