1.8.8 Help

Discussion in 'Spigot Plugin Development' started by QuackSporty, Aug 16, 2021.

  1. I'm wanting to get a typed message via a command, how can I do this? I've seen something like that

    EDIT
    If the player executes a command, I want to ask him if he really wants to save, if he types "yes" in chat, he will save. However, I want to do this through a method that already calls the event and the direct save function, using Consumer, but I don't know how to do that.

    Code (Text):

    method(message -> {
       player.sendMessage(message);
    });
     
     
    #1 QuackSporty, Aug 16, 2021
    Last edited: Aug 16, 2021
  2. You want what?
     
  3. /say message
     
  4. /tellraw vanilla
     
  5. If the player executes a command, I want to ask him if he really wants to save, if he types "yes" in chat, he will save. However, I want to do this through a method that already calls the event and the direct save function, using Consumer, but I don't know how to do that.
     
  6. That's not it, but thanks anyway!
     
  7. You can have a set of UUIDs and add the player's UUID to it when you send the message asking them if they want to save. Then in the AsyncPlayerChatEvent, check if they are in the set and if they have typed "yes", and if they did then do the saving or whatever you want to do.
     
    • Agree Agree x 1
  8. Do you mean something like this?
    Code (Java):
    //when the player executes the command
    //perhaps store the consumer somewhere
    Consumer<String> consumer = message -> /*save or whatever*/

    //somewhere where you check the input from the player
    //the passed in consumer is the one you created earlier
    public void isValidInput(String userInput, Consumer<String> consumer) {
        //if userInput is yes
        consumer.accept(userInput);
    }
    There are many similar solutions, this is just one.
    Sometimes it's also useful to make your own functional interface instead of using a consumer to give more clear method and variable names.
     
  9. Upon sending a message to the player, add his UUID to the list. Then listen to AsyncPlayerChatEvent and check if the list contains the UUID of the event player. In that case, check whether the message is "yes" or "no" (or even better "y"/"n") and work with it.
     
  10. At this point he might as well use a BooleanPrompt instead of reinventing the wheel.
     
  11. I want to do this, but using the Consumer, directly in command.
     
  12. I tried to do this, when executing a command, I call a method with the consumer parameter (Consumer<AsyncPlayerChatEvent>), then I add it to a Map and in the event part I check if the player is on that Map, if it exists, it calls the consumer.accept, but it doesn't work.
     
  13. That way I know how to do it, but I want to do it using the Consumer.
     
  14. Did you register a listener where you accept the consumer (the point where spigot calls your event and you supply that to the consumer)?
     
  15. Yes, look at the code:
    Code (Text):

    public class Main extends JavaPlugin implements CommandExecutor {

        private HashMap<Player, Consumer<AsyncPlayerChatEvent>> map = new HashMap<>();
       
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            method((Player)sender, consumer -> {
               consumer.getPlayer().sendMessage(consumer.getMessage());
            });
            return true;
        }

        @EventHandler
        public void event(AsyncPlayerChatEvent event){
            Player player = event.getPlayer();
            if(map.containsKey(player)){
                map.get(player).accept(event);
            }
        }
       
        public void method(Player player, Consumer<AsyncPlayerChatEvent> consumer){
            map.put(player, consumer);
        }
       
    }
     
     
  16. You never set the executor of the command to be your Main class (you should also rename this class; "Main" isn't the best name).
     
  17. In this case, I just sent an example of how my code is, the command is registered yes, it is sending the message normally, saying to type "yes" and the class name is not "Main", I just put it as "Main" in my code example.
     
  18. I want to do this using Consumer, I was wondering why it's not working with this code. But thanks anyway, I'll check it out later.