#645: Allow statistics to be accessed for offline players

By: SydMontague <sydmontague@phoenix-staffel.de>
This commit is contained in:
CraftBukkit/Spigot
2020-04-05 15:58:43 +10:00
parent 96b5fcb92f
commit b7d5dc8dd0
4 changed files with 353 additions and 64 deletions

View File

@@ -8,15 +8,19 @@ import java.util.Map;
import java.util.UUID;
import net.minecraft.server.DimensionManager;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.ServerStatisticManager;
import net.minecraft.server.WhiteListEntry;
import net.minecraft.server.WorldNBTStorage;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.Statistic;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
@@ -271,4 +275,200 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
public void removeMetadata(String metadataKey, Plugin plugin) {
server.getPlayerMetadata().removeMetadata(this, metadataKey, plugin);
}
private ServerStatisticManager getStatisticManager() {
return server.getHandle().getStatisticManager(getUniqueId(), getName());
}
@Override
public void incrementStatistic(Statistic statistic) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic);
manager.a();
}
}
@Override
public int getStatistic(Statistic statistic) {
if (isOnline()) {
return getPlayer().getStatistic(statistic);
} else {
return CraftStatistic.getStatistic(getStatisticManager(), statistic);
}
}
@Override
public void incrementStatistic(Statistic statistic, int amount) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic, amount);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic, int amount) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic, amount);
manager.a();
}
}
@Override
public void setStatistic(Statistic statistic, int newValue) {
if (isOnline()) {
getPlayer().setStatistic(statistic, newValue);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.setStatistic(manager, statistic, newValue);
manager.a();
}
}
@Override
public void incrementStatistic(Statistic statistic, Material material) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic, material);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic, material);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic, Material material) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic, material);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic, material);
manager.a();
}
}
@Override
public int getStatistic(Statistic statistic, Material material) {
if (isOnline()) {
return getPlayer().getStatistic(statistic, material);
} else {
return CraftStatistic.getStatistic(getStatisticManager(), statistic, material);
}
}
@Override
public void incrementStatistic(Statistic statistic, Material material, int amount) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic, material, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic, material, amount);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic, Material material, int amount) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic, material, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic, material, amount);
manager.a();
}
}
@Override
public void setStatistic(Statistic statistic, Material material, int newValue) {
if (isOnline()) {
getPlayer().setStatistic(statistic, material, newValue);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.setStatistic(manager, statistic, material, newValue);
manager.a();
}
}
@Override
public void incrementStatistic(Statistic statistic, EntityType entityType) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic, entityType);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic, entityType);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic, EntityType entityType) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic, entityType);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic, entityType);
manager.a();
}
}
@Override
public int getStatistic(Statistic statistic, EntityType entityType) {
if (isOnline()) {
return getPlayer().getStatistic(statistic, entityType);
} else {
return CraftStatistic.getStatistic(getStatisticManager(), statistic, entityType);
}
}
@Override
public void incrementStatistic(Statistic statistic, EntityType entityType, int amount) {
if (isOnline()) {
getPlayer().incrementStatistic(statistic, entityType, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.incrementStatistic(manager, statistic, entityType, amount);
manager.a();
}
}
@Override
public void decrementStatistic(Statistic statistic, EntityType entityType, int amount) {
if (isOnline()) {
getPlayer().decrementStatistic(statistic, entityType, amount);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.decrementStatistic(manager, statistic, entityType, amount);
manager.a();
}
}
@Override
public void setStatistic(Statistic statistic, EntityType entityType, int newValue) {
if (isOnline()) {
getPlayer().setStatistic(statistic, entityType, newValue);
} else {
ServerStatisticManager manager = getStatisticManager();
CraftStatistic.setStatistic(manager, statistic, entityType, newValue);
manager.a();
}
}
}

View File

@@ -8,9 +8,12 @@ import net.minecraft.server.EntityTypes;
import net.minecraft.server.IRegistry;
import net.minecraft.server.Item;
import net.minecraft.server.MinecraftKey;
import net.minecraft.server.ServerStatisticManager;
import net.minecraft.server.StatisticList;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.Statistic.Type;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.EntityType;
@@ -188,4 +191,110 @@ public enum CraftStatistic {
}
return null;
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic) {
incrementStatistic(manager, statistic, 1);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic) {
decrementStatistic(manager, statistic, 1);
}
public static int getStatistic(ServerStatisticManager manager, Statistic statistic) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.isTrue(statistic.getType() == Type.UNTYPED, "Must supply additional paramater for this statistic");
return manager.getStatisticValue(CraftStatistic.getNMSStatistic(statistic));
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, getStatistic(manager, statistic) + amount);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, getStatistic(manager, statistic) - amount);
}
public static void setStatistic(ServerStatisticManager manager, Statistic statistic, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.isTrue(statistic.getType() == Type.UNTYPED, "Must supply additional paramater for this statistic");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getNMSStatistic(statistic);
manager.setStatistic(null, nmsStatistic, newValue);;
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, Material material) {
incrementStatistic(manager, statistic, material, 1);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic, Material material) {
decrementStatistic(manager, statistic, material, 1);
}
public static int getStatistic(ServerStatisticManager manager, Statistic statistic, Material material) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
return manager.getStatisticValue(nmsStatistic);
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, Material material, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, material, getStatistic(manager, statistic, material) + amount);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic, Material material, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, material, getStatistic(manager, statistic, material) - amount);
}
public static void setStatistic(ServerStatisticManager manager, Statistic statistic, Material material, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
Validate.isTrue(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
manager.setStatistic(null, nmsStatistic, newValue);
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType) {
incrementStatistic(manager, statistic, entityType, 1);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType) {
decrementStatistic(manager, statistic, entityType, 1);
}
public static int getStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(entityType, "EntityType cannot be null");
Validate.isTrue(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
return manager.getStatisticValue(nmsStatistic);
}
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, entityType, getStatistic(manager, statistic, entityType) + amount);
}
public static void decrementStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(manager, statistic, entityType, getStatistic(manager, statistic, entityType) - amount);
}
public static void setStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(entityType, "EntityType cannot be null");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
Validate.isTrue(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
manager.setStatistic(null, nmsStatistic, newValue);
}
}

View File

@@ -80,7 +80,6 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.Statistic;
import org.bukkit.Statistic.Type;
import org.bukkit.WeatherType;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.serialization.DelegateDeserialization;
@@ -720,126 +719,92 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@Override
public void incrementStatistic(Statistic statistic) {
incrementStatistic(statistic, 1);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic);
}
@Override
public void decrementStatistic(Statistic statistic) {
decrementStatistic(statistic, 1);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic);
}
@Override
public int getStatistic(Statistic statistic) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.isTrue(statistic.getType() == Type.UNTYPED, "Must supply additional paramater for this statistic");
return getHandle().getStatisticManager().getStatisticValue(CraftStatistic.getNMSStatistic(statistic));
return CraftStatistic.getStatistic(getHandle().getStatisticManager(), statistic);
}
@Override
public void incrementStatistic(Statistic statistic, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, getStatistic(statistic) + amount);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic, amount);
}
@Override
public void decrementStatistic(Statistic statistic, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, getStatistic(statistic) - amount);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic, amount);
}
@Override
public void setStatistic(Statistic statistic, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.isTrue(statistic.getType() == Type.UNTYPED, "Must supply additional paramater for this statistic");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getNMSStatistic(statistic);
getHandle().getStatisticManager().setStatistic(getHandle(), nmsStatistic, newValue);
CraftStatistic.setStatistic(getHandle().getStatisticManager(), statistic, newValue);
}
@Override
public void incrementStatistic(Statistic statistic, Material material) {
incrementStatistic(statistic, material, 1);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic, material);
}
@Override
public void decrementStatistic(Statistic statistic, Material material) {
decrementStatistic(statistic, material, 1);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic, material);
}
@Override
public int getStatistic(Statistic statistic, Material material) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
return getHandle().getStatisticManager().getStatisticValue(nmsStatistic);
return CraftStatistic.getStatistic(getHandle().getStatisticManager(), statistic, material);
}
@Override
public void incrementStatistic(Statistic statistic, Material material, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, material, getStatistic(statistic, material) + amount);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic, material, amount);
}
@Override
public void decrementStatistic(Statistic statistic, Material material, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, material, getStatistic(statistic, material) - amount);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic, material, amount);
}
@Override
public void setStatistic(Statistic statistic, Material material, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
Validate.isTrue(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
getHandle().getStatisticManager().setStatistic(getHandle(), nmsStatistic, newValue);
CraftStatistic.setStatistic(getHandle().getStatisticManager(), statistic, material, newValue);
}
@Override
public void incrementStatistic(Statistic statistic, EntityType entityType) {
incrementStatistic(statistic, entityType, 1);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic, entityType);
}
@Override
public void decrementStatistic(Statistic statistic, EntityType entityType) {
decrementStatistic(statistic, entityType, 1);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic, entityType);
}
@Override
public int getStatistic(Statistic statistic, EntityType entityType) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(entityType, "EntityType cannot be null");
Validate.isTrue(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
return getHandle().getStatisticManager().getStatisticValue(nmsStatistic);
return CraftStatistic.getStatistic(getHandle().getStatisticManager(), statistic, entityType);
}
@Override
public void incrementStatistic(Statistic statistic, EntityType entityType, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, entityType, getStatistic(statistic, entityType) + amount);
CraftStatistic.incrementStatistic(getHandle().getStatisticManager(), statistic, entityType, amount);
}
@Override
public void decrementStatistic(Statistic statistic, EntityType entityType, int amount) {
Validate.isTrue(amount > 0, "Amount must be greater than 0");
setStatistic(statistic, entityType, getStatistic(statistic, entityType) - amount);
CraftStatistic.decrementStatistic(getHandle().getStatisticManager(), statistic, entityType, amount);
}
@Override
public void setStatistic(Statistic statistic, EntityType entityType, int newValue) {
Validate.notNull(statistic, "Statistic cannot be null");
Validate.notNull(entityType, "EntityType cannot be null");
Validate.isTrue(newValue >= 0, "Value must be greater than or equal to 0");
Validate.isTrue(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
net.minecraft.server.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
getHandle().getStatisticManager().setStatistic(getHandle(), nmsStatistic, newValue);
CraftStatistic.setStatistic(getHandle().getStatisticManager(), statistic, entityType, newValue);
}
@Override