Plugin Help Need help with my Staff Chat plugin.

Discussion in 'Plugin Help/Development/Requests' started by JohnathanPlays, Jul 27, 2015.

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

    JohnathanPlays

    Hello. I'm very new to making plugins and need some help. I'm trying to make a Staff Broadcast plugin. How do I make it so that anyone with the permission node "staffchat.recieve" can see broadcasts, and anyone without the permission node can't? Also, how can I make the command /sc <message> broadcast the <message> that a player with the permission node "staffchat.send" types in? For example:

    (both players have both permission nodes)
    Player1 types in /sc Hello!
    Player2 sees [StaffChat] Player1: Hello!

    Or

    Player1 types in /sc Staff Chat is Awesome!
    Player2 sees [StaffChat] Player1: Staff Chat is Awesome!

    Here is my main code:
    Code:
    package me.JohnathanPlays.StaffChat;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Staffchatmain extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
       
    public void onEnable(){
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Successfully Enabled!");
        }
    
    public void onDisable(){
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Successfully Disabled!");
       
        }
            public boolean onCommand(CommandSender sender, Command command,
                    String label, String[] args) {
                if(label.equalsIgnoreCase("sc")){
                    ((Player) sender).sendMessage("Not Working ATM, stand by for updates!");
                }
                return false;
    
            }
    
    }
    

    Thanks, in advance!
     
  2. @JohnathanPlays You have learnt from YouTube. Probably BcBroz. I can tell because the code has many not needed things and general bad practices. I would fix them before working on your plugin.

    Issues:
    1. Use Bukkit's logger not Java's
    2. You don't need to do enable and disable messages, Bukkit does it for you.
    3. Use cmd.getName() not label.
    4. Check before casting player. Look at @mine-care's signature.
     
    mine-care likes this.
  3. Offline

    JohnathanPlays

    I'm sorry for being so noobish, but i'm not sure on how to use bukkit's logger, or how to use cmd.getName. I'm also not sure if I set up if (sender instanceof Player) { Player p = (Player) sender; (DoStuff)} correctly. Here is an updated staffchatmain

    Code:
    package me.JohnathanPlays.StaffChat;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Staffchatmain extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
       
            public boolean onCommand(CommandSender sender, Command command,
                    String label, String[] args) {
                if (sender instanceof Player) { Player p = (Player) sender; }{
                    ((Player) sender).sendMessage("Not Working ATM, stand by for updates!");
                }
                return false;
    
            }
    
    }
    
     
  4. @JohnathanPlays Remove the }{ after sender; then you can change ((Player) sender).sendMessage... to p.sendMessage

    And look at my logger comment.
     
  5. Offline

    mine-care

    @JohnathanPlays

    Hmm, umm the reason why @bwfcwalshy recomended you read my signature to find out what is wrong with your code is because you had this line:
    Code:
    ((Player)sender).sendMessage("Not Working ATM, stand by for updates!");
    
    Lets break it down a bit, This line is functionally the same as:
    Code:
    Player p = (Player)sender;
    p.sendMessage("Not Working ATM, stand by for updates!");
    
    what this ^ does is:
    - Cast sender to Player, its like telling java that i cast an unknown object to something more specific like an apple.
    - Use this "apple" to perform an operation.
    but the thing is that we are not sure if this object is indeed an apple, and to get in grips with your code, we dont know if the sender (CommandSender) is a Player, it may be ConsoleSender and that is the server console.
    so we need to check if indeed the sender is a player before we cast it to one and use it.
    Read the code above carefully :- ) You may also format it to a more readabe format like:
    Code:
    if(sender instanceof Player){
    Player p = (Player)sender;
    }
    
    Now about the logger, umm instead of getting it the way you do already ^ you may use the method "getLogger" from the superclass JavaPlugin so lets say you want to log a message in the console:
    getLogger().info("This is an info message!!");
    Otherwise if you are not working in the main class of the plugin, and you need a logger you can use "Bukkit#getLogger()" it is practically the same thing.

    Also bwfcwalshy recomended you use "cmd.getName()" instead of "label" and that is to allow alias support when you set an alias command in Plugin.yml.

    Lastly Please follow java naming conventions both on your package and class names.

    I recomend you take a look into the following resources:
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
    https://howtoprogramwithjava.com/java-cast/
    https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
    http://www.oracle.com/technetwork/java/codeconventions-135099.html
     
  6. Offline

    JohnathanPlays

    Thank you, i'll get working. I'll check back when I'm done and I know it all works.
     
  7. Offline

    BizarrePlatinum

    @JohnathanPlays Just as simple as iterating through the online players and testing permissions. If they have them, send them the message.
     
  8. Offline

    JohnathanPlays

    Okay, so I ended up getting a "Programming in Java for Dummies" and I got it working using all of your guys's methods! Thanks for the help :D
     
Thread Status:
Not open for further replies.

Share This Page