1.8.8 Failed to initialize error!

Discussion in 'Spigot Plugin Development' started by TheJYT, Feb 21, 2020.

  1. Hi there! I was making a skyblock plugin and it worked! so i added a GUI to make the ilsand and then it failed to work. My error:
    Code (Text):

    > [14:38:39 ERROR]: Could not pass event InventoryClickEvent to CenturiesSkyblockCore v1.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_212]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_212]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at com.bensdev.CenturiesSkyblockCore.schematics.<init>(schematics.java:35) ~[?:?]
    at com.bensdev.CenturiesSkyblockCore.UI.Interface.clicked(Interface.java:40) ~[?:?]
    at com.bensdev.CenturiesSkyblockCore.listeners.InventoryClickListener.onCLick(InventoryClickListener.java:32) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[server.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more
    Caused by: java.lang.IllegalStateException: Initial initialization
    at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at com.bensdev.CenturiesSkyblockCore.schematics.<init>(schematics.java:35) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_212]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_212]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_212]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_212]
    at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_212]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) ~[server.jar:git-Spigot-db6de12-18fbb24]
    ... 1 more
     
    Here are my classes:

    Main schematics.java:
    Code (Text):

    package com.bensdev.CenturiesSkyblockCore;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.bensdev.CenturiesSkyblockCore.Commands.is;
    import com.bensdev.CenturiesSkyblockCore.UI.Interface;
    import com.bensdev.CenturiesSkyblockCore.Utils.Utils;
    import com.bensdev.CenturiesSkyblockCore.listeners.InventoryClickListener;
    import com.onarandombox.MultiverseCore.MultiverseCore;
    import com.onarandombox.MultiverseCore.api.MultiverseWorld;
    import com.onarandombox.MultiverseCore.utils.WorldManager;
    import com.sk89q.worldedit.CuboidClipboard;
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.schematic.MCEditSchematicFormat;
    import com.sk89q.worldedit.world.DataException;


    @SuppressWarnings("deprecation")
    public class schematics extends JavaPlugin implements Listener {
       WorldManager wm = new WorldManager(getMultiverseCore());
         MultiverseWorld world = wm.getMVWorld("Skyblock");
      public void onEnable() {
        loadConfig();          
       new InventoryClickListener(this);
       Interface.initialize();
       new is(this);
        getServer().getPluginManager().registerEvents(this, this);
      }
     
       public void loadConfig() {
           if (!this.getDataFolder().exists()) {
           this.getDataFolder().mkdir();
           this.getConfig().options().copyDefaults();
            saveDefaultConfig();
           }
       }
       
       public MultiverseCore getMultiverseCore() {
            Plugin plugin = getServer().getPluginManager().getPlugin("Multiverse-Core");
            if (plugin instanceof MultiverseCore) {
                return (MultiverseCore) plugin;
            }
            throw new RuntimeException("MultiVerse not found!");
        }

        @EventHandler
         public void interact(PlayerInteractEvent event)
         {
           Player player = event.getPlayer();
           if (player.getInventory().getItemInHand().getType().equals(Material.STICK))
           {
             loadSchemB(player);
             player.sendMessage(ChatColor.GREEN + "Schematic has been loaded.");
           }
         }
       
           public void loadSchemB(Player p) {
               p.sendMessage("Loading...");
                 String island = "basic-island";
                 double xx, zx;
                 double x, y, z;
                 xx = this.getConfig().getDouble("Data.Loc.X");
                 y = this.getConfig().getDouble("Data.Loc.Y");
                 zx = this.getConfig().getDouble("Data.Loc.Z");
                 
                 x = xx + 200;
                 z = zx + 200;
                 

                 Location location = new Location(Bukkit.getWorld("Skyblock"), x, y, z);
                 WorldEditPlugin worldEditPlugin = (WorldEditPlugin)Bukkit.getPluginManager().getPlugin("WorldEdit");
                 File schematic = new File(getDataFolder() + File.separator + "/schematics/" + island + ".schematic");
                 EditSession session = worldEditPlugin.getWorldEdit().getEditSessionFactory().getEditSession(new BukkitWorld(location.getWorld()), 10000);
                 if(schematic.exists() && schematic.isFile()) {
                     try {
                       CuboidClipboard clipboard = MCEditSchematicFormat.getFormat(schematic).load(schematic);
                         clipboard.rotate2D(90);
                         clipboard.paste(session, new Vector(x, y, z), false);
                         
                         this.getConfig().set("Data.Loc.X", x);
                         this.getConfig().set("Data.Loc.Z", z);
                         this.saveConfig();
                         p.sendMessage(Utils.chat("&a&lSucess!"));
                         p.teleport(location);
                         p.chat("/sethome");
                         
                     }
                     catch (MaxChangedBlocksException|DataException|IOException e) {
                         e.printStackTrace();
                         p.sendMessage(Utils.chat("&c&lError!"));
                     }
                 }
                 else {
                     Bukkit.broadcastMessage("File Does not exist: " + xx + y + zx + schematic);
                 }
             }
           public void loadSchemA(Player p) {
               p.sendMessage("Loading...");
                 String island = "ad-island";
                 double xx, zx;
                 double x, y, z;
                 xx = this.getConfig().getDouble("Data.Loc.X");
                 y = this.getConfig().getDouble("Data.Loc.Y");
                 zx = this.getConfig().getDouble("Data.Loc.Z");
                 
                 x = xx + 200;
                 z = zx + 200;
                 

                 Location location = new Location(Bukkit.getWorld("Skyblock"), x, y, z);
                 WorldEditPlugin worldEditPlugin = (WorldEditPlugin)Bukkit.getPluginManager().getPlugin("WorldEdit");
                 File schematic = new File(getDataFolder() + File.separator + "/schematics/" + island + ".schematic");
                 EditSession session = worldEditPlugin.getWorldEdit().getEditSessionFactory().getEditSession(new BukkitWorld(location.getWorld()), 10000);
                 if(schematic.exists() && schematic.isFile()) {
                     try {
                       CuboidClipboard clipboard = MCEditSchematicFormat.getFormat(schematic).load(schematic);
                         clipboard.rotate2D(90);
                         clipboard.paste(session, new Vector(x, y, z), false);
                         
                         this.getConfig().set("Data.Loc.X", x);
                         this.getConfig().set("Data.Loc.Z", z);
                         this.saveConfig();
                         p.sendMessage(Utils.chat("&a&lSucess!"));
                         p.teleport(location);
                         p.chat("/sethome");
                         
                     }
                     catch (MaxChangedBlocksException|DataException|IOException e) {
                         e.printStackTrace();
                         p.sendMessage(Utils.chat("&c&lError!"));
                     }
                 }
                 else {
                     Bukkit.broadcastMessage("File Does not exist: " + xx + y + zx + schematic);
                 }
           }
       }
     
    Interface.java:
    Code (Text):

    package com.bensdev.CenturiesSkyblockCore.UI;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import com.bensdev.CenturiesSkyblockCore.schematics;
    import com.bensdev.CenturiesSkyblockCore.Utils.Utils;

    public class Interface {
       private static schematics sc;
       
       public static Inventory inv;
       public static String inventory_name;
       public static int inv_boxes = 3 * 9;
       
       public static void initialize() {
           inventory_name = Utils.chat("&6&lCreate an Island!!!");
           
           inv = Bukkit.createInventory(null, inv_boxes);
       }
       
       public static Inventory GUI (Player p) {
           Inventory toReturn = Bukkit.createInventory(null, inv_boxes, inventory_name);
           
               Utils.createItem(inv, 1, 1, 5, "&6&lCreate a &e&lIsland", "Create your very first island here!");
               Utils.createItemByte(inv, 1, 4, 1, 13, "&6&lCreate a &7&lBasic &e&lIsland", "Create your basic island by clicking here!");
               Utils.createItemByte(inv, 1, 1, 1, 15,"&6&lCreate a &a&lAdvanced &e&lIsland", "Create your advanced island by clicking here!");
           

           
           toReturn.setContents(inv.getContents());
           return toReturn;
           
       }
       
       public static void clicked(Player p, int slot, ItemStack clicked, Inventory inv) {
           if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&6&lCreate a &7&lBasic &e&lIsland"))) {
               sc = new schematics();
               sc.loadSchemB(p);
                   
               } else if(clicked.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&6&lCreate a &a&lAdvanced &e&lIsland"))) {
                   if (p.hasPermission("core.is.ad")) {
                       sc = new schematics();
                       sc.loadSchemA(p);
                   } else {
                       p.sendMessage(Utils.chat("You dont have permission to create that ilsand!"));
                   }
               }
       }
    }
     
    Utils.java:
    Code (Text):

    package com.bensdev.CenturiesSkyblockCore.Utils;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    @SuppressWarnings("deprecation")
    public class Utils {
       
       public static String chat (String s) {
           return ChatColor.translateAlternateColorCodes('&' , s);
       }
       
       public static ItemStack createItem(Inventory inv, int materialId, int amount, int invSlot, String displayName, String... loreString) {
           ItemStack item;
           List<String> lore = new ArrayList();
           
           item = new ItemStack(Material.getMaterial(materialId), amount);
           
           ItemMeta meta = item.getItemMeta();
           meta.setDisplayName(Utils.chat(displayName));
           for (String s : loreString) {
               lore.add(Utils.chat(s));
           }
           meta.setLore(lore);
           item.setItemMeta(meta);
           inv.setItem(invSlot - 1, item);
           
           return item;
       }
       
       

       public static ItemStack createItemByte(Inventory inv, int materialId, int byteId, int amount, int invSlot, String displayName, String... loreString) {
           ItemStack item;
           List<String> lore = new ArrayList();
           
           item = new ItemStack(Material.getMaterial(materialId), amount, (short) byteId);
           
           ItemMeta meta = item.getItemMeta();
           meta.setDisplayName(Utils.chat(displayName));
           for (String s : loreString) {
               lore.add(Utils.chat(s));
           }
           meta.setLore(lore);
           item.setItemMeta(meta);
           inv.setItem(invSlot - 1, item);
           
           return item;
       }

    }
     
    is.java:
    Code (Text):

    package com.bensdev.CenturiesSkyblockCore.Commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;

    import com.bensdev.CenturiesSkyblockCore.schematics;
    import com.bensdev.CenturiesSkyblockCore.UI.Interface;

    public class is implements CommandExecutor{
       
       private schematics plugin;
       
       public is(schematics plugin) {
           this.plugin = plugin;
           
           plugin.getCommand("is").setExecutor(this);
       }
       public Player p;
       public Entity entity;
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
           if (sender instanceof Player) {
                Player player = (Player) sender;
               
                if (command.getName().equalsIgnoreCase("is")) {
                   if (player.hasPermission("core.is")) {
                       player.chat("/home");
                       
                   } else {
                       player.openInventory(Interface.GUI(p));
                   }
                   
                   
                } return true;
           } return true;
        }
    }
     
    InventoryClickListener.java
    Code (Text):

    package com.bensdev.CenturiesSkyblockCore.listeners;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    import com.bensdev.CenturiesSkyblockCore.schematics;
    import com.bensdev.CenturiesSkyblockCore.UI.Interface;

    public class InventoryClickListener implements Listener{
       
       private schematics plugin;
       
       public InventoryClickListener(schematics plugin) {
           this.plugin = plugin;
           
           Bukkit.getPluginManager().registerEvents(this, plugin);
       }
       
       
       @EventHandler
       public void onCLick(InventoryClickEvent e) {
           String title = e.getInventory().getTitle();
           if(title.equals(Interface.inventory_name)) {
               e.setCancelled(true);
               if(e.getCurrentItem() == null) {
                   return;
               }
               if (title.equals(Interface.inventory_name)) {
                   Interface.clicked((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), e.getInventory());
               }
           }
       }
       

    }
     
    Any help is apreciated!
     
  2. sc = new schematics(); in Interface class
    you're creating a new instance of your plugin. you cant do that. only one instance is allowed. thats what the error tells you
     
    • Like Like x 1
  3. You are creating a new instance of your main class, in the Interface class.

    sc = new schematics();
     
  4. drives_a_ford

    Moderator

    To add to the above, please read Beginner Programming Mistakes and Why You're Making Them. You're not adhering to the naming convention (which by the way reduces the readability of your code significantly), the Interface class is a mess of static abuse and lack of encapsulation. You're also violating the SRP (Singe Responsibility Principle) and many of your method and class names are not descriptive (or do things other than what they describe).
     
  5. How would i call the action if i didnt create a new instance then?
     
  6. Just create a new instance.
    Or you will get errors as the others are saying.
     
  7. There is only one main class. It is static which means it is accessible from anywhere.
     
  8. you call the action by using dependency injection like you have before (by passing through the constructor). this is hard to do with a class designed with static abuse. if you need to fetch instances from a static method class, then its not a static method class and you have design flaws. rid yourself of the methods that do not need to be static and have them instanced amongst something where you can access with dependency injection.
     
  9. i cant do schematics.loadSchemB(p); because it gives me an error telling me to change the function to static. I cant do that because im calling from a config file
     
  10. I recommend always naming your classes with capitals.
    For example: Schematis, not schematis.
    In java we mostly name variables with small letters.
    Hard to differ if class name Schematis or a variable schematis.
    So it can be confusing if you are trying to use a function as a static or not.
     
  11. How exactly would i call the function tho?
     
  12. i told you how. i told you how exactly. dependency injection. you're already using it too. if you have no idea what it means, look it up.