Bukkit插件开发求助:修改myInventory内物品自定义名称
Hey there! I totally get the frustration—having those bland default material names like IRON_BLOCK showing up in your GUI instead of clear, user-friendly labels isn’t ideal. Let’s get this sorted out quickly.
The root issue here is that you’re only creating basic ItemStack objects with the material type, but you haven’t set any custom metadata (specifically the display name) for each item. Bukkit uses ItemMeta to store this kind of custom information for items.
Here’s how to modify your code to add custom, readable names to each GUI item:
import org.bukkit.inventory.meta.ItemMeta; import java.util.Arrays; public static Inventory myInventory = Bukkit.createInventory(null, 9, "GamemodeGUI"); static { // Survival Mode Item ItemStack survivalItem = new ItemStack(Material.IRON_BLOCK, 1); ItemMeta survivalMeta = survivalItem.getItemMeta(); survivalMeta.setDisplayName("§aSurvival Mode"); // Green colored name survivalMeta.setLore(Arrays.asList("§7Click to switch to Survival mode")); // Optional descriptive lore survivalItem.setItemMeta(survivalMeta); myInventory.setItem(0, survivalItem); // Creative Mode Item ItemStack creativeItem = new ItemStack(Material.DIAMOND_BLOCK, 1); ItemMeta creativeMeta = creativeItem.getItemMeta(); creativeMeta.setDisplayName("§bCreative Mode"); // Light blue colored name creativeMeta.setLore(Arrays.asList("§7Click to switch to Creative mode")); creativeItem.setItemMeta(creativeMeta); myInventory.setItem(1, creativeItem); // Adventure Mode Item ItemStack adventureItem = new ItemStack(Material.GOLD_BLOCK, 1); ItemMeta adventureMeta = adventureItem.getItemMeta(); adventureMeta.setDisplayName("§6Adventure Mode"); // Gold colored name adventureMeta.setLore(Arrays.asList("§7Click to switch to Adventure mode")); adventureItem.setItemMeta(adventureMeta); myInventory.setItem(2, adventureItem); // Spectator Mode Item ItemStack spectatorItem = new ItemStack(Material.LAPIS_BLOCK, 1); ItemMeta spectatorMeta = spectatorItem.getItemMeta(); spectatorMeta.setDisplayName("§9Spectator Mode"); // Blue colored name spectatorMeta.setLore(Arrays.asList("§7Click to switch to Spectator mode")); spectatorItem.setItemMeta(spectatorMeta); myInventory.setItem(3, spectatorItem); // Cancel Item ItemStack cancelItem = new ItemStack(Material.STAINED_GLASS, 1); ItemMeta cancelMeta = cancelItem.getItemMeta(); cancelMeta.setDisplayName("§cCancel"); // Red colored name cancelMeta.setLore(Arrays.asList("§7Close the gamemode selector")); cancelItem.setItemMeta(cancelMeta); myInventory.setItem(8, cancelItem); }
Key Changes Explained:
- ItemMeta Usage: For each
ItemStack, we retrieve its metadata withgetItemMeta(), modify the display name (and optional lore), then apply the changes back withsetItemMeta(). - Color Codes: The
§character followed by a letter (like§afor green) adds color to the text, making your GUI look more polished and intuitive. - Lore (Optional): Adding a lore line gives users extra context about what each item does, which is a nice touch for usability.
Just make sure you have the necessary imports at the top of your class (I included the ones you’ll need in the code snippet above). Once you update your code, the items in your GUI should show the custom names instead of the default material identifiers.
内容的提问来源于stack exchange,提问作者coolio85

