help with issued server command

Discussion in 'Plugin Development' started by guitargun, Sep 8, 2015.

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

    guitargun

    I am trying to remove the message when a player issues a command from the console. I recently added a json library to my plugin and I am trying to prevent spam clicks on the words.
    I figured out that I need to use playercommandpreprocessevent but it doesn't seem to do anything.
    code:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onSpeech(PlayerCommandPreprocessEvent event) {
            if (event.getMessage().contains("speechtrait")) {
                UUID player = event.getPlayer().getUniqueId();
                String[] args = event.getMessage().split(" ");
                int id = Integer.parseInt(args[args.length - 1]);
                String type = args[args.length - 2];
                if (addCach(player, type, id, false)) {
                    event.setCancelled(true);
                }
            }
        }
    
        public boolean addCach(UUID player, String type, int number,
                boolean removing) {
            if (cachedrespone.get(player) != null) {
                if (cachedrespone.get(player).get(type) != null) {
                    if (cachedrespone.get(player).get(type).contains(number)) {
                        if (removing) {
                            cachedrespone.get(player).get(type).remove(number);
                            return false;
                        }
                        return true;
                    } else {
                        if (!removing) {
                            cachedrespone.get(player).get(type).add(number);
                        }
                        return false;
                    }
                } else {
                    if (!removing) {
                        Set<Integer> t = new HashSet<Integer>();
                        t.add(number);
                        cachedrespone.get(player).put(type, t);
                    }
                    return false;
                }
            } else {
                if (!removing) {
                    HashMap<String, Set<Integer>> temp = new HashMap<String, Set<Integer>>();
                    Set<Integer> t = new HashSet<Integer>();
                    t.add(number);
                    temp.put(type, t);
                    cachedrespone.put(player, temp);
                }
                return false;
            }
        }
    and yes I registered the event. I already tried debugging it with a message just below the event.setcaneled and I got that message.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @guitargun It will say the execute always.
    Why not hook into the logger and remove it from there?
     
  3. Offline

    guitargun

    I'll look into it when I can access my pc again. Do you have any tips for hooking into the logger?
     
  4. Offline

    timtower Administrator Administrator Moderator

    guitargun likes this.
  5. Offline

    guitargun

  6. Offline

    timtower Administrator Administrator Moderator

    Saving for crashes?
     
  7. Offline

    guitargun

    For my plugin it handles npcs as well. I don't want those death standing entities. With the close method you can use in the thread you mentioned I can kill those entities
     
  8. Offline

    timtower Administrator Administrator Moderator

    Just do that in the onDisable instead
     
  9. Offline

    guitargun

    @timtower I tried using the thread you provided and found a few problems.
    1. when I add a handler it does not trigger on command uses only when a plugin uses Logger.info.
    2. when I try to make a filter the server just stops working. no stacktrace, just stops.
    I tried decompiling the .jar file but couldn't find it.
     
  10. Offline

    guitargun

    @timtower small bumb but I found out that the issued command is some sort of System.out. now my Java knowledge is not sufficient enough to think about intercepting this print. do you have any ideas
     
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    guitargun

    @timtower
    ehm. when the code comes at the point of logger.addFilter. the server just stops. no stacktrace nothing. even when I remove the temp =. and just use new Filter is just stops working.

    Code:
        public SpeechListener(HUDmain instance) {
    
            System.out.println("getting logger");
            Logger logger = (Logger) LogManager.getRootLogger();
            if (logger != null) {
                Filter temp = logger.getFilters().next();
    
                System.out.println("level set");
                logger.setLevel(Level.ALL);
                System.out.println("adding filter");
                logger.addFilter(temp = new Filter() {
    
                    @Override
                    public Result filter(LogEvent event) {
                        System.out.println("in log event");
                        if (event != null && event.getMessage() != null) {
                            System.out.println(event.getMessage());
                        }
                        return null;
                    }
    
                    @Override
                    public Result filter(Logger arg0, Level arg1, Marker arg2,
                            String arg3, Object... arg4) {
                        System.out.println("filter object");
                        return null;
                    }
    
                    @Override
                    public Result filter(Logger arg0, Level arg1, Marker arg2,
                            Object arg3, Throwable arg4) {
                        System.out.println("filter throw");
                        return null;
                    }
    
                    @Override
                    public Result filter(Logger arg0, Level arg1, Marker arg2,
                            Message arg3, Throwable arg4) {
                        System.out.println("filter message");
                        return null;
                    }
    
                    @Override
                    public Result getOnMatch() {
                        System.out.println("on match");
                        return null;
                    }
    
                    @Override
                    public Result getOnMismatch() {
                        System.out.println("mismatch");
                        return null;
                    }
    
                });
                System.out.println("finished logger");
            }
        }
     
  13. Offline

    timtower Administrator Administrator Moderator

    @guitargun Set it to temp first, then use addFilter(temp)
    Then tell me what is happening.
     
  14. Offline

    guitargun

    @timtower sorry for late response had to catch a bus. the problem is still the same. when I add the filter it just stops.
    edit: when using .getLogger. it doesn't stop the server but it doesn't do anything. I can add the filter but the filter doesn't work (it doesn't fire the debug message)
     
  15. Offline

    timtower Administrator Administrator Moderator

    @guitargun Late response is relative, sometimes somebody is sleeping :p
    I will look at it, try some stuff.
    Will get back to you
     
  16. Offline

    guitargun

    @timtower could you find anything or did you hit a wall as well?
     
  17. Offline

    timtower Administrator Administrator Moderator

    @guitargun My wall is called school at the moment, busy week.
     
  18. Offline

    guitargun

    @timtower found it where it sends it to the console. I only found it in a spigot.jar unfortunately.
    it is indeed a standard system.out.println. which is in the craftbukkit.jar under craftconsolecommandsender.
    so basicly I need to intercept it trough a own printstream.
     
  19. Offline

    timtower Administrator Administrator Moderator

    Not really, the logger is standing in between as far as I know.
     
  20. Offline

    guitargun

    @timtower also found that the logger uses the printstream directly to print messages. Even setting a new printstream using system.setout won't work. Have not found the problem with the filters yet
     
Thread Status:
Not open for further replies.

Share This Page