Hey all, I have the following code. I am trying to save player coordinates in a file within a folder called 'playerdata'. However, when I run the code the 'playerdata' folder appears but the file does not appear within that folder... The code worked perfectly last week. Not sure why it is not working now? I have tried re-creating and re-uploading the '.jar' file onto the Shockbyte external server that I am using to host the game but this does not seem to work. I have also checked the wifi etc but this also does not seem to work. Is there something I could change with the code? Would be so grateful for a helping hand! Code: package newestfile.here.newestplugin; import java.util.UUID; import java.nio.charset.StandardCharsets; import java.nio.file.StandardOpenOption; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.io.File; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.entity.Player; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.Bukkit; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import org.bukkit.inventory.ItemStack; import java.nio.file.Files; public class Main extends JavaPlugin implements Listener { boolean stopRepeater; HashMap<UUID, BukkitTask> tasks = new HashMap<>(); private File dataFolder; public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this,this); getLogger().info("HELLO! WELCOME TO THE TRACKER PLUGIN"); dataFolder = new File(getDataFolder(), "playerdata"); dataFolder.mkdirs(); } public void onLogin(final PlayerJoinEvent event) { final Player thePlayer = event.getPlayer(); this.stopRepeater = true; final Location playerSpawnLocation = thePlayer.getLocation(); getLogger().info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation); BukkitTask task = getServer().getScheduler().runTaskTimer(this, () -> { if(this.stopRepeater) { this.logToFile(thePlayer, thePlayer.getLocation()); } }, 0L, 20L); tasks.put(thePlayer.getUniqueId(),task);} public void onQuit(final PlayerQuitEvent event) { Player thePlayer = event.getPlayer(); if(!thePlayer.isOnline()) { this.stopRepeater = false; getLogger().info(String.valueOf(event.getPlayer().getName()) + " has left the game"); BukkitTask task = tasks.remove(thePlayer.getUniqueId()); if(task != null) { task.cancel(); } } } public void logToFile(final Player currentPlayer, final Location playerCurrentLocation) { try { String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())) + " CurrentLocation(x,y,z): " + playerCurrentLocation.getBlockX() + " " + playerCurrentLocation.getBlockY() + " " + playerCurrentLocation.getBlockZ() + " Current Yaw: " + currentPlayer.getEyeLocation().getYaw() + " Current Pitch: " + currentPlayer.getEyeLocation().getPitch() + "Heading Direction:" + currentPlayer.getEyeLocation().getDirection() + "\n"; if (!getDataFolder().exists()) { getDataFolder().mkdirs(); } final File file = new File(dataFolder, currentPlayer.getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log"); if (!file.exists()) { file.createNewFile(); } Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND); } catch (Exception e) { e.printStackTrace(); } } }
Thank you so much for your support - I have changed my code by adding an 'event handler' and it now works Code: package newestfile.here.newestplugin; import java.util.UUID; import java.nio.charset.StandardCharsets; import java.nio.file.StandardOpenOption; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.io.File; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.entity.Player; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.Bukkit; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; import org.bukkit.inventory.ItemStack; import org.bukkit.event.EventHandler; import java.nio.file.Files; public class Main extends JavaPlugin implements Listener { private boolean stopRepeater; private HashMap <UUID, BukkitTask> tasks = new HashMap<>(); private File dataFolder; @EventHandler public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this,this); getLogger().info("HELLO! WELCOME TO THE TRACKER PLUGIN"); dataFolder = new File(getDataFolder(), "newdata"); dataFolder.mkdirs(); } @EventHandler public void onLogin(final PlayerJoinEvent event) { final Player thePlayer = event.getPlayer(); this.stopRepeater = true; final Location playerSpawnLocation = thePlayer.getLocation(); getLogger().info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation); BukkitTask task = getServer().getScheduler().runTaskTimer(this, () -> { this.logToFile(thePlayer, thePlayer.getLocation()); }, 0L, 20L); tasks.put(thePlayer.getUniqueId(),task);} @EventHandler public void onQuit(final PlayerQuitEvent event) { Player thePlayer = event.getPlayer(); if(!thePlayer.isOnline()) { this.stopRepeater = false; getLogger().info(String.valueOf(event.getPlayer().getName()) + " has left the game"); BukkitTask task = tasks.remove(thePlayer.getUniqueId()); if(task != null) { task.cancel(); } } } @EventHandler public void logToFile(final Player currentPlayer, final Location playerCurrentLocation) { try { String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())) + " CurrentLocation(x,y,z): " + playerCurrentLocation.getBlockX() + " " + playerCurrentLocation.getBlockY() + " " + playerCurrentLocation.getBlockZ() + " Current Yaw: " + currentPlayer.getEyeLocation().getYaw() + " Current Pitch: " + currentPlayer.getEyeLocation().getPitch() + "Heading Direction:" + currentPlayer.getEyeLocation().getDirection() + "\n"; if (!getDataFolder().exists()) { getDataFolder().mkdirs(); } final File file = new File(dataFolder, currentPlayer.getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log"); if (!file.exists()) { file.createNewFile(); } Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND); getLogger().info("File saved at " + file.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.