From 44650250c5492e54e232047bc5072978dd548640 Mon Sep 17 00:00:00 2001 From: Tech Date: Sat, 25 Jan 2025 12:18:32 -0500 Subject: [PATCH 1/2] initialize method --- .../listener/PlayerConnectionListener.java | 12 +- .../hmccosmetics/user/CosmeticUser.java | 149 ++++++++++++------ .../user/CosmeticUserProvider.java | 18 +-- 3 files changed, 109 insertions(+), 70 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java index a0617837..0ee3782f 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java @@ -46,10 +46,16 @@ public class PlayerConnectionListener implements Listener { Bukkit.getPluginManager().callEvent(preLoadEvent); if (preLoadEvent.isCancelled()) return; - Database.get(uuid).thenAccept(data -> { - if (data == null) return; + Database.get(uuid).thenAccept(userData -> { + if (userData == null) { + return; + } + Bukkit.getScheduler().runTask(HMCCosmeticsPlugin.getInstance(), () -> { - CosmeticUser cosmeticUser = CosmeticUsers.getProvider().createCosmeticUser(uuid, data); + CosmeticUser cosmeticUser = CosmeticUsers.getProvider() + .createCosmeticUser(uuid) + .initialize(userData); + CosmeticUsers.addUser(cosmeticUser); MessagesUtil.sendDebugMessages("Run User Join for " + uuid); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java index 3c0ff3bf..811f3021 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -21,7 +21,6 @@ import com.hibiscusmc.hmccosmetics.user.manager.UserEmoteManager; import com.hibiscusmc.hmccosmetics.user.manager.UserWardrobeManager; import com.hibiscusmc.hmccosmetics.util.HMCCInventoryUtils; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; -import com.hibiscusmc.hmccosmetics.util.HMCCPlayerUtils; import com.hibiscusmc.hmccosmetics.util.packets.HMCCPacketManager; import lombok.Getter; import me.lojosho.hibiscuscommons.hooks.Hooks; @@ -62,20 +61,42 @@ public class CosmeticUser { private final ArrayList hiddenReason = new ArrayList<>(); private final HashMap colors = new HashMap<>(); + @Deprecated(forRemoval = true, since = "2.7.5") + public CosmeticUser(UUID uuid, UserData data) { + this(uuid); + } + public CosmeticUser(UUID uuid) { this.uniqueId = uuid; - userEmoteManager = new UserEmoteManager(this); - tick(); + this.userEmoteManager = new UserEmoteManager(this); } - public CosmeticUser(UUID uuid, UserData data) { - this.uniqueId = uuid; - userEmoteManager = new UserEmoteManager(this); - loadData(data); - tick(); + /** + * Initialize the {@link CosmeticUser}. + * @param userData the associated {@link UserData} + * @return the {@link CosmeticUser} + * @apiNote Initialize is called after {@link CosmeticUserProvider#createCosmeticUser(UUID)} so it is possible to + * populate an extending version of {@link CosmeticUser} with data then override this method to apply your + * own state. + */ + public CosmeticUser initialize(final @Nullable UserData userData) { + if(userData != null) { + // CosmeticSlot -> Entry + for(final var entry : userData.getCosmetics().entrySet()) { + final Cosmetic cosmetic = entry.getValue().getKey(); + final Integer colorRGBInt = entry.getValue().getValue(); + + this.applyCosmetic(cosmetic, colorRGBInt); + } + + this.applyHiddenState(userData); + } + + this.startTickTask(); + return this; } - private void tick() { + private void startTickTask() { // Occasionally updates the entity cosmetics Runnable run = () -> { MessagesUtil.sendDebugMessages("Tick[uuid=" + uniqueId + "]", Level.INFO); @@ -98,51 +119,77 @@ public class CosmeticUser { despawnBalloon(); } - public void loadData(@NotNull UserData data) { - boolean permissionCheck = Settings.isForcePermissionJoin(); - - for (Map.Entry> entry : data.getCosmetics().entrySet()) { - Cosmetic cosmetic = entry.getValue().getKey(); - Color color = entry.getValue().getValue() == -1 ? null : Color.fromRGB(entry.getValue().getValue()); - - if (permissionCheck && cosmetic.requiresPermission()) { - if (getPlayer() != null && !getPlayer().hasPermission(cosmetic.getPermission())) { - continue; - } - } - addPlayerCosmetic(cosmetic, color); + protected boolean applyCosmetic(Cosmetic cosmetic, Integer colorRGBInt) { + final Player bukkitPlayer = getPlayer(); + if(bukkitPlayer == null) { + MessagesUtil.sendDebugMessages("Cannot apply cosmetic, bukkit player is null"); + return false; } - if (!hiddenReason.isEmpty()) { - for (CosmeticUser.HiddenReason reason : hiddenReason) silentlyAddHideFlag(reason); - } else { - for (HiddenReason reason : data.getHiddenReasons()) { - if (getPlayer() != null && Settings.isDisabledGamemodesEnabled() && Settings.getDisabledGamemodes().contains(getPlayer().getGameMode().toString())) { - MessagesUtil.sendDebugMessages("Hiding Cosmetics due to gamemode"); - hideCosmetics(CosmeticUser.HiddenReason.GAMEMODE); - return; - } else { - if (isHidden(CosmeticUser.HiddenReason.GAMEMODE)) { - MessagesUtil.sendDebugMessages("Join Gamemode Check: Showing Cosmetics"); - showCosmetics(CosmeticUser.HiddenReason.GAMEMODE); - return; - } - } - // Handle world check - if (getPlayer() != null && Settings.getDisabledWorlds().contains(getPlayer().getWorld().getName())) { - MessagesUtil.sendDebugMessages("Hiding Cosmetics due to world"); - hideCosmetics(CosmeticUser.HiddenReason.WORLD); - } else { - if (isHidden(CosmeticUser.HiddenReason.WORLD)) { - MessagesUtil.sendDebugMessages("Join World Check: Showing Cosmetics"); - showCosmetics(CosmeticUser.HiddenReason.WORLD); - } - } - if (Settings.isAllPlayersHidden()) { - hideCosmetics(CosmeticUser.HiddenReason.DISABLED); - } - silentlyAddHideFlag(reason); + if(!this.canApplyCosmetic(bukkitPlayer, cosmetic)) { + MessagesUtil.sendDebugMessages("Player cannot apply cosmetic"); + return false; + } + + final Color color = colorRGBInt == -1 + ? null + : Color.fromRGB(colorRGBInt); + + this.addPlayerCosmetic(cosmetic, color); + return true; + } + + protected boolean canApplyCosmetic(Player player, Cosmetic cosmetic) { + final boolean checkPermission = Settings.isForcePermissionJoin(); + if(!checkPermission || !cosmetic.requiresPermission()) { + return true; + } + + if(player == null) { + return false; + } + + return player.hasPermission(cosmetic.getPermission()); + } + + protected void applyHiddenState(UserData userData) { + if(!hiddenReason.isEmpty()) { + for(final var reason : this.hiddenReason) { + this.silentlyAddHideFlag(reason); } + return; + } + + final Player bukkitPlayer = getPlayer(); + + for(final var reason : userData.getHiddenReasons()) { + if(bukkitPlayer != null && Settings.isDisabledGamemodesEnabled() && Settings.getDisabledGamemodes().contains(bukkitPlayer.getGameMode().toString())) { + MessagesUtil.sendDebugMessages("Hiding cosmetics due to gamemode"); + this.hideCosmetics(HiddenReason.GAMEMODE); + + return; + } else if(this.isHidden(HiddenReason.GAMEMODE)) { + MessagesUtil.sendDebugMessages("Showing cosmetics for gamemode"); + this.showCosmetics(HiddenReason.GAMEMODE); + } + + if(bukkitPlayer != null && Settings.getDisabledGamemodes().contains(bukkitPlayer.getWorld().getName())) { + MessagesUtil.sendDebugMessages("Hiding Cosmetics due to gamemode"); + this.hideCosmetics(CosmeticUser.HiddenReason.GAMEMODE); + + return; + } else if(this.isHidden(HiddenReason.WORLD)) { + MessagesUtil.sendDebugMessages("Showing Cosmetics due to world"); + this.showCosmetics(HiddenReason.WORLD); + + return; + } + + if(Settings.isAllPlayersHidden()) { + this.hideCosmetics(HiddenReason.DISABLED); + } + + this.silentlyAddHideFlag(reason); } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUserProvider.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUserProvider.java index b27c0986..1ce06e0c 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUserProvider.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUserProvider.java @@ -17,19 +17,10 @@ public interface CosmeticUserProvider { /** * Construct the custom {@link CosmeticUser}. * @param playerId the player uuid - * @param userData the user data associated with the player * @return the {@link CosmeticUser} * @apiNote This method is called during the {@link PlayerJoinEvent}. */ - @NotNull CosmeticUser createCosmeticUser(@NotNull UUID playerId, @NotNull UserData userData); - - /** - * Construct the custom {@link CosmeticUser}. - * @param playerId the player uuid - * @return the {@link CosmeticUser} - * @apiNote This method is called during the {@link PlayerJoinEvent}. - */ - @NotNull CosmeticUser createCosmeticUserWithoutData(@NotNull UUID playerId); + @NotNull CosmeticUser createCosmeticUser(@NotNull UUID playerId); /** * Represents the plugin that is providing this {@link CosmeticUserProvider} @@ -42,12 +33,7 @@ public interface CosmeticUserProvider { */ class Default implements CosmeticUserProvider { @Override - public @NotNull CosmeticUser createCosmeticUser(@NotNull UUID playerId, @NotNull UserData userData) { - return new CosmeticUser(playerId, userData); - } - - @Override - public @NotNull CosmeticUser createCosmeticUserWithoutData(@NotNull UUID playerId) { + public @NotNull CosmeticUser createCosmeticUser(@NotNull UUID playerId) { return new CosmeticUser(playerId); } From 3aa5939225c066502006c8debe8546e0fd33945c Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Sat, 25 Jan 2025 21:13:38 -0600 Subject: [PATCH 2/2] clean: simplify logic surrounding new initialize logic --- .../hmccosmetics/user/CosmeticUser.java | 154 +++++++++--------- 1 file changed, 75 insertions(+), 79 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java index 811f3021..45f21535 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -61,12 +61,18 @@ public class CosmeticUser { private final ArrayList hiddenReason = new ArrayList<>(); private final HashMap colors = new HashMap<>(); + /** + * Use {@link #CosmeticUser(UUID)} instead and use {@link #initialize(UserData)} to populate the user with data. + * @param uuid + * @param data + */ @Deprecated(forRemoval = true, since = "2.7.5") public CosmeticUser(UUID uuid, UserData data) { this(uuid); + initialize(data); } - public CosmeticUser(UUID uuid) { + public CosmeticUser(@NotNull UUID uuid) { this.uniqueId = uuid; this.userEmoteManager = new UserEmoteManager(this); } @@ -82,20 +88,84 @@ public class CosmeticUser { public CosmeticUser initialize(final @Nullable UserData userData) { if(userData != null) { // CosmeticSlot -> Entry - for(final var entry : userData.getCosmetics().entrySet()) { + for(final Map.Entry> entry : userData.getCosmetics().entrySet()) { final Cosmetic cosmetic = entry.getValue().getKey(); final Integer colorRGBInt = entry.getValue().getValue(); - this.applyCosmetic(cosmetic, colorRGBInt); - } + if (!this.canApplyCosmetic(cosmetic)) { + MessagesUtil.sendDebugMessages("Cannot apply cosmetic[id=" + cosmetic.getId() + "]"); + continue; + } - this.applyHiddenState(userData); + Color color = null; + if (colorRGBInt != -1) color = Color.fromRGB(colorRGBInt); // -1 is defined as no color; anything else is a color + + this.addPlayerCosmetic(cosmetic, color); + } + this.applyHiddenState(userData.getHiddenReasons()); } this.startTickTask(); return this; } + /** + * This method is only called from {@link #initialize(UserData)} and can't be called directly. + * This is used to help hooking plugins apply custom logic to the user. + */ + protected boolean applyCosmetic(@NotNull Cosmetic cosmetic, @Nullable Color color) { + this.addPlayerCosmetic(cosmetic, color); + return true; + } + + /** + * This method is only called from {@link #initialize(UserData)} and can't be called directly. + * This is used to help hooking plugins apply custom logic to the user. + */ + protected boolean canApplyCosmetic(@NotNull Cosmetic cosmetic) { + return canEquipCosmetic(cosmetic, false); + } + + /** + * This method is only called from {@link #initialize(UserData)} and can't be called directly. + * This is used to help hooking plugins apply custom logic to the user. + */ + protected void applyHiddenState(@NotNull List hiddenReasons) { + if(!hiddenReason.isEmpty()) { + for(final HiddenReason reason : this.hiddenReason) { + this.silentlyAddHideFlag(reason); + } + return; + } + + Player bukkitPlayer = getPlayer(); + for (final HiddenReason reason : hiddenReasons) { + if(bukkitPlayer != null && Settings.isDisabledGamemodesEnabled() && Settings.getDisabledGamemodes().contains(bukkitPlayer.getGameMode().toString())) { + MessagesUtil.sendDebugMessages("Hiding cosmetics due to gamemode"); + this.hideCosmetics(HiddenReason.GAMEMODE); + return; + } else if(this.isHidden(HiddenReason.GAMEMODE)) { + MessagesUtil.sendDebugMessages("Showing cosmetics for gamemode"); + this.showCosmetics(HiddenReason.GAMEMODE); + } + + if(bukkitPlayer != null && Settings.getDisabledGamemodes().contains(bukkitPlayer.getWorld().getName())) { + MessagesUtil.sendDebugMessages("Hiding Cosmetics due to gamemode"); + this.hideCosmetics(CosmeticUser.HiddenReason.GAMEMODE); + return; + } else if(this.isHidden(HiddenReason.WORLD)) { + MessagesUtil.sendDebugMessages("Showing Cosmetics due to world"); + this.showCosmetics(HiddenReason.WORLD); + return; + } + if(Settings.isAllPlayersHidden()) { + this.hideCosmetics(HiddenReason.DISABLED); + } + + this.silentlyAddHideFlag(reason); + } + } + private void startTickTask() { // Occasionally updates the entity cosmetics Runnable run = () -> { @@ -119,80 +189,6 @@ public class CosmeticUser { despawnBalloon(); } - protected boolean applyCosmetic(Cosmetic cosmetic, Integer colorRGBInt) { - final Player bukkitPlayer = getPlayer(); - if(bukkitPlayer == null) { - MessagesUtil.sendDebugMessages("Cannot apply cosmetic, bukkit player is null"); - return false; - } - - if(!this.canApplyCosmetic(bukkitPlayer, cosmetic)) { - MessagesUtil.sendDebugMessages("Player cannot apply cosmetic"); - return false; - } - - final Color color = colorRGBInt == -1 - ? null - : Color.fromRGB(colorRGBInt); - - this.addPlayerCosmetic(cosmetic, color); - return true; - } - - protected boolean canApplyCosmetic(Player player, Cosmetic cosmetic) { - final boolean checkPermission = Settings.isForcePermissionJoin(); - if(!checkPermission || !cosmetic.requiresPermission()) { - return true; - } - - if(player == null) { - return false; - } - - return player.hasPermission(cosmetic.getPermission()); - } - - protected void applyHiddenState(UserData userData) { - if(!hiddenReason.isEmpty()) { - for(final var reason : this.hiddenReason) { - this.silentlyAddHideFlag(reason); - } - return; - } - - final Player bukkitPlayer = getPlayer(); - - for(final var reason : userData.getHiddenReasons()) { - if(bukkitPlayer != null && Settings.isDisabledGamemodesEnabled() && Settings.getDisabledGamemodes().contains(bukkitPlayer.getGameMode().toString())) { - MessagesUtil.sendDebugMessages("Hiding cosmetics due to gamemode"); - this.hideCosmetics(HiddenReason.GAMEMODE); - - return; - } else if(this.isHidden(HiddenReason.GAMEMODE)) { - MessagesUtil.sendDebugMessages("Showing cosmetics for gamemode"); - this.showCosmetics(HiddenReason.GAMEMODE); - } - - if(bukkitPlayer != null && Settings.getDisabledGamemodes().contains(bukkitPlayer.getWorld().getName())) { - MessagesUtil.sendDebugMessages("Hiding Cosmetics due to gamemode"); - this.hideCosmetics(CosmeticUser.HiddenReason.GAMEMODE); - - return; - } else if(this.isHidden(HiddenReason.WORLD)) { - MessagesUtil.sendDebugMessages("Showing Cosmetics due to world"); - this.showCosmetics(HiddenReason.WORLD); - - return; - } - - if(Settings.isAllPlayersHidden()) { - this.hideCosmetics(HiddenReason.DISABLED); - } - - this.silentlyAddHideFlag(reason); - } - } - public Cosmetic getCosmetic(CosmeticSlot slot) { return playerCosmetics.get(slot); }