I have the following code. I am trying to store the coordinates of players on a Minecraft (Java edition) server every second (20 ticks).
The server is a 'paper' server (hosted externally by Shockbyte), and the code was created using a Bukkit API with the Maven tool and the Java programming language.
The version of the paper server is: Paper Latest (Java 17 - current 1.19.2).
The coordinates will be stored in the file 'saveTo' (i.e., where the data recorded by the plugin is stored).
Although my code runs fine (with no errors) on eclipse - the software I am using to amend the code - when I try export it as a jar file and then upload it onto my server (in the plugins folder), it doesn't seem to work and I'm not sure why.
I would be so grateful for a helping hand!
package newestfile.here.newestplugin;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.entity.Player;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.Bukkit;
import java.util.logging.Logger;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
public class Main extends JavaPlugin implements Listener
{
Logger trackerLogger;
boolean stopRepeater;
public Main() {
this.trackerLogger = Bukkit.getLogger();
this.stopRepeater = true;
}
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this,this);
this.trackerLogger.info("HELLO! WELCOME TO THE TRACKER PLUGIN");
if(!this.getDataFolder().exists())
{
this.getDataFolder().mkdir();
}
}
public void onDisable() {
this.trackerLogger.info("SHUTTING DOWN!");
}
@EventHandler
public void onLogin(final PlayerJoinEvent event) {
final Player thePlayer = event.getPlayer();
this.stopRepeater = true;
final Location playerSpawnLocation = new Location(thePlayer.getWorld(), (double)thePlayer.getLocation().getBlockX(), (double)thePlayer.getLocation().getBlockY(), (double)thePlayer.getLocation().getBlockZ());
this.trackerLogger.info(String.valueOf(thePlayer.getPlayer().getName()) + " is logging in!");
this.trackerLogger.info("Welcome " + thePlayer.getPlayer().getName() + ". Your current position is: " + playerSpawnLocation);
new BukkitRunnable(){
public void run() {
if (Main.this.stopRepeater) {
Main.this.logToFile(thePlayer, thePlayer.getLocation());
}
}
}.runTaskTimer(this, 0, 20);}
@EventHandler
public void onQuit(final PlayerQuitEvent event) {
this.stopRepeater = false;
this.trackerLogger.info(String.valueOf(event.getPlayer().getName()) + " has left the game");
}
public void logToFile(final Player currentPlayer, final Location playerCurrentLocation) {
try {
final File dataFolder = new File(this.getDataFolder() + File.separator + currentPlayer.getPlayer().getName());
if (!dataFolder.exists()) {
dataFolder.mkdir();
}
final File saveTo = new File(dataFolder, String.valueOf(currentPlayer.getPlayer().getName()) + ".log");
if (!saveTo.exists()) {
saveTo.createNewFile();
}
final Date nowDate = new Date();
final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
final FileWriter fw = new FileWriter(saveTo, true);
final PrintWriter pw = new PrintWriter(fw);
pw.println(String.valueOf(format.format(nowDate)) + " CurrentLocation(x,y,z): " + playerCurrentLocation.getBlockX() + " " + playerCurrentLocation.getBlockY() + " " + playerCurrentLocation.getBlockZ());
pw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
I have the following code. I am trying to store the coordinates of players on a Minecraft (Java edition) server every second (20 ticks).
The server is a 'paper' server (hosted externally by Shockbyte), and the code was created using a Bukkit API with the Maven tool and the Java programming language.
The version of the paper server is: Paper Latest (Java 17 - current 1.19.2).
The coordinates will be stored in the file 'saveTo' (i.e., where the data recorded by the plugin is stored).
Although my code runs fine (with no errors) on eclipse - the software I am using to amend the code - when I try export it as a jar file and then upload it onto my server (in the plugins folder), it doesn't seem to work and I'm not sure why.
I would be so grateful for a helping hand!
Hey all,
My code now works using the following:
plugin.yml:
pom.xml:
source code: