2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2021-11-27 09:11:43 +01:00
From: MiniDigger <admin@benndorf.dev>
2021-06-11 14:02:28 +02:00
Date: Mon, 20 Jan 2020 21:38:34 +0100
Subject: [PATCH] Add Player Client Options API
diff --git a/src/main/java/com/destroystokyo/paper/ClientOption.java b/src/main/java/com/destroystokyo/paper/ClientOption.java
new file mode 100644
2024-11-26 18:46:12 +01:00
index 0000000000000000000000000000000000000000..7af28d6ba27c97a87ffbb9db03a5c340277853cc
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/ClientOption.java
2024-11-26 18:46:12 +01:00
@@ -0,0 +1,70 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper;
+
2021-08-14 06:11:12 +02:00
+import net.kyori.adventure.translation.Translatable;
+import net.kyori.adventure.util.Index;
2021-06-11 14:02:28 +02:00
+import org.bukkit.inventory.MainHand;
2024-09-30 01:48:34 +02:00
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
2024-09-30 01:48:34 +02:00
+@NullMarked
2021-06-11 14:02:28 +02:00
+public final class ClientOption<T> {
+
+ public static final ClientOption<SkinParts> SKIN_PARTS = new ClientOption<>(SkinParts.class);
+ public static final ClientOption<Boolean> CHAT_COLORS_ENABLED = new ClientOption<>(Boolean.class);
+ public static final ClientOption<ChatVisibility> CHAT_VISIBILITY = new ClientOption<>(ChatVisibility.class);
+ public static final ClientOption<String> LOCALE = new ClientOption<>(String.class);
+ public static final ClientOption<MainHand> MAIN_HAND = new ClientOption<>(MainHand.class);
+ public static final ClientOption<Integer> VIEW_DISTANCE = new ClientOption<>(Integer.class);
2022-10-27 01:09:03 +02:00
+ public static final ClientOption<Boolean> TEXT_FILTERING_ENABLED = new ClientOption<>(Boolean.class);
2024-11-26 18:46:12 +01:00
+ public static final ClientOption<Boolean> ALLOW_SERVER_LISTINGS = new ClientOption<>(Boolean.class);
+ public static final ClientOption<ParticleVisibility> PARTICLE_VISIBILITY = new ClientOption<>(ParticleVisibility.class);
2021-06-11 14:02:28 +02:00
+
+ private final Class<T> type;
+
2024-09-30 01:48:34 +02:00
+ private ClientOption(final Class<T> type) {
2021-06-11 14:02:28 +02:00
+ this.type = type;
+ }
+
+ public Class<T> getType() {
2024-09-30 01:48:34 +02:00
+ return this.type;
2021-06-11 14:02:28 +02:00
+ }
+
2021-08-14 06:11:12 +02:00
+ public enum ChatVisibility implements Translatable {
+ FULL("full"),
+ SYSTEM("system"),
+ HIDDEN("hidden"),
+ UNKNOWN("unknown");
+
2024-09-30 01:48:34 +02:00
+ public static final Index<String, ChatVisibility> NAMES = Index.create(ChatVisibility.class, chatVisibility -> chatVisibility.name);
2021-08-14 06:11:12 +02:00
+ private final String name;
+
2024-09-30 01:48:34 +02:00
+ ChatVisibility(final String name) {
2021-08-14 06:11:12 +02:00
+ this.name = name;
+ }
+
+ @Override
2024-09-30 01:48:34 +02:00
+ public String translationKey() {
2021-08-14 06:11:12 +02:00
+ if (this == UNKNOWN) {
+ throw new UnsupportedOperationException(this.name + " doesn't have a translation key");
+ }
+ return "options.chat.visibility." + this.name;
+ }
2021-06-11 14:02:28 +02:00
+ }
2024-11-26 18:46:12 +01:00
+
+ public enum ParticleVisibility implements Translatable {
+ ALL("all"),
+ DECREASED("decreased"),
+ MINIMAL("minimal");
+
+ public static final Index<String, ParticleVisibility> NAMES = Index.create(ParticleVisibility.class, particleVisibility -> particleVisibility.name);
+ private final String name;
+
+ ParticleVisibility(final String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String translationKey() {
+ return "options.particles." + this.name;
+ }
+ }
2021-06-11 14:02:28 +02:00
+}
diff --git a/src/main/java/com/destroystokyo/paper/SkinParts.java b/src/main/java/com/destroystokyo/paper/SkinParts.java
new file mode 100644
index 0000000000000000000000000000000000000000..4a0c39405d4fbed457787e3c6ded4cc6591bc8c2
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/SkinParts.java
@@ -0,0 +1,20 @@
+package com.destroystokyo.paper;
+
+public interface SkinParts {
+
+ boolean hasCapeEnabled();
+
+ boolean hasJacketEnabled();
+
+ boolean hasLeftSleeveEnabled();
+
+ boolean hasRightSleeveEnabled();
+
+ boolean hasLeftPantsEnabled();
+
+ boolean hasRightPantsEnabled();
+
+ boolean hasHatsEnabled();
+
+ int getRaw();
+}
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerClientOptionsChangeEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerClientOptionsChangeEvent.java
new file mode 100644
2024-11-26 18:46:12 +01:00
index 0000000000000000000000000000000000000000..5245955fb3466d2b89eaad4027d145ebf7bb122c
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerClientOptionsChangeEvent.java
2024-11-26 18:46:12 +01:00
@@ -0,0 +1,142 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.player;
+
+import com.destroystokyo.paper.ClientOption;
+import com.destroystokyo.paper.ClientOption.ChatVisibility;
2024-11-26 18:46:12 +01:00
+import com.destroystokyo.paper.ClientOption.ParticleVisibility;
2021-06-11 14:02:28 +02:00
+import com.destroystokyo.paper.SkinParts;
2024-09-30 01:48:34 +02:00
+import java.util.Map;
2021-06-11 14:02:28 +02:00
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.MainHand;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2024-09-30 01:48:34 +02:00
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
2022-10-27 01:09:03 +02:00
+ * Called when the player changes their client settings
2021-06-11 14:02:28 +02:00
+ */
2024-09-30 01:48:34 +02:00
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PlayerClientOptionsChangeEvent extends PlayerEvent {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
2021-06-11 14:02:28 +02:00
+
+ private final String locale;
+ private final int viewDistance;
+ private final ChatVisibility chatVisibility;
+ private final boolean chatColors;
+ private final SkinParts skinparts;
+ private final MainHand mainHand;
2022-10-27 01:09:03 +02:00
+ private final boolean allowsServerListings;
+ private final boolean textFilteringEnabled;
2024-11-26 18:46:12 +01:00
+ private final ParticleVisibility particleVisibility;
2021-06-11 14:02:28 +02:00
+
2022-10-27 01:09:03 +02:00
+ @Deprecated
2024-09-30 01:48:34 +02:00
+ public PlayerClientOptionsChangeEvent(final Player player, final String locale, final int viewDistance, final ChatVisibility chatVisibility, final boolean chatColors, final SkinParts skinParts, final MainHand mainHand) {
2021-06-11 14:02:28 +02:00
+ super(player);
+ this.locale = locale;
+ this.viewDistance = viewDistance;
+ this.chatVisibility = chatVisibility;
+ this.chatColors = chatColors;
+ this.skinparts = skinParts;
+ this.mainHand = mainHand;
2022-10-27 01:09:03 +02:00
+ this.allowsServerListings = false;
+ this.textFilteringEnabled = false;
2024-11-26 18:46:12 +01:00
+ this.particleVisibility = ParticleVisibility.ALL;
2022-10-27 01:09:03 +02:00
+ }
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-30 01:48:34 +02:00
+ public PlayerClientOptionsChangeEvent(final Player player, final Map<ClientOption<?>, ?> options) {
2022-10-27 01:09:03 +02:00
+ super(player);
+
+ this.locale = (String) options.get(ClientOption.LOCALE);
+ this.viewDistance = (int) options.get(ClientOption.VIEW_DISTANCE);
+ this.chatVisibility = (ChatVisibility) options.get(ClientOption.CHAT_VISIBILITY);
+ this.chatColors = (boolean) options.get(ClientOption.CHAT_COLORS_ENABLED);
+ this.skinparts = (SkinParts) options.get(ClientOption.SKIN_PARTS);
+ this.mainHand = (MainHand) options.get(ClientOption.MAIN_HAND);
+ this.allowsServerListings = (boolean) options.get(ClientOption.ALLOW_SERVER_LISTINGS);
+ this.textFilteringEnabled = (boolean) options.get(ClientOption.TEXT_FILTERING_ENABLED);
2024-11-26 18:46:12 +01:00
+ this.particleVisibility = (ParticleVisibility) options.get(ClientOption.PARTICLE_VISIBILITY);
2021-06-11 14:02:28 +02:00
+ }
+
+ public String getLocale() {
2024-02-01 10:15:57 +01:00
+ return this.locale;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasLocaleChanged() {
2024-02-01 10:15:57 +01:00
+ return !this.locale.equals(this.player.getClientOption(ClientOption.LOCALE));
2021-06-11 14:02:28 +02:00
+ }
+
+ public int getViewDistance() {
2024-02-01 10:15:57 +01:00
+ return this.viewDistance;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasViewDistanceChanged() {
2024-02-01 10:15:57 +01:00
+ return this.viewDistance != this.player.getClientOption(ClientOption.VIEW_DISTANCE);
2021-06-11 14:02:28 +02:00
+ }
+
+ public ChatVisibility getChatVisibility() {
2024-02-01 10:15:57 +01:00
+ return this.chatVisibility;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasChatVisibilityChanged() {
2024-02-01 10:15:57 +01:00
+ return this.chatVisibility != this.player.getClientOption(ClientOption.CHAT_VISIBILITY);
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasChatColorsEnabled() {
2024-02-01 10:15:57 +01:00
+ return this.chatColors;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasChatColorsEnabledChanged() {
2024-02-01 10:15:57 +01:00
+ return this.chatColors != this.player.getClientOption(ClientOption.CHAT_COLORS_ENABLED);
2021-06-11 14:02:28 +02:00
+ }
+
+ public SkinParts getSkinParts() {
2024-02-01 10:15:57 +01:00
+ return this.skinparts;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasSkinPartsChanged() {
2024-02-01 10:15:57 +01:00
+ return this.skinparts.getRaw() != this.player.getClientOption(ClientOption.SKIN_PARTS).getRaw();
2021-06-11 14:02:28 +02:00
+ }
+
+ public MainHand getMainHand() {
2024-02-01 10:15:57 +01:00
+ return this.mainHand;
2021-06-11 14:02:28 +02:00
+ }
+
+ public boolean hasMainHandChanged() {
2024-02-01 10:15:57 +01:00
+ return this.mainHand != this.player.getClientOption(ClientOption.MAIN_HAND);
2021-06-11 14:02:28 +02:00
+ }
+
2024-11-26 18:46:12 +01:00
+ public boolean hasTextFilteringEnabled() {
+ return this.textFilteringEnabled;
+ }
+
+ public boolean hasTextFilteringChanged() {
+ return this.textFilteringEnabled != this.player.getClientOption(ClientOption.TEXT_FILTERING_ENABLED);
+ }
+
2022-10-27 01:09:03 +02:00
+ public boolean allowsServerListings() {
2024-02-01 10:15:57 +01:00
+ return this.allowsServerListings;
2022-10-27 01:09:03 +02:00
+ }
+
+ public boolean hasAllowServerListingsChanged() {
2024-02-01 10:15:57 +01:00
+ return this.allowsServerListings != this.player.getClientOption(ClientOption.ALLOW_SERVER_LISTINGS);
2022-10-27 01:09:03 +02:00
+ }
+
2024-11-26 18:46:12 +01:00
+ public ParticleVisibility getParticleVisibility() {
+ return this.particleVisibility;
2022-10-27 01:09:03 +02:00
+ }
+
2024-11-26 18:46:12 +01:00
+ public boolean hasParticleVisibilityChanged() {
+ return this.particleVisibility != this.player.getClientOption(ClientOption.PARTICLE_VISIBILITY);
2022-10-27 01:09:03 +02:00
+ }
+
2021-06-11 14:02:28 +02:00
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
2024-10-24 19:29:35 +02:00
index 80e894e3d625cde14bfe881d2c367b43a4882cfd..2d3c8febbbab959433101fb9dfc1e0ff9deca192 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
2024-10-24 19:29:35 +02:00
@@ -3375,6 +3375,13 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM
2021-06-11 14:02:28 +02:00
void resetCooldown();
2023-12-09 00:13:02 +01:00
// Paper end - attack cooldown API
+ // Paper start - client option API
2021-06-11 14:02:28 +02:00
+ /**
+ * @return the client option value of the player
+ */
2023-12-09 00:13:02 +01:00
+ <T> @NotNull T getClientOption(com.destroystokyo.paper.@NotNull ClientOption<T> option);
+ // Paper end - client option API
+
2021-06-11 14:02:28 +02:00
// Spigot start
2023-12-09 00:13:02 +01:00
public class Spigot extends Entity.Spigot {