-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #986 from KyoriPowered/feat/mc/1.20.3
1.20.3
- Loading branch information
Showing
6 changed files
with
188 additions
and
12 deletions.
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
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
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
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
60 changes: 60 additions & 0 deletions
60
...erializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/UUIDSerializer.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,60 @@ | ||
/* | ||
* This file is part of adventure, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2017-2023 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.adventure.text.serializer.gson; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
final class UUIDSerializer extends TypeAdapter<UUID> { | ||
static final TypeAdapter<UUID> INSTANCE = new UUIDSerializer().nullSafe(); | ||
|
||
private UUIDSerializer() { | ||
} | ||
|
||
@Override | ||
public void write(final JsonWriter out, final UUID value) throws IOException { | ||
// todo: feature flag to choose whether to emit as 4-int syntax | ||
out.value(value.toString()); | ||
} | ||
|
||
@Override | ||
public UUID read(final JsonReader in) throws IOException { | ||
// int-array format was added in 23w40a, a pre for 1.20.3 | ||
if (in.peek() == JsonToken.BEGIN_ARRAY) { | ||
in.beginArray(); | ||
final int msb0 = in.nextInt(); | ||
final int msb1 = in.nextInt(); | ||
final int lsb0 = in.nextInt(); | ||
final int lsb1 = in.nextInt(); | ||
in.endArray(); | ||
return new UUID((long) msb0 << 32 | ((long) msb1 & 0xffffffffl), (long) lsb0 << 32 | ((long) lsb1 & 0xffffffffl)); | ||
} | ||
|
||
return UUID.fromString(in.nextString()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
.../testFixtures/java/net/kyori/adventure/text/serializer/json/ShowEntitySerializerTest.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,73 @@ | ||
/* | ||
* This file is part of adventure, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2017-2023 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.adventure.text.serializer.json; | ||
|
||
import java.util.UUID; | ||
import net.kyori.adventure.key.Key; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.event.HoverEvent; | ||
import net.kyori.adventure.text.format.Style; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
final class ShowEntitySerializerTest extends SerializerTest { | ||
@Test | ||
void testWithStringUuid() { | ||
final UUID id = UUID.randomUUID(); | ||
this.testStyle(Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build(), json -> { | ||
json.add(JSONComponentConstants.HOVER_EVENT, object(hover -> { | ||
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_entity"); | ||
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> { | ||
contents.addProperty(JSONComponentConstants.SHOW_ENTITY_TYPE, "minecraft:zombie"); | ||
contents.addProperty(JSONComponentConstants.SHOW_ENTITY_ID, id.toString()); | ||
})); | ||
})); | ||
}); | ||
} | ||
|
||
@Test | ||
void testWithIntArrayUuid() { | ||
final UUID id = UUID.randomUUID(); | ||
assertEquals( | ||
Component.text("", Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build()), | ||
this.deserialize(object(comp -> { | ||
comp.addProperty(JSONComponentConstants.TEXT, ""); | ||
comp.add(JSONComponentConstants.HOVER_EVENT, object(hover -> { | ||
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_entity"); | ||
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> { | ||
contents.addProperty(JSONComponentConstants.SHOW_ENTITY_TYPE, "minecraft:zombie"); | ||
contents.add(JSONComponentConstants.SHOW_ENTITY_ID, array(idArray -> { | ||
idArray.add((int) (id.getMostSignificantBits() >> 32)); | ||
idArray.add((int) (id.getMostSignificantBits() & 0xffffffffl)); | ||
idArray.add((int) (id.getLeastSignificantBits() >> 32)); | ||
idArray.add((int) (id.getLeastSignificantBits() & 0xffffffffl)); | ||
})); | ||
})); | ||
})); | ||
}) | ||
) | ||
); | ||
} | ||
} |