Solved Plugin not working

Discussion in 'Spigot Plugin Development' started by Ninjablader101, Mar 1, 2020.

  1. 17:28:44 INFO]: [Prisons] Enabling Prisons v1.0
    [17:28:44 ERROR]: Error occurred while enabling Prisons v1.0 (Is it up to date?)
    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 prisons.prisons.LevelHashmap.<init>(LevelHashmap.java:25) ~[?:?]
    at prisons.prisons.Prisons.onEnable(Prisons.java:17) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [server.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [server.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [server.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    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 prisons.prisons.Prisons.<init>(Prisons.java:11) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_221]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_221]
    at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_221]
    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]
    ... 2 more

    That's my error. I think it's makeing none of my plugins work i don't understand it. i am useing intellij if that makes a differentes.
     
  2. Only one class should extend JavaPlugin, and that's your main class.
     
  3. Then how do i implement my config.yml into a class

    public class Wood implements Listener {
    Plugin plugin;
    public Wood (Plugin plugin)
    {
    this.plugin = plugin;
    }
    @EventHandler
    public void unbreakable(BlockBreakEvent event){
    Player player = event.getPlayer();
    if(player.getItemInHand().getType() == Material.WOOD_PICKAXE){
    player.getItemInHand().setDurability((short) - 10);
    player.updateInventory();
    }
    }
    @EventHandler
    public void xpcheck(BlockBreakEvent e, PlayerLevelManager playerLevelManager) {
    Player player = e.getPlayer();
    int xpneeded = this.getConfig().getInt("Levels.1.xp");
    int xp = playerLevelManager.getXp();
    if (xp < xpneeded){
    player.sendMessage(ChatColor.RED + "You need to be level " + ChatColor.GREEN + "1");
    e.setCancelled(true);
    }
    if (xp == xpneeded) {
    if (player.getItemInHand().getType() == Material.WOOD_PICKAXE) {
    e.setCancelled(true);
    }
    }
    }
    }
     
  4. If you are making your plugin as a Maven project, go to the root project folder of your plugin and create a file named config.yml, in there, Insert anything you want to use then in your onEnable(), use saveDefaultConfig(), get values with getConfig(), note that if you are getting the config from another class, you need to get an instance of your main class to use getConfig().
     
  5. Sorry, I went to bed yesterday before your reply.
    As @Wilsoon said, you need an instance of your main class. You already have this, which is "plugin". All you need to do is call plugin.getConfig() to get your config.
     
  6. how do I call my plugin.getConfig()
     
  7. Strahan

    Benefactor

    ...uhh just like that.

    Like how you use getConfig() in your main class, you do the same in your other class just using the plugin variable you passed to access the config.
     
  8. O yeah im dumb Thanks
    Thx
     
  9. https://sourceb.in/afcb3b21b2 I get tons of errors upload_2020-3-2_18-50-26.png
    I'm kinda new to java and codeing in general so if you could teach me a way to debug this my self that would be great
     
  10. Strahan

    Benefactor

    We literally just talked about how to access the config, using the plugin variable, then you do it with "this"? That won't work. This refers to the current object, which is not your plugin instance as you aren't in the class that extends JavaPlugin.

    Also, don't embed color codes. Why is that map public? Also you declare it but never initialize it. When you attempt to access it, it will be null and the plugin will shit itself. Also remember that as a part of SOLID design you want to declare it Map<UUID, PlayerLevelManager> not HashMap. Initialize it with HashMap, but declare it Map.

    Also what is the point of
    Code (Text):
    if (this.levelManagerHashMap.containsKey(player.getUniqueId())) {
        this.getConfig().getInt("PlayerLevels." + player.getUniqueId() + ".level", playerLevelManager.getLevel());
        this.getConfig().getInt("PlayerLevels." + player.getUniqueId() + ".xp", playerLevelManager.getXp());
        this.saveConfig();
        this.levelManagerHashMap.remove(player.getUniqueId());
    }
    getInt is a method that returns a value, so calling it like that (not utilizing its return) achieves absolutely nothing. I assume you are trying to save data, so that should be getConfig().set instead.

    PS for future reference, it's a little more helpful when you take a screenshot if you don't truncate the text; though a copy and paste of the actual text is even more preferable.

    PPS for:
    Code (Text):
    private void xpcheck(Player player, PlayerLevelManager playerLevelManager) {
        int xpneeded = this.getConfig().getInt("Levels.1.xp");
        int xp = playerLevelManager.getXp();

        if (xp  >=xpneeded ) {
            player.sendMessage("ยง6Leveled UP!");
            playerLevelManager.setLevel(1);
        }
    }
    You really should not be hardcoding the value like that. It makes the method very limited in scope; i.e. it only works to check if they qualify for level 1. I'd make an argument to pass as to what level should be checked, then it becomes far more useful.