Skip to content

Commit 5cb864b

Browse files
committed
merge impl under PaperTrackableStats and update scoreboard for offline players
1 parent 7b5aaf7 commit 5cb864b

File tree

12 files changed

+193
-159
lines changed

12 files changed

+193
-159
lines changed

paper-server/patches/sources/net/minecraft/stats/ServerStatsCounter.java.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
+ player.getBukkitEntity(),
4343
+ map.object2IntEntrySet()
4444
+ .stream()
45-
+ .collect(Object2IntOpenHashMap::new, (map1, entry) -> map1.put(io.papermc.paper.statistic.PaperStatistics.getPaperStatistic(entry.getKey()), entry.getIntValue()), Object2IntOpenHashMap::putAll)
45+
+ .collect(Object2IntOpenHashMap::new, (map1, entry) -> map1.put(io.papermc.paper.statistic.PaperStatistic.getPaperStatistic(entry.getKey()), entry.getIntValue()), Object2IntOpenHashMap::putAll)
4646
+ );
4747
+ if (!statEvent.callEvent()) {
4848
+ return;
4949
+ }
50-
+ map = statEvent.getStatisticMap().object2IntEntrySet().stream().collect(Object2IntOpenHashMap::new, (map1, entry) -> map1.put(io.papermc.paper.statistic.PaperStatistics.getNMSStatistic(entry.getKey()), entry.getIntValue()), Object2IntOpenHashMap::putAll);
50+
+ map = statEvent.getStatisticMap().object2IntEntrySet().stream().collect(Object2IntOpenHashMap::new, (map1, entry) -> map1.put(io.papermc.paper.statistic.PaperStatistic.getNMSStatistic(entry.getKey()), entry.getIntValue()), Object2IntOpenHashMap::putAll);
5151
+ }
5252
+ // Paper end
5353

paper-server/patches/sources/net/minecraft/stats/StatsCounter.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public void increment(Player player, Stat<?> stat, int amount) {
66
int i = (int)Math.min((long)this.getValue(stat) + amount, 2147483647L);
77
+ // CraftBukkit start - fire Statistic events
8-
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callStatisticIncreaseEvent(player, stat, this.getValue(stat), i)) {
8+
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callStatisticIncrementEvent(player, stat, this.getValue(stat), i)) {
99
+ return;
1010
+ }
1111
+ // CraftBukkit end

paper-server/src/main/java/io/papermc/paper/statistic/PaperStatistic.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import org.bukkit.craftbukkit.scoreboard.CraftScoreboardTranslations;
55
import org.bukkit.scoreboard.RenderType;
66

7-
public record PaperStatistic<S, M>(Stat<M> handle, S owner, M nmsValue, StatisticType<S> type) implements Statistic<S> {
7+
public record PaperStatistic<S, M>(Stat<M> handle, S owner, StatisticType<S> type) implements Statistic<S> {
88

99
@Override
1010
public String getName() {
11-
return Stat.buildName(this.handle.getType(), this.nmsValue);
11+
return this.handle.getName();
1212
}
1313

1414
@Override
@@ -20,4 +20,12 @@ public boolean isReadOnly() {
2020
public RenderType getDefaultRenderType() {
2121
return CraftScoreboardTranslations.toBukkitRender(this.handle.getDefaultRenderType());
2222
}
23+
24+
public static <M> Statistic<?> getPaperStatistic(final Stat<M> stat) {
25+
return PaperStatisticType.minecraftToBukkit(stat.getType()).convertStat(stat);
26+
}
27+
28+
public static Stat<?> getNMSStatistic(final Statistic<?> statistic) {
29+
return ((PaperStatistic<?, ?>) statistic).handle();
30+
}
2331
}

paper-server/src/main/java/io/papermc/paper/statistic/PaperStatisticType.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class PaperStatisticType<S extends Keyed, M> extends HolderableBase<StatT
3232
private final BiFunction<M, ResourceKey<? extends Registry<M>>, S> minecraftToBukkit;
3333

3434
private PaperStatisticType(final Holder<StatType<M>> holder) {
35-
this(holder, s -> true, CraftRegistry::bukkitToMinecraft, CraftRegistry::minecraftToBukkit);
35+
this(holder, $ -> true, CraftRegistry::bukkitToMinecraft, CraftRegistry::minecraftToBukkit);
3636
}
3737

3838
private PaperStatisticType(final Holder<StatType<M>> holder, final Predicate<S> typeCheck, final Function<S, M> bukkitToMinecraft, final BiFunction<M, ResourceKey<? extends Registry<M>>, S> minecraftToBukkit) {
@@ -52,7 +52,7 @@ public static <M> StatisticType<?> create(final Holder<?> holder) {
5252
(Holder<StatType<net.minecraft.world.entity.EntityType<?>>>) holder,
5353
t -> t != EntityType.UNKNOWN,
5454
CraftEntityType::bukkitToMinecraft,
55-
(entityType, ignored) -> CraftEntityType.minecraftToBukkit(entityType)
55+
(entityType, $) -> CraftEntityType.minecraftToBukkit(entityType)
5656
);
5757
} else {
5858
return new PaperStatisticType<>((Holder<StatType<M>>) holder);
@@ -67,22 +67,20 @@ public static <S, M> StatType<M> bukkitToMinecraft(final StatisticType<S> bukkit
6767
return CraftRegistry.bukkitToMinecraft(bukkit);
6868
}
6969

70-
public Statistic<S> convertStat(final Stat<? extends M> nmsStat) {
71-
return this.forValue(this.minecraftToBukkit.apply(nmsStat.getValue(), this.getHandle().getRegistry().key()));
70+
public Statistic<S> convertStat(final Stat<M> stat) {
71+
return this.forValue(this.minecraftToBukkit.apply(stat.getValue(), this.getHandle().getRegistry().key()));
7272
}
7373

7474
@Override
7575
public Statistic<S> forValue(final S value) {
7676
if (!this.typeCheck.test(value)) {
7777
throw new IllegalArgumentException(value + " is not valid for stat type " + this.getKey());
7878
}
79-
return this.statCacheMap.computeIfAbsent(
80-
value, newValue -> {
81-
final M nmsValue = this.bukkitToMinecraft.apply(newValue);
82-
final Stat<M> nmsStat = this.getHandle().get(nmsValue);
83-
return new PaperStatistic<>(nmsStat, newValue, nmsValue, this);
84-
}
85-
);
79+
return this.statCacheMap.computeIfAbsent(value, newValue -> {
80+
final M nmsValue = this.bukkitToMinecraft.apply(newValue);
81+
final Stat<M> stat = this.getHandle().get(nmsValue);
82+
return new PaperStatistic<>(stat, newValue, this);
83+
});
8684
}
8785

8886
@Override

paper-server/src/main/java/io/papermc/paper/statistic/PaperStatistics.java

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)