#1182: Consolidate Preconditions use and minor cleanup
By: Doc <nachito94@msn.com>
This commit is contained in:
@@ -11,7 +11,6 @@ import net.minecraft.stats.StatisticList;
|
||||
import net.minecraft.world.entity.EntityTypes;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.Statistic.Type;
|
||||
@@ -123,6 +122,7 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
public static org.bukkit.Statistic getBukkitStatistic(net.minecraft.stats.Statistic<?> statistic) {
|
||||
Preconditions.checkArgument(statistic != null, "NMS Statistic cannot be null");
|
||||
IRegistry statRegistry = statistic.getType().getRegistry();
|
||||
MinecraftKey nmsKey = BuiltInRegistries.STAT_TYPE.getKey(statistic.getType());
|
||||
|
||||
@@ -169,6 +169,7 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
public static net.minecraft.stats.Statistic getEntityStatistic(org.bukkit.Statistic stat, EntityType entity) {
|
||||
Preconditions.checkArgument(entity != null, "EntityType cannot be null");
|
||||
if (entity.getName() != null) {
|
||||
EntityTypes<?> nmsEntity = BuiltInRegistries.ENTITY_TYPE.get(new MinecraftKey(entity.getName()));
|
||||
|
||||
@@ -183,16 +184,17 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
public static EntityType getEntityTypeFromStatistic(net.minecraft.stats.Statistic<EntityTypes<?>> statistic) {
|
||||
Preconditions.checkArgument(statistic != null, "NMS Statistic cannot be null");
|
||||
MinecraftKey name = EntityTypes.getKey(statistic.getValue());
|
||||
return EntityType.fromName(name.getPath());
|
||||
}
|
||||
|
||||
public static Material getMaterialFromStatistic(net.minecraft.stats.Statistic<?> statistic) {
|
||||
if (statistic.getValue() instanceof Item) {
|
||||
return CraftMagicNumbers.getMaterial((Item) statistic.getValue());
|
||||
if (statistic.getValue() instanceof Item statisticItemValue) {
|
||||
return CraftMagicNumbers.getMaterial(statisticItemValue);
|
||||
}
|
||||
if (statistic.getValue() instanceof Block) {
|
||||
return CraftMagicNumbers.getMaterial((Block) statistic.getValue());
|
||||
if (statistic.getValue() instanceof Block statisticBlockValue) {
|
||||
return CraftMagicNumbers.getMaterial(statisticBlockValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -206,25 +208,25 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.UNTYPED, "Must supply additional parameter for this statistic");
|
||||
return manager.getValue(CraftStatistic.getNMSStatistic(statistic));
|
||||
}
|
||||
|
||||
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, int amount) {
|
||||
Validate.isTrue(amount > 0, "Amount must be greater than 0");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.UNTYPED, "Must supply additional parameter for this statistic");
|
||||
Preconditions.checkArgument(newValue >= 0, "Value must be greater than or equal to 0");
|
||||
net.minecraft.stats.Statistic nmsStatistic = CraftStatistic.getNMSStatistic(statistic);
|
||||
manager.setValue(null, nmsStatistic, newValue);;
|
||||
}
|
||||
@@ -238,31 +240,31 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(material != null, "Material cannot be null");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
|
||||
net.minecraft.stats.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
|
||||
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
|
||||
Preconditions.checkArgument(nmsStatistic != null, "The supplied Material %s does not have a corresponding statistic", material);
|
||||
return manager.getValue(nmsStatistic);
|
||||
}
|
||||
|
||||
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, Material material, int amount) {
|
||||
Validate.isTrue(amount > 0, "Amount must be greater than 0");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(material != null, "Material cannot be null");
|
||||
Preconditions.checkArgument(newValue >= 0, "Value must be greater than or equal to 0");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.BLOCK || statistic.getType() == Type.ITEM, "This statistic does not take a Material parameter");
|
||||
net.minecraft.stats.Statistic nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
|
||||
Validate.notNull(nmsStatistic, "The supplied Material does not have a corresponding statistic");
|
||||
Preconditions.checkArgument(nmsStatistic != null, "The supplied Material %s does not have a corresponding statistic", material);
|
||||
manager.setValue(null, nmsStatistic, newValue);
|
||||
}
|
||||
|
||||
@@ -275,31 +277,31 @@ public enum CraftStatistic {
|
||||
}
|
||||
|
||||
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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(entityType != null, "EntityType cannot be null");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
|
||||
net.minecraft.stats.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
|
||||
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
|
||||
Preconditions.checkArgument(nmsStatistic != null, "The supplied EntityType %s does not have a corresponding statistic", entityType);
|
||||
return manager.getValue(nmsStatistic);
|
||||
}
|
||||
|
||||
public static void incrementStatistic(ServerStatisticManager manager, Statistic statistic, EntityType entityType, int amount) {
|
||||
Validate.isTrue(amount > 0, "Amount must be greater than 0");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(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");
|
||||
Preconditions.checkArgument(statistic != null, "Statistic cannot be null");
|
||||
Preconditions.checkArgument(entityType != null, "EntityType cannot be null");
|
||||
Preconditions.checkArgument(newValue >= 0, "Value must be greater than or equal to 0");
|
||||
Preconditions.checkArgument(statistic.getType() == Type.ENTITY, "This statistic does not take an EntityType parameter");
|
||||
net.minecraft.stats.Statistic nmsStatistic = CraftStatistic.getEntityStatistic(statistic, entityType);
|
||||
Validate.notNull(nmsStatistic, "The supplied EntityType does not have a corresponding statistic");
|
||||
Preconditions.checkArgument(nmsStatistic != null, "The supplied EntityType %s does not have a corresponding statistic", entityType);
|
||||
manager.setValue(null, nmsStatistic, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user