|
| 1 | +package io.papermc.paper.statistic; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import java.util.function.IntUnaryOperator; |
| 5 | +import net.minecraft.server.level.ServerPlayer; |
| 6 | +import net.minecraft.stats.ServerStatsCounter; |
| 7 | +import net.minecraft.stats.Stat; |
| 8 | +import net.minecraft.world.scores.ScoreHolder; |
| 9 | +import org.bukkit.Bukkit; |
| 10 | +import org.bukkit.craftbukkit.scoreboard.CraftScoreboardManager; |
| 11 | +import org.jspecify.annotations.Nullable; |
| 12 | + |
| 13 | +public interface PaperTrackableStats { |
| 14 | + |
| 15 | + ServerStatsCounter getStatCounter(); |
| 16 | + |
| 17 | + @Nullable ScoreHolder getScoreHolder(); |
| 18 | + |
| 19 | + private ServerStatsCounter getUsableCounter() { |
| 20 | + return this.getUsableCounter(this.getScoreHolder()); |
| 21 | + } |
| 22 | + |
| 23 | + private ServerStatsCounter getUsableCounter(final @Nullable ScoreHolder from) { |
| 24 | + return from instanceof ServerPlayer player ? player.getStats() : this.getStatCounter(); |
| 25 | + } |
| 26 | + |
| 27 | + default void decrementStatistic(final Statistic<?> statistic, final int amount) { |
| 28 | + Preconditions.checkArgument(statistic != null, "Statistic cannot be null"); |
| 29 | + Preconditions.checkArgument(amount > 0, "Amount must be greater than 0"); |
| 30 | + |
| 31 | + this.updateStatistic(PaperStatistic.getNMSStatistic(statistic), originalAmount -> { |
| 32 | + final int newAmount = originalAmount - amount; |
| 33 | + Preconditions.checkArgument(newAmount >= 0, "New amount must be greater than or equal to 0"); |
| 34 | + return newAmount; |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + default void incrementStatistic(final Statistic<?> statistic, final int amount) { |
| 39 | + Preconditions.checkArgument(statistic != null, "Statistic cannot be null"); |
| 40 | + Preconditions.checkArgument(amount > 0, "Amount must be greater than 0"); |
| 41 | + |
| 42 | + this.updateStatistic(PaperStatistic.getNMSStatistic(statistic), originalAmount -> (int) Math.min((long) originalAmount + amount, Integer.MAX_VALUE)); // clamp from StatsCounter |
| 43 | + } |
| 44 | + |
| 45 | + default void setStatistic(final Statistic<?> statistic, final int newAmount) { |
| 46 | + Preconditions.checkArgument(statistic != null, "Statistic cannot be null"); |
| 47 | + Preconditions.checkArgument(newAmount >= 0, "New amount must be greater than or equal to 0"); |
| 48 | + |
| 49 | + this.setStatistic(PaperStatistic.getNMSStatistic(statistic), newAmount); |
| 50 | + } |
| 51 | + |
| 52 | + private void updateStatistic(final Stat<?> stat, final IntUnaryOperator operation) { |
| 53 | + final ScoreHolder holder = this.getScoreHolder(); |
| 54 | + final ServerStatsCounter counter = this.getUsableCounter(holder); |
| 55 | + final int newAmount = operation.applyAsInt(counter.getValue(stat)); |
| 56 | + setStatistic(counter, stat, newAmount, holder); |
| 57 | + } |
| 58 | + |
| 59 | + private void setStatistic(final Stat<?> stat, final int newAmount) { |
| 60 | + final ScoreHolder holder = this.getScoreHolder(); |
| 61 | + setStatistic(this.getUsableCounter(holder), stat, newAmount, holder); |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings("ConstantConditions") |
| 65 | + private static void setStatistic(final ServerStatsCounter counter, final Stat<?> stat, final int newAmount, final @Nullable ScoreHolder holder) { |
| 66 | + final ServerPlayer player = holder instanceof ServerPlayer p ? p : null; |
| 67 | + counter.setValue(player, stat, newAmount); |
| 68 | + if (holder != null) { |
| 69 | + ((CraftScoreboardManager) Bukkit.getScoreboardManager()).forAllObjectives(stat, holder, score -> { |
| 70 | + score.set(newAmount); |
| 71 | + }); |
| 72 | + } |
| 73 | + if (player == null) { // offline |
| 74 | + counter.save(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + default int getStatistic(final Statistic<?> statistic) { |
| 79 | + Preconditions.checkArgument(statistic != null, "Statistic cannot be null"); |
| 80 | + return this.getUsableCounter().getValue(PaperStatistic.getNMSStatistic(statistic)); |
| 81 | + } |
| 82 | + |
| 83 | + default String getFormattedValue(final Statistic<?> statistic) { |
| 84 | + Preconditions.checkArgument(statistic != null, "Statistic cannot be null"); |
| 85 | + final Stat<?> stat = PaperStatistic.getNMSStatistic(statistic); |
| 86 | + return stat.format(this.getUsableCounter().getValue(stat)); |
| 87 | + } |
| 88 | +} |
0 commit comments