Skip to content

Commit

Permalink
Merge pull request #986 from KyoriPowered/feat/mc/1.20.3
Browse files Browse the repository at this point in the history
1.20.3
  • Loading branch information
zml2008 authored Dec 7, 2023
2 parents 6b56578 + 35a4051 commit 3df534c
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.kyori.adventure.resource;

import java.net.URI;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.builder.AbstractBuilder;
import net.kyori.adventure.text.Component;
Expand All @@ -42,28 +43,30 @@ public interface ResourcePackRequest extends Examinable, ResourcePackRequestLike
/**
* Creates a resource pack request.
*
* @param id the id
* @param uri the uri
* @param hash the sha-1 hash
* @param required whether the resource pack is required or not
* @return the resource pack request
* @since 4.15.0
*/
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull URI uri, final @NotNull String hash, final boolean required) {
return resourcePackRequest(uri, hash, required, null);
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull UUID id, final @NotNull URI uri, final @NotNull String hash, final boolean required) {
return resourcePackRequest(id, uri, hash, required, null);
}

/**
* Creates a resource pack request.
*
* @param id the id
* @param uri the uri
* @param hash the sha-1 hash
* @param required whether the resource pack is required or not
* @param prompt the prompt
* @return the resource pack request
* @since 4.15.0
*/
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull URI uri, final @NotNull String hash, final boolean required, final @Nullable Component prompt) {
return new ResourcePackRequestImpl(uri, hash, required, prompt);
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull UUID id, final @NotNull URI uri, final @NotNull String hash, final boolean required, final @Nullable Component prompt) {
return new ResourcePackRequestImpl(id, uri, hash, required, prompt);
}

/**
Expand All @@ -76,6 +79,14 @@ public interface ResourcePackRequest extends Examinable, ResourcePackRequestLike
return new ResourcePackRequestImpl.BuilderImpl();
}

/**
* Gets the id.
*
* @return the id
* @since 4.15.0
*/
@NotNull UUID id();

/**
* Gets the uri.
*
Expand Down Expand Up @@ -121,6 +132,16 @@ public interface ResourcePackRequest extends Examinable, ResourcePackRequestLike
* @since 4.15.0
*/
interface Builder extends AbstractBuilder<ResourcePackRequest>, ResourcePackRequestLike {
/**
* Sets the id.
*
* @param id the id
* @return this builder
* @since 4.15.0
*/
@Contract("_ -> this")
@NotNull Builder id(final @NotNull UUID id);

/**
* Sets the uri.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.net.URI;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Stream;
import net.kyori.adventure.internal.Internals;
import net.kyori.adventure.text.Component;
Expand All @@ -35,18 +36,25 @@
import static java.util.Objects.requireNonNull;

final class ResourcePackRequestImpl implements ResourcePackRequest {
private final UUID id;
private final URI uri;
private final String hash;
private final boolean required;
private final Component prompt;

ResourcePackRequestImpl(final @NotNull URI uri, final @NotNull String hash, final boolean required, final @Nullable Component prompt) {
ResourcePackRequestImpl(final @NotNull UUID id, final @NotNull URI uri, final @NotNull String hash, final boolean required, final @Nullable Component prompt) {
this.id = requireNonNull(id, "id");
this.uri = requireNonNull(uri, "uri");
this.hash = requireNonNull(hash, "hash");
this.required = required;
this.prompt = prompt;
}

@Override
public @NotNull UUID id() {
return this.id;
}

@Override
public @NotNull URI uri() {
return this.uri;
Expand All @@ -70,6 +78,7 @@ public boolean required() {
@Override
public @NotNull Stream<? extends ExaminableProperty> examinableProperties() {
return Stream.of(
ExaminableProperty.of("id", this.id),
ExaminableProperty.of("uri", this.uri),
ExaminableProperty.of("hash", this.hash),
ExaminableProperty.of("required", this.required),
Expand All @@ -87,22 +96,25 @@ public boolean equals(final @Nullable Object other) {
if (this == other) return true;
if (!(other instanceof ResourcePackRequestImpl)) return false;
final ResourcePackRequestImpl that = (ResourcePackRequestImpl) other;
return this.required == that.required
&& this.uri.equals(that.uri)
&& this.hash.equals(that.hash)
&& Objects.equals(this.prompt, that.prompt);
return this.id.equals(that.id) &&
this.uri.equals(that.uri) &&
this.hash.equals(that.hash) &&
this.required == that.required &&
Objects.equals(this.prompt, that.prompt);
}

@Override
public int hashCode() {
int result = this.uri.hashCode();
int result = this.id.hashCode();
result = 31 * result + this.uri.hashCode();
result = 31 * result + this.hash.hashCode();
result = 31 * result + (this.required ? 1 : 0);
result = 31 * result + (this.prompt != null ? this.prompt.hashCode() : 0);
return result;
}

static final class BuilderImpl implements Builder {
private UUID id;
private URI uri;
private String hash;
private boolean required;
Expand All @@ -111,6 +123,12 @@ static final class BuilderImpl implements Builder {
BuilderImpl() {
}

@Override
public @NotNull Builder id(final @NotNull UUID id) {
this.id = requireNonNull(id, "id");
return this;
}

@Override
public @NotNull Builder uri(final @NotNull URI uri) {
this.uri = requireNonNull(uri, "uri");
Expand All @@ -137,7 +155,7 @@ static final class BuilderImpl implements Builder {

@Override
public @NotNull ResourcePackRequest build() {
return new ResourcePackRequestImpl(this.uri, this.hash, this.required, this.prompt);
return new ResourcePackRequestImpl(this.id, this.uri, this.hash, this.required, this.prompt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import java.util.UUID;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.BlockNBTComponent;
import net.kyori.adventure.text.Component;
Expand All @@ -50,6 +51,7 @@ final class SerializerFactory implements TypeAdapterFactory {
static final Class<TextColor> COLOR_TYPE = TextColor.class;
static final Class<TextDecoration> TEXT_DECORATION_TYPE = TextDecoration.class;
static final Class<BlockNBTComponent.Pos> BLOCK_NBT_POS_TYPE = BlockNBTComponent.Pos.class;
static final Class<UUID> UUID_TYPE = UUID.class;

private final boolean downsampleColors;
private final net.kyori.adventure.text.serializer.json.LegacyHoverEventSerializer legacyHoverSerializer;
Expand Down Expand Up @@ -87,6 +89,8 @@ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
return (TypeAdapter<T>) TextDecorationSerializer.INSTANCE;
} else if (BLOCK_NBT_POS_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) BlockNBTComponentPosSerializer.INSTANCE;
} else if (UUID_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) UUIDSerializer.INSTANCE;
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public HoverEvent.ShowEntity read(final JsonReader in) throws IOException {
if (fieldName.equals(SHOW_ENTITY_TYPE)) {
type = this.gson.fromJson(in, SerializerFactory.KEY_TYPE);
} else if (fieldName.equals(SHOW_ENTITY_ID)) {
id = UUID.fromString(in.nextString());
id = this.gson.fromJson(in, SerializerFactory.UUID_TYPE);
} else if (fieldName.equals(SHOW_ENTITY_NAME)) {
name = this.gson.fromJson(in, SerializerFactory.COMPONENT_TYPE);
} else {
Expand Down
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());
}
}
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));
}));
}));
}));
})
)
);
}
}

0 comments on commit 3df534c

Please sign in to comment.