Example output
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
public class PigGUI implements InventoryHolder {
private final Inventory inventory;
public PigGUI() {
inventory = Bukkit.createInventory(this, 27, "Pig Menu");
initializeItems();
}
@Override
public Inventory getInventory() {
return inventory;
}
// You can customize the GUI layout and items here
private void initializeItems() {
inventory.setItem(11, createGuiItem(Material.PORKCHOP, "Spawn Pigs"));
inventory.setItem(15, createGuiItem(Material.BARRIER, "Close"));
}
protected void onClick(Player player, int slot) {
if (slot == 11) {
player.performCommand("summon pig");
} else if (slot == 15) {
player.closeInventory();
}
}
private ItemStack createGuiItem(final Material material, final String name) {
final ItemStack item = new ItemStack(material, 1);
final var meta = item.getItemMeta();
meta.setDisplayName(name);
item.setItemMeta(meta);
return item;
}
@EventHandler
public void onInventoryClick(final InventoryClickEvent event) {
if (event.getInventory().getHolder() != this) {
return;
}
event.setCancelled(true);
final var clicked = event.getCurrentItem();
if (clicked == null || clicked.getType() == Material.AIR) {
return;
}
onClick((Player) event.getWhoClicked(), event.getRawSlot());
}
}
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class PiggiesCommand implements CommandExecutor {
private final JavaPlugin plugin;
public PiggiesCommand(JavaPlugin plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
return false;
}
final var player = (Player) sender;
final var pigGUI = new PigGUI();
player.openInventory(pigGUI.getInventory());
return true;
}
}
YML:
name: PigPlugin
version: 1.0
main: com.example.PigPlugin
commands:
piggies:
description: Summon pigs
usage: /piggies