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

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."