Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.excellentcrates.crate.impl.Crate;
import su.nightexpress.excellentcrates.crate.impl.Rarity;
import su.nightexpress.excellentcrates.crate.limit.LimitValues;
Expand Down Expand Up @@ -49,6 +50,8 @@ public interface Reward extends Writeable {

double getRollChance();

double getRollChance(@Nullable Player player);

@NotNull String getId();

@NotNull Crate getCrate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,12 @@ public LinkedHashMap<String, Reward> getRewardsMap() {

@NotNull
public Set<Rarity> getRarities() {
return this.getRewards().stream().map(Reward::getRarity).collect(Collectors.toSet());
return this.getRarities(null);
}

@NotNull
public Set<Rarity> getRarities(@Nullable Player player) {
return this.getRewards(player, null).stream().map(Reward::getRarity).collect(Collectors.toSet());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package su.nightexpress.excellentcrates.crate.impl;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.excellentcrates.CratesPlugin;
import su.nightexpress.excellentcrates.Placeholders;
import su.nightexpress.nightcore.config.FileConfig;
Expand Down Expand Up @@ -47,7 +49,11 @@ public double getRollChance() {
}

public double getRollChance(@NotNull Crate crate) {
return this.getRollChance(crate.getRarities());
return this.getRollChance(crate, null);
}

public double getRollChance(@NotNull Crate crate, @Nullable Player player) {
return this.getRollChance(crate.getRarities(player));
}

public double getRollChance(@NotNull Collection<Rarity> rarities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import su.nightexpress.nightcore.ui.menu.item.MenuItem;
import su.nightexpress.nightcore.ui.menu.type.LinkedMenu;
import su.nightexpress.nightcore.util.Lists;
import su.nightexpress.nightcore.util.NumberUtil;
import su.nightexpress.nightcore.util.bukkit.NightItem;
import su.nightexpress.nightcore.util.placeholder.Replacer;

Expand Down Expand Up @@ -100,6 +101,10 @@ public MenuFiller<Reward> createFiller(@NotNull MenuViewer viewer) {
.setLore(this.rewardLore)
.replacement(replacer -> {
replacer
// Player-aware odds: reflect the rewards actually rollable for THIS player,
// so permission-locked rewards don't skew the previewed percentages.
.replace(REWARD_ROLL_CHANCE, () -> NumberUtil.format(reward.getRollChance(player)))
.replace(REWARD_RARITY_ROLL_CHANCE, () -> NumberUtil.format(reward.getRarity().getRollChance(crate, player)))
.replace(GENERIC_LIMITS, limits)
.replace(NO_PERMISSION, restrictions)
.replace("%win_limit_amount%", limits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.excellentcrates.CratesPlugin;
import su.nightexpress.excellentcrates.Placeholders;
import su.nightexpress.excellentcrates.api.crate.Reward;
Expand Down Expand Up @@ -199,8 +200,20 @@ public void give(@NotNull Player player) {

@Override
public double getRollChance() {
double sum = this.crate.getRewards(this.rarity).stream().mapToDouble(Reward::getWeight).sum();
double rarityChance = this.rarity.getRollChance(this.crate);
return this.getRollChance(null);
}

@Override
public double getRollChance(@Nullable Player player) {
// A player who can't win this reward has a real chance of 0%.
if (player != null && !this.canWin(player)) return 0D;

// Use the reward pool actually available to the player so previewed odds match real odds
// (rewards locked behind permissions are excluded from the roll, raising everyone else's chance).
double sum = this.crate.getRewards(player, this.rarity).stream().mapToDouble(Reward::getWeight).sum();
if (sum <= 0D) return 0D;

double rarityChance = this.rarity.getRollChance(this.crate, player);
double chance = (this.weight / sum) * (rarityChance / 100D);

return chance * 100D;
Expand Down