Skip to content

Commit

Permalink
feat: update to 1.20.2 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
0utplay authored Oct 18, 2023
1 parent 46a1d5f commit 17ef844
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 43 deletions.
6 changes: 3 additions & 3 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

dependencies {
implementation(libs.gson)
compileOnly(libs.gson)
implementation(libs.geantyref)
}

tasks.withType<ShadowJar> {
minimize()
relocate("com.google.gson", "com.github.juliarn.npclib.relocate.gson")
relocate("io.leangen.geantyref", "com.github.juliarn.npclib.relocate.geantyref")
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import com.github.juliarn.npclib.api.util.Util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import io.leangen.geantyref.TypeFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
Expand All @@ -55,11 +55,8 @@ final class MojangProfileResolver implements ProfileResolver {

private static final int DEFAULT_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(10);

private static final Type PROFILE_PROPERTIES_TYPE = TypeToken
.getParameterized(Set.class, ProfileProperty.class)
.getType();
private static final Type PROFILE_PROPERTIES_TYPE = TypeFactory.parameterizedClass(Set.class, ProfileProperty.class);
private static final Gson GSON = new GsonBuilder()
.disableJdkUnsafe()
.disableHtmlEscaping()
.registerTypeAdapter(ProfileProperty.class, new ProfilePropertyTypeAdapter())
.create();
Expand Down Expand Up @@ -99,7 +96,7 @@ final class MojangProfileResolver implements ProfileResolver {
if (status == HttpURLConnection.HTTP_OK) {
// parse the incoming data
try (Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) {
return JsonParser.parseReader(reader).getAsJsonObject();
return GSON.fromJson(reader, JsonElement.class).getAsJsonObject();
}
} else {
// rate limit, invalid name/uuid etc.
Expand Down
3 changes: 2 additions & 1 deletion bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation(projects.npcLibCommon)

implementation(libs.paperLib)
implementation(libs.geantyref)
implementation(libs.packetEvents)

compileOnly(libs.netty)
Expand All @@ -39,8 +40,8 @@ dependencies {
tasks.withType<ShadowJar> {
dependsOn(":npc-lib-common:shadowJar")

relocate("com.google.gson", "com.github.juliarn.npclib.relocate.gson")
relocate("io.papermc.lib", "com.github.juliarn.npclib.relocate.paperlib")
relocate("io.leangen.geantyref", "com.github.juliarn.npclib.relocate.geantyref")
relocate("io.github.retrooper", "com.github.juliarn.npclib.relocate.io.packetevents")
relocate("com.github.retrooper", "com.github.juliarn.npclib.relocate.com.packetevents")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.player.Equipment;
import com.github.retrooper.packetevents.protocol.player.EquipmentSlot;
Expand All @@ -75,11 +76,12 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPluginMessage;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnPlayer;
import com.google.common.collect.ImmutableMap;
import com.google.gson.reflect.TypeToken;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import io.leangen.geantyref.TypeFactory;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -109,10 +111,10 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl
.reEncodeByDefault(false)
.timeStampMode(TimeStampMode.NONE);

private static final Type OPTIONAL_CHAT_COMPONENT_TYPE = TypeToken.getParameterized(
private static final Type OPTIONAL_CHAT_COMPONENT_TYPE = TypeFactory.parameterizedClass(
Optional.class,
net.kyori.adventure.text.Component.class
).getType();
net.kyori.adventure.text.Component.class);


// lazy initialized, then never null again
private ServerVersion serverVersion;
Expand Down Expand Up @@ -151,7 +153,22 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl
return (player, npc) -> {
// SpawnPlayer (https://wiki.vg/Protocol#Spawn_Player)
Location location = npcLocation(npc);
PacketWrapper<?> wrapper = new WrapperPlayServerSpawnPlayer(npc.entityId(), npc.profile().uniqueId(), location);

PacketWrapper<?> wrapper;
if (this.serverVersion.isNewerThanOrEquals(ServerVersion.V_1_20_2)) {
wrapper = new WrapperPlayServerSpawnEntity(
npc.entityId(),
Optional.of(npc.profile().uniqueId()),
EntityTypes.PLAYER,
location.getPosition(),
location.getPitch(),
location.getYaw(),
0,
0,
Optional.empty());
} else {
wrapper = new WrapperPlayServerSpawnPlayer(npc.entityId(), npc.profile().uniqueId(), location);
}

// send the packet without notifying any listeners
this.packetPlayerManager.sendPacketSilently(player, wrapper);
Expand Down Expand Up @@ -283,6 +300,7 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl
) {
return (player, npc) -> {
// CustomPayload (https://wiki.vg/Protocol#Custom_Payload)

PacketWrapper<?> wrapper = new WrapperPlayServerPluginMessage(channelId, payload);
this.packetPlayerManager.sendPacketSilently(player, wrapper);
};
Expand Down Expand Up @@ -503,7 +521,7 @@ private static final class Lazy {
com.github.retrooper.packetevents.protocol.entity.pose.EntityPose.class,
ENTITY_POSE_CONVERTER.get(value)))
.put(
TypeToken.getParameterized(Optional.class, Component.class).getType(),
TypeFactory.parameterizedClass(Optional.class, Component.class),
(versionAccessor, value) -> {
//noinspection unchecked
Optional<Component> optionalComponent = (Optional<Component>) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.CustomPacketPayloadWrapper;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.MinecraftKey;
import com.comphenix.protocol.wrappers.Pair;
Expand Down Expand Up @@ -61,7 +62,7 @@
import com.github.juliarn.npclib.common.event.DefaultInteractNpcEvent;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import io.leangen.geantyref.TypeFactory;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.lang.reflect.ParameterizedType;
Expand All @@ -79,6 +80,7 @@
import java.util.function.BiFunction;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
Expand All @@ -89,10 +91,9 @@ final class ProtocolLibPacketAdapter implements PlatformPacketAdapter<World, Pla

static final ProtocolLibPacketAdapter INSTANCE = new ProtocolLibPacketAdapter();

private static final Type OPTIONAL_COMPONENT_TYPE = TypeToken.getParameterized(
private static final Type OPTIONAL_COMPONENT_TYPE = TypeFactory.parameterizedClass(
Optional.class,
MinecraftReflection.getIChatBaseComponentClass()
).getType();
MinecraftReflection.getIChatBaseComponentClass());

private static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary.getProtocolManager();
private static final MinecraftVersion SERVER_VERSION = MinecraftVersion.fromServerVersion(Bukkit.getVersion());
Expand Down Expand Up @@ -173,7 +174,7 @@ final class ProtocolLibPacketAdapter implements PlatformPacketAdapter<World, Pla
EnumWrappers.getEntityPoseClass(),
ENTITY_POSE_CONVERTER.get(value)))
.put(
TypeToken.getParameterized(Optional.class, Component.class).getType(),
TypeFactory.parameterizedClass(Optional.class, Component.class),
(versionAccess, value) -> {
//noinspection unchecked
Optional<Component> optionalComponent = (Optional<Component>) value;
Expand Down Expand Up @@ -237,13 +238,23 @@ final class ProtocolLibPacketAdapter implements PlatformPacketAdapter<World, Pla
@Override
public @NotNull OutboundPacket<World, Player, ItemStack, Plugin> createEntitySpawnPacket() {
return (player, npc) -> {
// SpawnPlayer (https://wiki.vg/Protocol#Spawn_Player)
PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
PacketContainer container;
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
// SpawnEntity (https://wiki.vg/Protocol#Spawn_Entity)
container = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
} else {
// SpawnPlayer (https://wiki.vg/Protocol#Spawn_Player)
container = new PacketContainer(PacketType.Play.Server.NAMED_ENTITY_SPAWN);
}

// base information
container.getIntegers().write(0, npc.entityId());
container.getUUIDs().write(0, npc.profile().uniqueId());

if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
container.getEntityTypeModifier().write(0, EntityType.PLAYER);
}

// position
if (MinecraftVersion.COMBAT_UPDATE.atOrAbove()) {
// mc 1.9: new position format (plain doubles)
Expand Down Expand Up @@ -461,22 +472,29 @@ final class ProtocolLibPacketAdapter implements PlatformPacketAdapter<World, Pla
// CustomPayload (https://wiki.vg/Protocol#Custom_Payload)
PacketContainer container = new PacketContainer(PacketType.Play.Server.CUSTOM_PAYLOAD);

// channel id
if (MinecraftVersion.AQUATIC_UPDATE.atOrAbove()) {
// mc 1.13: channel id is now in the format of a resource location
String[] parts = channelId.split(":", 2);
MinecraftKey key = parts.length == 1 ? new MinecraftKey(channelId) : new MinecraftKey(parts[0], parts[1]);

container.getMinecraftKeys().write(0, key);
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
// mc 1.20.2:
CustomPacketPayloadWrapper payloadWrapper = new CustomPacketPayloadWrapper(payload, key);
container.getCustomPacketPayloads().write(0, payloadWrapper);
} else {
container.getMinecraftKeys().write(0, key);
}
} else {
// mc 1.8: channel id is a string
container.getStrings().write(0, channelId);
}

// payload
ByteBuf buffer = Unpooled.copiedBuffer(payload);
Object wrappedSerializableBuffer = MinecraftReflection.getPacketDataSerializer(buffer);
container.getModifier().withType(ByteBuf.class).write(0, wrappedSerializableBuffer);
if (!MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) {
// payload
ByteBuf buffer = Unpooled.copiedBuffer(payload);
Object wrappedSerializableBuffer = MinecraftReflection.getPacketDataSerializer(buffer);
container.getModifier().withType(ByteBuf.class).write(0, wrappedSerializableBuffer);
}

// send the packet without notifying any bound packet listeners
PROTOCOL_MANAGER.sendServerPacket(player, container, false);
Expand Down
4 changes: 0 additions & 4 deletions ext/labymod/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,3 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
dependencies {
compileOnly(libs.gson)
}

tasks.withType<ShadowJar> {
relocate("com.google.gson", "com.github.juliarn.npclib.relocate.gson")
}
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ checkstyleTools = "10.12.2"

# general
gson = "2.10.1"
geantyref = "1.3.14"
netty = "4.1.96.Final"
annotations = "24.0.1"

Expand All @@ -19,8 +20,8 @@ paper = "1.20.1-R0.1-SNAPSHOT"

# platform extensions
paperLib = "1.0.8"
protocolLib = "02e917cd08"
packetEvents = "800ffafb96"
protocolLib = "03d7be13d0"
packetEvents = "b48a91bcee"


[libraries]
Expand All @@ -29,6 +30,7 @@ packetEvents = "800ffafb96"
netty = { group = "io.netty", name = "netty-buffer", version.ref = "netty" }
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
annotations = { group = "org.jetbrains", name = "annotations", version.ref = "annotations" }
geantyref = { group = "io.leangen.geantyref", name = "geantyref", version.ref = "geantyref" }

# platform api
paper = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper" }
Expand Down
7 changes: 5 additions & 2 deletions minestom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

dependencies {
api(projects.npcLibApi)
compileOnly(libs.minestom)
implementation(projects.npcLibCommon)

compileOnly(libs.minestom)
implementation(libs.geantyref)
}

tasks.withType<JavaCompile> {
Expand All @@ -36,5 +38,6 @@ tasks.withType<JavaCompile> {

tasks.withType<ShadowJar> {
dependsOn(":npc-lib-common:shadowJar")
relocate("com.google.gson", "com.github.juliarn.npclib.relocate.gson")

relocate("io.leangen.geantyref", "com.github.juliarn.npclib.relocate.geantyref")
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import com.github.juliarn.npclib.common.event.DefaultAttackNpcEvent;
import com.github.juliarn.npclib.common.event.DefaultInteractNpcEvent;
import com.github.juliarn.npclib.minestom.util.MinestomUtil;
import com.google.gson.reflect.TypeToken;
import io.leangen.geantyref.TypeFactory;
import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down Expand Up @@ -84,10 +84,9 @@ public final class MinestomProtocolAdapter implements PlatformPacketAdapter<Inst

private static final MinestomProtocolAdapter INSTANCE = new MinestomProtocolAdapter();

private static final Type OPTIONAL_CHAT_COMPONENT_TYPE = TypeToken.getParameterized(
private static final Type OPTIONAL_CHAT_COMPONENT_TYPE = TypeFactory.parameterizedClass(
Optional.class,
net.kyori.adventure.text.Component.class
).getType();
net.kyori.adventure.text.Component.class);

private static final EnumMap<ItemSlot, EquipmentSlot> ITEM_SLOT_CONVERTER;
private static final EnumMap<EntityPose, Entity.Pose> ENTITY_POSE_CONVERTER;
Expand Down Expand Up @@ -155,7 +154,7 @@ public final class MinestomProtocolAdapter implements PlatformPacketAdapter<Inst
Entity.Pose.class,
ENTITY_POSE_CONVERTER::get));
SERIALIZER_CONVERTERS.put(
TypeToken.getParameterized(Optional.class, Component.class).getType(),
TypeFactory.parameterizedClass(Optional.class, Component.class),
new AbstractMap.SimpleImmutableEntry<>(
OPTIONAL_CHAT_COMPONENT_TYPE,
value -> {
Expand Down

0 comments on commit 17ef844

Please sign in to comment.