Add item utility functions for Minecraft icons and formatting

This commit is contained in:
MayaTheShy
2026-03-21 16:43:37 -04:00
parent 7e95cf64c6
commit cf83e9ef35

View File

@@ -0,0 +1,128 @@
/**
* Minecraft item icon utility
* Uses mc-heads.net item renders for actual Minecraft item icons
*/
// Format an item name for display
export function formatItemName(name) {
if (!name) return '';
return name
.replace(/^[a-z0-9_]+:/, '') // Strip mod prefix
.replace(/_/g, ' ')
.replace(/\b\w/g, (c) => c.toUpperCase());
}
// Get icon URL for a Minecraft item
// Uses mc-heads.net which provides 2D item renders
export function getItemIconUrl(itemName, size = 32) {
if (!itemName) return null;
// mc-heads.net format: https://mc-heads.net/item/{item_name}
// Alternatively we can use crafatar or other MC icon APIs
// For items, we use a simple mapping approach
return `https://mc-heads.net/item/${itemName}/${size}`;
}
// Fallback emoji mapping for common item categories
const CATEGORY_EMOJI = {
ingot: '🪙',
ore: '⛏️',
raw: '🪨',
log: '🪵',
planks: '🪵',
wool: '🧶',
dye: '🎨',
seed: '🌱',
crop: '🌾',
wheat: '🌾',
food: '🍖',
cooked: '🍗',
beef: '🥩',
porkchop: '🥩',
chicken: '🍗',
mutton: '🥩',
cod: '🐟',
salmon: '🐟',
potato: '🥔',
carrot: '🥕',
apple: '🍎',
bread: '🍞',
sword: '⚔️',
pickaxe: '⛏️',
axe: '🪓',
shovel: '🪏',
hoe: '🌿',
helmet: '🪖',
chestplate: '👕',
leggings: '👖',
boots: '👢',
bow: '🏹',
arrow: '🏹',
shield: '🛡️',
diamond: '💎',
emerald: '💚',
redstone: '🔴',
lapis: '🔵',
coal: '⚫',
iron: '⬜',
gold: '🟡',
netherite: '⬛',
stone: '🪨',
cobblestone: '🪨',
dirt: '🟫',
sand: '🟨',
gravel: '⬜',
glass: '🔲',
brick: '🧱',
torch: '🔦',
bone: '🦴',
string: '🧵',
leather: '🟤',
feather: '🪶',
egg: '🥚',
book: '📕',
paper: '📄',
bucket: '🪣',
potion: '🧪',
pearl: '🟣',
blaze: '🔥',
gunpowder: '💣',
sugar: '🍬',
stick: '🪵',
chest: '📦',
furnace: '🔥',
sponge: '🧽',
kelp: '🌿',
cactus: '🌵',
};
export function getItemEmoji(itemName) {
if (!itemName) return '📦';
const name = itemName.replace(/^[a-z0-9_]+:/, '').toLowerCase();
// Check direct matches
for (const [key, emoji] of Object.entries(CATEGORY_EMOJI)) {
if (name.includes(key)) return emoji;
}
return '📦';
}
// Compact number formatting
export function formatCount(count) {
if (count >= 1000000) return `${(count / 1000000).toFixed(1)}M`;
if (count >= 1000) return `${(count / 1000).toFixed(1)}K`;
return count.toString();
}
// Get rarity color based on item name
export function getRarityColor(itemName) {
if (!itemName) return '#ffffff';
const name = itemName.toLowerCase();
if (name.includes('netherite')) return '#4a2a4a';
if (name.includes('diamond')) return '#4aedd9';
if (name.includes('emerald')) return '#17dd62';
if (name.includes('gold') || name.includes('golden')) return '#ffaa00';
if (name.includes('iron')) return '#d8d8d8';
if (name.includes('enchanted') || name.includes('nether_star')) return '#ff55ff';
return '#ffffff';
}