Skip to content

Commit 89ab2a1

Browse files
committed
Better Stats API
1 parent 354a5d5 commit 89ab2a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1990
-1091
lines changed

paper-api/src/generated/java/io/papermc/paper/registry/keys/CustomStatisticKeys.java

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.papermc.paper.registry.keys;
2+
3+
import static net.kyori.adventure.key.Key.key;
4+
5+
import io.papermc.paper.annotation.GeneratedClass;
6+
import io.papermc.paper.registry.RegistryKey;
7+
import io.papermc.paper.registry.TypedKey;
8+
import io.papermc.paper.statistic.StatisticType;
9+
import net.kyori.adventure.key.Key;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
/**
13+
* Vanilla keys for {@link RegistryKey#STAT_TYPE}.
14+
*
15+
* @apiNote The fields provided here are a direct representation of
16+
* what is available from the vanilla game source. They may be
17+
* changed (including removals) on any Minecraft version
18+
* bump, so cross-version compatibility is not provided on the
19+
* same level as it is on most of the other API.
20+
*/
21+
@SuppressWarnings({
22+
"unused",
23+
"SpellCheckingInspection"
24+
})
25+
@NullMarked
26+
@GeneratedClass
27+
public final class StatisticTypeKeys {
28+
/**
29+
* {@code minecraft:broken}
30+
*
31+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
32+
*/
33+
public static final TypedKey<StatisticType<?>> BROKEN = create(key("broken"));
34+
35+
/**
36+
* {@code minecraft:crafted}
37+
*
38+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
39+
*/
40+
public static final TypedKey<StatisticType<?>> CRAFTED = create(key("crafted"));
41+
42+
/**
43+
* {@code minecraft:custom}
44+
*
45+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
46+
*/
47+
public static final TypedKey<StatisticType<?>> CUSTOM = create(key("custom"));
48+
49+
/**
50+
* {@code minecraft:dropped}
51+
*
52+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
53+
*/
54+
public static final TypedKey<StatisticType<?>> DROPPED = create(key("dropped"));
55+
56+
/**
57+
* {@code minecraft:killed}
58+
*
59+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
60+
*/
61+
public static final TypedKey<StatisticType<?>> KILLED = create(key("killed"));
62+
63+
/**
64+
* {@code minecraft:killed_by}
65+
*
66+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
67+
*/
68+
public static final TypedKey<StatisticType<?>> KILLED_BY = create(key("killed_by"));
69+
70+
/**
71+
* {@code minecraft:mined}
72+
*
73+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
74+
*/
75+
public static final TypedKey<StatisticType<?>> MINED = create(key("mined"));
76+
77+
/**
78+
* {@code minecraft:picked_up}
79+
*
80+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
81+
*/
82+
public static final TypedKey<StatisticType<?>> PICKED_UP = create(key("picked_up"));
83+
84+
/**
85+
* {@code minecraft:used}
86+
*
87+
* @apiNote This field is version-dependant and may be removed in future Minecraft versions
88+
*/
89+
public static final TypedKey<StatisticType<?>> USED = create(key("used"));
90+
91+
private StatisticTypeKeys() {
92+
}
93+
94+
private static TypedKey<StatisticType<?>> create(final Key key) {
95+
return TypedKey.create(RegistryKey.STAT_TYPE, key);
96+
}
97+
}

paper-api/src/main/java/io/papermc/paper/InternalAPIBridge.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bukkit.damage.DamageEffect;
1515
import org.bukkit.damage.DamageSource;
1616
import org.bukkit.entity.LivingEntity;
17+
import org.bukkit.scoreboard.Criteria;
1718
import org.jetbrains.annotations.ApiStatus;
1819
import org.jetbrains.annotations.Contract;
1920
import org.jspecify.annotations.NullMarked;
@@ -100,4 +101,12 @@ class Holder {
100101
Component defaultMannequinDescription();
101102

102103
<MODERN, LEGACY> GameRule<LEGACY> legacyGameRuleBridge(GameRule<MODERN> rule, Function<LEGACY, MODERN> fromLegacyToModern, Function<MODERN, LEGACY> toLegacyFromModern, Class<LEGACY> legacyClass);
104+
105+
/**
106+
* Gets the criteria for the non-stat built-in scoreboard criteria.
107+
*
108+
* @param key the key
109+
* @return the criteria
110+
*/
111+
Criteria getCriteria(final String key);
103112
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.papermc.paper.event.player;
2+
3+
import io.papermc.paper.statistic.Statistic;
4+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.Cancellable;
7+
import org.bukkit.event.HandlerList;
8+
import org.bukkit.event.player.PlayerEvent;
9+
import org.jetbrains.annotations.ApiStatus;
10+
import org.jspecify.annotations.NullMarked;
11+
12+
/**
13+
* Called when the player requests their statistics.
14+
*/
15+
@NullMarked
16+
public class PlayerRequestStatisticsEvent extends PlayerEvent implements Cancellable {
17+
18+
private static final HandlerList HANDLER_LIST = new HandlerList();
19+
20+
private final Object2IntMap<Statistic<?>> statisticMap;
21+
private boolean cancelled;
22+
23+
@ApiStatus.Internal
24+
public PlayerRequestStatisticsEvent(final Player player, final Object2IntMap<Statistic<?>> statisticMap) {
25+
super(player);
26+
this.statisticMap = statisticMap;
27+
}
28+
29+
/**
30+
* Gets the statistic map to be sent to the player.
31+
*
32+
* @return the mutable statistic map
33+
*/
34+
public Object2IntMap<Statistic<?>> getStatisticMap() {
35+
return this.statisticMap;
36+
}
37+
38+
@Override
39+
public boolean isCancelled() {
40+
return this.cancelled;
41+
}
42+
43+
@Override
44+
public void setCancelled(final boolean cancel) {
45+
this.cancelled = cancel;
46+
}
47+
48+
@Override
49+
public HandlerList getHandlers() {
50+
return HANDLER_LIST;
51+
}
52+
53+
public static HandlerList getHandlerList() {
54+
return HANDLER_LIST;
55+
}
56+
}

paper-api/src/main/java/io/papermc/paper/registry/RegistryKey.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.papermc.paper.datacomponent.DataComponentType;
44
import io.papermc.paper.dialog.Dialog;
55
import io.papermc.paper.registry.tag.TagKey;
6+
import io.papermc.paper.statistic.CustomStatistic;
7+
import io.papermc.paper.statistic.StatisticType;
68
import net.kyori.adventure.key.Key;
79
import net.kyori.adventure.key.KeyPattern;
810
import net.kyori.adventure.key.Keyed;
@@ -132,6 +134,18 @@ public sealed interface RegistryKey<T> extends Keyed permits RegistryKeyImpl {
132134
* @see io.papermc.paper.registry.keys.GameRuleKeys
133135
*/
134136
RegistryKey<GameRule<?>> GAME_RULE = create("game_rule");
137+
/**
138+
* Built-in registry for custom statistics.
139+
* @see io.papermc.paper.registry.keys.CustomStatisticKeys
140+
*/
141+
RegistryKey<CustomStatistic> CUSTOM_STAT = create("custom_stat");
142+
/**
143+
* Built-in registry for statistic types.
144+
* @see io.papermc.paper.registry.keys.StatisticTypeKeys
145+
*/
146+
RegistryKey<StatisticType<?>> STAT_TYPE = create("stat_type");
147+
148+
135149

136150
/* ********************** *
137151
* Data-driven Registries *
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.papermc.paper.statistic;
2+
3+
import net.kyori.adventure.translation.Translatable;
4+
import org.bukkit.Keyed;
5+
import org.jetbrains.annotations.ApiStatus;
6+
7+
/**
8+
* Represents a statistic of the type {@link StatisticTypes#CUSTOM}.
9+
*
10+
* @see CustomStatistics
11+
*/
12+
@ApiStatus.NonExtendable
13+
public interface CustomStatistic extends Keyed, Translatable {
14+
15+
/**
16+
* Gets the statistic with the given custom stat.
17+
*
18+
* @return the statistic for the custom stat.
19+
*/
20+
default Statistic<CustomStatistic> stat() {
21+
return StatisticTypes.CUSTOM.forValue(this);
22+
}
23+
}

0 commit comments

Comments
 (0)