Custom Plugin

Discussion in 'Plugin Development' started by kieranmclean, Jun 2, 2013.

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

    kieranmclean

    Hi, I'm quite new to making a bukkit plugin and I'm having 1 small issue... I made a bandage plugin (when you right click with a piece of paper you get a potion effect and it also deletes 1 item for the stack, ) I did that myself but....


    Everything works fine until I hit a block it comes up with a huge error here. Here's the PLUGIN code

    Code:
    package me.kieran.bandage;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    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.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Bandages extends JavaPlugin implements Listener {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Bandages plugin;
       
        @Override
        public void onEnable(){
            log.info("Bandages has been enabled.");
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        @Override
        public void onDisable(){
            log.info("Bandages has been disabled.");
           
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            if(event.getItem().getType() == Material.PAPER) {
                Player player = event.getPlayer();
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 50, 1));
                player.sendMessage(ChatColor.GREEN + "You have applied a bandage");
               
                player.getInventory().removeItem(new ItemStack[] { new ItemStack(339, 1) });
               
            }
           
           
        }
    }

    And here's the error.

    Code:
    [SEVERE] Could not pass event PlayerInteractEvent to Bandages v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_5_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:186)
        at net.minecraft.server.v1_5_R3.PlayerInteractManager.interact(PlayerInteractManager.java:370)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:631)
        at net.minecraft.server.v1_5_R3.Packet15Place.handle(SourceFile:58)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
        at me.kieran.bandage.Bandages.onPlayerInteract(Bandages.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 16 more
    I've seen problems with NullPointerException on other plugins how can I fix this?

    Thanks!
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    kieranmclean You get a null pointer exception, when you try to perform an illegal operation on a null pointer (such as trying to invoke a method on it). You can avoid the null pointer exception by checking if the return value of a method is null before you invoke a method.
     
  3. Offline

    kieranmclean

    Thanks man.

    So I have to add == null? If so what line

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Offline

    Sagacious_Zed Bukkit Docs

    Read the stack trace, it tells you what line it encountered the NPE
     
  5. Offline

    Rungario

    You never defined what to do if the if statement isn't true on line 34
     
  6. Offline

    Zach_1919

    kieranmclean Just make an if statement before the one checking if the item is paper saying this:
    Code:
    if(event.getItem() != null)
    {
       if(event.getItem().equals(Material.PAPER))
       {
          //DO STUFF
       }
    }
    
     
  7. Offline

    Tirelessly

    It would be better to check the action first, then use the player's item in hand. Don't want people consuming paper when going over a pressure plate.
     
  8. Offline

    kieranmclean

    Code:
    package me.kieran.bandage;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Bandages extends JavaPlugin implements Listener {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Bandages plugin;
       
        @Override
        public void onEnable(){
            log.info("Bandages has been enabled.");
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        @Override
        public void onDisable(){
            log.info("Bandages has been disabled.");
           
        }
       
        ArrayList<Player> cooldown = new ArrayList<Player>();
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            if (event.getItem() != null && event.getItem().getType() == Material.PAPER){
                final Player player = event.getPlayer();
               
                if(cooldown.contains(player)) {
                    player.sendMessage(ChatColor.RED + "You cannot apply another bandage just yet.");
                    return;
                }
               
                cooldown.add(player);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                   
                    public void run(){
                        cooldown.remove(player);
                        player.sendMessage(ChatColor.GREEN + "You can now apply another bandage.");
                    }
                }, 180);
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 100, 1));
                player.sendMessage(ChatColor.GREEN + "You have applied a bandage to yourself.");
               
                player.getInventory().removeItem(new ItemStack[] { new ItemStack(339, 1) });
            }
        }
    }
    This is what I got now, anything I could do to make it better?
     
  9. Offline

    GreySwordz

    add another if at the very beginning of the method, with:
    Code:
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
     
  10. Offline

    kieranmclean

    Ah Thank you! just added it.

    Oh, and also how would I be able to rename the item so when people come across it they know it's a bandage and not just a piece of paper :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page