From a6639b358f2c30c1fd3cee741dc5cfc6b0c232fc Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Thu, 3 Mar 2022 18:55:51 +1100 Subject: [PATCH] #876: Add CreativeCategory API for Materials By: Parker Hawke --- .../inventory/CraftCreativeCategory.java | 30 +++++++++++++++++++ .../craftbukkit/util/CraftMagicNumbers.java | 9 ++++++ 2 files changed, 39 insertions(+) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftCreativeCategory.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftCreativeCategory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftCreativeCategory.java new file mode 100644 index 0000000000..9d32bc4da0 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftCreativeCategory.java @@ -0,0 +1,30 @@ +package org.bukkit.craftbukkit.inventory; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import net.minecraft.world.item.CreativeModeTab; +import org.bukkit.inventory.CreativeCategory; + +public final class CraftCreativeCategory { + + private static final Map NMS_TO_BUKKIT = ImmutableMap.builder() + .put(CreativeModeTab.TAB_BUILDING_BLOCKS, CreativeCategory.BUILDING_BLOCKS) + .put(CreativeModeTab.TAB_DECORATIONS, CreativeCategory.DECORATIONS) + .put(CreativeModeTab.TAB_REDSTONE, CreativeCategory.REDSTONE) + .put(CreativeModeTab.TAB_TRANSPORTATION, CreativeCategory.TRANSPORTATION) + .put(CreativeModeTab.TAB_MISC, CreativeCategory.MISC) // Interchangeable in NMS + .put(CreativeModeTab.TAB_MATERIALS, CreativeCategory.MISC) // Interchangeable in NMS + .put(CreativeModeTab.TAB_FOOD, CreativeCategory.FOOD) + .put(CreativeModeTab.TAB_TOOLS, CreativeCategory.TOOLS) + .put(CreativeModeTab.TAB_COMBAT, CreativeCategory.COMBAT) + .put(CreativeModeTab.TAB_BREWING, CreativeCategory.BREWING) + .build(); + + public static CreativeCategory fromNMS(CreativeModeTab tab) { + if (!NMS_TO_BUKKIT.containsKey(tab)) { + throw new UnsupportedOperationException("Item is not present in any known CreativeModeTab. This is a bug."); + } + + return (tab != null) ? NMS_TO_BUKKIT.get(tab) : null; + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java index 3de9ccf84d..24f201883d 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -36,6 +36,7 @@ import net.minecraft.util.ChatDeserializer; import net.minecraft.util.datafix.DataConverterRegistry; import net.minecraft.util.datafix.fixes.DataConverterTypes; import net.minecraft.world.entity.ai.attributes.AttributeBase; +import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.IBlockData; @@ -55,8 +56,10 @@ import org.bukkit.craftbukkit.CraftEquipmentSlot; import org.bukkit.craftbukkit.attribute.CraftAttributeInstance; import org.bukkit.craftbukkit.attribute.CraftAttributeMap; import org.bukkit.craftbukkit.block.data.CraftBlockData; +import org.bukkit.craftbukkit.inventory.CraftCreativeCategory; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.legacy.CraftLegacy; +import org.bukkit.inventory.CreativeCategory; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; @@ -355,6 +358,12 @@ public final class CraftMagicNumbers implements UnsafeValues { return defaultAttributes.build(); } + @Override + public CreativeCategory getCreativeCategory(Material material) { + CreativeModeTab category = getItem(material).getItemCategory(); + return CraftCreativeCategory.fromNMS(category); + } + /** * This helper class represents the different NBT Tags. *