SPIGOT-8034: Validate API-set statistic types are correct

This commit is contained in:
md_5 2025-04-13 10:05:35 +10:00
parent a6012a1e0d
commit f6c49052a8
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -146,29 +146,40 @@ public enum CraftStatistic {
} }
public static net.minecraft.stats.Statistic getMaterialStatistic(org.bukkit.Statistic stat, Material material) { public static net.minecraft.stats.Statistic getMaterialStatistic(org.bukkit.Statistic stat, Material material) {
try { Type type = stat.getType();
if (stat == Statistic.MINE_BLOCK) {
return StatisticList.BLOCK_MINED.get(CraftBlockType.bukkitToMinecraft(material)); switch (type) {
} case BLOCK:
if (stat == Statistic.CRAFT_ITEM) { Preconditions.checkArgument(material.isBlock(), "statistic type is BLOCK but got non-block Material, %s", material);
return StatisticList.ITEM_CRAFTED.get(CraftItemType.bukkitToMinecraft(material));
} if (stat == Statistic.MINE_BLOCK) {
if (stat == Statistic.USE_ITEM) { return StatisticList.BLOCK_MINED.get(CraftBlockType.bukkitToMinecraft(material));
return StatisticList.ITEM_USED.get(CraftItemType.bukkitToMinecraft(material)); }
} break;
if (stat == Statistic.BREAK_ITEM) { case ITEM:
return StatisticList.ITEM_BROKEN.get(CraftItemType.bukkitToMinecraft(material)); Preconditions.checkArgument(material.isItem(), "statistic type is ITEM but got non-item Material, %s", material);
}
if (stat == Statistic.PICKUP) { if (stat == Statistic.CRAFT_ITEM) {
return StatisticList.ITEM_PICKED_UP.get(CraftItemType.bukkitToMinecraft(material)); return StatisticList.ITEM_CRAFTED.get(CraftItemType.bukkitToMinecraft(material));
} }
if (stat == Statistic.DROP) { if (stat == Statistic.USE_ITEM) {
return StatisticList.ITEM_DROPPED.get(CraftItemType.bukkitToMinecraft(material)); return StatisticList.ITEM_USED.get(CraftItemType.bukkitToMinecraft(material));
} }
} catch (ArrayIndexOutOfBoundsException e) { if (stat == Statistic.BREAK_ITEM) {
return null; return StatisticList.ITEM_BROKEN.get(CraftItemType.bukkitToMinecraft(material));
}
if (stat == Statistic.PICKUP) {
return StatisticList.ITEM_PICKED_UP.get(CraftItemType.bukkitToMinecraft(material));
}
if (stat == Statistic.DROP) {
return StatisticList.ITEM_DROPPED.get(CraftItemType.bukkitToMinecraft(material));
}
break;
default:
throw new IllegalArgumentException("statistic type must be either BLOCK or ITEM, given " + type);
} }
return null;
throw new IllegalArgumentException("Unknwon material statistic " + stat);
} }
public static net.minecraft.stats.Statistic getEntityStatistic(org.bukkit.Statistic stat, EntityType entity) { public static net.minecraft.stats.Statistic getEntityStatistic(org.bukkit.Statistic stat, EntityType entity) {