#876: Add CreativeCategory API for Materials

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot 2022-03-03 18:55:51 +11:00
parent 59e5f5af8f
commit a6639b358f
2 changed files with 39 additions and 0 deletions

View file

@ -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<CreativeModeTab, CreativeCategory> NMS_TO_BUKKIT = ImmutableMap.<CreativeModeTab, CreativeCategory>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;
}
}

View file

@ -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.
* <p>