feat: use packetevents

This commit is contained in:
ineanto 2023-04-03 12:15:47 +02:00
parent d17aadc09a
commit fd732fc886
5 changed files with 220 additions and 63 deletions

164
dependency-reduced-pom.xml Normal file
View file

@ -0,0 +1,164 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.artelnatif</groupId>
<artifactId>nicko</artifactId>
<name>Nicko</name>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>net.wesjd:anvilgui</include>
<include>xyz.xenondevs.invui:*</include>
<include>com.github.jsixface:*</include>
<include>com.fasterxml.jackson.dataformat</include>
<include>com.fasterxml.jackson.core</include>
<include>org.mariadb.jdbc</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>net.wesjd.anvilgui</pattern>
<shadedPattern>net.artelnatif.libs.anvilgui</shadedPattern>
</relocation>
<relocation>
<pattern>xyz.xenondevs.invui</pattern>
<shadedPattern>net.artelnatif.libs.invui</shadedPattern>
</relocation>
<relocation>
<pattern>com.github.jsixface</pattern>
<shadedPattern>net.artelnatif.libs.yaml</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml.jackson.dataformat</pattern>
<shadedPattern>net.artelnatif.libs.jackson.yaml</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml.jackson.core</pattern>
<shadedPattern>net.artelnatif.libs.jackson.core</shadedPattern>
</relocation>
<relocation>
<pattern>org.mariadb.jdbc</pattern>
<shadedPattern>net.artelnatif.libs.mariadb</shadedPattern>
</relocation>
</relocations>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.retrooper.packetevents</groupId>
<artifactId>spigot</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.seeseemelk</groupId>
<artifactId>MockBukkit-v1.19</artifactId>
<version>2.29.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>paper-api</artifactId>
<groupId>io.papermc.paper</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>hamcrest-library</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
<exclusion>
<artifactId>adventure-platform-bungeecord</artifactId>
<groupId>net.kyori</groupId>
</exclusion>
<exclusion>
<artifactId>maven-resolver-provider</artifactId>
<groupId>org.apache.maven</groupId>
</exclusion>
<exclusion>
<artifactId>maven-resolver-connector-basic</artifactId>
<groupId>org.apache.maven.resolver</groupId>
</exclusion>
<exclusion>
<artifactId>maven-resolver-transport-http</artifactId>
<groupId>org.apache.maven.resolver</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

10
pom.xml
View file

@ -16,10 +16,6 @@
</properties>
<repositories>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
@ -40,9 +36,9 @@
<dependencies>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.0.0-SNAPSHOT</version>
<groupId>com.github.retrooper.packetevents</groupId>
<artifactId>spigot</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- PlaceHolder API -->

View file

@ -1,7 +1,5 @@
package net.artelnatif.nicko;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import net.artelnatif.nicko.command.NickoCommand;
import net.artelnatif.nicko.config.Configuration;
import net.artelnatif.nicko.config.ConfigurationManager;
@ -39,7 +37,6 @@ public class NickoBukkit extends JavaPlugin {
private Configuration configuration;
private LocaleFileManager localeFileManager;
private PlayerNameStore nameStore;
private ProtocolManager protocolManager;
public NickoBukkit() { this.unitTesting = false; }
@ -66,7 +63,6 @@ public class NickoBukkit extends JavaPlugin {
configurationManager = new ConfigurationManager(getDataFolder());
configurationManager.saveDefaultConfig();
protocolManager = ProtocolLibrary.getProtocolManager();
mojangAPI = new MojangAPI();
dataStore = new PlayerDataStore(mojangAPI, getNickoConfig());
nameStore = new PlayerNameStore();
@ -154,6 +150,4 @@ public class NickoBukkit extends JavaPlugin {
public LocaleFileManager getLocaleFileManager() {
return localeFileManager;
}
public ProtocolManager getProtocolManager() { return protocolManager; }
}

View file

@ -1,9 +1,13 @@
package net.artelnatif.nicko.appearance;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.*;
import com.google.common.collect.Multimap;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.TextureProperty;
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.protocol.world.Difficulty;
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.WrapperPlayServerRespawn;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
@ -12,14 +16,15 @@ import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.artelnatif.nicko.storage.PlayerDataStore;
import net.artelnatif.nicko.storage.name.PlayerNameStore;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
@ -99,16 +104,17 @@ public class AppearanceManager {
public ActionResult<Void> updatePlayer(boolean skinChange) {
final String displayName = profile.getName() == null ? player.getName() : profile.getName();
final WrappedGameProfile gameProfile = new WrappedGameProfile(player.getUniqueId(), displayName);
Bukkit.broadcastMessage("Building UserProfile");
final UserProfile userProfile = new UserProfile(player.getUniqueId(), displayName);
final ActionResult<Void> result = updateGameProfileSkin(gameProfile, skinChange);
final ActionResult<Void> result = updateGameProfileSkin(userProfile, skinChange);
if (!result.isError()) {
updateTabList(gameProfile, displayName);
updateTabList(userProfile, displayName);
}
return new ActionResult<>();
}
private ActionResult<Void> updateGameProfileSkin(WrappedGameProfile gameProfile, boolean skinChange) {
private ActionResult<Void> updateGameProfileSkin(UserProfile userProfile, boolean skinChange) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
if (skinChange || changeOnlyName) {
@ -120,9 +126,9 @@ public class AppearanceManager {
skin = mojang.getSkin(uuid.get());
if (skin.isPresent()) {
final MojangSkin skinResult = skin.get();
final Multimap<String, WrappedSignedProperty> properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new WrappedSignedProperty("textures", skinResult.getValue(), skinResult.getSignature()));
final List<TextureProperty> properties = userProfile.getTextureProperties();
properties.clear();
properties.add(new TextureProperty("textures", skinResult.getValue(), skinResult.getSignature()));
Bukkit.broadcastMessage("Modified properties");
}
}
@ -139,47 +145,43 @@ public class AppearanceManager {
}
private void respawnPlayer() {
final PacketContainer respawnOtherWorld = getRespawnPacket(Bukkit.getWorld("world_the_end"));
final PacketContainer respawn = getRespawnPacket(player.getWorld());
instance.getProtocolManager().sendServerPacket(player, respawnOtherWorld);
instance.getProtocolManager().sendServerPacket(player, respawn);
final World world = player.getWorld();
final WrapperPlayServerRespawn respawn = new WrapperPlayServerRespawn(
SpigotConversionUtil.fromBukkitWorld(world),
world.getName(),
Difficulty.getById(world.getDifficulty().ordinal()),
world.getSeed(),
SpigotConversionUtil.fromBukkitGameMode(player.getGameMode()),
null,
false,
false,
false,
null,
null
);
PacketEvents.getAPI().getPlayerManager().sendPacket(player, respawn);
}
private PacketContainer getRespawnPacket(World world) {
final PacketContainer packet = new PacketContainer(PacketType.Play.Server.RESPAWN);
final EnumWrappers.NativeGameMode gamemode = EnumWrappers.NativeGameMode.fromBukkit(player.getGameMode());
packet.getWorldKeys().write(0, world);
packet.getLongs().write(0, world.getSeed());
packet.getGameModes().write(0, gamemode); // gamemode
packet.getGameModes().write(1, gamemode); // previous gamemode
packet.getBooleans().write(0, false);
packet.getBooleans().write(1, false);
return packet;
}
private void updateTabList(WrappedGameProfile gameProfile, String displayName) {
final PacketContainer infoAdd = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
infoAdd.getPlayerInfoActions().write(0, Set.of(
EnumWrappers.PlayerInfoAction.ADD_PLAYER,
EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE,
EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME,
EnumWrappers.PlayerInfoAction.UPDATE_LISTED,
EnumWrappers.PlayerInfoAction.UPDATE_LATENCY
private void updateTabList(UserProfile userProfile, String displayName) {
final WrapperPlayServerPlayerInfoUpdate infoAdd = new WrapperPlayServerPlayerInfoUpdate(EnumSet.of(
WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_GAME_MODE,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_DISPLAY_NAME,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LISTED,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LATENCY
), new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
userProfile,
true,
0,
SpigotConversionUtil.fromBukkitGameMode(player.getGameMode()),
Component.text(displayName),
null
));
infoAdd.getPlayerInfoDataLists().write(1, List.of(new PlayerInfoData(
gameProfile,
0,
EnumWrappers.NativeGameMode.fromBukkit(player.getGameMode()),
WrappedChatComponent.fromText(displayName)
)));
final WrapperPlayServerPlayerInfoRemove infoRemove = new WrapperPlayServerPlayerInfoRemove(player.getUniqueId());
Bukkit.broadcastMessage("Updating tablist");
PacketEvents.getAPI().getPlayerManager().sendPacket(player, infoRemove);
PacketEvents.getAPI().getPlayerManager().sendPacket(player, infoAdd);
final PacketContainer infoRemove = new PacketContainer(PacketType.Play.Server.PLAYER_INFO_REMOVE);
infoRemove.getUUIDLists().write(0, List.of(player.getUniqueId()));
instance.getProtocolManager().broadcastServerPacket(infoRemove);
instance.getProtocolManager().broadcastServerPacket(infoAdd);
}
}

View file

@ -4,7 +4,8 @@ version: 1.0-SNAPSHOT
author: Ineanto
api-version: 1.13
softdepend: [ PlaceholderAPI ]
depend: [ ProtocolLib ]
load: POSTWORLD
depend: [ packetevents ]
commands:
nicko:
description: "Opens Nicko's GUI."