Answer the question
In order to leave comments, you need to log in
How to access inventory from another file?
The situation is this: I am writing a plugin for the spigot minecraft server, I declare an event in main.java:
getServer().getPluginManager().registerEvents(new PlayerInventoryEvent(this), this);
public class settingsInventoryCommand implements CommandExecutor {
private final Main plugin;
public static settingsInventoryCommand i;
public settingsInventoryCommand(Main plugin) {
this.plugin = plugin;
};
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) { return true; }
Player p = (Player) sender;
String inventoryTitle = plugin.getConfig().getString("messages.inventoryTitle").replace("{nickname}", p.getName()).replace("&", "\u00a7");
Inventory inv = Bukkit.createInventory(null, 27, inventoryTitle);
ItemStack tempIS = new ItemStack(Material.APPLE);
ItemMeta tempIM = tempIS.getItemMeta();
tempIM.setDisplayName("APPLE");
tempIS.setItemMeta(tempIM);
inv.setItem(13, tempIS);
p.openInventory(inv);
plugin.settingsInv.put(p.getName(), p.getAddress().getHostName());
return true;
}
public static settingsInventoryCommand getInv() { return i; }
}
Answer the question
In order to leave comments, you need to log in
Inventory inv you have declared with a minimum scope.
If there is a need to access it somehow from the entire application, you can, for example, make an "InventoryRepository" - a separate object that will store instances of these Inventories, issue them on demand, save, change, etc...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question