Need help cleaning up/fixing my first plugin's code.

Discussion in 'Programming' started by tristianchase, Jun 22, 2016.

  1. Hello,

    I've recently have became interested in coding in Java, and I've started working on my first plugin. I have a question about how to do something, and I am also getting a few errors. If anyone can help that would be fantastic. Thanks!

    Code (Text):
    package me.tristianchase.customjoinmessage;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    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 {

        @Override
        public void onEnable() {
            getLogger().info("Custom Join Message has been enabled.");
            getServer().getPluginManager().registerEvents(this, this);
            saveDefaultConfig();
        }
        @Override
        public void onDisable() {
            getLogger().info("Custom Join Message has been disabled.");
        }
       
        // Make it so the player, that is sent the message, doesn't see the broadcast.
       
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            event.setJoinMessage(null);
            if(!event.getPlayer().hasPlayedBefore()) {
                event.getPlayer().sendMessage(getConfig().getString("HasPlayedBefore"));
            }
            else {
                event.getPlayer().sendMessage(getConfig().getString("HasNotPlayedBefore"));
            }
    }
     
    I can't figure out how to close it properly, I am getting an error on line 37. Also, as you can see my comment, I was wondering if I wanted to make a broadcast message send to everyone on the server except the player who got the custom message, how would I do that? Thanks!
     
  2. MiniDigger

    Supporter

    you have unbalanced brackets. you forgot to close the method with a }.
    iterate over every player online, check if the player is the player that joined, and if not, send the broadcast.

    on a more general note: it is considered a bad practise to send enable and disable messages in your onEnable and onDisable methods because bukkit already does that for you.
     
    • Like Like x 1
  3. @MiniDigger thanks for the speedy reply, and the tips! Very appreciated!
     
  4. @MiniDigger

    Hey, I have updated my code, and it shows as I have not played on the server before every time. I stopped the server and restarted it to see if that would make a difference, but it didn't.

    Code (Text):
    package me.tristianchase.customjoinmessage;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {

        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            saveDefaultConfig();
        }
       
        // Make it so the player, that is sent the message, doesn't see the broadcast.
       
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            event.setJoinMessage(null);
            if(!event.getPlayer().hasPlayedBefore()) {
                event.getPlayer().sendMessage(getConfig().getString("HasPlayedBefore"));
            }
            else {
                event.getPlayer().sendMessage(getConfig().getString("HasNotPlayedBefore"));
            }
        }
    }
     
    Thanks!
     
  5. MiniDigger

    Supporter

    I remember that there are some hickups with the hasPlayedBefore method. it only works well for offline players.
    try checking if
    getLastPlayed is 0.
    you are checking if he has NOT played before. if thats true, you send the hasplayedbefore message. just remove the negation form the condition ^^
     
    • Like Like x 1
    • Agree Agree x 1
  6. You can benefit a whole lot by learning java properly first before jumping into something like the spigot API.
     
    • Like Like x 1
  7. @StalexDev you're right. Any videos/documents you would recommend?
     
  8. I would recommend watching the tutorial series of TheNewBoston on youtube. Be sure to fully understand a topic before moving on to the next video.
     
    • Like Like x 1
  9. Also, you should import org.bukkit.ChatColor instead of net.md_5.bungee.api.ChatColor. The second one refers to the Bungee chat API which is not included in all Bukkit implementations.
     
    • Like Like x 1
  10. @Sataniel thank you! I ended up removing the colors for now, but I'm defiantly going to add them back. Thanks for the help, very appreciated!
     
  11. If you have money, go check out https://www.udemy.com/courses/. Every course has around 50 lectures and they're incredibly helpful.
     
    • Like Like x 1
  12. @bob7l coolio! I'll check it out. Thanks!