eclipse Java/Bukkit代码用于存储不工作的Minecraft玩家的坐标

cpjpxq1n  于 2022-11-29  发布在  Eclipse
关注(0)|答案(1)|浏览(161)

我有下面的代码。我正在尝试存储玩家的坐标在Minecraft(Java版)服务器上每秒(20个滴答声)。
服务器是一个“纸”服务器(由Shockbyte在外部托管),代码是使用Bukkit APIMaven工具和Java编程语言创建的。
纸质服务器的版本为:最新论文(Java 17 -当前为1.19.2)。
坐标将存储在文件**'saveTo'中(即存储插件记录的数据的位置)。
虽然我的代码在
eclipse**(我用来修改代码的软件)上运行得很好(没有错误),但当我尝试将其导出为jar文件,然后将其上传到我的服务器(在插件文件夹中)时,它似乎不工作,我不知道为什么。
我会非常感激你的帮助!

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();
        }
    }
}
yvgpqqbh

yvgpqqbh1#

您从不计划任务,而只是定义它。因此,您将没有数据
此外,当您运行Paper服务器时,我建议使用Paper API(它更快,与Bukkit/Spigot几乎相同)
下面是Paper API的maven导入:

<dependency>
   <groupId>io.papermc.paper</groupId>
   <artifactId>paper-api</artifactId>
   <version>1.19.2-R0.1-SNAPSHOT</version>
   <scope>provided</scope>
</dependency>

这里的固定代码(应该工作):

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 = thePlayer.getLocation();
        this.trackerLogger.info(String.valueOf(thePlayer.getName()) + " is logging in!");
        this.trackerLogger.info("Welcome " + thePlayer.getName() + ". Your current position is: " + playerSpawnLocation);
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
            if(this.stopRepeater) {
                this.logToFile(thePlayer, thePlayer.getLocation());
            }
        }, 0L, 20L)
    
    @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.getName());
            if (!dataFolder.exists()) {
                dataFolder.mkdir();
            }
            final File saveTo = new File(dataFolder, String.valueOf(currentPlayer.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();
        }
    }
}

注意:我删除了player.getPlayer()调用,它是多余的,以及Main.this。此外,你可以使用player.getLocation()函数从得到去。
我不知道这是不是有意的,但你应该知道,一旦任何玩家注销,所有玩家的跟踪都会被禁用。如果这是无意的,你可能需要使用带有玩家UUID的Map来防止这种情况。

相关问题