easy plugin create file

Discussion in 'Plugin Development' started by Lexter_, Jan 27, 2022.

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

    Lexter_

    Hello,

    I'm trying to create a simple plugin that would allow me to count the number of players and write it in a txt file, only, it doesn't work : the plugin loads on the server side but doesn't create the txt file or even when a player joins/leaves but creates a null file when I stop the server and disable the plugin.

    Is this a normal behavior or did I miss something in my code?


    Code:
    package test.test;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.file.Files;
    
    public final class Test extends JavaPlugin implements Listener {
    
        int num = 0;
        @Override
        public void onEnable() {
            // Plugin startup logic
    
                // Recevoir le fichier
                File f = new File("nbPlayers.txt");
    
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
    
            num++;
    
           try {
               //ecrire le fichier
               FileWriter myWriter = new FileWriter("nbPlayers.txt");
               myWriter.write(num);
               myWriter.close();
           }
               catch (IOException ex){
                System.out.println("an error occured");
                ex.printStackTrace();
            }
    
        }
    
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            num--;
            try {
                FileWriter myWriter = new FileWriter("nbPlayers.txt");
                myWriter.write(num);
                myWriter.close();
            } catch (IOException ex) {
                System.out.println("an error occured");
                ex.printStackTrace();
            }
           
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
            num = 0;
            try {
                FileWriter myWriter = new FileWriter("nbPlayers.txt");
                myWriter.write(num);
                myWriter.close();
            } catch (IOException ex) {
                System.out.println("an error occured");
                ex.printStackTrace();
            }
        }
    }
     
  2. Online

    timtower Administrator Administrator Moderator

    @Lexter_ You didn't register the event
     
  3. Offline

    Lexter_

    H
    Hello timtower and thx for your answer.
    I think you are tellin about this line ?
    getServer().getPluginManager().registerEvents(new Test(), this);
    I am new to programming in general
    I added it but it doesn't work
     
  4. Online

    timtower Administrator Administrator Moderator

    @Lexter_ That line yes, but the parameters are "this, this" for this one as your main class is also the listener.
     
  5. Offline

    Lexter_

    @timtower it returns me upload_2022-1-28_20-2-55.png is this like normal for you ?
     
  6. Online

    timtower Administrator Administrator Moderator

    @Lexter_ Might be writing bytes, and not values.
     
Thread Status:
Not open for further replies.

Share This Page