Simple Login message

Discussion in 'Plugin Development' started by saturnine, Mar 17, 2011.

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

    saturnine

    Can someone please show me the Plugin.java and PluginPlayerListener.java code for a simple join message.


    Ideally it would: - Show a broadcast message when enabled.
    - Send a message saying "Server is running plugin." to the player that joins.


    I'm pulling my hair out atm because I can't even get this to load apparently. (According to Essentials /plugin list), and I've looked over the code a dozen times.

    If I can get this working then I can figure the rest of the code out myself I think. I just need to be able to get the thing running so I can test and debug.

    Thanks in advance. I'll be happy to paste the code I currently have if needed.
     
  2. Offline

    SunShe

    Code:
          // Player Join
          public void onPlayerJoin(PlayerEvent event){
              Player player = event.getPlayer();
              player.sendMessage("Server is running plugin.");
          }
    And dont forget that on "OnEnable()"
    Code:
    pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
     
  3. Offline

    saturnine

    Thanks for the quick reply. That looks exactly like the code I have:

    PluginPlayerListener:
    Code:
    public void onPlayerJoin(PlayerEvent event) {
            String storeIP;
            storeIP = "10.";
            event.getPlayer().sendMessage("Password plugin is running.");
            event.getPlayer().sendMessage("DEBUG: Player IP is: " + event.getPlayer().getAddress().getAddress().getHostAddress());
            event.getPlayer().sendMessage("DEBUG: Store IP variable is: " + storeIP);
    }
    ^ I had event.getPlayer() set to a variable before like: Player player = event.getPlayer(); It didn't work that way either, not sure if it matters either way.

    Plugin:
    Code:
    public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Type.PLAYER_JOIN, this.playerListener, Priority.Monitor, this);
            getServer().broadcastMessage("Password plugin initialized.");
     
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            PluginDescriptionFile pdfFile = getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
        }
    I've tried Priority.Normal, and importing just Event and using Event.Type, etc. I've changed and re-changed a lot. There aren't any errors, and I've imported all the relevant dependencies.


    I've also added the plugin.yml file in the correct format to the JAR.


    Thanks!
     
  4. Offline

    Edward Hand

    Are you getting any errors in the server console when the server starts?
     
  5. Offline

    saturnine

    Hard to tell since I'm running MineOS. I'll need to set up on another port to check, give me 5 minutes.

    I can't paste the output, but it throws a "Could not load plugin: null org.bukkit.etc.etc InvalidPLuginException.
    Caused by: java.lang.ClassNotFoundException com.ruagamer.ExternalPassword

    It throws similar errors for Permissions, but it loads fine.

    If I need to set up a debug server on my VPS so I have more flexibility to test I will.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 12, 2016
  6. Offline

    Edward Hand

    I use a server on my local machine (connecting through minecraft with 127.0.0.1) for testing plugins. That's nice and simple.

    There are a few common reasons to get that error:
    1. Make sure you are not using com.bukkit or org.bukkit or anything.bukkit as the start of your namespace
    2. Make sure your plugin.yml file is correctly formatted in the correct place
    3. Make sure you do not have a constructor function in your JavaPlugin class.
     
  7. Offline

    saturnine

    ExternalPassword.java
    Code:
    package com.ruagamer.ExternalPassword;
    
    //import java.io.File;
    import java.util.HashMap;
    import org.bukkit.entity.Player;
    //import org.bukkit.Server;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.event.Event.Type;
    // import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    //import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    /**
     * ExternalPassword for Bukkit
     *
     * @author RUaGamer
     */
    public class ExternalPassword extends JavaPlugin {
        private final ExternalPasswordPlayerListener playerListener = new ExternalPasswordPlayerListener(this);
        private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
     
        public void onEnable() {
            // TODO: Place any custom enable code here including the registration of any events
    
            // Register our events
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Monitor, this);
            getServer().broadcastMessage("Password plugin initialized.");
     
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            PluginDescriptionFile pdfFile = getDescription();
            System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
        }
        public void onDisable() {
            // TODO: Place any custom disable code here
    
            // NOTE: All registered events are automatically unregistered when a plugin is disabled
    
            // EXAMPLE: Custom code, here we just output some info so we can check all is well
            System.out.println("ExternalPassword unloaded.");
        }
        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);
        }
    }
    ExternalPasswordPlayerListener.java
    Code:
    
    package com.ruagamer.ExternalPassword;
    
    import java.lang.String;
    //import java.io.*;
    //import java.util.*;
    //import org.bukkit.*;
    //import org.bukkit.Location;
    //import org.bukkit.entity.Player;
    //import org.bukkit.event.player.PlayerChatEvent;
    //import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerEvent;
    // import org.bukkit.event.player.PlayerMoveEvent;
    
    /**
     * Handle events for all Player related events
     * @author RUaGamer
     */
    public class ExternalPasswordPlayerListener extends PlayerListener {
        //private final ExternalPassword plugin;
    
        public ExternalPasswordPlayerListener(ExternalPassword instance) {
            //this.plugin = instance;
        }
        public void onPlayerJoin(PlayerEvent event) {
            String storeIP;
            storeIP = "10.";
            event.getPlayer().sendMessage("Password plugin is running.");
            event.getPlayer().sendMessage("DEBUG: Player IP is: " + event.getPlayer().getAddress().getAddress().getHostAddress());
            event.getPlayer().sendMessage("DEBUG: Store IP variable is: " + storeIP);
     
            //if (!playerIP.startsWith(storeIP)) {
            //    player.sendMessage("Please enter this months password: /password <password>");
         // }
      }
    
    }
    plugin.yml (In the root of the Jar file)
    Code:
    name: ExternalPassword
    main: com.ruagamer.ExternalPassword
    version: 0.1

    Anything commented out is simply code that I'm not implementing until I get it to load at least. Same with imports.

    I'm sure it's just something stupid I've done. Never done any Java coding before this. I'm working off the video tutorials and code examples on the site here, as well as looking at the source of other plugins.

    I'll get a dev. environment set up to make this easier in the future.

    Thanks!
     
  8. Offline

    eltorqiro

    The ClassNotFoundException is because it is trying to load com.ruagamer.ExternalPassword but that does not exist. That is what you have specified in your plugin.yml - please make sure that is actually the class, not the package, that should be used for the plugin (the one that extends JavaPlugin).

    Typically you'd use something like com.ruagamer.externalpassword as your package name, then your class will be ExternalPassword, making your plugin.yml main look like this:

    main: com.ruagamer.externalpassword.ExternalPassword

    I am assuming that you haven't named your package just com.ruagamer.

    EDIT: Oh, i see you posted while I was posting - yes, your package is com.ruagamer.ExternalPassword and your main class is ExternalPassword, so simply modify your plugin.yml to refer to this, i.e. :

    main: com.ruagamer.ExternalPassword.ExternalPassword
     
  9. Offline

    saturnine

    I'll give that a try.

    IT WORKS!

    You're my hero.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 12, 2016
  10. Offline

    SunShe

    Warning! You calling 3 times "event.getPlayer()", that's not good and it decrease performences for nothing, here it can feel nothing but if everyone code like that, performences can be fast decreased. Please, directly take good habits.
     
  11. Offline

    saturnine

    Yeah I know it's sloppy code. Just using it to debug some things atm. I'll polish it all up once the plugin is working as intended.

    And don't worry, the world doesn't have to fear lag from me. I very much doubt I'll get into public plugin programming. The only reason I'm doing this is because no one that already knows coding seemed interested in the functionality I need.
     
  12. Offline

    SunShe

    It's ok dw. i still like you [​IMG]
     
  13. Offline

    Tim Visee

    Great! It's simple but i love it!
     
Thread Status:
Not open for further replies.

Share This Page