Help with chat.

Discussion in 'Spigot Plugin Development' started by nickelectron, May 20, 2017.

Thread Status:
Not open for further replies.
  1. May sound like a noobish question but this one stumps me.
    So I have a staff chat feature in my plugin that publishs the message to redis if the player is authorized to use the staff chat and is in staff chat mode (hashset w/ players uuid.).

    Now when I use staff chat w/ the command it works fine (sending, pubing, and subbing). But the listener doesn't work and yes its registered in the main class.

    Code for command and listener.
    Code (Text):

    @RequiredArgsConstructor
    public class StaffChatCommand implements Listener {

        private final API plugin;

        private Set<UUID> staffChatMap = Sets.newHashSet();

        @Command(name = "staffchat", aliases = {"sc", "ac", "gsc", "lsc"}, inGameOnly = true, permission = "staff")
        public void onCommand(CommandArgs args){
            Player sender = args.getPlayer();
            if (args.length() == 0){
                if (staffChatMap.contains(sender.getUniqueId())){
                    staffChatMap.remove(sender.getUniqueId());
                    sender.sendMessage(ChatColor.RED + "You have toggled out of staff chat mode.");
                    return;
                } else {
                    staffChatMap.add(sender.getUniqueId());
                    sender.sendMessage(ChatColor.GREEN + "You are now in staff chat mode.");
                    return;
                }
            }
            if (args.length() > 0){
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < args.length(); ++i){
                    sb.append(String.valueOf(args.getArgs(i)) + " ");
                }
                final String message = sb.toString().trim();
                new StaffChat(sender.getName(), message, plugin.getServer().getServerName());
            }
        }

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onChatSpeak(AsyncPlayerChatEvent event){
            Player player = event.getPlayer();
            String msg = event.getMessage();
            if (staffChatMap.contains(player.getUniqueId())){
                event.setCancelled(true);
                 new StaffChat(player.getName(), msg, plugin.getServer().getServerName());
            }
        }
    }
     
    Code for constructing a staff chat message:
    Code (Text):

    @Getter
    @Setter
    public class StaffChat {

        private final API plugin = API.getPlugin();

        private String sender;
        private String message;
        private String server;

        public StaffChat(String sender, String message, String server) {
            this.sender = sender;
            this.message = message;
            this.server = server;
            createAndSendStaffChat();
        }

        private void createAndSendStaffChat() {
            JsonObject object = new JsonObject();
            object.addProperty("action", Actions.STAFFCHAT.name());

            JsonObject payload = new JsonObject();
            payload.addProperty("message", message);
            payload.addProperty("sender", sender);
            payload.addProperty("server", server);

            object.add("payload", payload);

            plugin.getPublisher().pub(object.toString());

            plugin.getLogger().info(plugin.getPrefix() + "Sent " + Actions.STAFFCHAT.name() + " to the API. Payload -> " + object.toString());
        }
    }
     
    Any help would be appreciated.
    Thanks!
     
  2. Did you register the correct listener in the main class?
     
  3. Is the event canelling?
     
  4. Yes
     

  5. Send us your main class

    Edit: use this HashSet instead Set<UUID> staffChatMap = new HashSet<UUID>();

    If this doesn't work, use debug messages to get your problem.
     
    #5 SteviePlaysMc4, May 20, 2017
    Last edited: May 20, 2017
  6. Code (Text):

    public class API extends JavaPlugin {

        @Getter
        private static API plugin;
        @Getter
        private final String prefix = "[API Bukkit] -> ";
        @Getter
        private CommandFramework commandFramework;
        @Getter
        private JedisConnection connection;
        @Getter
        private Publisher publisher;

        private JedisSubscriber subscriber;
        @Getter
        private Configuration configuration;

        @Override
        public void onEnable() {
            plugin = this;
            saveDefaultConfig();
            setupJedis();
            sendWelcomeMessageToAPI();
            startHeartbeatTask();
            commandFramework = new CommandFramework(this);
            registerCommands();
            Cooldowns.createCooldown("report");
            Cooldowns.createCooldown("request");
        }

        private void registerCommands() {
            commandFramework.registerCommands(new ReportCommand(this));
            commandFramework.registerCommands(new RequestCommand(this));
            commandFramework.registerCommands(new StaffChatCommand(this));
            getServer().getPluginManager().registerEvents(new StaffChatCommand(this), this);
        }

        private void startHeartbeatTask() {
        //REDACTED
        }

        private void sendWelcomeMessageToAPI() {
            //REDACTED
        }

        private void setupJedis() {
          //REDACTED
        }

        private void sendGoodbyeMessageToAPI() {
        //REDACTED
        }

        @Override
        public void onDisable() {
            sendGoodbyeMessageToAPI();
            connection.getPool().close();
            plugin = null;
        }
    }
     
     
  7. Try moving the this after registering the command . So it will look like this

    getServer().getPluginManager().registerEvents(new StaffChatCommand, this);
    }
     
  8. Mas

    Mas

    You are creating two instances of the StaffChatCommand class, then you register one as a listener and one as a command.

    Create one instance of the class and pass it to both methods instead.
     
    • Like Like x 1
Thread Status:
Not open for further replies.