Skip to content
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

Library patching #814

Merged
merged 23 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
21dface
Custom library patching toolchain
AbhigyaKrishna Jun 2, 2024
e535380
Patch module
AbhigyaKrishna Jun 3, 2024
531cef2
Configuration changes
AbhigyaKrishna Jun 3, 2024
18ac493
Adventure gson serializer patches
AbhigyaKrishna Jun 3, 2024
df0d135
GsonComponentSerializer patch
AbhigyaKrishna Jun 3, 2024
990cc38
Configuration update to conventions
AbhigyaKrishna Jun 3, 2024
d3968b1
Grouping change for adventure bundle
AbhigyaKrishna Jun 3, 2024
5d14703
Removed legacy fixes and updated Adventure serializer
AbhigyaKrishna Jun 3, 2024
51de60a
Gradle script update
AbhigyaKrishna Jun 3, 2024
8391c11
Parent build function to not include patch module jar
AbhigyaKrishna Jun 3, 2024
9586d8d
Changed adventure library configuration
AbhigyaKrishna Jun 3, 2024
596a961
Rollback constants
AbhigyaKrishna Jun 3, 2024
6b7ebc2
adventure version bump
AbhigyaKrishna Jun 3, 2024
1f0792f
Patch ShowItemSerializer and ShowEntitySerializer
AbhigyaKrishna Jun 3, 2024
cc8bb89
TranslatableArgument patch
AbhigyaKrishna Jun 3, 2024
5fbf597
Removed gradle daemon as we are having issues
AbhigyaKrishna Jun 3, 2024
b973820
Shadow configuration for patch module and gson inclusion
AbhigyaKrishna Jun 3, 2024
a00ae8f
Fix argument parsing
AbhigyaKrishna Jun 3, 2024
baec4bd
MIT Licence
AbhigyaKrishna Jun 3, 2024
9c4d7cb
Adventure version resolution fix
AbhigyaKrishna Jun 3, 2024
2888afa
Update to publish configuration which now includes shadow publication
AbhigyaKrishna Jun 3, 2024
cbfccaf
Unregister debug listener
AbhigyaKrishna Jun 3, 2024
af3c440
Backward compatibility to AdventureNBTSerializer
AbhigyaKrishna Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ java {
}

dependencies {
compileOnly(libs.bundles.adventure)
api(libs.bundles.adventure)
api(project(":patch:adventure-text-serializer-gson", "shadow"))
api(libs.adventure.text.serializer.legacy)
compileOnly(libs.gson)

testImplementation(libs.bundles.adventure)
testImplementation(project(":patch:adventure-text-serializer-gson"))
testImplementation(libs.adventure.text.serializer.legacy)
testImplementation(project(":netty-common"))
testImplementation(testlibs.mockbukkit)
testImplementation(testlibs.slf4j)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.text.serializer.gson.BackwardCompatUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -79,7 +80,12 @@ public AdventureNBTSerializer(boolean downsampleColor) {

String translate = reader.readUTF("translate", Function.identity());
String translateFallback = reader.readUTF("fallback", Function.identity());
List<Component> translateWith = reader.readList("with", this::deserializeComponentList);
List<? extends ComponentLike> translateWith;
if (BackwardCompatUtil.IS_4_15_0_OR_NEWER) {
translateWith = reader.readList("with", this::deserializeTranslationArgumentList);
} else {
translateWith = reader.readList("with", this::deserializeComponentList);
}
NBTReader score = reader.child("score");
String selector = reader.readUTF("selector", Function.identity());
String keybind = reader.readUTF("keybind", Function.identity());
Expand All @@ -98,7 +104,11 @@ public AdventureNBTSerializer(boolean downsampleColor) {
builder = Component.text().content(text);
} else if (translate != null) {
if (translateWith != null) {
builder = Component.translatable().key(translate).fallback(translateFallback).arguments(translateWith);
if (BackwardCompatUtil.IS_4_15_0_OR_NEWER) {
builder = Component.translatable().key(translate).fallback(translateFallback).arguments(translateWith);
} else {
builder = Component.translatable().key(translate).fallback(translateFallback).args(translateWith);
}
} else {
builder = Component.translatable().key(translate).fallback(translateFallback);
}
Expand Down Expand Up @@ -168,7 +178,11 @@ public AdventureNBTSerializer(boolean downsampleColor) {
// translation arguments
List<Component> args = ((TranslatableComponent) component).args();
if (!args.isEmpty()) {
writer.writeList("with", NBTType.COMPOUND, serializeComponentList(args));
if (BackwardCompatUtil.IS_4_15_0_OR_NEWER) {
writer.writeList("with", NBTType.COMPOUND, serializeTranslationArgumentList(((TranslatableComponent) component).arguments()));
} else {
writer.writeList("with", NBTType.COMPOUND, serializeComponentList(args));
}
}
} else if (component instanceof ScoreComponent) {
// nested compound
Expand Down Expand Up @@ -427,6 +441,33 @@ private List<NBTCompound> serializeComponentList(List<Component> value) {
}
// -------------------------------------------------

// ------------ TranslationArgument List ------------
private @NotNull List<TranslationArgument> deserializeTranslationArgumentList(List<?> value) {
if (value.isEmpty()) return Collections.emptyList();

List<TranslationArgument> arguments = new ArrayList<>(value.size());
for (Object nbt : value) {
if (nbt instanceof NBTByte) {
arguments.add(TranslationArgument.bool(((NBTByte) nbt).getAsByte() == 1));
} else if (nbt instanceof NBTNumber) {
arguments.add(TranslationArgument.numeric(((NBTNumber) nbt).getAsInt()));
} else {
arguments.add(TranslationArgument.component(deserialize(requireType((NBT) nbt, NBTType.COMPOUND))));
}
}

return arguments;
}

private List<NBTCompound> serializeTranslationArgumentList(List<TranslationArgument> value) {
List<NBTCompound> arguments = new ArrayList<>(value.size());
for (TranslationArgument argument : value) {
arguments.add(serializeComponent(argument.asComponent()));
}
return arguments;
}
// -------------------------------------------------

static class NBTReader {
private final NBTCompound compound;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.stats.Statistics;
import com.google.gson.JsonElement;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.json.JSONOptions;
import net.kyori.adventure.text.serializer.json.legacyimpl.NBTLegacyHoverEventSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

public class AdventureSerializer {
Expand All @@ -35,10 +38,16 @@ public class AdventureSerializer {
public static GsonComponentSerializer getGsonSerializer() {
if (GSON == null) {
ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion();
GSON = new GsonComponentSerializerExtended(
version.isOlderThan(ServerVersion.V_1_16) || PacketEvents.getAPI().getSettings().shouldDownsampleColors(),
version.isOlderThanOrEquals(ServerVersion.V_1_12_2)
);
GSON = GsonComponentSerializer.builder().editOptions(builder -> {
boolean is_1_20_3_or_new = version.isNewerThanOrEquals(ServerVersion.V_1_20_3);
builder.value(JSONOptions.EMIT_RGB, version.isNewerThanOrEquals(ServerVersion.V_1_16) && !PacketEvents.getAPI().getSettings().shouldDownsampleColors());
builder.value(JSONOptions.EMIT_HOVER_EVENT_TYPE, JSONOptions.HoverEventValueMode.BOTH);
builder.value(JSONOptions.EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, is_1_20_3_or_new);
builder.value(JSONOptions.VALIDATE_STRICT_EVENTS, is_1_20_3_or_new);
})
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.showAchievementToComponent(input -> Statistics.getById(input).display())
.build();
}
return GSON;
}
Expand Down
Loading
Loading