-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathFixedIntArraySyncValue.java
More file actions
109 lines (93 loc) · 3.32 KB
/
FixedIntArraySyncValue.java
File metadata and controls
109 lines (93 loc) · 3.32 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
package gregtech.api.mui.sync;
import net.minecraft.network.PacketBuffer;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.value.sync.ValueSyncHandler;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Sync Value for an array with fixed length.
* <p>
* Does not create new arrays.
*/
public class FixedIntArraySyncValue extends ValueSyncHandler<int[]> {
private final int[] cache;
private final Supplier<int[]> getter;
private final @Nullable Consumer<int[]> setter;
public FixedIntArraySyncValue(@NotNull Supplier<int[]> getter) {
this(getter, null);
}
public FixedIntArraySyncValue(@NotNull Supplier<int[]> getter, @Nullable Consumer<int[]> setter) {
this.getter = Objects.requireNonNull(getter);
this.setter = setter;
this.cache = getter.get();
}
@Contract("null, _, null, _ -> fail")
public FixedIntArraySyncValue(@Nullable Supplier<int[]> clientGetter, @Nullable Consumer<int[]> clientSetter,
@Nullable Supplier<int[]> serverGetter, @Nullable Consumer<int[]> serverSetter) {
if (clientGetter == null && serverGetter == null) {
throw new NullPointerException("Client or server getter must not be null!");
}
if (NetworkUtils.isClient()) {
this.getter = clientGetter != null ? clientGetter : serverGetter;
this.setter = clientSetter != null ? clientSetter : serverSetter;
} else {
this.getter = serverGetter != null ? serverGetter : clientGetter;
this.setter = serverSetter != null ? serverSetter : clientSetter;
}
this.cache = this.getter.get();
}
@Override
public void setValue(int @NotNull [] value, boolean setSource, boolean sync) {
if (value.length != cache.length) {
throw new IllegalArgumentException("Incompatible array lengths");
}
System.arraycopy(value, 0, cache, 0, value.length);
if (setSource && this.setter != null) {
this.setter.accept(value);
}
if (sync) {
sync(0, this::write);
}
}
@Override
public boolean updateCacheFromSource(boolean isFirstSync) {
if (isFirstSync || !Arrays.equals(this.getter.get(), this.cache)) {
setValue(this.getter.get(), false, false);
return true;
}
return false;
}
@Override
public void notifyUpdate() {
setValue(this.getter.get(), false, true);
}
@Override
public void write(@NotNull PacketBuffer buffer) throws IOException {
for (int i : cache) {
buffer.writeVarInt(i);
}
}
@Override
public void read(@NotNull PacketBuffer buffer) throws IOException {
for (int i = 0; i < cache.length; i++) {
cache[i] = buffer.readVarInt();
}
}
@Override
public int[] getValue() {
return this.cache;
}
public int getValue(int index) {
return this.cache[index];
}
@Override
public Class<int[]> getValueType() {
return int[].class;
}
}