onPlayerJoin help?

Discussion in 'Plugin Development' started by jkctech, Apr 25, 2014.

Thread Status:
Not open for further replies.
  1. Hello, im currently working on a login plugin, but i have something which isnt working.
    when the player joins, it should receive a message, but instead it does nothing.
    as far as i know, ive registered the events.
    this is the code:
    Code:java
    1. package me.jkctech.JKCLogin;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener{
    18.  
    19. List<Player> hasLoggedIn = new ArrayList<Player>();
    20. List<Player> needToLogin = new ArrayList<Player>();
    21.  
    22. public void loadConfiguration(){
    23. getConfig().options().copyDefaults(true);
    24. saveConfig();
    25. }
    26.  
    27. @Override
    28. public void onEnable(){
    29. PluginDescriptionFile ymlFile = this.getDescription();
    30. this.getLogger().info(ymlFile.getName() + " Version " + ymlFile.getVersion() + " is enabled!");
    31. this.getLogger().info("This plugin is made by jkctech!");
    32. this.getServer().getPluginManager().registerEvents(this, this);
    33. loadConfiguration();
    34. }
    35.  
    36. @Override
    37. public void onDisable(){
    38. PluginDescriptionFile ymlFile = this.getDescription();
    39. this.getLogger().info(ymlFile.getName() + ymlFile.getVersion() + " is now disabled!");
    40. }
    41.  
    42. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    43. final Player p = (Player) sender;
    44. if(label.equalsIgnoreCase("register") || label.equalsIgnoreCase("login")){
    45. if(args.length == 1){
    46. if(label.equalsIgnoreCase("register")){
    47. if(getConfig().contains("players."+p.getName())){
    48. p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.DARK_RED+"You are already registered!");
    49. } else {
    50. getConfig().set("players."+p.getName(), p.getName());
    51. getConfig().set("players."+p.getName()+".password", args[0]);
    52. saveConfig();
    53. p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.GREEN+"You are now registered!");
    54. }
    55. }else if(label.equalsIgnoreCase("login")){
    56. String configValue = getConfig().getString("players."+p.getName()+".password");
    57. if(configValue != null && configValue.equals(args[0])) {
    58. hasLoggedIn.add(p);
    59. p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.GREEN+"You are now logged in!");
    60. this.getLogger().info(p.getName()+" has logged in!");
    61. } else {
    62. p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.DARK_RED+"Incorrect password!");
    63. }
    64. }
    65. }
    66. }
    67. return true;
    68. }
    69.  
    70. @EventHandler
    71. public void onplayerjoin(PlayerJoinEvent event){
    72. Player p = event.getPlayer();
    73. if(getConfig().getString("players").contains(p.getName())){
    74. p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.DARK_RED+"You need to login!");
    75. }
    76. }
    77.  
    78. }


    i also want that if the player is in the config and not in array, all his events are canceled.
    i know i have to do this for every single event, but im not sure what events there are and what their syntaxes are.
    could somebody maybe create a macro-like thingy which i can copy?
    thanks in advance!
     
  2. Offline

    Zethariel

    Gecco1234 AdamQpzm It's cute how you guys didn't even read the code. Hint: line 32

    Not sure if you can register every single event - you could try with just the base Event and see if it gets captured with an @EventHandler.
     
    badboysteee98 and AdamQpzm like this.
  3. Zethariel Oops, my mistake. I read the loadConfiguration() method as the onEnable() :p
     
  4. jkctech Have you tried putting a debug message before the if?
     
  5. Offline

    Zethariel

    jkctech
    getConfig().getString("players").contains(p.getName()) is your problem. You are returning a string, not a string list. use getStringList or similar

    EDIT:
    Also, the way you register players, you need to seek the path "players."+event.getName() or whatever it is you set it to. Please read up on how YML data is stored and how to access it.
     
  6. Zethariel AdamQpzm ive added this debug code:
    Code:java
    1. p.sendMessage(ChatColor.GREEN+getConfig().getString("players"));
    2. p.sendMessage(ChatColor.GREEN+p.getName());

    the getconfig part returns a strange thing: MemorySection[path='players', root='YamlConfiguration']
    the second part returns jkctech, which is correct.
    what am i doing wrong?
    also, im an autistic 14 YO XD, can somebody make an example code for me?
     
  7. Offline

    Zethariel

    jkctech Who you are and what age is kinda irrelevant, but whatevs >.>
    MemorySection means that the path you are accessing does not contain a value, and instead contains child nodes. You need to access the child nodes and read those values.
    As said before, read the documentation about YamlConfiguration, storing data in YML files in general. The Bukkit Wiki is a good place to start.
     
  8. Zethariel AdamQpzm
    this is my YAML code:
    Code:
    players:
      Notch:
        password: HELLEU!
      jkctech:
        password: lolz
    please, i cant seem to get it working, can somebody help me with this?
     
  9. jkctech Try this, instead of your if:

    Code:
    if(getConfig().contains("players." + p.getName()) { //your code }
    Then read this and switch to UUIDs.
     
  10. AdamQpzm hello, ive got this code:
    Code:
    @EventHandler
        public void onplayerjoin(PlayerJoinEvent event){
            Player p = event.getPlayer();
            if(getConfig().contains("players." + p.getName())){
           hasToLogin.add(p);
           p.sendMessage(ChatColor.GOLD+"[JKC Login] "+ChatColor.DARK_RED+"You need to login!");
            }
        }
    //Alle Events
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent event) {
    Player player = event.getPlayer();
    String name = player.getName();
    Location from = event.getFrom();
    Location to = event.getTo().clone();
     
    if (hasToLogin.contains(name)) {
    to.setX(from.getX());
    to.setZ(from.getZ());
    event.setTo(to);
    }
    }
    but they both only partialy work, the onplayerjoin works, if i am in the config, i will get a message and i will be added to array.(im not sure if im actually added to array, because i dont know how to debug it.)
    the onmoveevent also works, but only if i remove the if statement.
    so i think i messed up the array.
    what am i doing wrong with the array?
    ive created it like this:
    Code:
        List<Player> hasLoggedIn = new ArrayList<Player>();
        List<Player> hasToLogin = new ArrayList<Player>();
    thanks in advance!
     
Thread Status:
Not open for further replies.

Share This Page