Error when giving player an item.

Discussion in 'Plugin Development' started by Edmond506, Jul 18, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Edmond506

    Hey guys, I'm working on a crates (Not voting) plugin. I get an error when the server tries to give me the item that is in the config list.
    Here is my code:
    Code:
        @EventHandler
        public void placeblock(BlockPlaceEvent event) {
            Player player = (Player) event.getPlayer();
            List<String> list = Main.main.getConfig().getStringList("Items");
            int index = new Random().nextInt(list.size());
            String cmd = list.get(index);
            Block block = event.getBlockPlaced();
            Location loc = block.getLocation();
            int x = block.getLocation().getBlockX();
            int y1 = block.getLocation().getBlockY();
            int y2 = block.getLocation().getBlockY() + 1;
            int y3 = block.getLocation().getBlockY() + 2;
            int y4 = block.getLocation().getBlockY() + 3;
            int y5 = block.getLocation().getBlockY() + 4;
            int z = block.getLocation().getBlockZ();
            String[] idAndName = cmd.split(":");
            String id = idAndName[0];
            String name = idAndName[1];
            if (block.getType() == Material.ENDER_CHEST) {
                Main.main.getServer().dispatchCommand(Bukkit.getConsoleSender(), "summon FireworksRocketEntity " + x + " " + y1 + " " + z + " {LifeTime:5,FireworksItem:{id:401,Count:1,tag:{Fireworks:{Explosions:[{Type:3,Flicker:1,Trail:1,Colors:[6487839],FadeColors:[16716032]},{Type:4,Flicker:1,Trail:1,Colors:[16711680],FadeColors:[16729438]},{Type:4,Flicker:1,Trail:1,Colors:[3374847],FadeColors:[16777024]},{Type:1,Flicker:1,Trail:1,Colors:[16769953],FadeColors:[16735389]}]}}}}");
                Main.main.getServer().dispatchCommand(Bukkit.getConsoleSender(), "summon FireworksRocketEntity " + x + " " + y2 + " " + z + " {LifeTime:5,FireworksItem:{id:401,Count:1,tag:{Fireworks:{Explosions:[{Type:3,Flicker:1,Trail:1,Colors:[6487839],FadeColors:[16716032]},{Type:4,Flicker:1,Trail:1,Colors:[16711680],FadeColors:[16729438]},{Type:4,Flicker:1,Trail:1,Colors:[3374847],FadeColors:[16777024]},{Type:1,Flicker:1,Trail:1,Colors:[16769953],FadeColors:[16735389]}]}}}}");
                Main.main.getServer().dispatchCommand(Bukkit.getConsoleSender(), "summon FireworksRocketEntity " + x + " " + y3 + " " + z + " {LifeTime:5,FireworksItem:{id:401,Count:1,tag:{Fireworks:{Explosions:[{Type:3,Flicker:1,Trail:1,Colors:[6487839],FadeColors:[16716032]},{Type:4,Flicker:1,Trail:1,Colors:[16711680],FadeColors:[16729438]},{Type:4,Flicker:1,Trail:1,Colors:[3374847],FadeColors:[16777024]},{Type:1,Flicker:1,Trail:1,Colors:[16769953],FadeColors:[16735389]}]}}}}");
                Main.main.getServer().dispatchCommand(Bukkit.getConsoleSender(), "summon FireworksRocketEntity " + x + " " + y4 + " " + z + " {LifeTime:5,FireworksItem:{id:401,Count:1,tag:{Fireworks:{Explosions:[{Type:3,Flicker:1,Trail:1,Colors:[6487839],FadeColors:[16716032]},{Type:4,Flicker:1,Trail:1,Colors:[16711680],FadeColors:[16729438]},{Type:4,Flicker:1,Trail:1,Colors:[3374847],FadeColors:[16777024]},{Type:1,Flicker:1,Trail:1,Colors:[16769953],FadeColors:[16735389]}]}}}}");
                Main.main.getServer().dispatchCommand(Bukkit.getConsoleSender(), "summon FireworksRocketEntity " + x + " " + y5 + " " + z + " {LifeTime:5,FireworksItem:{id:401,Count:1,tag:{Fireworks:{Explosions:[{Type:3,Flicker:1,Trail:1,Colors:[6487839],FadeColors:[16716032]},{Type:4,Flicker:1,Trail:1,Colors:[16711680],FadeColors:[16729438]},{Type:4,Flicker:1,Trail:1,Colors:[3374847],FadeColors:[16777024]},{Type:1,Flicker:1,Trail:1,Colors:[16769953],FadeColors:[16735389]}]}}}}");
    
                block.getWorld().strikeLightning(loc);
                TitleAPI.sendTitle(player, 10, 15, 10, "§a§lYou got", "§e§l" + name);
                player.getInventory().addItem(new ItemStack(Material.getMaterial(id)));
                Bukkit.getScheduler().scheduleSyncDelayedTask( Main.main, new Runnable() {
                    @Override
                    public void run() {
                        event.getBlock().setType(Material.AIR);
                    }
                }, 20L);
            }
        }
    The error is at player.getInventory().addItem(new ItemStack(Material.getMaterial(id)));
    My modified config is:
    Code:
    Items:
    - 322:Apple
    - 267:IronSword
    - 276:DiamondSword
    - 310:Cap
    - 311:ChestProtector
    - 312:Leggings
    - 313:FarmerBoots
    
    Error:
    Code:
    18.07 13:33:16 [Server] ERROR Could not pass event BlockPlaceEvent to Rebusz v1.0
    18.07 13:33:16 [Server] INFO org.bukkit.event.EventException
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:127) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.ItemStack.placeItem(ItemStack.java:158) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:503) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    18.07 13:33:16 [Server] INFO Caused by: java.lang.NullPointerException
    18.07 13:33:16 [Server] INFO at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:46) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at me.skyvko.rebusz.listener.Effect.placeblock(Effect.java:47) ~[?:?]
    18.07 13:33:16 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO ... 18 more
     
  2. Offline

    DarkenCake

    Code:
    18.07 13:33:16 [Server] INFO Caused by: java.lang.NullPointerException
    18.07 13:33:16 [Server] INFO at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:46) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO at me.skyvko.rebusz.listener.Effect.placeblock(Effect.java:47) ~[?:?]
    18.07 13:33:16 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
    18.07 13:33:16 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigots.jar:git-Spigot-db6de12-18fbb24]
    18.07 13:33:16 [Server] INFO ... 18 more
     
  3. Offline

    Lordloss

    @DarkenCake your comment was totally useless.

    @Edmond506 For me it looks like it has problems with parsing the id to Material, try to print out what it is exactly before changing it to material. And check if the material is null after doing so.
     
  4. Offline

    Edmond506

    Can you give me an example? It works when I don't seperate the id and the name. But I need to seperate it to display the item name and give the player the exact item displayed.
     
  5. Offline

    Lordloss

    Example for what? System.out.println, player.sendMessage() ?
     
  6. Offline

    ArsenArsen

    Ok so
    First you do Material myMaterial = Material.getMaterail(MaterialName)
    then if myMaterial is null send player a error message
    else do what you wanted to do with material.

    If you need the ID then loop through them and compare the ID with the input. If its the same then just use that material. WHen the loop ends you send the error message if material is not found.

    And no, noone will give you "example" code. Its called spoonfeeding, as known as getting someone else to do your work.
     
Thread Status:
Not open for further replies.

Share This Page