8

General Info: I am using the Bukkit/Spigot API in version git-Spigot-1d14d5f-ba32592 (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT), IntelliJ IDEA 14.1.3 and compile with its default compiler. The java jdk version is 1.8.0_25.

So when I try to call this class's constructor, it throws the runtime exception from the title.

Inventory menu class

package me.lakan.util.inventory;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused") // Util functionality is not always used
public class InventoryMenu implements Listener {

    private InventoryType type;
    private String title;

    private Map<Integer, MenuOption> options;

    // Constructors
    public InventoryMenu(InventoryType type, String title, JavaPlugin plugin) {

        this.options = new HashMap<Integer, MenuOption>();
        this.type = type;
        this.title = title;

        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }

    public boolean addOption(int position, MenuOption option) {
        boolean res = isPosEmpty(position);
        this.options.put(position, option);

        return res;
    }

    public void addOption(String name, int position, ItemStack icon) {
        addOption(position, new MenuOption(icon, name));
    }

    public boolean isPosEmpty(int position) {
        return !this.options.containsKey(position);
    }

    public void openFor(Player p) {
        // Create a new inventory
        Inventory inv = Bukkit.createInventory(p, this.type, this.title);

        // Fill all icons at their positions
        for (Map.Entry<Integer, MenuOption> key : this.options.entrySet()) {
            inv.setItem(key.getKey(), key.getValue().getIcon());
        }

        // If the inventory is a player inventory, update the player's
        if (inv.getType() == InventoryType.PLAYER) {
            p.getInventory().setContents(inv.getContents());
        }
        // For any openable inventory, just open it up
        else {
            p.openInventory(inv);
        }
    }

    /**
     * Listens for inventory clicks
     * If the inventory is a menu:
     * - Cancel movement
     * - Push event
     * - Close inventory if it should
     * @param e The actual event
     */
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onInventoryClick(InventoryClickEvent e) {

        // Prevent clicking if this inventory was clicked
        if (e.getClickedInventory().getName().equals(this.title)) {
            e.setCancelled(true);

            // Check for option
            if (this.options.containsKey(e.getRawSlot())) {
                // Get the option for this slot
                MenuOption option = this.options.get(e.getRawSlot());

                // Fill out an event and push it
                MenuClickEvent event = new MenuClickEvent((Player) e.getWhoClicked(), true, option.getName(), e.getRawSlot());
                Bukkit.getServer().getPluginManager().callEvent(event);

                // Now close inventory if not cancelled
                if (event.willCLose()) {
                    e.getWhoClicked().closeInventory();
                }
            }
        }
    }

    @SuppressWarnings("unused")
    public interface OptionClickEventHandler {
        public void onOptionClick(MenuClickEvent event);
    }
}

The item menu class

package me.lakan.test;

import me.lakan.util.inventory.InventoryMenu;
import me.lakan.util.inventory.MenuClickEvent;
import me.lakan.util.inventory.MenuOption;
import me.lakan.util.item.ItemBuilder;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryType;

public class ItemMenu implements Listener {
    private PluginEntry plugin;
    private InventoryMenu menu;

    // Commands
    private TestCommand testCmd;

    public ItemMenu(PluginEntry plugin) {

        Validate.notNull(this.plugin, "The plugin reference may not be null");
        this.plugin = plugin;

        this.menu = new InventoryMenu(InventoryType.CHEST, "" + ChatColor.DARK_GRAY + "Abilities", this.plugin);

        // Test
        this.menu.addOption(1,
                new MenuOption(
                        new ItemBuilder()
                                .amount(1)
                                .material(Material.RAW_FISH)
                                .name(ChatColor.LIGHT_PURPLE + "Test")
                                .lore(ChatColor.WHITE + "Click me")
                                .build(),
                        "TestIdentifier"));
        this.testCmd= new TestCmd(this.plugin);
}

    public void openFor(Player p) {
        this.menu.openFor(p);
    }



    @EventHandler(priority = EventPriority.NORMAL)
    public void onOptionClick(MenuClickEvent e) {
        // Test
        if (e.getName().equals("TestIdentifier")) {
            this.testCmd.executeFor(e.getWhoClicked());
        }
    }
}

Exception stack trace

[12:48:25] [Server thread/ERROR]: Error occurred while enabling Test v1.0 (Is it up to date?) java.lang.NoSuchMethodError: me.lakan.util.inventory.InventoryMenu.(Lorg/bukkit/event/inventory/InventoryType;Ljava/lang/String;Lorg/bukkit/plugin/java/JavaPlugin;)V

at me.lakan.test.ItemMenu.(ItemMenu.java:33) ~[?:?] at me.lakan.test.CommandParser.(CommandParser.java:20) ~[?:?] at me.lakan.test.PluginEntry.onEnable(PluginEntry.java:21) ~[?:?] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugin(CraftServer.java:356) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at org.bukkit.craftbukkit.v1_8_R2.CraftServer.enablePlugins(CraftServer.java:316) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.r(MinecraftServer.java:416) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.k(MinecraftServer.java:382) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.a(MinecraftServer.java:337) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.java:257) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:522) [spigot_server.jar:git-Spigot-1d14d5f-ba32592] at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]

As you can see the constructor is there and is to my knowledge properly called. So is this error a mistake in my project setup, an API thing, or something entirely different?

The utility classes are in a seperate module and everything works if I paste them into my test plugin module, but not inside the other module. However any other constructor in any other class inside me.lakan.util.inventory can be called normally.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Do you have different jar's at compile versus runtime? It should not compile in the first place if the constructors don't match – André Schild Jul 18 '15 at 11:41
  • No. It uses the module output of the util plugin for the test plugin at compile time and puts all relevant classes into the output jar. So at runtime every class and method should be available... – redpandamonium Jul 18 '15 at 11:47
  • Instead of editing the answer into the question, you should answer your own question, and then accept the answer – Jojodmo Jul 18 '15 at 23:22
  • 1
    Do a clean build and redeploy. – user207421 Jul 18 '15 at 11:45
  • Did not work. It does the same thing again – redpandamonium Jul 18 '15 at 11:52
  • @EJP yeah, I thought the same. However I have been shifting around the project structure (especially the dependency handling) for hours and rebuilt it over and over... – redpandamonium Jul 18 '15 at 11:58
  • Nothing else it can be. Remove all the artefacts of the webapp from the container and redeploy. And make sure that all the JARs you compile against *are* deployed with the webapp. – user207421 Jul 18 '15 at 12:19

1 Answers1

3

The problem was in the project structure.

For the test artifact, I chose the module output of the util module. Changing it to the artifact output of the util module fixed it.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107