M
M
mrsakura522021-12-04 21:58:58
Java
mrsakura52, 2021-12-04 21:58:58

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);

There is also a class that is responsible for the inventory opening command:
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; }
}

How can I access the inv variable (from the command class) from the event class, for example? Given that the class with the event and the class with the team are in different packages?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-12-05
@mrsakura52

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 question

Ask a Question

731 491 924 answers to any question