I have a problem with PlayerJoinEvent

Discussion in 'Plugin Development' started by fighterhaha, Feb 24, 2017.

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

    fighterhaha

    I try to code a plugin that you can set event messages in the config.
    but It does't work and there was no error in the console

    this is my code
    Code:
    package me.fighterhaha.emplus;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class main extends JavaPlugin implements Listener{
       
        public void onEnable(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable(){
            saveConfig();
        }
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = e.getPlayer();
            if(p.hasPlayedBefore() == false){
                getConfig().getString("firstjoinmessage").replaceAll("<player>", p.getName());
                String firstjoinmessage = getConfig().getString("firstjoinmessage");
                String sfirstjoinmessage = ChatColor.translateAlternateColorCodes('&', firstjoinmessage);
                e.setJoinMessage(sfirstjoinmessage);
            }else if(p.hasPlayedBefore() == true){
                getConfig().getString("normaljoinmessage").replaceAll("<player>", p.getName());
                String normaljoinmessage = getConfig().getString("normaljoinmessage");
                String snormaljoinmessage = ChatColor.translateAlternateColorCodes('&', normaljoinmessage);
                e.setJoinMessage(snormaljoinmessage);
            }
        }
    }
    and this is my config

    Code:
    firstjoinmessage: <player> has joined to this server first time, Take care of him
    normaljoinmessage: <player> has joined to this server
     
  2. Offline

    bwfcwalshy Retired Staff

    @fighterhaha Remove your config stuff in onEnable and just put saveDefaultConfig and if you want the config to actually change on a reload if a user edits remove the onDisable. Then stop comparing a boolean to a boolean, just do if(p.hasJoinedBefore()) and negative that for the other one. You are also using the wrong ChatColor, that will mean the plugin will not work on any Bukkit servers, remove it and use Bukkits. You can also condense that code down a lot and not create variables used one time. Once you have fixed the issues above I will say what the simple problem is.

    You may also want to actually look at some Java tutorials as it's clear you don't actually know it. Bukkit is made in Java so you need to have at least basic knowledge of it.
     
  3. Offline

    fighterhaha

    @bwfcwalshy
    Is there any thing still wrong with my code
    Code:
    package me.fighterhaha.emplus;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class main extends JavaPlugin implements Listener{
       
        public void onEnable(){
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = e.getPlayer();
            if(!p.hasPlayedBefore()){
                getConfig().getString("firstjoinmessage").replace("<player>", p.getName());
                String firstjoinmessage = ChatColor.translateAlternateColorCodes('&', getConfig().getString("firstjoinmessage"));
                e.setJoinMessage(firstjoinmessage);
            }else if(p.hasPlayedBefore()){
                getConfig().getString("normaljoinmessage").replace("<player>", p.getName());
                String normaljoinmessage = ChatColor.translateAlternateColorCodes('&', getConfig().getString("normaljoinmessage"));
                e.setJoinMessage(normaljoinmessage);
            }
        }
    }
    @bwfcwalshy
    I know the way to solve this problem I just forgot to add EventHandler above my playerjoinevent.
    I am so fool and thx for your reply that make my code look better

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 25, 2017
  4. Offline

    timtower Moderator Moderator

    @fighterhaha Please mark the thread as filled.
    And there is a Tahg User button, then you don't need to copy the user url every time (won't alert them)
    Or just type @<name here> without links.
     
  5. Offline

    PQE

    Bretherin from what I can see your code will not work. My man you need to use @EventHandler first off bcuz yeah. Sup man after that what are you doing in this line:
    Code:
    getConfig().getString("firstjoinmessage").replace("<player>", p.getName());
    Bretherin what you want to do is something like this:
    Code:
    String rawMessage = getConfig().getString("firstjoinmessage");
    rawMessage.replace("<player">, p.getName());
    etc.
    
    Methinks that your code would a) not work, or b) call the method replace on your config, and set the value WRANG
     
  6. Offline

    ipodtouch0218

    @PQE
    Actually his would still work, and yours will not. Because Strings in Java are immutable, you'd have to do:
    Code:
    rawMessage = rawMessage.replace("<player>", p.getName())
     
    PQE likes this.
Thread Status:
Not open for further replies.

Share This Page