-
Notifications
You must be signed in to change notification settings - Fork 2
feat: entity editing #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThatGravyBoat
wants to merge
6
commits into
main
Choose a base branch
from
feat/entity-editing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: entity editing #877
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce00d6a
feat: entity editing
ThatGravyBoat 0c038bc
chore: check if the player can edit before pick block or pick entity
ThatGravyBoat eca6248
chore: update minestom and horse stuff
ThatGravyBoat 2bbb6cc
chore: resolve comments
ThatGravyBoat 41f3a7e
chore: slop snbt parser (adventures doesnt nicely support all modern …
ThatGravyBoat 07c35b9
chore: reuse scope
ThatGravyBoat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,15 @@ | ||
| <component name="ProjectRunConfigurationManager"> | ||
| <configuration default="false" name="DevServer" type="Application" factoryName="Application"> | ||
| <option name="ALTERNATIVE_JRE_PATH" value="jbr-25"/> | ||
| <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true"/> | ||
| <envs> | ||
| <env name="MAPMAKER_RESOURCE_PACK_HASH" value="dev"/> | ||
| </envs> | ||
| <option name="MAIN_CLASS_NAME" value="net.hollowcube.mapmaker.dev.DevMain"/> | ||
| <module name="mapmaker.bin.development.main"/> | ||
| <option name="VM_PARAMETERS" value="--enable-native-access=ALL-UNNAMED -XX:+AllowEnhancedClassRedefinition"/> | ||
| <method v="2"> | ||
| <option name="Make" enabled="true"/> | ||
| </method> | ||
| </configuration> | ||
| <configuration default="false" name="DevServer" type="Application" factoryName="Application"> | ||
| <option name="ALTERNATIVE_JRE_PATH" value="25" /> | ||
| <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" /> | ||
| <envs> | ||
| <env name="MAPMAKER_RESOURCE_PACK_HASH" value="dev" /> | ||
| </envs> | ||
| <option name="MAIN_CLASS_NAME" value="net.hollowcube.mapmaker.dev.DevMain" /> | ||
| <module name="mapmaker.bin.development.main" /> | ||
| <option name="VM_PARAMETERS" value="--enable-native-access=ALL-UNNAMED" /> | ||
| <method v="2"> | ||
| <option name="Make" enabled="true" /> | ||
| </method> | ||
| </configuration> | ||
| </component> |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
54 changes: 54 additions & 0 deletions
54
modules/command/src/main/java/net/hollowcube/command/arg/ArgumentCompoundTag.java
This file contains hidden or 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,54 @@ | ||
| package net.hollowcube.command.arg; | ||
|
|
||
| import net.hollowcube.command.util.StringReader; | ||
| import net.hollowcube.common.parsing.ParsingException; | ||
| import net.hollowcube.common.parsing.snbt.Snbt; | ||
| import net.hollowcube.common.parsing.snbt.SnbtReader; | ||
| import net.kyori.adventure.nbt.CompoundBinaryTag; | ||
| import net.minestom.server.command.ArgumentParserType; | ||
| import net.minestom.server.command.CommandSender; | ||
| import net.minestom.server.network.NetworkBuffer; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public class ArgumentCompoundTag extends Argument<CompoundBinaryTag> { | ||
|
|
||
| private static final int COMMAND_SNBT_MAX_DEPTH = 16; | ||
|
|
||
| ArgumentCompoundTag(@NotNull String id) { | ||
| super(id); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull ParseResult<CompoundBinaryTag> parse(@NotNull CommandSender sender, @NotNull StringReader reader) { | ||
| var remainingText = reader.raw().substring(reader.pos()); | ||
| var snbtReader = new SnbtReader(remainingText, COMMAND_SNBT_MAX_DEPTH); | ||
| try { | ||
| var tag = Snbt.parse(snbtReader); | ||
| if (!(tag instanceof CompoundBinaryTag compoundTag)) { | ||
| return new ParseResult.Failure<>(reader.pos(), "Expected a compound tag"); | ||
| } | ||
| return new ParseResult.Success<>(compoundTag); | ||
| } catch (ParsingException exception) { | ||
| return new ParseResult.Failure<>( | ||
| reader.pos() + exception.cursor(), | ||
| "Failed to parse tag, " + exception.getMessage() | ||
| ); | ||
| } finally { | ||
| reader.restore(reader.pos() + snbtReader.cursor()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull ArgumentParserType argumentType() { | ||
| return ArgumentParserType.NBT_COMPOUND_TAG; | ||
| } | ||
|
|
||
| @Override | ||
| public void properties(@NotNull NetworkBuffer buffer) { | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldSuggest() { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
modules/common/src/main/java/net/hollowcube/common/parsing/ParsingException.java
This file contains hidden or 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,15 @@ | ||
| package net.hollowcube.common.parsing; | ||
|
|
||
| public final class ParsingException extends RuntimeException { | ||
|
|
||
| private final int cursor; | ||
|
|
||
| public ParsingException(int cursor, String message) { | ||
| super(message); | ||
| this.cursor = cursor; | ||
| } | ||
|
|
||
| public int cursor() { | ||
| return cursor; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
modules/common/src/main/java/net/hollowcube/common/parsing/snbt/Snbt.java
This file contains hidden or 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,34 @@ | ||
| package net.hollowcube.common.parsing.snbt; | ||
|
|
||
| import net.hollowcube.common.parsing.ParsingException; | ||
| import net.kyori.adventure.nbt.BinaryTag; | ||
|
|
||
| public class Snbt { | ||
|
|
||
| private static final int DEFAULT_MAX_DEPTH = 64; | ||
|
|
||
| public static BinaryTag parse(String input) throws ParsingException { | ||
| return parse(new SnbtReader(input, DEFAULT_MAX_DEPTH)); | ||
| } | ||
|
|
||
| public static BinaryTag parse(SnbtReader reader) throws ParsingException { | ||
| return switch (reader.peek()) { | ||
| case '{' -> SnbtObject.parseObject(reader); | ||
| case '[' -> SnbtArray.parseArray(reader); | ||
| case 't', 'T', 'f', 'F' -> SnbtMisc.parseBoolean(reader); | ||
| case '\"', '\'' -> SnbtString.parse(reader); | ||
| case '-', '+', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> SnbtNumber.parse(reader); | ||
| default -> { | ||
| if (SnbtString.ALPHA_PREDICATE.test(reader.peek())) { | ||
| var string = SnbtString.parseUnquoted(reader); | ||
| var operation = SnbtMisc.parseOperation(reader, string.value()); | ||
| if (operation != null) { | ||
| yield operation; | ||
| } | ||
| yield string; | ||
| } | ||
| throw new ParsingException(reader.cursor(), "Unexpected character: '" + reader.peek() + "'"); | ||
| } | ||
| }; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
modules/common/src/main/java/net/hollowcube/common/parsing/snbt/SnbtArray.java
This file contains hidden or 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,79 @@ | ||
| package net.hollowcube.common.parsing.snbt; | ||
|
|
||
| import it.unimi.dsi.fastutil.bytes.ByteArrayList; | ||
| import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
| import it.unimi.dsi.fastutil.longs.LongArrayList; | ||
| import net.hollowcube.common.parsing.ParsingException; | ||
| import net.kyori.adventure.nbt.*; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| public class SnbtArray { | ||
|
|
||
| static BinaryTag parseArray(SnbtReader reader) throws ParsingException { | ||
| try (var _ = reader.scope()) { | ||
| reader.require('['); | ||
| reader.read(SnbtMisc.WHITESPACE_PREDICATE); | ||
| var type = getArrayType(reader); | ||
| if (type == '\0') { | ||
| var output = ListBinaryTag.builder(); | ||
| parseArray(reader, () -> output.add(Snbt.parse(reader))); | ||
| return output.build(); | ||
| } else if (type == 'B' || type == 'b') { | ||
| var output = new ByteArrayList(); | ||
| parseArray(reader, () -> output.add(SnbtMisc.parseBooleanOrByte(reader).value())); | ||
| return ByteArrayBinaryTag.byteArrayBinaryTag(output.toByteArray()); | ||
| } else if (type == 'I' || type == 'i') { | ||
| var output = new IntArrayList(); | ||
| parseArray(reader, () -> output.add(expect(reader, SnbtNumber::parse, IntBinaryTag.class).value())); | ||
| return IntArrayBinaryTag.intArrayBinaryTag(output.toIntArray()); | ||
| } else if (type == 'L' || type == 'l') { | ||
| var output = new LongArrayList(); | ||
| parseArray(reader, () -> output.add(expect(reader, SnbtNumber::parse, LongBinaryTag.class).value())); | ||
| return LongArrayBinaryTag.longArrayBinaryTag(output.toLongArray()); | ||
| } | ||
| throw new ParsingException(reader.cursor(), "Expected array type but got '" + type + "'"); | ||
| } | ||
| } | ||
|
|
||
| private static char getArrayType(SnbtReader reader) throws ParsingException { | ||
| var type = reader.peek(); | ||
| var offset = 0; | ||
| while (SnbtMisc.WHITESPACE_PREDICATE.test(reader.peek(offset))) { | ||
| offset++; | ||
| } | ||
| if (reader.peek(offset) == ';') { | ||
| reader.read(offset); | ||
| reader.require(';'); | ||
| return type; | ||
| } | ||
| return '\0'; | ||
| } | ||
|
|
||
| private static <T extends BinaryTag> T expect(SnbtReader reader, Function<SnbtReader, BinaryTag> parser, Class<T> expected) { | ||
| var tag = parser.apply(reader); | ||
| if (expected.isInstance(tag)) { | ||
| return expected.cast(tag); | ||
| } else { | ||
| throw new ParsingException(reader.cursor(), "Expected " + expected.getSimpleName() + " but got " + tag.getClass().getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
| private static void parseArray(SnbtReader reader, Runnable elementReader) throws ParsingException { | ||
| reader.read(SnbtMisc.WHITESPACE_PREDICATE); | ||
|
|
||
| while (reader.peek() != ']') { | ||
| elementReader.run(); | ||
| reader.read(SnbtMisc.WHITESPACE_PREDICATE); | ||
| if (reader.peek() == ',') { | ||
| reader.read(); | ||
| reader.read(SnbtMisc.WHITESPACE_PREDICATE); | ||
| } else if (reader.peek() == ']') { | ||
| break; | ||
| } else { | ||
| throw new ParsingException(reader.cursor(), "Expected ',' or ']' but got '" + reader.peek() + "'"); | ||
| } | ||
| } | ||
| reader.require(']'); | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
modules/common/src/main/java/net/hollowcube/common/parsing/snbt/SnbtMisc.java
This file contains hidden or 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,70 @@ | ||
| package net.hollowcube.common.parsing.snbt; | ||
|
|
||
| import it.unimi.dsi.fastutil.chars.CharPredicate; | ||
| import net.hollowcube.common.parsing.ParsingException; | ||
| import net.kyori.adventure.nbt.BinaryTag; | ||
| import net.kyori.adventure.nbt.ByteBinaryTag; | ||
| import net.kyori.adventure.nbt.IntArrayBinaryTag; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class SnbtMisc { | ||
|
|
||
| static final CharPredicate WHITESPACE_PREDICATE = Character::isWhitespace; | ||
|
|
||
| static ByteBinaryTag parseBoolean(SnbtReader reader) throws ParsingException { | ||
| return switch (reader.peek()) { | ||
| case 't', 'T' -> { | ||
| reader.require("true"); | ||
| yield ByteBinaryTag.byteBinaryTag((byte) 1); | ||
| } | ||
| case 'f', 'F' -> { | ||
| reader.require("false"); | ||
| yield ByteBinaryTag.byteBinaryTag((byte) 0); | ||
| } | ||
| default -> throw new ParsingException(reader.cursor(), "Expected 'true' or 'false'"); | ||
| }; | ||
| } | ||
|
|
||
| static ByteBinaryTag parseBooleanOrByte(SnbtReader reader) throws ParsingException { | ||
| if (reader.peek() == 't' || reader.peek() == 'T' || reader.peek() == 'f' || reader.peek() == 'F') { | ||
| return SnbtMisc.parseBoolean(reader); | ||
| } else if (SnbtNumber.parse(reader) instanceof ByteBinaryTag tag) { | ||
| return tag; | ||
| } else { | ||
| throw new ParsingException(reader.cursor(), "Expected boolean or byte value"); | ||
| } | ||
| } | ||
|
|
||
| static BinaryTag parseOperation(SnbtReader reader, String name) throws ParsingException { | ||
| return switch (name) { | ||
| case "uuid" -> { | ||
| try { | ||
| reader.read(WHITESPACE_PREDICATE); | ||
| reader.require('('); | ||
| var uuid = UUID.fromString(SnbtString.parseUnquoted(reader).value()); | ||
| reader.read(WHITESPACE_PREDICATE); | ||
| reader.require(')'); | ||
|
|
||
| yield IntArrayBinaryTag.intArrayBinaryTag( | ||
| (int) (uuid.getMostSignificantBits() >> 32), | ||
| (int) uuid.getMostSignificantBits(), | ||
| (int) (uuid.getLeastSignificantBits() >> 32), | ||
| (int) uuid.getLeastSignificantBits() | ||
| ); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new ParsingException(reader.cursor(), "Invalid UUID format"); | ||
| } | ||
| } | ||
| case "bool" -> { | ||
| reader.read(WHITESPACE_PREDICATE); | ||
| reader.require('('); | ||
| var value = parseBooleanOrByte(reader); | ||
| reader.read(WHITESPACE_PREDICATE); | ||
| reader.require(')'); | ||
| yield ByteBinaryTag.byteBinaryTag(value.value() != 0 ? (byte) 1 : (byte) 0); | ||
| } | ||
| default -> null; | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.