bukkit creating an inventory window

Solutions on MaxInterview for bukkit creating an inventory window by the best coders in the world

showing results for - "bukkit creating an inventory window"
Mahdi
05 Jul 2017
1        IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
2            @Override
3            public void onOptionClick(IconMenu.OptionClickEvent event) {
4                event.getPlayer().sendMessage("You have chosen " + event.getName());
5                event.setWillClose(true);
6            }
7        }, plugin)
8        .setOption(3, new ItemStack(Material.APPLE, 1), "Food", "The food is delicious")
9        .setOption(4, new ItemStack(Material.IRON_SWORD, 1), "Weapon", "Weapons are for awesome people")
10        .setOption(5, new ItemStack(Material.EMERALD, 1), "Money", "Money brings happiness");
11
Fabio
12 Apr 2020
1import java.util.Arrays;
2
3import org.bukkit.Bukkit;
4import org.bukkit.entity.Player;
5import org.bukkit.event.EventHandler;
6import org.bukkit.event.EventPriority;
7import org.bukkit.event.HandlerList;
8import org.bukkit.event.Listener;
9import org.bukkit.event.inventory.InventoryClickEvent;
10import org.bukkit.inventory.Inventory;
11import org.bukkit.inventory.ItemStack;
12import org.bukkit.inventory.meta.ItemMeta;
13import org.bukkit.plugin.Plugin;
14 
15public class IconMenu implements Listener {
16 
17    private String name;
18    private int size;
19    private OptionClickEventHandler handler;
20    private Plugin plugin;
21   
22    private String[] optionNames;
23    private ItemStack[] optionIcons;
24   
25    public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
26        this.name = name;
27        this.size = size;
28        this.handler = handler;
29        this.plugin = plugin;
30        this.optionNames = new String[size];
31        this.optionIcons = new ItemStack[size];
32        plugin.getServer().getPluginManager().registerEvents(this, plugin);
33    }
34   
35    public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
36        optionNames[position] = name;
37        optionIcons[position] = setItemNameAndLore(icon, name, info);
38        return this;
39    }
40   
41    public void open(Player player) {
42        Inventory inventory = Bukkit.createInventory(player, size, name);
43        for (int i = 0; i < optionIcons.length; i++) {
44            if (optionIcons[i] != null) {
45                inventory.setItem(i, optionIcons[i]);
46            }
47        }
48        player.openInventory(inventory);
49    }
50   
51    public void destroy() {
52        HandlerList.unregisterAll(this);
53        handler = null;
54        plugin = null;
55        optionNames = null;
56        optionIcons = null;
57    }
58   
59    @EventHandler(priority=EventPriority.MONITOR)
60    void onInventoryClick(InventoryClickEvent event) {
61        if (event.getInventory().getTitle().equals(name)) {
62            event.setCancelled(true);
63            int slot = event.getRawSlot();
64            if (slot >= 0 && slot < size && optionNames[slot] != null) {
65                Plugin plugin = this.plugin;
66                OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
67                handler.onOptionClick(e);
68                if (e.willClose()) {
69                    final Player p = (Player)event.getWhoClicked();
70                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
71                        public void run() {
72                            p.closeInventory();
73                        }
74                    }, 1);
75                }
76                if (e.willDestroy()) {
77                    destroy();
78                }
79            }
80        }
81    }
82    
83    public interface OptionClickEventHandler {
84        public void onOptionClick(OptionClickEvent event);       
85    }
86    
87    public class OptionClickEvent {
88        private Player player;
89        private int position;
90        private String name;
91        private boolean close;
92        private boolean destroy;
93       
94        public OptionClickEvent(Player player, int position, String name) {
95            this.player = player;
96            this.position = position;
97            this.name = name;
98            this.close = true;
99            this.destroy = false;
100        }
101       
102        public Player getPlayer() {
103            return player;
104        }
105       
106        public int getPosition() {
107            return position;
108        }
109       
110        public String getName() {
111            return name;
112        }
113       
114        public boolean willClose() {
115            return close;
116        }
117       
118        public boolean willDestroy() {
119            return destroy;
120        }
121       
122        public void setWillClose(boolean close) {
123            this.close = close;
124        }
125       
126        public void setWillDestroy(boolean destroy) {
127            this.destroy = destroy;
128        }
129    }
130   
131    private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
132        ItemMeta im = item.getItemMeta();
133            im.setDisplayName(name);
134            im.setLore(Arrays.asList(lore));
135        item.setItemMeta(im);
136        return item;
137    }
138   
139}
Karolina
13 Nov 2020
1menu.open(player);
2