在Java插件上使用dataFolder时出现问题-找不到文件夹的保存位置

x9ybnkn6  于 2022-12-10  发布在  Java
关注(0)|答案(2)|浏览(166)

我想将Minecraft游戏的输出(玩家坐标)保存到名为player.log的文件中。
如果我的玩家用户名是“abc”,则文件名应为“abce.log”。
然而,我找不到这个文件在我的服务器上的存储位置-它不在我希望它在的插件文件夹中。
我已经附上了下面的截图,我想在哪里保存文件(这是远程外部shock-byte服务器插件文件夹)。
下面的代码:

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.event.EventHandler;
import org.bukkit.entity.Player;
import org.bukkit.Location;
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.BukkitTask;
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(), "data"); 
        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, () -> {
            if(this.stopRepeater) {
                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();
            }
        }
    }

    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() + "\n";
            final File file = new File(dataFolder, currentPlayer.getName() + ".log");
            if (!file.exists()) {
                file.createNewFile();
            }
            Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
}

  • *“latest.log”**文件的输出:

rqqzpn5f

rqqzpn5f1#

事实上,你的插件是由server.jar执行的,所以文件在与spigot.jar相同的目录下产生
Example

cgvd09ve

cgvd09ve2#

我的代码最后当我的**'plugin.yml'' pom.xml'**文件包含以下信息时。
我现在可以在我的plugins文件夹中找到一个嵌套的“player.log”文件,它可以打印出播放器坐标,如下所示:

09-12-2022 09:55:21 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:22 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:22 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:23 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:23 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:24 CurrentLocation(x,y,z): 47 -50 41
09-12-2022 09:55:26 CurrentLocation(x,y,z): 47 -50 43
09-12-2022 09:55:26 CurrentLocation(x,y,z): 46 -50 46
09-12-2022 09:55:27 CurrentLocation(x,y,z): 45 -49 45
09-12-2022 09:55:28 CurrentLocation(x,y,z): 46 -50 42
09-12-2022 09:55:29 CurrentLocation(x,y,z): 47 -50 37
09-12-2022 09:55:30 CurrentLocation(x,y,z): 47 -49 33
09-12-2022 09:55:31 CurrentLocation(x,y,z): 46 -49 30

插件.yml:

name: newestplugin
main: newestfile.here.newestplugin.Main 
version: 0.0.1-SNAPSHOT
api-version: 1.19

pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>newestfile.here</groupId>
  <artifactId>newestplugin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
   <resources>
     <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>plugin.yml</include>
        </includes>
        <filtering>true</filtering>
     </resource>
   </resources>   
    <pluginManagement>
      <plugins>
        <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.10.1</version>       
              <configuration>
                  <release>17</release>
              </configuration>   
        </plugin>
      </plugins>
    </pluginManagement>
</build>
   <repositories>
       <repository>
          <id>papermc-repo</id>
          <url>https://papermc.io/repo/repository/maven-public/</url>
       </repository>
   </repositories>
    <dependencies>
       <dependency>
          <groupId>io.papermc.paper</groupId>
          <artifactId>paper-api</artifactId>
          <version>1.19.2-R0.1-SNAPSHOT</version>
          <scope>provided</scope>
       </dependency>
   </dependencies>
</project>

相关问题