Staffchat problem

Discussion in 'Programming' started by RemiG06, Mar 22, 2020.

  1. I made a staffchat plugin, the Name of the players shows up and the [STAFFCHAT] does too, but the message does not show up because i don't know what the thing is for the chat

    for(Player all : Bukkit.getOnlinePlayers()){
    if (all.hasPermission("crystal.sc")) {
    all.sendMessage(ChatColor.BLUE + "[STAFFCHAT] " + ChatColor.GREEN + sender.getName() + ": " + msgcommand);

    What should it say where i put msgcommand?
     
  2. package me.crystalpvp.core;

    import javax.script.ScriptEngine;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerChatEvent;

    public class StaffChat implements CommandExecutor
    {
    @EventHandler
    public void onChat(ChatEvent e)
    {
    ProxiedPlayer p = (ProxiedPlayer) e.getSender();
    String message = e.getMessage();
    if(!message.startsWith("@")) return;
    if(message.startsWith("@"))
    {
    e.setCancelled(true);
    String msg = ChatColor.translateAlternateColorCodes('&', "[StaffChat] "+p.getName()+": "+message.replaceFirst("@", ""));
    for(ProxiedPlayer players : ProxyServer.getInstance().getPlayers())
    {
    players.sendMessage(new TextComponent(msg));
    }
    }
    }

    public void onEnable() {

    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Starting");
    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Successfully loaded the plugin.");

    }

    public void onDisable() {

    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SC] Stopped!");


    }

    public boolean onCommand(CommandSender sender, Command cmd, String list, String args[]) {

    if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.RED + "You cannot use the commands from console!");
    return true;
    }

    Player player = (Player) sender;

    if(cmd.getName().equalsIgnoreCase("staffchat")) {
    if(!sender.hasPermission("crystal.sc")) {
    sender.sendMessage(ChatColor.RED + "You are not permitted to do that!");
    return true;
    }
    if(args.length == 0) {
    sender.sendMessage(ChatColor.GRAY + "Usage: /staffchat (msg)");
    return true;

    }


    for(Player all : Bukkit.getOnlinePlayers()){
    if (all.hasPermission("crystal.sc")) {
    all.sendMessage(ChatColor.BLUE + "[STAFFCHAT] " + ChatColor.GREEN + sender.getName() + ": " + getPlayerMessage?);

    }
    }


    }

    return true;

    }

    }
     
  3. 1: when showing code in here try using
    (remove the spaces) [ code ] Code(); [ /code ]

    2: you need to add an implementer "Listener"
    3: you also need to extends "JavaPlugin"

    Try use something like this this:
    Code (Java):

    String message = "";
    for(int i = 0; i < args.length; i++){
         message+=args[i+1] + " ";
    }
     
    Here you should be able to get all text that comes after "/staffchat".
     
  4. (Player all : Bukkit.getOnlinePlayers()){
    if (all.hasPermission("crystal.sc")) {
    all.sendMessage(ChatColor.BLUE + "[STAFFCHAT] " + ChatColor.GREEN + sender.getName() + ": " + String.message);

    }
    }

    what do i type where i put string.message?
    i put in this code

    upload_2020-3-22_12-50-12.png
     
  5. Bredo showed you how to get the message
     
  6. Code (Java):


    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;

    public class ds implements CommandExecutor {
        @EventHandler
        public void onChat(ChatEvent e) {
            ProxiedPlayer p = (ProxiedPlayer) e.getSender();
            String message = e.getMessage();
            if (!message.startsWith("@"))
                return;

            if (message.startsWith("@")) {
                e.setCancelled(true);
                String msg = ChatColor.translateAlternateColorCodes('&',
                        "[StaffChat] " + p.getName() + ": " + message.replaceFirst("@", ""));
                for (ProxiedPlayer players : ((List) ProxyServer.getInstance()).getPlayers()) {
                    players.sendMessage(new TextComponent(msg));
                }
            }
        }

        public void onEnable() {
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Starting");
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Successfully loaded the plugin.");
        }

        public void onDisable() {
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SC] Stopped!");
        }

        public boolean onCommand(CommandSender sender, Command cmd, String list, String args[]) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You cannot use the commands from console!");
                return true;
            }

            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("staffchat")) {
                if (!sender.hasPermission("crystal.sc")) {
                    sender.sendMessage(ChatColor.RED + "You are not permitted to do that!");
                    return true;
                }

                if (args.length == 0) {
                    sender.sendMessage(ChatColor.GRAY + "Usage: /staffchat (msg)");
                    return true;
                }
                        String message = "";
                        for (int i = 0; i < args.length; i++) {
                            message += args[i + 1] + " ";
                        }
                for (Player all : Bukkit.getOnlinePlayers()) {
                    if (all.hasPermission("crystal.sc")) {
                        all.sendMessage(
                                ChatColor.BLUE + "[STAFFCHAT] " + ChatColor.GREEN + sender.getName() + ": " + message);
                    }
                }
            }
            return true;
        }
    }
     
    This should work
     
  7. am i suppost to get these errors? upload_2020-3-22_14-11-32.png
     
  8. hmm
    if it still gives an error, it must be your code or imports
    Code (Java):

    package me.crystalpvp.core;

    import javax.script.ScriptEngine;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerChatEvent;

    public class StaffChat implements CommandExecutor
    {
    @EventHandler
    public void onChat(ChatEvent e)
    {
    ProxiedPlayer p = (ProxiedPlayer) e.getSender();
    String message = e.getMessage();
    if(!message.startsWith("@")) return;
    if(message.startsWith("@"))
    {
    e.setCancelled(true);
    String msg = ChatColor.translateAlternateColorCodes('&', "[StaffChat] "+p.getName()+": "+message.replaceFirst("@", ""));
    for(ProxiedPlayer players : ProxyServer.getInstance().getPlayers())
    {
    players.sendMessage(new TextComponent(msg));
    }
    }
    }

    public void onEnable() {

    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Starting");
    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Successfully loaded the plugin.");

    }

    public void onDisable() {

    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SC] Stopped!");


    }

    public boolean onCommand(CommandSender sender, Command cmd, String list, String args[]) {

    if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.RED + "You cannot use the commands from console!");
    return true;
    }

    Player player = (Player) sender;

    if(cmd.getName().equalsIgnoreCase("staffchat")) {
    if(!sender.hasPermission("crystal.sc")) {
    sender.sendMessage(ChatColor.RED + "You are not permitted to do that!");
    return true;
    }
    if(args.length == 0) {
    sender.sendMessage(ChatColor.GRAY + "Usage: /staffchat (msg)");
    return true;

    }

     String message = "";
                        for (int i = 0; i < args.length; i++) {
                            message += args[i + 1] + " ";
                        }

    for(Player all : Bukkit.getOnlinePlayers()){
    if (all.hasPermission("crystal.sc")) {
    all.sendMessage(ChatColor.BLUE + "[STAFFCHAT] " + ChatColor.GREEN + sender.getName() + ": " + message);

    }
    }


    }

    return true;

    }

    }
     
    I just copied your first code and added the message loop.
     
  9. You haven't imported the types you're using. Press Ctrl+Shift+O.
     
  10. nothing to import
     
  11. upload_2020-3-22_14-18-32.png
     

    Attached Files:

  12. If you're using the code Bredo posted, change this line:
    Code (Text):
    String message = "";
    for(int i = 0; i < args.length; i++){
    >    message+=args[i+1] + " ";
    }
    to this:
    Code (Text):

    String message = "";
    for(int i = 0; i < args.length; i++){
     >   message+=args[i] + " ";
    }
     
  13. my mistake added the +1 by a mistake
     
  14. upload_2020-3-22_14-23-53.png
    still doesnt work
     
  15. I haven't done Bungee plugins, but isn't ProxiedPlayer a Bungee thing? You need to change your types over to Bukkit/Spigot.
     
  16. The constructor TextComponent(String) is not visible, List cannot be resolved to a type, ProxiedPlayer cannot be resolved to a type, ChatEvent cannot be resolved to a type are the errors
     

    Attached Files:

  17. ProxyServer.getInstance()) doesnt work either :/