feat: remove NMS dependency

This commit is contained in:
ineanto 2023-04-03 10:56:43 +02:00
parent d37a0bd3ba
commit 35e2db02e9
35 changed files with 127 additions and 2139 deletions

View file

@ -18,6 +18,10 @@
</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>
@ -37,6 +41,12 @@
</repositories>
<dependencies>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- PlaceHolder API -->
<dependency>
<groupId>me.clip</groupId>
@ -92,12 +102,12 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.2</version>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.14.2</version>
<version>2.13.3</version>
</dependency>
<!-- Redis -->
<dependency>

View file

@ -1,19 +1,16 @@
package net.artelnatif.nicko;
import net.artelnatif.nicko.gui.items.common.OptionUnavailable;
import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SimpleItem;
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;
import net.artelnatif.nicko.event.PlayerJoinListener;
import net.artelnatif.nicko.event.PlayerQuitListener;
import net.artelnatif.nicko.gui.items.common.OptionUnavailable;
import net.artelnatif.nicko.gui.items.main.ExitGUI;
import net.artelnatif.nicko.i18n.Locale;
import net.artelnatif.nicko.i18n.LocaleFileManager;
import net.artelnatif.nicko.impl.Internals;
import net.artelnatif.nicko.impl.InternalsProvider;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.placeholder.PlaceHolderHook;
import net.artelnatif.nicko.storage.PlayerDataStore;
@ -24,6 +21,9 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SimpleItem;
import java.io.File;
import java.io.IOException;
@ -39,6 +39,7 @@ public class NickoBukkit extends JavaPlugin {
private Configuration configuration;
private LocaleFileManager localeFileManager;
private PlayerNameStore nameStore;
private ProtocolManager protocolManager;
public NickoBukkit() { this.unitTesting = false; }
@ -65,6 +66,7 @@ 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();
@ -78,14 +80,6 @@ public class NickoBukkit extends JavaPlugin {
}
if (!unitTesting) {
getLogger().info("Loading internals...");
if (getInternals() == null) {
getLogger().severe("Nicko could not find a valid implementation for this server version. Is your server supported?");
dataStore.getStorage().setError(true);
getServer().getPluginManager().disablePlugin(this);
}
localeFileManager = new LocaleFileManager();
if (configuration.isCustomLocale()) {
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
@ -161,7 +155,5 @@ public class NickoBukkit extends JavaPlugin {
return localeFileManager;
}
public Internals getInternals() {
return InternalsProvider.getInternals();
}
public ProtocolManager getProtocolManager() { return protocolManager; }
}

View file

@ -1,14 +1,27 @@
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 net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
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 org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
public class AppearanceManager {
private final NickoProfile profile;
@ -80,10 +93,93 @@ public class AppearanceManager {
}
public ActionResult<Void> resetPlayer() {
return NickoBukkit.getInstance().getInternals().updateProfile(player, profile, true, true);
// TODO: 4/3/23 Reset player
return new ActionResult<>();
}
public ActionResult<Void> updatePlayer(boolean skinChange) {
return NickoBukkit.getInstance().getInternals().updateProfile(player, profile, skinChange, false);
final String displayName = profile.getName() == null ? player.getName() : profile.getName();
final WrappedGameProfile gameProfile = new WrappedGameProfile(player.getUniqueId(), displayName);
final ActionResult<Void> result = updateGameProfileSkin(gameProfile, skinChange);
if (!result.isError()) {
updateTabList(gameProfile, displayName);
}
return new ActionResult<>();
}
private ActionResult<Void> updateGameProfileSkin(WrappedGameProfile gameProfile, boolean skinChange) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
if (skinChange || changeOnlyName) {
Optional<MojangSkin> skin;
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
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()));
Bukkit.broadcastMessage("Modified properties");
}
}
Bukkit.broadcastMessage("Respawning player");
respawnPlayer();
return new ActionResult<>();
} catch (ExecutionException e) {
return new ActionResult<>(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult<>(I18NDict.Error.NAME_FAIL_MOJANG);
}
}
return new ActionResult<>();
}
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);
}
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
));
infoAdd.getPlayerInfoDataLists().write(1, List.of(new PlayerInfoData(
gameProfile,
0,
EnumWrappers.NativeGameMode.fromBukkit(player.getGameMode()),
WrappedChatComponent.fromText(displayName)
)));
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

@ -34,7 +34,7 @@ public class PlayerJoinListener implements Listener {
if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.PreviousSkin.SUCCESS));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Event.PreviousSkin.FAIL, I18N.translate(player, actionResult.getErrorMessage())));
player.sendMessage(I18N.translate(player, I18NDict.Event.PreviousSkin.FAIL, I18N.translateWithoutPrefix(player, actionResult.getErrorMessage())));
}
}
}, 20L);

View file

@ -1,38 +0,0 @@
package net.artelnatif.nicko.impl;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public interface Internals {
void updateSelf(Player player);
void updateOthers(Player player);
ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset);
default ActionResult<MojangSkin> fetchSkinTextures(NickoProfile profile, boolean reset) {
Optional<MojangSkin> skin;
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
return skin.map(ActionResult::new).orElseGet(() -> new ActionResult<>(I18NDict.Error.SKIN_FAIL_MOJANG));
}
return new ActionResult<>(I18NDict.Error.NAME_FAIL_MOJANG);
} catch (ExecutionException e) {
return new ActionResult<>(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult<>(I18NDict.Error.NAME_FAIL_MOJANG);
}
}
}

View file

@ -1,29 +0,0 @@
package net.artelnatif.nicko.impl;
import org.bukkit.Bukkit;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
public class InternalsProvider {
private static final Logger logger = Logger.getLogger("Internals");
private static Internals internals;
static {
try {
final String packageName = Internals.class.getPackage().getName();
final String bukkitVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
final String fullClassName = packageName + "." + bukkitVersion;
final Class<?> clazz = Class.forName(fullClassName);
internals = (Internals) clazz.getConstructors()[0].newInstance();
logger.info("Loaded support for " + bukkitVersion);
} catch (InvocationTargetException | ClassNotFoundException | InstantiationException | IllegalAccessException |
ClassCastException exception) {
internals = null;
}
}
public static Internals getInternals() {
return internals;
}
}

View file

@ -25,7 +25,7 @@ public class MojangAPI {
private final Logger logger = Logger.getLogger("MojangAPI");
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<String, Optional<MojangSkin>>() {
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<>() {
@Nonnull
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
return getSkinFromMojang(uuid);

View file

@ -1,9 +1,10 @@
name: Nicko
main: net.artelnatif.nicko.NickoBukkit
version: 1.0-SNAPSHOT
author: Aro
author: Ineanto
api-version: 1.13
softdepend: [ PlaceholderAPI ]
depend: [ ProtocolLib ]
commands:
nicko:
description: "Opens Nicko's GUI."