Solved Place a chest with an item.

Discussion in 'Spigot Plugin Development' started by mattwithabat, Mar 14, 2020.

  1. Hello, I am trying to make it so once I place a chest it would spawn an item this is the code I have right now and it doesn't work for some reason. (Yes the event is registered)


    Code (Java):
    public class Handler extends JavaPlugin implements Listener {

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            Block b = e.getBlockPlaced();
            Location l = b.getLocation();
            World w = b.getWorld();
            if (b.getType() == Material.CHEST) {
                Chest c = (Chest) b.getState();
                Inventory chestInv = c.getInventory();
                chestInv.addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
            }
        }
    }
     
     
  2. Most likely the problem I see is that the chest isn't actually placed before you try to put an item in it.
    The event is calling hey, does anyone wanna cancel this event or do something with it? And if so, it makes relevant changes.
    You are attempting to edit the internals of the chest before it has even been placed.
    The easiest solution to this is to just use a Bukkit Scheduler to run the task 1 tick later (making sure to check if the chest is actually there)
     
  3. could I create a scheduler inside of a method?
     
  4. Yes, look up a bukkit scheduler tutorial if you are confused, there are plenty.
     
  5. Code (Java):
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            new BukkitRunnable() {

                public void run() {
                    Player p = e.getPlayer();
                    Block b = e.getBlockPlaced();
                    Location l = b.getLocation();
                    World w = b.getWorld();
                    if (b.getType() == Material.CHEST) {
                        System.out.println("debug check 1");
                        Chest c = (Chest) b.getState();
                        Inventory chestInv = c.getInventory();
                        chestInv.addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                    }
                }
            }.runTaskLater(this, 1);
        }
    }
     
    No errors in console im highly confused why this is not working didn't even pass my debug check.
     
  6. Nevermind there is an error...

    Code (Text):
    [00:13:28 ERROR]: Error occurred while enabling PrisonChest v1.0-SNAPSHOT (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:183) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:60) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at me.mattwithabat.prisonchest.prisonchest.Handler.<init>(Handler.java:17) ~[?:?]
            at me.mattwithabat.prisonchest.prisonchest.PrisonChest.registerEvents(PrisonChest.java:20) ~[?:?]
            at me.mattwithabat.prisonchest.prisonchest.PrisonChest.onEnable(PrisonChest.java:11) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:186) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:60) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at me.mattwithabat.prisonchest.prisonchest.PrisonChest.<init>(PrisonChest.java:6) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:90) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:327) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:248) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:305) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:205) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            ... 2 more
    Main class


    Code (Java):
    package me.mattwithabat.prisonchest.prisonchest;

    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class PrisonChest extends JavaPlugin {

        @Override
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(color("&cPrison Chest &aEnabled!"));
            registerEvents();
        }

        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(color("&cPrison Chest &4Disabled!"));
        }

        public void registerEvents() {
        getServer().getPluginManager().registerEvents(new Handler(), this);
        }


        public static String color(String message) { return ChatColor.translateAlternateColorCodes('&', message); }

    }
     
     
  7. Looks like you have exported your plugin multiple times to the plugin folder. Seems like the old one loads first, so you do not get the new code with the BukkitRunnable, then the new one refuses to load because the old one has already been loaded.
    Go to your plugins folder and remove your duplicates.
     
  8. That’s what I thought too but there’s only 1 plugin there that’s the one I made.