Solved Can't register Event correctly

Discussion in 'Spigot Plugin Development' started by Deathworld, Apr 29, 2017.

Thread Status:
Not open for further replies.
  1. Hey, i'm actually trying to add AutoLapis into my Core, but I can't register Event correctly, it says "AutoLapis(AutoLapis) in AutoLapi cannot be applied to ()"...
    Here is my code :

    Code (Text):

    package fr.dragonia;

    import fr.dragonia.commands.OpCommand;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.EnchantingInventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;

    public class Main extends JavaPlugin implements Listener {

        public ArrayList<EnchantingInventory> inventories = new ArrayList<>();
        public static Main instance;

        public static Main getInstance(){
            return instance;
        }

        @Override
        public void onEnable() {
            this.getCommand("vote").setExecutor(this);
            instance = this;
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new AutoLapis(), this);
            inventories = new ArrayList<EnchantingInventory>();
            getServer().getPluginManager().registerEvents(new OpCommand(), this);
        }

        @Override
        public void onDisable() {
            for (EnchantingInventory ei : inventories) {
                ei.setItem(1, null);
            }
            inventories = null;
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            e.setJoinMessage(null);
            if(!e.getPlayer().hasPlayedBefore()){
                e.getPlayer().teleport(new Location(Bukkit.getWorld("Dragonia"), 3025.4, 74, 703.5));
            }
        }

        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
            e.setQuitMessage(null);
        }
    }
     

    Code (Text):

    package fr.dragonia;

    import org.bukkit.DyeColor;
    import fr.dragonia.Main;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.enchantment.EnchantItemEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.inventory.EnchantingInventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.material.Dye;

    import static fr.dragonia.Main.getInstance;

    public class AutoLapis implements Listener {

        private AutoLapis plugin;

        private ItemStack lapis;

        public AutoLapis(AutoLapis plugin) {
            this.plugin = plugin;

            // Create a stack of 64 lapis lazuli
            Dye d = new Dye();
            d.setColor(DyeColor.BLUE);
            this.lapis = d.toItemStack();
            this.lapis.setAmount(64);
        }

        @EventHandler
        public void openInventoryEvent(InventoryOpenEvent e) {
            if (e.getInventory() instanceof EnchantingInventory) {
                if (e.getPlayer().hasPermission("autolapis.autofill")
                        || e.getPlayer().isOp() || e.getPlayer().hasPermission("*")) {
                    e.getInventory().setItem(1, this.lapis);
                    Main.getInstance().inventories.add((EnchantingInventory) e.getInventory());
                }
            }
        }

        @EventHandler
        public void closeInventoryEvent(InventoryCloseEvent e) {
            if (e.getInventory() instanceof EnchantingInventory) {
                if (getInstance().inventories.contains((EnchantingInventory) e
                        .getInventory())) {
                    e.getInventory().setItem(1, null);
                    Main.getInstance().inventories.remove((EnchantingInventory) e.getInventory());
                }
            }
        }

        @EventHandler
        public void inventoryClickEvent(InventoryClickEvent e) {
            if (e.getClickedInventory() instanceof EnchantingInventory) {
                if (Main.getInstance().inventories.contains((EnchantingInventory) e
                        .getInventory())) {
                    if (e.getSlot() == 1) {
                        e.setCancelled(true);
                    }
                }
            }
        }

        @EventHandler
        public void enchantItemEvent(EnchantItemEvent e) {
            if (Main.getInstance().inventories.contains((EnchantingInventory) e
                    .getInventory())) {
                e.getInventory().setItem(1, this.lapis);
            }
        }

    }

    }

    Could you please help me ? :3
    Sorry, I'm actually learning Java and i'm bad in english... ^^
    Thanks in advance !
     
  2. Code (Text):
    private AutoLapis plugin;

    public AutoLapis(AutoLapis plugin) {
        this.plugin = plugin;
        ...
    Why would you want to store an AutoLapis class instance inside the AutoLapis class? I'm pretty sure you want to store your Main class there, so change:
    private AutoLapis plugin -> private Main plugin
    public AutoLapis(AutoLapis plugin) -> public AutoLapis(Main plugin)
     
    • Agree Agree x 1
  3. That worked, thanks you ! :D
     
Thread Status:
Not open for further replies.