Skip to content

Commit

Permalink
Hide the tiers when initially opening cosmic crate
Browse files Browse the repository at this point in the history
They've been cached in a weak hash map to ensure GC will clean it if needed.
  • Loading branch information
ryderbelserion committed Jul 22, 2024
1 parent a201bb8 commit 81843b6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
### Changes:
- Play knockback/sounds if they don't have the required keys.
- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -222,14 +222,17 @@ 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);

// Overwrite the current item.
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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -338,6 +345,8 @@ public void run() {
crateManager.removePlayerFromOpeningList(player);
crateManager.removePlayerKeyType(player);

crateManager.removeTier(player);

// Cancel crate task.
crateManager.removeCrateTask(player);

Expand Down Expand Up @@ -396,15 +405,15 @@ 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);
cosmic.sendTitleChange();

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));
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -88,6 +89,32 @@ public class CrateManager {
private final List<String> brokeCrates = new ArrayList<>();
private final List<Crate> crates = new ArrayList<>();

private final Map<UUID, Map<Integer, Tier>> 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<UUID, Map<Integer, Tier>> getTiers() {
return Collections.unmodifiableMap(this.tiers);
}

private HologramManager holograms;

private boolean giveNewPlayersKeys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 81843b6

Please sign in to comment.