Help with basic plugin

Discussion in 'Plugin Development' started by moose517, Aug 7, 2011.

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

    moose517

    LOL so i followed a tutorial and had the plugin working, loaded fine, but then i added nijiko's permissions system and now when i start the server the plugin crashes.

    here is what shows in the console
    13:08:49 [INFO] [Basic] Found and will use plugin Permissions v3.1.6
    13:08:49 [SEVERE] Error occurred while enabling Basic v0.1 (Is it up to date?):
    null
    java.lang.NullPointerException
    at me.torrent.basic.Basic.onEnable(Basic.java:38)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:878)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:272)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:162)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:146
    )
    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:284)
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:271)
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)

    and here is the code, nothing spits out any errors in eclipse
    Code:
    package me.torrent.basic;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;
    
    public class Basic extends JavaPlugin {
    	public static Basic plugin;
    	public final Logger logger = Logger.getLogger("Mincraft");
    	private final BasicBlockListener blockListener = new BasicBlockListener(this);
    	public final HashMap<Player, ArrayList<Block>> basicUsers = new HashMap<Player, ArrayList<Block>>();
    	private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
    	public static PermissionHandler permissionHandler;
    	PluginDescriptionFile pdfFile = this.getDescription();
    	@Override
    	public void onDisable() {
    		this.logger.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is disabled!");
    	}
    	@Override
    	public void onEnable() {
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.BLOCK_PLACE, this.blockListener, Event.Priority.Normal, this);
    		setupPermissions();
    		this.logger.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is enabled!");
    	}
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    		if (commandLabel.equalsIgnoreCase("/basic") || commandLabel.equalsIgnoreCase("/b")) {
    			this.logger.info("[Basic] Command issued by " + sender);
    			// see if the user has the permissions to do it
    			if (permissionHandler.has((Player) sender, "basic.basic")) {
    				toggleVision((Player) sender);
    				return true;
    			}
    		}
    		return true;
    	}
    	public boolean isDebugging(final Player player){
    		if (debugees.containsKey(player)) {
    			return debugees.get(player);
    		}
    		else {
    			return false;
    		}
    	}
    	public void setDebugging(final Player player, final boolean value) {
    		debugees.put(player,  value);
    	}
    	public boolean enabled(Player player) {
    		return this.basicUsers.containsKey(player);
    	}
    	public void toggleVision(Player player) {
    		if (enabled(player)) {
    			this.basicUsers.remove(player);
    			player.sendMessage("Basic disabled");
    		}
    		else {
    			this.basicUsers.put(player, null);
    			player.sendMessage("Basic enabled");
    		}
    	}
    	private void setupPermissions() {
    		if (permissionHandler != null) {
    			return;
    		}
    		Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    		if (permissionsPlugin == null) {
    			logger.info("[Basic] Permission system not detected, defaulting to OP");
    			return;
    		}
    		permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    		logger.info("[Basic] Found and will use plugin " + ((Permissions)permissionsPlugin).getDescription().getFullName());
    	}
    }
    
    and the block listener
    Code:
    package me.torrent.basic;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class BasicBlockListener extends BlockListener {
    	public static Basic plugin;
    	public BasicBlockListener(Basic instance) {
    		plugin = instance;
    	}
    
    	public void onBlockPlace(BlockPlaceEvent event) {
    		Player player = event.getPlayer();
    		Block block = event.getBlockPlaced();
    		if ( (block.getType() == Material.TORCH) && (plugin.enabled(player)) ) {
    			player.sendMessage("You placed a torch!");
    		}
    	}
    }
    
     
  2. Offline

    Baummann

  3. Offline

    DrBowe

    Which line is line 38 in your main class? I'm too lazy to count.
     
  4. Offline

    moose517

    this is line 38
    this.logger.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is enabled!");
     
  5. Offline

    DrBowe

    Ah, you spelled Minecraft wrong when you declare your logger, so it's not loading properly (I think) :p
     
  6. Offline

    moose517

    doh haha i see it now

    EDIT: still errors out on line 38
     
  7. Offline

    DrBowe

    @moose517
    Oh the joy of making one small typo and breaking an entire plugin :)
     
  8. Offline

    Shamebot

    Put some debugging lines in there
    Code:
    if(... == null)
    {
        System.out.println("oh no we have a null variable here");
    }
     
  9. Offline

    moose517

    i actually got it LOL. Thanks anyways. I'm now working on a simple kicking plugin so i can see how to do multiple part commands IE /kick moose517 but its not going so well ATM. LOL.
     
  10. Offline

    moose517

    Ok i'm back again, maybe someone can help me. I'm making a simple login MOTD type plugin for use on my server, i know there are many out there its just more for experience with login setup stuff for anything i might do. Anyways, i have the playerjoinevent function in place but when i log in it never appears, but if i type /motd it shows just fine. here is the file

    Code:
    package com.moosemanstudios.LoginMessage;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    import org.bukkit.plugin.Plugin;
    
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    public class LoginMessage extends JavaPlugin {
    	Logger log = Logger.getLogger("minecraft");	// console log
    	public static PermissionHandler permissionHandler;
    	String motd;	// message of the day
    
    	public void onDisable() {
    		PluginDescriptionFile pdfFile = this.getDescription();
    		log.info("[" + pdfFile.getName() + "] is disabled!");
    	}
    	public void onEnable() {
    		// get the configuration file
    		Configuration conf = this.getConfiguration();
    		// check if property exists to avoid overwriting it
    		if (!propertyExists("motd")) {
    			// doesn't exists, set it
    			conf.setProperty("motd", "No Message Of The Day");
    		}
    		// write the config file
    		conf.save();
    		// get the properties
    		motd = conf.getString("motd");
    		setupPermissions();	// setup permissions system
    		PluginDescriptionFile pdfFile = this.getDescription();
    		log.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is enabled.");
    	}
    
    	private boolean propertyExists(String path) {
    		return this.getConfiguration().getProperty(path) != null;
    	}
    
    	public void onPlayerJoin(PlayerJoinEvent event) {
    		Player player = event.getPlayer();
    		player.sendMessage(motd);
    	}
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    		if (commandLabel.equalsIgnoreCase("motd")) {
    			if (permissionHandler.has((Player)sender, "loginmessage.motd")) {
    				sender.sendMessage(motd);
    				return true;
    			} else {
    				sender.sendMessage("You don't have permissions to do that");
    				return false;
    			}
    		}
    		return false;
    	}
    	private void setupPermissions() {
    		if (permissionHandler != null) {
    			return;
    		}
    		Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    		if (permissionsPlugin == null) {
    			log.info("[LoginMessage] Permission system not detected, defaulting to OP");
    			return;
    		}
    		permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    		log.info("[LoginMessage] Found and will use plugin " + ((Permissions)permissionsPlugin).getDescription().getFullName());
    	}
    }
    
     
  11. Offline

    Darkman2412

    You're PlayerJoinEvent need to be in a class that extends the PlayerListener.
    So it will look like this:

    LMPlayerListener (or something):
    Code:java
    1. package com.moosemanstudios.LoginMessage;
    2. //imports//
    3. //[...]//
    4. public class LMPlayerListener extends PlayerListener {
    5. public static LoginMessage plugin;
    6. public LMPlayerListener(LoginMessage instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public void onPlayerJoin(PlayerJoinEvent event) {
    11. Player player = event.getPlayer();
    12. player.sendMessage(motd);
    13. }
    14. }


    Your main class:
    Code:java
    1. package com.moosemanstudios.LoginMessage;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.util.config.Configuration;
    12. import org.bukkit.plugin.Plugin;
    13.  
    14. import com.nijiko.permissions.PermissionHandler;
    15. import com.nijikokun.bukkit.Permissions.Permissions;
    16.  
    17. public class LoginMessage extends JavaPlugin {
    18. Logger log = Logger.getLogger("minecraft"); // console log
    19. private final LMPlayerListener playerListener = new LMPlayerListener(this);
    20. public static PermissionHandler permissionHandler;
    21. String motd; // message of the day
    22.  
    23. public void onDisable() {
    24. PluginDescriptionFile pdfFile = this.getDescription();
    25. log.info("[" + pdfFile.getName() + "] is disabled!");
    26. }
    27. public void onEnable() {
    28. // get the configuration file
    29. Configuration conf = this.getConfiguration();
    30. // check if property exists to avoid overwriting it
    31. if (!propertyExists("motd")) {
    32. // doesn't exists, set it
    33. conf.setProperty("motd", "No Message Of The Day");
    34. }
    35. // write the config file
    36. conf.save();
    37. // get the properties
    38. motd = conf.getString("motd");
    39. setupPermissions(); // setup permissions system
    40. PluginDescriptionFile pdfFile = this.getDescription();
    41. log.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + " is enabled.");
    42. PluginManager pm = this.getServer().getPluginManager();
    43. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
    44. }
    45.  
    46. private boolean propertyExists(String path) {
    47. return this.getConfiguration().getProperty(path) != null;
    48. }
    49.  
    50. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    51. if (commandLabel.equalsIgnoreCase("motd")) {
    52. if (permissionHandler.has((Player)sender, "loginmessage.motd")) {
    53. sender.sendMessage(motd);
    54. return true;
    55. } else {
    56. sender.sendMessage("You don't have permissions to do that");
    57. return false;
    58. }
    59. }
    60. return false;
    61. }
    62.  
    63. private void setupPermissions() {
    64. if (permissionHandler != null) {
    65. return;
    66. }
    67. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    68. if (permissionsPlugin == null) {
    69. log.info("[LoginMessage] Permission system not detected, defaulting to OP");
    70. return;
    71. }
    72. permissionHandler = ((Permissions) permissionsPlugin).getHandler();
    73. log.info("[LoginMessage] Found and will use plugin " + ((Permissions)permissionsPlugin).getDescription().getFullName());
    74. }
    75. }
     
  12. Offline

    moose517

    made the changes you said and it still doesn't display the motd on player join. Any other thoughts?
     
  13. Offline

    Darkman2412

    Didn't see something ^^
    Code:java
    1. package com.moosemanstudios.LoginMessage;
    2. //imports//
    3. //[...]//
    4. public class LMPlayerListener extends PlayerListener {
    5. public static LoginMessage plugin;
    6. public LMPlayerListener(LoginMessage instance) {
    7. plugin = instance;
    8. }
    9.  
    10. public void onPlayerJoin(PlayerJoinEvent event) {
    11. Player player = event.getPlayer();
    12. player.sendMessage(LoginMessage.motd);
    13. }
    14. }
     
  14. Offline

    moose517

    well i caught that part, but i put plugin.motd vs loginmessage.motd

    EDIT: hmmmm so for sh*ts and giggles i decided to send an initial message of hello, it doesn't show up, but the MOTD does then. do i need it to wait a few seconds more first or something?
     
  15. Offline

    thehutch

    should it be

    player.sendMessage("hello type MOTD in here")

    because your sending text so it has to be in quotations. Unless of course your sending a predetermined message from a config or something
     
  16. Offline

    moose517

    plugin.motd contains a string read from the config file that contains the MOTD, it displays it fine, its just i'm having to add a "fake" message to the player first for it to show up. so my onplayerjoin looks like this

    Code:java
    1.  
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. Player player = event.getPlayer();
    4. player.sendMessage("");
    5. player.sendMessage(plugin.motd);
    6. }
     
Thread Status:
Not open for further replies.

Share This Page