Reading keyboard input on bukkit

Discussion in 'Plugin Development' started by caledonian26, Apr 18, 2023.

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

    caledonian26

    Hey all,

    I have the following code which will display a message when a player logs into the minecraft server (bukkit/spigot API):

    Code:
    @EventHandler
        public void onLogin(final PlayerJoinEvent event) {
            final Player thePlayer = event.getPlayer();
            final Location playerSpawnLocation = thePlayer.getLocation();
            getLogger().info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation);  
    I want the log to record when a player presses the keyboard, and so I have added 3 lines to the code in order to record the time point at which a key is pressed in the players log file using 'Scanner'.

    Code:
    @EventHandler
        public void onLogin(final PlayerJoinEvent event) {
            final Player thePlayer = event.getPlayer();
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            System.out.println("Keyboardpress:"+ i + "Player:" + event.getPlayer().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.sss").format(new Date())));
            final Location playerSpawnLocation = thePlayer.getLocation();
            getLogger().info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation);  
    However, when I add these 3 lines of code, the system crashes.

    I would be so grateful for a helping hand as to where I may be going wrong!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @caledonian26 You can't.
    Scanner is the terminal.
    You don't get those client side events on the server.
     
  3. Offline

    caledonian26

    I would like to synchronise my game with the fMRI machine - therefore, I need to record when the player presses a key on the keyboard.

    Is there not a way to bypass the system?
     
  4. Offline

    timtower Administrator Administrator Moderator

    There is not
     
  5. Offline

    Strahan

    The only possible way I can think of would be to get them to use a Forge mod. Forge mods run client side, and you could have it communicate with your server to relay when keys are pressed. Granted, I've never made a Forge mod so I'm making assumptions there but at least that would be local code. Seems like a lot of work though, guess it depends how important it is to you. Also having to convince your users to use a Forge client and trust your mod is another hurdle.
     
Thread Status:
Not open for further replies.

Share This Page