From 81843b64908e31068766ce38dbc92a58ae242b0f Mon Sep 17 00:00:00 2001 From: Ryder Belserion Date: Mon, 22 Jul 2024 11:45:07 -0400 Subject: [PATCH] Hide the tiers when initially opening cosmic crate They've been cached in a weak hash map to ensure GC will clean it if needed. --- CHANGELOG.md | 6 ++- .../crazycrates/api/PrizeManager.java | 1 - .../crazycrates/listeners/MiscListener.java | 4 ++ .../listeners/crates/CosmicCrateListener.java | 43 ++++++++++++------- .../tasks/crates/CrateManager.java | 27 ++++++++++++ .../tasks/crates/types/CosmicCrate.java | 2 +- 6 files changed, 64 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b65806ee4..aa1a5c364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,6 @@ ### Changes: -- Play knockback/sounds if they don't have the required keys. \ No newline at end of file +- Play knockback/sounds if they don't have the required keys. +#### Cosmic Crate: +- Cosmic Crate was changed back in March for the calculation of tiers to be handled when you open the inventory, so that when picking a mystery crate. The choice would actually matter! +- It has been brought to my attention of being able to skimp it using client side mods. this has been addressed, the mods will no longer be able to skimp it by seeing item differences. +- The calculation still happens but, the picks are stored internally to the player's uuid in a cache which clears when the player quits, the inventory closes or the crate task ends for X reason. \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java b/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java index 0f055f8ca..183a93087 100644 --- a/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java +++ b/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java @@ -7,7 +7,6 @@ import com.badbones69.crazycrates.api.events.PlayerPrizeEvent; import com.badbones69.crazycrates.api.objects.Crate; import com.badbones69.crazycrates.api.objects.Prize; -import com.ryderbelserion.vital.paper.util.AdvUtil; import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; diff --git a/src/main/java/com/badbones69/crazycrates/listeners/MiscListener.java b/src/main/java/com/badbones69/crazycrates/listeners/MiscListener.java index 359c8e447..3aa629197 100644 --- a/src/main/java/com/badbones69/crazycrates/listeners/MiscListener.java +++ b/src/main/java/com/badbones69/crazycrates/listeners/MiscListener.java @@ -79,6 +79,8 @@ public void onPlayerQuit(PlayerQuitEvent event) { this.inventoryManager.removeCrateViewer(player); this.inventoryManager.removePageViewer(player); + this.crateManager.removeTier(player); + this.crateManager.endQuickCrate(player, player.getLocation(), this.crateManager.getOpeningCrate(player), false); // End just in case. @@ -149,6 +151,8 @@ public void onInventoryCloseEvent(InventoryCloseEvent event) { this.crateManager.removePlayerFromOpeningList(player); this.crateManager.removePlayerKeyType(player); + this.crateManager.removeTier(player); + // Cancel crate task just in case. this.crateManager.removeCrateTask(player); diff --git a/src/main/java/com/badbones69/crazycrates/listeners/crates/CosmicCrateListener.java b/src/main/java/com/badbones69/crazycrates/listeners/crates/CosmicCrateListener.java index 73635339c..b238b2351 100644 --- a/src/main/java/com/badbones69/crazycrates/listeners/crates/CosmicCrateListener.java +++ b/src/main/java/com/badbones69/crazycrates/listeners/crates/CosmicCrateListener.java @@ -28,7 +28,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; import us.crazycrew.crazycrates.api.enums.types.CrateType; import us.crazycrew.crazycrates.api.enums.types.KeyType; @@ -55,7 +54,7 @@ public class CosmicCrateListener implements Listener { private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); @EventHandler - public void onInventoryClickPrize(InventoryClickEvent event) { + public void onPrizeReceive(InventoryClickEvent event) { // Get the inventory. final Inventory inventory = event.getInventory(); @@ -94,7 +93,7 @@ public void onInventoryClickPrize(InventoryClickEvent event) { // Check if null or air. if (itemStack == null || itemStack.getType() == Material.AIR) return; - final Tier tier = this.crateManager.getTier(crate, itemStack); + final Tier tier = this.crateManager.getTier(player, slot); // If tier is null, return if (tier == null) return; @@ -119,7 +118,7 @@ public void onInventoryClickPrize(InventoryClickEvent event) { } @EventHandler - public void onInventoryClick(InventoryClickEvent event) { + public void onMysteryBoxClick(InventoryClickEvent event) { // Get the inventory. final Inventory inventory = event.getInventory(); @@ -183,12 +182,14 @@ public void onInventoryClick(InventoryClickEvent event) { // Check if prizes is less than or equal to totalPrizes before we change any items. if (size < totalPrizes) { - // Gets the tier name from the pdc. - final String tierName = container.get(PersistentKeys.crate_tier.getNamespacedKey(), PersistentDataType.STRING); + final Tier tier = this.crateManager.getTier(player, pickedSlot); - // If the tier is null, return. - final Tier tier = crate.getTier(tierName); - if (tier == null) return; + if (tier == null) { + return; + } + + // Gets the tier name. + final String tierName = tier.getName(); // Get item builder. ItemBuilder builder = cosmicCrateManager.getPickedCrate().setPlayer(player) @@ -205,14 +206,13 @@ public void onInventoryClick(InventoryClickEvent event) { event.setCurrentItem(builder.getStack()); // Add the picked prize. - cosmicCrateManager.addPickedPrize(player, slot, tier); + cosmicCrateManager.addPickedPrize(player, pickedSlot, tier); // Play a sound to indicate they clicked a chest. holder.getCrate().playSound(player, player.getLocation(), "click-sound","ui.button.click", Sound.Source.PLAYER); } } else if (container.has(PersistentKeys.cosmic_picked_crate.getNamespacedKey())) { - // Gets the tier name from the pdc. - final String tierName = container.get(PersistentKeys.crate_tier.getNamespacedKey(), PersistentDataType.STRING); + final Tier tier = this.crateManager.getTier(player, pickedSlot); // Get item builder. ItemBuilder builder = cosmicCrateManager.getMysteryCrate().setPlayer(player) @@ -222,6 +222,9 @@ public void onInventoryClick(InventoryClickEvent event) { // Set the amount. builder.setAmount(pickedSlot); + // Gets the tier name. + final String tierName = tier.getName(); + // Set the tier name from before to the pdc if it exists. cosmicCrateManager.setTier(builder, tierName); @@ -229,7 +232,7 @@ public void onInventoryClick(InventoryClickEvent event) { event.setCurrentItem(builder.getStack()); // Remove slot if we click it. - cosmicCrateManager.removePickedPrize(player, slot); + cosmicCrateManager.removePickedPrize(player, pickedSlot); // Play a sound to indicate they clicked a chest. holder.getCrate().playSound(player, player.getLocation(), "click-sound","ui.button.click", Sound.Source.PLAYER); @@ -259,6 +262,8 @@ public void onInventoryClick(InventoryClickEvent event) { this.crateManager.removePlayerFromOpeningList(player); this.crateManager.removePlayerKeyType(player); + this.crateManager.removeTier(player); + // Remove hand checks. this.crateManager.removeHands(player); @@ -282,6 +287,8 @@ public void onInventoryClick(InventoryClickEvent event) { this.crateManager.removePlayerFromOpeningList(player); this.crateManager.removePlayerKeyType(player); + this.crateManager.removeTier(player); + // Remove hand checks. this.crateManager.removeHands(player); @@ -338,6 +345,8 @@ public void run() { crateManager.removePlayerFromOpeningList(player); crateManager.removePlayerKeyType(player); + crateManager.removeTier(player); + // Cancel crate task. crateManager.removeCrateTask(player); @@ -396,7 +405,7 @@ private void startRollingAnimation(final Player player, final InventoryView view player.updateInventory(); } - private void showRewards(final Player player, final InventoryView view, final CratePrizeMenu cosmic, final CosmicCrateManager crateManager) { + private void showRewards(final Player player, final InventoryView view, final CratePrizeMenu cosmic, final CosmicCrateManager cosmicCrateManager) { final String rewardsName = cosmic.getCrate().getCrateInventoryName() + " - Prizes"; cosmic.title(rewardsName); @@ -404,7 +413,7 @@ private void showRewards(final Player player, final InventoryView view, final Cr view.getTopInventory().clear(); - crateManager.getPrizes(player).forEach((slot, tier) -> { + cosmicCrateManager.getPrizes(player).forEach((slot, tier) -> { Inventory inventory = view.getTopInventory(); inventory.setItem(slot, tier.getTierItem(player)); @@ -419,7 +428,9 @@ public void run() { // Close inventory. player.getScheduler().run(plugin, scheduledTask -> player.closeInventory(InventoryCloseEvent.Reason.UNLOADED), null); - crateManager.removePickedPlayer(player); + crateManager.removeTier(player); + + cosmicCrateManager.removePickedPlayer(player); // Log it if (MiscUtils.isLogging()) { diff --git a/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java b/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java index 605eddf5e..06e509398 100644 --- a/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java +++ b/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java @@ -72,6 +72,7 @@ import java.util.Objects; import java.util.TimerTask; import java.util.UUID; +import java.util.WeakHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -88,6 +89,32 @@ public class CrateManager { private final List brokeCrates = new ArrayList<>(); private final List crates = new ArrayList<>(); + private final Map> tiers = new WeakHashMap<>(); + + public void addTier(final Player player, final int slot, final Tier tier) { + if (this.tiers.containsKey(player.getUniqueId())) { + this.tiers.get(player.getUniqueId()).put(slot, tier); + + return; + } + + this.tiers.put(player.getUniqueId(), new WeakHashMap<>() {{ + put(slot, tier); + }}); + } + + public void removeTier(final Player player) { + this.tiers.remove(player.getUniqueId()); + } + + public final Tier getTier(final Player player, final int slot) { + return this.tiers.get(player.getUniqueId()).get(slot); + } + + public Map> getTiers() { + return Collections.unmodifiableMap(this.tiers); + } + private HologramManager holograms; private boolean giveNewPlayersKeys; diff --git a/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java b/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java index 9b9d5fcaa..0ba959761 100644 --- a/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java +++ b/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java @@ -41,7 +41,7 @@ public void open(@NotNull final KeyType type, final boolean checkHand) { final Tier tier = PrizeManager.getTier(crate); if (tier != null) { - stack.setPersistentString(PersistentKeys.crate_tier.getNamespacedKey(), tier.getName()); + this.crateManager.addTier(player, index, tier); setItem(index, stack.getStack());