forked from Wilyicaro/FactoryAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgress.java
More file actions
120 lines (107 loc) · 3.9 KB
/
Progress.java
File metadata and controls
120 lines (107 loc) · 3.9 KB
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
package wily.factoryapi.base;
import net.minecraft.nbt.CompoundTag;
import org.apache.commons.lang3.ArrayUtils;
import wily.factoryapi.util.CompoundTagUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Progress implements ITagSerializable<CompoundTag> {
public Progress.Identifier identifier;
private final List<ProgressEntry> entries;
public Progress(Progress.Identifier identifier, List<ProgressEntry> entries){
this.identifier = identifier;
this.entries = entries;
}
public Progress(Progress.Identifier identifier) {
this(identifier, new ArrayList<>());
}
public Progress(Progress.Identifier identifier, int x, int y, int initialMaxProgress){
this(identifier, List.of(new ProgressEntry(x,y,initialMaxProgress)));
}
public Progress(Progress.Identifier identifier, int entries, int defaultMaxProgress){
this(identifier);
for (int i = 0; i < entries; i++)
add(0,0,defaultMaxProgress);
}
public Progress add(int x, int y, int maxProgress){
entries.add(new ProgressEntry(x,y,maxProgress));
return this;
}
public List<ProgressEntry> getEntries() {
return entries;
}
public ProgressEntry first() {
return get(0);
}
public ProgressEntry get(int index) {
return entries.get(index);
}
public void forEach(Consumer<ProgressEntry> consumer){
getEntries().forEach(consumer);
}
public void setValues(int[] array) {
for (int i = 0; i < entries.size(); i++) {
ProgressEntry p = entries.get(i);
p.set(array[i*2]);
p.maxProgress = array[i*2 + 1];
}
}
public int[] getValues(){
int[] values = new int[0];
for (int i = 0; i < entries.size(); i++) {
ProgressEntry p = entries.get(i);
values = ArrayUtils.addAll(values,p.get(),p.maxProgress);
}
return values;
}
@Override
public CompoundTag serializeTag() {
CompoundTag compoundTag = new CompoundTag();
compoundTag.putIntArray(identifier.name, getValues());
return compoundTag;
}
@Override
public void deserializeTag(CompoundTag tag) {
for (int i = 0; i < entries.size(); i++) {
ProgressEntry p = entries.get(i);
int[] array = CompoundTagUtil.getIntArrayOrEmpty(tag, identifier.name);
if (i * 2 < array.length) {
p.set(array[i * 2]);
p.maxProgress = array[i * 2 + 1];
}
}
}
public static class ProgressEntry extends Stocker<Integer>{
public int maxProgress;
public int x;
public int y;
public int minValue = 0;
public ProgressEntry(int x, int y, int maxProgress){
super(0);
this.maxProgress = maxProgress;
this.x = x;
this.y = y;
}
public void set(int value){
super.set(Math.max(minValue,Math.min(value,maxProgress)));
}
public int add(int value){
int oldValue = get();
set(get() + value);
return get() - oldValue;
}
public int shrink(int value){
int oldValue = get();
set(get() - value);
return oldValue - get();
}
}
public record Identifier(String name) {
public static Progress.Identifier DEFAULT = new Progress.Identifier("progress");
public static Progress.Identifier ENERGY_STORAGE = new Progress.Identifier("energyStorage");
public static Progress.Identifier TANK = new Progress.Identifier("tank");
public static Progress.Identifier BURN_TIME = new Progress.Identifier("burnTime");
public static Progress.Identifier GENERATING = new Progress.Identifier("gen");
public static Progress.Identifier MATTER = new Progress.Identifier("matter");
}
}