9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00

Backport Paper: Check type of Material in get/set stats

Co-Authored-By: hayanesuru <68378576+hayanesuru@users.noreply.github.com>
This commit is contained in:
Dreeam
2025-06-26 05:23:09 +08:00
parent 577a47bf47
commit 7b26b73702
3 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Pedro <3602279+Doc94@users.noreply.github.com>
Date: Fri, 20 Jun 2025 10:45:40 -0400
Subject: [PATCH] Paper: Check type of Material in get/set stats
Original license: GPLv3
Original project: https://github.com/PaperMC/Paper
https://github.com/PaperMC/Paper/commit/74fbcce5aefccddae474614ddfce50f3b9de769d
This PR fix #12606 where the Craft class for stats use material and
just ignore the type of material then i just make use of the ItemType/BlockType where
necesary and make the valid checks for avoid pass a block material for set stats
where its only items are valid.
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftStatistic.java b/src/main/java/org/bukkit/craftbukkit/CraftStatistic.java
index 5ce9304f7c4bb44636b8443d3def1b237bee75a3..0e33b8c6c68d4a1cc46a9619747d447e90eea5be 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftStatistic.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftStatistic.java
@@ -150,31 +150,43 @@ public enum CraftStatistic {
return nms;
}
- public static net.minecraft.stats.Stat getMaterialStatistic(org.bukkit.Statistic stat, Material material) {
+ // Paper start - Check type of Material in get/set stats
+ private static net.minecraft.stats.Stat getBlockTypeStatistic(org.bukkit.Statistic stat, org.bukkit.block.BlockType blockType) {
+ Preconditions.checkArgument(blockType != null, "BlockType cannot be null");
try {
if (stat == Statistic.MINE_BLOCK) {
- return Stats.BLOCK_MINED.get(CraftBlockType.bukkitToMinecraft(material));
+ return Stats.BLOCK_MINED.get(CraftBlockType.bukkitToMinecraftNew(blockType));
}
+ } catch (ArrayIndexOutOfBoundsException e) {
+ return null;
+ }
+ return null;
+ }
+
+ private static net.minecraft.stats.Stat getItemTypeStatistic(org.bukkit.Statistic stat, org.bukkit.inventory.ItemType itemType) {
+ Preconditions.checkArgument(itemType != null, "ItemType cannot be null");
+ try {
if (stat == Statistic.CRAFT_ITEM) {
- return Stats.ITEM_CRAFTED.get(CraftItemType.bukkitToMinecraft(material));
+ return Stats.ITEM_CRAFTED.get(CraftItemType.bukkitToMinecraftNew(itemType));
}
if (stat == Statistic.USE_ITEM) {
- return Stats.ITEM_USED.get(CraftItemType.bukkitToMinecraft(material));
+ return Stats.ITEM_USED.get(CraftItemType.bukkitToMinecraftNew(itemType));
}
if (stat == Statistic.BREAK_ITEM) {
- return Stats.ITEM_BROKEN.get(CraftItemType.bukkitToMinecraft(material));
+ return Stats.ITEM_BROKEN.get(CraftItemType.bukkitToMinecraftNew(itemType));
}
if (stat == Statistic.PICKUP) {
- return Stats.ITEM_PICKED_UP.get(CraftItemType.bukkitToMinecraft(material));
+ return Stats.ITEM_PICKED_UP.get(CraftItemType.bukkitToMinecraftNew(itemType));
}
if (stat == Statistic.DROP) {
- return Stats.ITEM_DROPPED.get(CraftItemType.bukkitToMinecraft(material));
+ return Stats.ITEM_DROPPED.get(CraftItemType.bukkitToMinecraftNew(itemType));
}
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
return null;
}
+ // Paper end - Check type of Material in get/set stats
public static net.minecraft.stats.Stat getEntityStatistic(org.bukkit.Statistic stat, EntityType entity) {
Preconditions.checkArgument(entity != null, "EntityType cannot be null");
@@ -256,8 +268,20 @@ public enum CraftStatistic {
public static int getStatistic(ServerStatsCounter manager, Statistic statistic, Material material) {
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.Stat nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
+ // Paper start - Check type of Material in get/set stats
+ net.minecraft.stats.Stat nmsStatistic;
+ if (statistic.getType() == Type.BLOCK) {
+ Preconditions.checkArgument(material.isBlock(), "Material %s must be a block", material);
+ org.bukkit.block.BlockType blockType = material.asBlockType();
+ nmsStatistic = CraftStatistic.getBlockTypeStatistic(statistic, blockType);
+ } else if (statistic.getType() == Type.ITEM) {
+ Preconditions.checkArgument(material.isItem(), "Material %s must be an item", material);
+ org.bukkit.inventory.ItemType itemType = material.asItemType();
+ nmsStatistic = CraftStatistic.getItemTypeStatistic(statistic, itemType);
+ } else {
+ throw new IllegalArgumentException("This statistic does not take a Material parameter");
+ }
+ // Paper end - Check type of Material in get/set stats
Preconditions.checkArgument(nmsStatistic != null, "The supplied Material %s does not have a corresponding statistic", material);
return manager.getValue(nmsStatistic);
}
@@ -276,8 +300,20 @@ public enum CraftStatistic {
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.Stat nmsStatistic = CraftStatistic.getMaterialStatistic(statistic, material);
+ // Paper start - Check type of Material in get/set stats
+ net.minecraft.stats.Stat nmsStatistic;
+ if (statistic.getType() == Type.BLOCK) {
+ Preconditions.checkArgument(material.isBlock(), "Material %s must be a block", material);
+ org.bukkit.block.BlockType blockType = material.asBlockType();
+ nmsStatistic = CraftStatistic.getBlockTypeStatistic(statistic, blockType);
+ } else if (statistic.getType() == Type.ITEM) {
+ Preconditions.checkArgument(material.isItem(), "Material %s must be an item", material);
+ org.bukkit.inventory.ItemType itemType = material.asItemType();
+ nmsStatistic = CraftStatistic.getItemTypeStatistic(statistic, itemType);
+ } else {
+ throw new IllegalArgumentException("This statistic does not take a Material parameter");
+ }
+ // Paper end - Check type of Material in get/set stats
Preconditions.checkArgument(nmsStatistic != null, "The supplied Material %s does not have a corresponding statistic", material);
manager.setValue(null, nmsStatistic, newValue);