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..45f21535 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,112 @@ public class CosmeticUser { private final ArrayList hiddenReason = new ArrayList<>(); private final HashMap colors = new HashMap<>(); - public CosmeticUser(UUID uuid) { - this.uniqueId = uuid; - userEmoteManager = new UserEmoteManager(this); - tick(); - } - + /** + * 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.uniqueId = uuid; - userEmoteManager = new UserEmoteManager(this); - loadData(data); - tick(); + this(uuid); + initialize(data); } - private void tick() { + public CosmeticUser(@NotNull UUID uuid) { + this.uniqueId = uuid; + this.userEmoteManager = new UserEmoteManager(this); + } + + /** + * 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 Map.Entry> entry : userData.getCosmetics().entrySet()) { + final Cosmetic cosmetic = entry.getValue().getKey(); + final Integer colorRGBInt = entry.getValue().getValue(); + + if (!this.canApplyCosmetic(cosmetic)) { + MessagesUtil.sendDebugMessages("Cannot apply cosmetic[id=" + cosmetic.getId() + "]"); + continue; + } + + 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 = () -> { MessagesUtil.sendDebugMessages("Tick[uuid=" + uniqueId + "]", Level.INFO); @@ -98,54 +189,6 @@ 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); - } - - 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); - } - } - } - public Cosmetic getCosmetic(CosmeticSlot slot) { return playerCosmetics.get(slot); } 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); }