Crates Plugin Problem

Discussion in 'Plugin Development' started by MCJoshua345, Mar 16, 2016.

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

    MCJoshua345

    Hello! I'm having a little problem. Here's the code:
    Code:
    package me.alphagladiator.crates.listeners;
    
    import java.util.List;
    import java.util.Random;
    
    import me.alphagladiator.crates.ItemBank;
    import me.alphagladiator.crates.StringStorage;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Chest;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class CrateOpen implements Listener{
    
        @EventHandler
        public void onChestOpen(PlayerInteractEvent e){
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(e.getClickedBlock() != null){
                    if(e.getClickedBlock().getType() == Material.CHEST){
                        Chest chest = (Chest) e.getClickedBlock().getState();
                        Player p = e.getPlayer();
                        Inventory chestInventory = chest.getInventory();
                        if(chestInventory.getItem(0) != null){
                            ItemStack item = chestInventory.getItem(0);
                            if(item.getType() == Material.PAPER){
                                if(item.getItemMeta().hasLore()){
                                    List<String> lore = item.getItemMeta().getLore();
                                    if(lore.contains(ChatColor.GREEN + "Common Chest")){
                                        e.setCancelled(true);
                                        openCrate(p, "Common");
                                        return;
                                    }else if(lore.contains(ChatColor.BLUE + "MVP Chest")){
                                        e.setCancelled(true);
                                        openCrate(p, "MVP");
                                        return;
                                    }else if(lore.contains("" + ChatColor.BLUE + ChatColor.BOLD + "MVP+ Chest")){
                                        e.setCancelled(true);
                                        openCrate(p, "MVP+");
                                        return;
                                    }else if(lore.contains(ChatColor.LIGHT_PURPLE + "Mythical Chest")){
                                        e.setCancelled(true);
                                        openCrate(p, "Mythical");
                                        return;
                                    }else{ return; }
                                }
                            }
                        }
                    }
                }
            }
        }
    
    
        public static void openCrate(Player p, String type){
            if(p.getInventory().getItemInMainHand().getType().equals(Material.TRIPWIRE_HOOK) && p.getInventory().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.GREEN + "A crate key!")){
                String name = p.getInventory().getItemInMainHand().getItemMeta().getDisplayName();
                if(name.equals(ChatColor.GREEN + "Common Key") && type.equals("Common")){
                    int itemNum = randomNumber(0, ItemBank.commonList().size());
                    ItemStack item = ItemBank.commonList().get(itemNum);
                    ItemStack handItem = p.getInventory().getItemInMainHand();
                    if(p.getInventory().getItemInMainHand().getAmount() == 1){
                        p.getInventory().removeItem(p.getInventory().getItemInMainHand());
                    }
                    handItem.setAmount(handItem.getAmount() - 1);
                    p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 10, 1);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.act + ChatColor.GREEN + "You opened a Common Chest and recieved your item! Congratulations!");
                    return;
                }else if(name.equals(ChatColor.BLUE + "MVP Key") && type.equals("MVP")){
                    int itemNum = randomNumber(0, ItemBank.MVPList().size());
                    ItemStack item = ItemBank.MVPList().get(itemNum);
                    p.getInventory().getItemInMainHand().setAmount(p.getInventory().getItemInMainHand().getAmount() - 1);
                    if(p.getInventory().getItemInMainHand().getAmount() == 1){
                        p.getInventory().removeItem(p.getInventory().getItemInMainHand());
                    }
                    p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 10, 1);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.act + ChatColor.GREEN + "You opened a MVP Chest and recieved your item! Congratulations!");
                    return;
                }else if(name.equals("" + ChatColor.BLUE + ChatColor.BOLD + "MVP+ Key") && type.equals("MVP+")){
                    int itemNum = randomNumber(0, ItemBank.MVP2List().size());
                    ItemStack item = ItemBank.MVP2List().get(itemNum);
                    p.getInventory().getItemInMainHand().setAmount(p.getInventory().getItemInMainHand().getAmount() - 1);
                    if(p.getInventory().getItemInMainHand().getAmount() == 1){
                        p.getInventory().removeItem(p.getInventory().getItemInMainHand());
                    }
                    p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 10, 1);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.act + ChatColor.GREEN + "You opened a MVP Chest and recieved your item! Congratulations!");
                    return;
                }else if(name.equals(ChatColor.LIGHT_PURPLE + "Mythical Key") && type.equals("Mythical")){
                    int itemNum = randomNumber(0, ItemBank.mythicalList().size());
                    ItemStack item = ItemBank.mythicalList().get(itemNum);
                    p.getInventory().getItemInMainHand().setAmount(p.getInventory().getItemInMainHand().getAmount() - 1);
                    if(p.getInventory().getItemInMainHand().getAmount() == 1){
                        p.getInventory().removeItem(p.getInventory().getItemInMainHand());
                    }
                    p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_BREAK, 10, 1);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.act + ChatColor.GREEN + "You opened a Mythical Chest and recieved your item! Congratulations!");
                    return;
                }else{
                    p.sendMessage(StringStorage.act + ChatColor.RED + "ERROR: Either you renamed your key, or you are trying to use the wrong key type!");
                    return;
                }
            }else{
                p.sendMessage(StringStorage.act + ChatColor.RED + "You don't have a crate key in your hand!");
                return;
            }
        }
    
        private static int randomNumber(int min, int max){
            Random rand = new Random();
            int randomNum = rand.nextInt(max) + min;
            return randomNum;
        }
    
    }
    
    And the problem is, that whenever i right click a block that's not a chest, this problems:
    Code:
     ERROR Could not pass event PlayerInteractEvent to AlphaCrates v1.0
    15.03 17:45:25 [Server] INFO org.bukkit.event.EventException
    15.03 17:45:25 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.PlayerInteractManager.a(PlayerInteractManager.java:482) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:888) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:55) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:11) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72]
    15.03 17:45:25 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    15.03 17:45:25 [Server] INFO Caused by: java.lang.NullPointerException
    15.03 17:45:25 [Server] INFO at me.alphagladiator.crates.listeners.CrateOpen.onChestOpen(CrateOpen.java:34) ~[?:?]
    15.03 17:45:25 [Server] INFO at sun.reflect.GeneratedMethodAccessor87.invoke(Unknown Source) ~[?:?]
    15.03 17:45:25 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_72]
    15.03 17:45:25 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_72]
    15.03 17:45:25 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.9.jar:git-Spigot-104c8c8-e43b278]
    15.03 17:45:25 [Server] INFO ... 17 more
    I really hope you can help me, I've driven myself half-insane trying to fix this. Thanks for the help!
     
  2. Offline

    Zombie_Striker

  3. Offline

    MCJoshua345

  4. Offline

    Zombie_Striker

    @MCJoshua345
    Since the stacktrace should be easy to decode, and learning how to read error messages are necessary if you want to program bukkit plugins, what are you having trouble with?
     
  5. Offline

    Edvio

    Just add a check if it's not a chest and then return.
     
  6. Offline

    MCJoshua345

    Line 27
     
  7. Offline

    mcdorli

    Please, don't abuse static

    The problem is, that not all item has an itemMeta, check first, if hasItemMeta is true
     
  8. Offline

    Edvio

    yea, but you have to check if it's not a chest as well, otherwise it will create an error
     
Thread Status:
Not open for further replies.

Share This Page