-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44acdba
commit 3dfdf70
Showing
2 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/net/goldenstack/loot/util/ListOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package net.goldenstack.loot.util; | ||
|
||
import net.minestom.server.utils.nbt.BinaryTagSerializer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
public sealed interface ListOperation { | ||
|
||
@SuppressWarnings("UnstableApiUsage") | ||
@NotNull BinaryTagSerializer<ListOperation> SERIALIZER = Template.registry("mode", | ||
Template.entry("append", Append.class, Template.template(Append::new)), | ||
Template.entry("insert", Insert.class, Template.template( | ||
"offset", BinaryTagSerializer.INT.optional(0), Insert::offset, | ||
Insert::new | ||
)), | ||
Template.entry("replace_all", ReplaceAll.class, Template.template(ReplaceAll::new)), | ||
Template.entry("replace_section", ReplaceSection.class, Template.template( | ||
"offset", BinaryTagSerializer.INT.optional(0), ReplaceSection::offset, | ||
"size", BinaryTagSerializer.INT.optional(), ReplaceSection::size, | ||
ReplaceSection::new | ||
)) | ||
); | ||
|
||
<T> @NotNull List<T> apply(@NotNull List<T> values, @NotNull List<T> input); | ||
|
||
record Append() implements ListOperation { | ||
@Override | ||
public @NotNull <T> List<T> apply(@NotNull List<T> values, @NotNull List<T> input) { | ||
return Stream.concat(input.stream(), values.stream()).toList(); | ||
|
||
} | ||
} | ||
|
||
record Insert(int offset) implements ListOperation { | ||
@Override | ||
public @NotNull <T> List<T> apply(@NotNull List<T> values, @NotNull List<T> input) { | ||
List<T> items = new ArrayList<>(); | ||
items.addAll(input.subList(0, this.offset)); | ||
items.addAll(values); | ||
items.addAll(input.subList(this.offset, input.size())); | ||
return items; | ||
} | ||
} | ||
|
||
record ReplaceAll() implements ListOperation { | ||
@Override | ||
public @NotNull <T> List<T> apply(@NotNull List<T> values, @NotNull List<T> input) { | ||
return values; | ||
} | ||
} | ||
|
||
record ReplaceSection(int offset, @Nullable Integer size) implements ListOperation { | ||
@Override | ||
public @NotNull <T> List<T> apply(@NotNull List<T> values, @NotNull List<T> input) { | ||
List<T> items = new ArrayList<>(); | ||
items.addAll(input.subList(0, offset)); | ||
items.addAll(values); | ||
|
||
int size = this.size != null ? this.size : values.size(); | ||
|
||
// Add truncated part of list of possible | ||
if (offset + size < input.size()) { | ||
items.addAll(input.subList(offset + size, input.size())); | ||
} | ||
|
||
return items; | ||
} | ||
} | ||
|
||
} |