Skip to content

Commit dc4ae07

Browse files
committed
chore: use interface for intermediate value output data
1 parent 6ff787f commit dc4ae07

File tree

1 file changed

+58
-69
lines changed
  • worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11

1 file changed

+58
-69
lines changed

worldedit-bukkit/adapters/adapter-1_21_11/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_11/LinValueOutput.java

Lines changed: 58 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class LinValueOutput implements ValueOutput {
4040
private final ProblemReporter problemReporter;
4141
private final DynamicOps<LinTag<?>> ops;
4242
private final LinCompoundTag.Builder builder;
43-
private final Map<String, PendingTagEntry> collector;
43+
private final Map<String, PendingEntry> collector;
4444

4545
LinValueOutput(final ProblemReporter reporter, final DynamicOps<LinTag<?>> ops, final LinCompoundTag.Builder output) {
4646
this.problemReporter = reporter;
@@ -49,13 +49,6 @@ public class LinValueOutput implements ValueOutput {
4949
this.collector = new LinkedHashMap<>();
5050
}
5151

52-
public static LinValueOutput createWrappingWithContext(
53-
ProblemReporter problemReporter,
54-
HolderLookup.Provider lookup
55-
) {
56-
return new LinValueOutput(problemReporter, lookup.createSerializationContext(LinOps.INSTANCE), LinCompoundTag.builder());
57-
}
58-
5952
public static LinValueOutput createWithContext(ProblemReporter reporter, HolderLookup.Provider lookup) {
6053
return new LinValueOutput(reporter, lookup.createSerializationContext(LinOps.INSTANCE), LinCompoundTag.builder());
6154
}
@@ -64,10 +57,10 @@ public static LinValueOutput createWithContext(ProblemReporter reporter, HolderL
6457
public <T> void store(final @NotNull String key, final @NotNull Codec<T> codec, final @NotNull T value) {
6558
DataResult<LinTag<?>> result = codec.encodeStart(this.ops, value);
6659
switch (result) {
67-
case DataResult.Success<LinTag<?>> success -> this.collector.put(key, PendingTagEntry.ofTag(success.value()));
60+
case DataResult.Success<LinTag<?>> success -> this.collector.put(key, PendingEntry.ofTag(success.value()));
6861
case DataResult.Error<LinTag<?>> error -> {
6962
this.problemReporter.report(() -> "Failed to encode value '" + value + "' to field '" + key + "': " + error.message());
70-
error.partialValue().ifPresent(tag -> this.collector.put(key, PendingTagEntry.ofTag(tag)));
63+
error.partialValue().ifPresent(tag -> this.collector.put(key, PendingEntry.ofTag(tag)));
7164
}
7265
}
7366
}
@@ -99,72 +92,72 @@ public <T> void store(final @NotNull MapCodec<T> mapCodec, final @NotNull T valu
9992
private void merge(LinCompoundTag other) {
10093
other.value().forEach((key, tag) -> {
10194
if (tag instanceof LinCompoundTag compoundTag && this.collector.containsKey(key)) {
102-
PendingTagEntry entry = this.collector.get(key);
103-
if (entry.isCompoundBuilder()) {
104-
entry.compoundBuilder().put(key, compoundTag);
105-
return;
106-
}
107-
if (entry.isTag() && entry.tag instanceof LinCompoundTag storedCompound) {
108-
this.collector.put(
109-
key,
110-
PendingTagEntry.ofCompoundBuilder(storedCompound.toBuilder().put(key, compoundTag))
111-
);
112-
return;
95+
switch (this.collector.get(key)) {
96+
case PendingCompoundBuilderEntry entry -> entry.compoundBuilder().put(key, compoundTag);
97+
case PendingTagEntry entry -> {
98+
if (entry.tag() instanceof LinCompoundTag storedCompound) {
99+
this.collector.put(
100+
key, PendingEntry.ofCompoundBuilder(storedCompound.toBuilder().put(key, compoundTag))
101+
);
102+
}
103+
}
104+
default -> {
105+
}
113106
}
114107
}
115-
this.collector.put(key, PendingTagEntry.ofTag(tag));
108+
this.collector.put(key, PendingEntry.ofTag(tag));
116109
});
117110
}
118111

119112
@Override
120113
public void putBoolean(final @NotNull String key, final boolean value) {
121-
this.collector.put(key, PendingTagEntry.ofTag(LinByteTag.of(value ? (byte) 1 : 0)));
114+
this.collector.put(key, PendingEntry.ofTag(LinByteTag.of(value ? (byte) 1 : 0)));
122115
}
123116

124117
@Override
125118
public void putByte(final @NotNull String key, final byte value) {
126-
this.collector.put(key, PendingTagEntry.ofTag(LinByteTag.of(value)));
119+
this.collector.put(key, PendingEntry.ofTag(LinByteTag.of(value)));
127120
}
128121

129122
@Override
130123
public void putShort(final @NotNull String key, final short value) {
131-
this.collector.put(key, PendingTagEntry.ofTag(LinShortTag.of(value)));
124+
this.collector.put(key, PendingEntry.ofTag(LinShortTag.of(value)));
132125
}
133126

134127
@Override
135128
public void putInt(final @NotNull String key, final int value) {
136-
this.collector.put(key, PendingTagEntry.ofTag(LinIntTag.of(value)));
129+
this.collector.put(key, PendingEntry.ofTag(LinIntTag.of(value)));
137130
}
138131

139132
@Override
140133
public void putLong(final @NotNull String key, final long value) {
141-
this.collector.put(key, PendingTagEntry.ofTag(LinLongTag.of(value)));
134+
this.collector.put(key, PendingEntry.ofTag(LinLongTag.of(value)));
142135
}
143136

144137
@Override
145138
public void putFloat(final @NotNull String key, final float value) {
146-
this.collector.put(key, PendingTagEntry.ofTag(LinFloatTag.of(value)));
139+
this.collector.put(key, PendingEntry.ofTag(LinFloatTag.of(value)));
147140
}
148141

149142
@Override
150143
public void putDouble(final @NotNull String key, final double value) {
151-
this.collector.put(key, PendingTagEntry.ofTag(LinDoubleTag.of(value)));
144+
this.collector.put(key, PendingEntry.ofTag(LinDoubleTag.of(value)));
152145
}
153146

154147
@Override
155148
public void putString(final @NotNull String key, final @NotNull String value) {
156-
this.collector.put(key, PendingTagEntry.ofTag(LinStringTag.of(value)));
149+
this.collector.put(key, PendingEntry.ofTag(LinStringTag.of(value)));
157150
}
158151

159152
@Override
160153
public void putIntArray(final @NotNull String key, final int @NotNull [] value) {
161-
this.collector.put(key, PendingTagEntry.ofTag(LinIntArrayTag.of(value)));
154+
this.collector.put(key, PendingEntry.ofTag(LinIntArrayTag.of(value)));
162155
}
163156

164157
@Override
165158
public @NotNull ValueOutput child(final @NotNull String key) {
166159
LinCompoundTag.Builder builder = LinCompoundTag.builder();
167-
this.collector.put(key, PendingTagEntry.ofCompoundBuilder(builder));
160+
this.collector.put(key, PendingEntry.ofCompoundBuilder(builder));
168161
return new LinValueOutput(
169162
this.problemReporter.forChild(new ProblemReporter.FieldPathElement(key)),
170163
this.ops,
@@ -174,15 +167,15 @@ public void putIntArray(final @NotNull String key, final int @NotNull [] value)
174167

175168
@Override
176169
public @NotNull ValueOutputList childrenList(final @NotNull String key) {
177-
List<PendingTagEntry> list = new LinkedList<>();
178-
this.collector.put(key, PendingTagEntry.ofList(list));
170+
List<PendingEntry> list = new LinkedList<>();
171+
this.collector.put(key, PendingEntry.ofList(list));
179172
return new ListWrapper(key, this.problemReporter, this.ops, list);
180173
}
181174

182175
@Override
183176
public <T> @NotNull TypedOutputList<T> list(final @NotNull String key, final @NotNull Codec<T> codec) {
184-
final List<PendingTagEntry> list = new LinkedList<>();
185-
this.collector.put(key, PendingTagEntry.ofList(list));
177+
final List<PendingEntry> list = new LinkedList<>();
178+
this.collector.put(key, PendingEntry.ofList(list));
186179
return new TypedListWrapper<>(this.problemReporter, key, this.ops, codec, list);
187180
}
188181

@@ -205,27 +198,23 @@ public LinCompoundTag.Builder toBuilder() {
205198
return this.builder;
206199
}
207200

208-
private LinTag<?> unwrapPendingEntry(final PendingTagEntry entry) {
209-
if (entry.isTag()) {
210-
return entry.tag();
211-
}
212-
if (entry.isCompoundBuilder()) {
213-
return entry.compoundBuilder().build();
214-
}
215-
if (entry.isList()) {
216-
return LinOps.rawTagListToLinList(entry.list().stream()
217-
.map(this::unwrapPendingEntry).collect(Collectors.toUnmodifiableList()));
218-
}
219-
throw new IllegalStateException();
201+
private LinTag<?> unwrapPendingEntry(final PendingEntry entry) {
202+
return switch (entry) {
203+
case PendingTagEntry e -> e.tag();
204+
case PendingCompoundBuilderEntry e -> e.compoundBuilder().build();
205+
case PendingListEntry e -> LinOps.rawTagListToLinList(
206+
e.entries().stream().map(this::unwrapPendingEntry).collect(Collectors.toList())
207+
);
208+
};
220209
}
221210

222211
private record ListWrapper(String fieldName, ProblemReporter problemReporter, DynamicOps<LinTag<?>> ops,
223-
List<PendingTagEntry> list) implements ValueOutputList {
212+
List<PendingEntry> list) implements ValueOutputList {
224213

225214
@Override
226215
public @NotNull ValueOutput addChild() {
227216
LinCompoundTag.Builder tag = LinCompoundTag.builder();
228-
this.list.add(PendingTagEntry.ofCompoundBuilder(tag));
217+
this.list.add(PendingEntry.ofCompoundBuilder(tag));
229218
return new LinValueOutput(
230219
this.problemReporter.forChild(
231220
new ProblemReporter.IndexedFieldPathElement(this.fieldName, this.list.size() - 1)
@@ -248,16 +237,16 @@ public boolean isEmpty() {
248237
}
249238

250239
private record TypedListWrapper<T>(ProblemReporter problemReporter, String name, DynamicOps<LinTag<?>> ops, Codec<T> codec,
251-
List<PendingTagEntry> list) implements TypedOutputList<T> {
240+
List<PendingEntry> list) implements TypedOutputList<T> {
252241

253242
@Override
254243
public void add(final T value) {
255244
DataResult<LinTag<?>> dataResult = this.codec.encodeStart(this.ops, value);
256245
switch (dataResult) {
257-
case DataResult.Success<LinTag<?>> success -> this.list.add(PendingTagEntry.ofTag(success.value()));
246+
case DataResult.Success<LinTag<?>> success -> this.list.add(PendingEntry.ofTag(success.value()));
258247
case DataResult.Error<LinTag<?>> error -> {
259248
this.problemReporter.report(() -> "Failed to append value '" + value + "' to list '" + this.name + "':" + error.message());
260-
error.partialValue().map(PendingTagEntry::ofTag).ifPresent(list::add);
249+
error.partialValue().map(PendingEntry::ofTag).ifPresent(list::add);
261250
}
262251
}
263252
}
@@ -269,31 +258,31 @@ public boolean isEmpty() {
269258

270259
}
271260

272-
private record PendingTagEntry(LinTag<?> tag, List<PendingTagEntry> list, LinCompoundTag.Builder compoundBuilder) {
261+
private sealed interface PendingEntry permits PendingTagEntry, PendingListEntry, PendingCompoundBuilderEntry {
273262

274-
public static PendingTagEntry ofTag(LinTag<?> tag) {
275-
return new PendingTagEntry(tag, null, null);
263+
static PendingEntry ofTag(LinTag<?> tag) {
264+
return new PendingTagEntry(tag);
276265
}
277266

278-
public static PendingTagEntry ofList(List<PendingTagEntry> list) {
279-
return new PendingTagEntry(null, list, null);
267+
static PendingEntry ofList(List<PendingEntry> list) {
268+
return new PendingListEntry(list);
280269
}
281270

282-
public static PendingTagEntry ofCompoundBuilder(LinCompoundTag.Builder compoundBuilder) {
283-
return new PendingTagEntry(null, null, compoundBuilder);
271+
static PendingEntry ofCompoundBuilder(LinCompoundTag.Builder builder) {
272+
return new PendingCompoundBuilderEntry(builder);
284273
}
285274

286-
public boolean isTag() {
287-
return this.tag != null;
288-
}
275+
}
289276

290-
public boolean isList() {
291-
return this.list != null;
292-
}
277+
private record PendingTagEntry(LinTag<?> tag) implements PendingEntry {
293278

294-
public boolean isCompoundBuilder() {
295-
return this.compoundBuilder != null;
296-
}
279+
}
280+
281+
private record PendingListEntry(List<PendingEntry> entries) implements PendingEntry {
282+
283+
}
284+
285+
private record PendingCompoundBuilderEntry(LinCompoundTag.Builder compoundBuilder) implements PendingEntry {
297286

298287
}
299288

0 commit comments

Comments
 (0)