From 37a267eae5fb07715de558c85b5d53a89d096b63 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 22 Jul 2022 02:16:45 +0200 Subject: [PATCH] new module pandalib-paper-players --- pandalib-bungee-players/pom.xml | 5 + .../bungee/players/BungeeOnlinePlayer.java | 31 +++ pandalib-paper-players/pom.xml | 44 ++++ .../lib/paper/players/PaperOffPlayer.java | 90 +++++++ .../lib/paper/players/PaperOnlinePlayer.java | 228 ++++++++++++++++++ pom.xml | 9 +- 6 files changed, 403 insertions(+), 4 deletions(-) create mode 100644 pandalib-paper-players/pom.xml create mode 100644 pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOffPlayer.java create mode 100644 pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOnlinePlayer.java diff --git a/pandalib-bungee-players/pom.xml b/pandalib-bungee-players/pom.xml index 42fbd04..2404434 100644 --- a/pandalib-bungee-players/pom.xml +++ b/pandalib-bungee-players/pom.xml @@ -19,6 +19,11 @@ + + fr.pandacube.lib + pandalib-util + ${project.version} + fr.pandacube.lib pandalib-players-standalone diff --git a/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java b/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java index 7f33036..c5d8036 100644 --- a/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java +++ b/pandalib-bungee-players/src/main/java/fr/pandacube/lib/bungee/players/BungeeOnlinePlayer.java @@ -13,6 +13,7 @@ import net.md_5.bungee.api.SkinConfiguration; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer.ChatMode; import net.md_5.bungee.api.connection.ProxiedPlayer.MainHand; +import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.packet.ClientSettings; import net.md_5.bungee.protocol.packet.PluginMessage; @@ -20,9 +21,39 @@ import net.md_5.bungee.protocol.packet.PluginMessage; import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.players.standalone.StandaloneOnlinePlayer; import fr.pandacube.lib.reflect.Reflect; +import fr.pandacube.lib.util.MinecraftVersion; public interface BungeeOnlinePlayer extends BungeeOffPlayer, StandaloneOnlinePlayer { + /* + * General data and state + */ + + + @Override + default String getName() { + return getBungeeProxiedPlayer().getName(); + } + + @Override + default String getServerName() { + if (getServer() == null) return null; + return getServer().getInfo().getName(); + } + + default Server getServer() { + return getBungeeProxiedPlayer().getServer(); + } + + default MinecraftVersion getMinecraftVersion() { + return MinecraftVersion.getVersion(getBungeeProxiedPlayer().getPendingConnection().getVersion()); + } + + + + /* + * Related class instances + */ @Override ProxiedPlayer getBungeeProxiedPlayer(); diff --git a/pandalib-paper-players/pom.xml b/pandalib-paper-players/pom.xml new file mode 100644 index 0000000..4a3884d --- /dev/null +++ b/pandalib-paper-players/pom.xml @@ -0,0 +1,44 @@ + + + + pandalib-parent + fr.pandacube.lib + 1.0-SNAPSHOT + + 4.0.0 + + pandalib-paper-players + + + + papermc + https://papermc.io/repo/repository/maven-public/ + + + + + + fr.pandacube.lib + pandalib-players-standalone + ${project.version} + + + + + + io.papermc.paper + paper-api + ${paper.version}-SNAPSHOT + provided + + + io.papermc.paper + paper-mojangapi + ${paper.version}-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOffPlayer.java b/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOffPlayer.java new file mode 100644 index 0000000..464a35d --- /dev/null +++ b/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOffPlayer.java @@ -0,0 +1,90 @@ +package fr.pandacub.lib.paper.players; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.scoreboard.Team; + +import fr.pandacube.lib.players.standalone.StandaloneOffPlayer; + +public interface PaperOffPlayer extends StandaloneOffPlayer { + + /* + * General data and state + */ + + @Override + default boolean isOnline() { + return (getBukkitPlayer() != null); + } + + + + + /* + * Related class instances + */ + + @Override + PaperOnlinePlayer getOnlineInstance(); + + /** + * @return l'instance Bukkit du joueur en ligne, ou null si il n'est pas en + * ligne + */ + default Player getBukkitPlayer() { + return Bukkit.getPlayer(getUniqueId()); + } + + /** + * Get the Bukkit’s {@link OfflinePlayer} instance for this player. + * May represent a player that never joined the server before. + * @return an instance of {@link OfflinePlayer} for this player. + */ + default OfflinePlayer getBukkitOfflinePlayer() { + return Bukkit.getOfflinePlayer(getUniqueId()); + } + + + + + + /* + * Display name + */ + /** + * Get the display name of the user, in legacy format. + * @return the display name of the player. + * + * @implNote This default implementation gets the display name from bukkit (if the player is online). + * If its different to the player name, it returns it. Otherwise, it tries to generate the team displayname with {@link #getTeamDisplayName()}. + * If the player is not in a team, then the player name is used. + */ + @Override + default String getDisplayName() { + String name = getName(); + Player p = getBukkitPlayer(); + String bukkitDispName = p != null ? p.getDisplayName() : name; + if (!name.equals(bukkitDispName)) + return bukkitDispName; + String teamDispName = getTeamDisplayName(); + return teamDispName == null ? name : teamDispName; + } + + /** + * Computes and returns the the name of the player with the prefix, suffix and color of the team the player is in. + * @return The legacy formated player display name, if he is in a {@link Team}, or null otherwise. + */ + default String getTeamDisplayName() { + String name = getName(); + Team team = Bukkit.getScoreboardManager().getMainScoreboard().getEntryTeam(name); + if (team == null) + return null; + String teamPrefix = team.getPrefix(); + String teamSuffix = team.getSuffix(); + String teamColor = team.getColor().toString(); + + return teamPrefix + teamColor + name + teamSuffix; + } + +} diff --git a/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOnlinePlayer.java b/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOnlinePlayer.java new file mode 100644 index 0000000..bf3392e --- /dev/null +++ b/pandalib-paper-players/src/main/java/fr/pandacub/lib/paper/players/PaperOnlinePlayer.java @@ -0,0 +1,228 @@ +package fr.pandacub.lib.paper.players; + +import java.util.Locale; +import java.util.UUID; + +import com.destroystokyo.paper.ClientOption; +import com.destroystokyo.paper.ClientOption.ChatVisibility; +import com.destroystokyo.paper.SkinParts; +import net.kyori.adventure.audience.MessageType; +import net.kyori.adventure.identity.Identified; +import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.title.Title; +import net.kyori.adventure.title.Title.Times; +import net.kyori.adventure.util.Ticks; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.Sound; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.MainHand; + +import fr.pandacube.lib.players.standalone.StandaloneOnlinePlayer; + +public interface PaperOnlinePlayer extends PaperOffPlayer, StandaloneOnlinePlayer { + + /* + * General data and state + */ + + @Override + default String getName() { + return getBukkitPlayer().getName(); + } + + @Override + default String getWorldName() { + return getBukkitPlayer().getWorld().getName(); + } + + + + + /* + * Related class instances + */ + + /** + * @return l'instance Bukkit du joueur en ligne, ou null si il n'est pas en + * ligne + */ + Player getBukkitPlayer(); + + @Override + default OfflinePlayer getBukkitOfflinePlayer() { + return getBukkitPlayer(); + } + + + + + + /* + * Sending packet and stuff to player + */ + + @Override + default void sendMessage(Component message) { + getBukkitPlayer().sendMessage(message); + } + + @Override + default void sendMessage(ComponentLike message, UUID sender) { + getBukkitPlayer().sendMessage(sender == null ? Identity.nil() : Identity.identity(sender), message, MessageType.CHAT); + } + + @Override + default void sendMessage(Component message, Identified sender) { + getBukkitPlayer().sendMessage(sender == null ? Identity.nil() : sender.identity(), message, MessageType.CHAT); + } + + @Override + default void sendTitle(Component title, Component subtitle, int fadeIn, int stay, int fadeOut) { + getBukkitPlayer().showTitle(Title.title(title, subtitle, Times.times(Ticks.duration(fadeIn), Ticks.duration(stay), Ticks.duration(fadeOut)))); + } + + default void playSound(Sound sound, float volume, float pitch) { + playSound(sound, getBukkitPlayer().getLocation(), volume, pitch); + } + + default void playSound(Sound sound, Location location, float volume, float pitch) { + getBukkitPlayer().playSound(location, sound, volume, pitch); + } + + + + + + /* + * Client options + */ + + @Override + PaperClientOptions getClientOptions(); + + public abstract class PaperClientOptions implements StandaloneOnlinePlayer.ClientOptions { + + private final PaperOnlinePlayer op; + + public PaperClientOptions(PaperOnlinePlayer op) { + this.op = op; + } + + @Override + public boolean hasChatColorEnabled() { + return op.getBukkitPlayer().getClientOption(ClientOption.CHAT_COLORS_ENABLED); + } + + public ChatVisibility getChatVisibility() { + return op.getBukkitPlayer().getClientOption(ClientOption.CHAT_VISIBILITY); + } + + @Override + public boolean isChatFullyVisible() { + ChatVisibility v = getChatVisibility(); + return v == ChatVisibility.FULL || v == ChatVisibility.UNKNOWN; + } + + @Override + public boolean isChatOnlyDisplayingSystemMessages() { + return getChatVisibility() == ChatVisibility.SYSTEM; + } + + @Override + public boolean isChatHidden() { + return getChatVisibility() == ChatVisibility.HIDDEN; + } + + @Override + public Locale getLocale() { + return op.getBukkitPlayer().locale(); + } + + @Override + public int getViewDistance() { + return op.getBukkitPlayer().getClientViewDistance(); + } + + public MainHand getMainHand() { + return op.getBukkitPlayer().getMainHand(); + } + + @Override + public boolean isLeftHanded() { + return getMainHand() == MainHand.LEFT; + } + + @Override + public boolean isRightHanded() { + return getMainHand() == MainHand.RIGHT; + } + + @Override + public abstract boolean isTextFilteringEnabled(); // needs reflection + + @Override + public boolean allowsServerListing() { + return op.getBukkitPlayer().isAllowingServerListings(); + } + + public SkinParts getSkinParts() { + return op.getBukkitPlayer().getClientOption(ClientOption.SKIN_PARTS); + } + + @Override + public boolean hasSkinCapeEnabled() { + return getSkinParts().hasCapeEnabled(); + } + + @Override + public boolean hasSkinJacketEnabled() { + return getSkinParts().hasJacketEnabled(); + } + + @Override + public boolean hasSkinLeftSleeveEnabled() { + return getSkinParts().hasLeftSleeveEnabled(); + } + + @Override + public boolean hasSkinRightSleeveEnabled() { + return getSkinParts().hasRightSleeveEnabled(); + } + + @Override + public boolean hasSkinLeftPantsEnabled() { + return getSkinParts().hasLeftPantsEnabled(); + } + + @Override + public boolean hasSkinRightPantsEnabled() { + return getSkinParts().hasRightPantsEnabled(); + } + + @Override + public boolean hasSkinHatsEnabled() { + return getSkinParts().hasHatsEnabled(); + } + } + + + + + /* + * Custom damage + */ + + default void damage(double amount) { + getBukkitPlayer().damage(amount); // uses DamageSource.GENERIC + } + + default void damage(double amount, LivingEntity source) { + getBukkitPlayer().damage(amount, source); // uses appropriate DamageSource according to provided player or entity + } + + +} diff --git a/pom.xml b/pom.xml index 6f59487..670d401 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,8 @@ pandalib-bungee + pandalib-bungee-permissions + pandalib-bungee-players pandalib-chat pandalib-cli pandalib-core @@ -53,15 +55,14 @@ pandalib-net pandalib-netapi pandalib-paper + pandalib-paper-permissions + pandalib-paper-players pandalib-paper-reflect pandalib-permissions + pandalib-players-permissible pandalib-players-standalone pandalib-reflect pandalib-util - pandalib-players-permissible - pandalib-bungee-permissions - pandalib-bungee-players - pandalib-paper-permissions