我有下面的代码。我正在尝试将玩家坐标保存在一个名为**'playerdata'的文件夹中。
然而,当我运行代码时,“playerdata”文件夹出现,但文件没有出现在该文件夹中**...
代码上周运行得很好。不知道为什么现在不工作了?
我试过重新创建和重新上传'.jar'文件到我用来托管游戏的Shockbyte外部服务器上,但似乎不起作用。我还检查了wifi等,但似乎也不起作用。
我可以修改代码吗?
会如此感激你的帮助!
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();
}
}
}
1条答案
按热度按时间zysjyyx41#
我已经设法找到了解决办法。
我添加了“事件处理程序”来指定事件出现的时间,现在文件可以正确保存了!