feat: protocollib, java 8

This commit is contained in:
ineanto 2023-04-25 18:34:49 +02:00
parent fd732fc886
commit 7f3ecddc51
16 changed files with 387 additions and 79 deletions

View file

@ -1,5 +1,7 @@
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;
@ -37,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; }
@ -76,6 +79,7 @@ public class NickoBukkit extends JavaPlugin {
}
if (!unitTesting) {
protocolManager = ProtocolLibrary.getProtocolManager();
localeFileManager = new LocaleFileManager();
if (configuration.isCustomLocale()) {
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
@ -150,4 +154,6 @@ public class NickoBukkit extends JavaPlugin {
public LocaleFileManager getLocaleFileManager() {
return localeFileManager;
}
public ProtocolManager getProtocolManager() { return protocolManager; }
}

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.anvil;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.appearance.AppearanceManager;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.command.sub;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.appearance.AppearanceManager;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangUtils;

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.command.sub;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.appearance.AppearanceManager;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.mojang.MojangUtils;
import org.bukkit.Bukkit;
import org.bukkit.Sound;

View file

@ -1,29 +1,20 @@
package net.artelnatif.nicko.appearance;
package net.artelnatif.nicko.disguise;
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 com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
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 net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
@ -105,16 +96,17 @@ public class AppearanceManager {
public ActionResult<Void> updatePlayer(boolean skinChange) {
final String displayName = profile.getName() == null ? player.getName() : profile.getName();
Bukkit.broadcastMessage("Building UserProfile");
final UserProfile userProfile = new UserProfile(player.getUniqueId(), displayName);
final WrappedGameProfile gameProfile = new WrappedGameProfile(player.getUniqueId(), displayName);
final ActionResult<Void> result = updateGameProfileSkin(userProfile, skinChange);
final ActionResult<Void> result = updateGameProfileSkin(gameProfile, skinChange);
if (!result.isError()) {
updateTabList(userProfile, displayName);
updateTabList(gameProfile, displayName);
respawnPlayer();
}
return new ActionResult<>();
}
private ActionResult<Void> updateGameProfileSkin(UserProfile userProfile, boolean skinChange) {
private ActionResult<Void> updateGameProfileSkin(WrappedGameProfile gameProfile, boolean skinChange) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
if (skinChange || changeOnlyName) {
@ -126,14 +118,12 @@ public class AppearanceManager {
skin = mojang.getSkin(uuid.get());
if (skin.isPresent()) {
final MojangSkin skinResult = skin.get();
final List<TextureProperty> properties = userProfile.getTextureProperties();
final Collection<WrappedSignedProperty> properties = gameProfile.getProperties().values();
properties.clear();
properties.add(new TextureProperty("textures", skinResult.getValue(), skinResult.getSignature()));
properties.add(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);
@ -145,43 +135,16 @@ public class AppearanceManager {
}
private void respawnPlayer() {
Bukkit.broadcastMessage("Respawning player");
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);
// TODO (Ineanto, 4/23/23): Respawn Packet
player.teleport(player.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
}
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
));
final WrapperPlayServerPlayerInfoRemove infoRemove = new WrapperPlayServerPlayerInfoRemove(player.getUniqueId());
private void updateTabList(WrappedGameProfile gameProfile, String displayName) {
// TODO (Ineanto, 4/23/23): Update player info packet
// TODO (Ineanto, 4/23/23): Remove player info packet
Bukkit.broadcastMessage("Updating tablist");
PacketEvents.getAPI().getPlayerManager().sendPacket(player, infoRemove);
PacketEvents.getAPI().getPlayerManager().sendPacket(player, infoAdd);
// TODO (Ineanto, 4/23/23): Send packets
}
}

View file

@ -1,7 +1,7 @@
package net.artelnatif.nicko.event;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.appearance.AppearanceManager;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18N;

View file

@ -1,6 +1,6 @@
package net.artelnatif.nicko.gui.items.main;
import net.artelnatif.nicko.appearance.AppearanceManager;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import org.bukkit.Material;

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<>() {
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<String, Optional<MojangSkin>>() {
@Nonnull
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
return getSkinFromMojang(uuid);

View file

@ -0,0 +1,88 @@
/**
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
* Copyright (C) dmulloy2 <http://dmulloy2.net>
* Copyright (C) Kristian S. Strangeland
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.artelnatif.nicko.wrapper;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.google.common.base.Objects;
import org.bukkit.entity.Player;
public abstract class AbstractPacket {
protected PacketContainer handle;
/**
* Constructs a new strongly typed wrapper for the given packet.
*
* @param handle - handle to the raw packet data.
* @param type - the packet type.
*/
protected AbstractPacket(PacketContainer handle, PacketType type) {
// Make sure we're given a valid packet
if (handle == null)
throw new IllegalArgumentException("Packet handle cannot be NULL.");
if (!Objects.equal(handle.getType(), type))
throw new IllegalArgumentException(handle.getHandle()
+ " is not a packet of type " + type);
this.handle = handle;
}
/**
* Retrieve a handle to the raw packet data.
*
* @return Raw packet data.
*/
public PacketContainer getHandle() {
return handle;
}
/**
* Send the current packet to the given receiver.
*
* @param receiver - the receiver.
* @throws RuntimeException If the packet cannot be sent.
*/
public void sendPacket(Player receiver) {
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver,
getHandle());
}
/**
* Send the current packet to all online players.
*/
public void broadcastPacket() {
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle());
}
/**
* Simulate receiving the current packet from the given sender.
*
* @param sender - the sender.
* @throws RuntimeException if the packet cannot be received.
*/
public void receivePacket(Player sender) {
try {
ProtocolLibrary.getProtocolManager().receiveClientPacket(sender,
getHandle());
} catch (Exception e) {
throw new RuntimeException("Cannot receive packet.", e);
}
}
}

View file

@ -0,0 +1,37 @@
package net.artelnatif.nicko.wrapper;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import org.bukkit.World;
import java.util.Optional;
/**
* 1.19.4 compliant version of the WrapperPlayServerRespawn.
*
* @author ineanto, based on work from dmulloy2 and Kristian S. Strangeland
*/
public class WrapperPlayServerRespawn extends AbstractPacket {
public static final PacketType TYPE = PacketType.Play.Server.RESPAWN;
public WrapperPlayServerRespawn() {
super(new PacketContainer(TYPE), TYPE);
handle.getModifier().writeDefaults();
}
public Optional<World> getDimension() {
return handle.getDimensionTypes().optionRead(0);
}
public void setDimension(World value) {
handle.getDimensionTypes().write(0, value);
}
public Optional<Long> getSeed() {
return handle.getLongs().optionRead(0);
}
public void setSeed(long value) {
handle.getLongs().write(0, value);
}
}

View file

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