Skip to content

PlayerStatisticChangeEvent

The PlayerStatisticChangeEvent is triggered whenever a player’s persistent statistic is about to be updated. This event is fired before the new value is stored, allowing developers to intercept, modify, or cancel the change.


  • Package: dev.despical.tntrun.api.event.player
  • Parent Class: PlayerEvent
  • Cancellable: Yes

In addition to getPlayer() and getUser() from the base class, this event provides:

MethodReturn TypeDescription
getStat()StatisticType<T>Returns the type of statistic being modified.
getOldValue()TReturns the value of the statistic before this change.
getNewValue()TReturns the proposed new value.
setNewValue(T)voidOverrides the final value to be saved.
isCancelled()booleanChecks if the statistic update has been blocked.
setCancelled(boolean)voidIf set to true, the statistic update is completely ignored.

You can check for specific permissions to reward certain players with double progress.

import dev.despical.tntrun.api.event.player.PlayerStatisticChangeEvent;
import dev.despical.tntrun.stats.Statistics;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class StatisticBoosterListener implements Listener {
@EventHandler
public void onStatChange(PlayerStatisticChangeEvent<Integer> event) {
if (event.getStat() == Statistics.WIN && event.getPlayer().hasPermission("vip.booster")) {
int currentGain = event.getNewValue() - event.getOldValue();
event.setNewValue(event.getOldValue() + (currentGain * 2));
}
}
}

Prevent a statistic from exceeding a certain limit.

@EventHandler
public void onStatCap(PlayerStatisticChangeEvent<Integer> event) {
int maxCap = 5000;
if (event.getNewValue() > maxCap) {
event.setNewValue(maxCap);
}
}

Block suspicious spikes in statistics.

@EventHandler
public void onSuspiciousStatChange(PlayerStatisticChangeEvent<Integer> event) {
int difference = event.getNewValue() - event.getOldValue();
if (difference > 100) {
event.setCancelled(true);
}
}