Error with booleans

Discussion in 'Plugin Development' started by bigbeno37, Jun 23, 2012.

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

    bigbeno37

    Hey there!

    I am developing a plugin which I am currently writing that will protect diamond blocks from anyone else destroying them. Right now, as I am working with commands, I am receiving an error when trying to output the boolean from the onCommand method and getting it to my Listener class successfully. The classes and server.log are below and I have isolated the main error to line 33 in the TestPluginListener class. That is when I try to import the boolean into that method.

    [UPDATED]TestPlugin: http://pastie.org/4141174
    [UPDATED]TestPluginListener: http://pastie.org/4141176
    [UPDATED]Server.log: http://pastie.org/4141181

    Can someone help me determine a possible way to import the boolean from the specific if statement into the TestPluginListener with errors? Just ask for any more info.

    Thanks,
    bigbeno37

    (P.S. When looking at line 33 in TestPluginListener, 'Plugin' is TestPlugin and 'onCommandState' is a method which I built which returns the boolean of the method called 'onCommandBoolean' which returns the boolean from the 'onCommand' method)
     
  2. You get an error because the value of the onCommandState will always return null. to fix this you should create a global variable called commandState or something like that and get that from your main class instead of using that function.

    greetz blackwolf12333
     
  3. Offline

    bigbeno37

    So in the main class, I should have something like Boolean onCommandState = onCommand(null, null, null, null)? And in the Listener I have Boolean onCommandState2 = Plugin.onCommandState?
     
  4. no, wait, i will show you
    this is how your main class should look like:
    Code:
    package com.github.bigbeno37.TestPlugin;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    * @author bigbeno37
    *
    */
     
    public class TestPlugin extends JavaPlugin{
       
        Boolean commandState = false;
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new TestPluginListener(this), this);
            System.out.println("TestPlugin has been enabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("TestPlugin Protect")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "ERROR: /TestPlugin [Protect / StopProtect]");
                    commandState = false;
                    return false;
                }else{
                    sender.sendMessage(ChatColor.GREEN + "You're protecting diamond blocks!");
                    commandState = true;
                    return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("TestPlugin StopProtect")){
                sender.sendMessage(ChatColor.GREEN + "You're not protecting diamond blocks!");
                commandState = true;
                return false;
            }
            return false;
        }
       
        public Boolean getCommandState() {
            return commandState;
        }
       
        public void onDisable(){
            System.out.println("TestPlugin has been disabled");
        }
    }
    and than your listener should look like this:
    Code:
    package com.github.bigbeno37.TestPlugin;
     
    import java.util.HashMap;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    * @author bigbeno37
    *
    */
     
    public class TestPluginListener extends JavaPlugin implements Listener{
       
        TestPlugin plugin;
       
        public TestPluginListener(TestPlugin p){
            p = plugin;
        }
       
        private HashMap<Location, String> blockProtection = new HashMap<Location, String>();
       
        //Listener for blocks placed.
        @EventHandler
        public void DiamondBlockPlaceEvent(BlockPlaceEvent event){
            Boolean onCommand = plugin.getCommandState();
            Location l = event.getBlockPlaced().getLocation();
               
            if(onCommand == true){
                if(event.getBlock().getType() == Material.DIAMOND_BLOCK){
                    blockProtection.put(l, event.getPlayer().getName());
                    event.getPlayer().sendMessage(ChatColor.GREEN + "You have protected a diamond block");
                }else{
                    event.getPlayer().sendMessage(ChatColor.RED + "To start protecting blocks, type in '/TestPlugin Protect'");
                }
            }
        }
       
        //Listener for blocks broke.
        @EventHandler
        public void DiamondBlockBreakEvent(BlockBreakEvent event){
            Location l = event.getBlock().getLocation();
            Player player = event.getPlayer();
           
            if(blockProtection.containsKey(event.getBlock().getLocation())){
                if(blockProtection.get(event.getBlock().getLocation()).equals(event.getPlayer().getName())){
                    event.getPlayer().sendMessage(ChatColor.GREEN + "You destroyed your diamond block");
                    blockProtection.remove(l);
                }else{
                    if(player.hasPermission("TestPlugin.Override")){
                        event.setCancelled(false);
                        event.getPlayer().sendMessage("You have overriden the protection");
                        System.out.println("[TestPlugin] " + event.getPlayer().getName() + " overrid the protection.");
                        blockProtection.remove(l);
                    }else{
                        event.setCancelled(true);
                        event.getPlayer().sendMessage(ChatColor.RED + "You cannot destroy protected blocks");
                        System.out.println("[TestPlugin] " + event.getPlayer().getName() + " tried to destroy a protected diamond block");
                    }
                }
            }
        }
    }
    Notice that i now use a function which gets a global variable which is set in the onCommand function.
    Sorry for the long post:p

    greetz blackwolf12333
     
  5. Offline

    bigbeno37

    Thanks for the reply blackwolf,

    I am still receiving a NullPointerException. This means that somewhere in there I am using a null instead of an object. Do you have any more suggestions?
     
  6. Offline

    hammale

    lemme c the error
     
  7. Offline

    bigbeno37

    The original post has been updated with the server.log and the two classes to include the suggested fix.
     
  8. Code:java
    1. TestPlugin Plugin;
    2.  
    3. public TestPluginListener(TestPlugin p){
    4. p = Plugin;
    5. }

    has to be:
    Code:java
    1. TestPlugin Plugin;
    2.  
    3. public TestPluginListener(TestPlugin p){
    4. Plugin = p;
    5. }
     
Thread Status:
Not open for further replies.

Share This Page