为一个基本的Minecraft插件得分

时间:2015-11-21 22:54:42

标签: java plugins minecraft

我在找到一个好的,可靠的方法来保存我的插件时遇到问题。我搜索了很多,但没找到任何可以使用的东西。

我已经开始了,虽然只是将数据保存到文本文件中,正如您可以从文件编写器中看到的那样,我认为这将是计算机密集型的,每秒不断写入一个文件数十次。

我虽然使用字符串来做这件事,但是没有找到任何方法让它使用用户名加载金额,或者找出如何找到前3名最高/

如果你需要它,这是我的代码:

package me.Dankrushen.ShiftGame;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginDescriptionFile;

public class ShiftGame extends JavaPlugin implements Listener{

    public final Logger logger = Logger.getLogger("Minecraft");
    public static ShiftGame plugin;

    public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " v" + pdfFile.getVersion() + " Has Been Disabled.");
    }

    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " v" + pdfFile.getVersion() + " Has Been Enabled.");
        initialiseConfig();
    }

    public void initialiseConfig(){
        final FileConfiguration config = this.getConfig();
        config.options().copyDefaults(true);
        this.saveDefaultConfig();
    }

    @EventHandler
    public void onPlayerSneak(PlayerToggleSneakEvent e){
        Player player = e.getPlayer();
        String name = player.getDisplayName();

        if(e.isSneaking() == false){
            try {
                PrintWriter out;
                System.out.print("\nWriting to data file.");
                out = new PrintWriter("PlayerShiftAmounts.txt");
                out.println(name);
                out.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
                System.out.print("\nCreating file...");
                File file = new File("PlayerShiftAmounts.txt");
                try {
                    file.createNewFile();
                } catch (IOException e11) {
                    e11.printStackTrace();
                }
                if(!file.exists()){
                    System.out.print("\nFailed to create file.");
                }
                else{
                    System.out.print("\nCreated file successfully!");
                    System.out.print("\nTrying to write to file again...");
                    try {
                        PrintWriter out;
                        out = new PrintWriter("PlayerShiftAmounts.txt");
                        out.println(name);
                        out.close();
                    } catch (FileNotFoundException e2) {
                        e2.printStackTrace();
                        System.out.print("\nGiving up writing to file.");
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Minecraft有一些配置文件类:FileConfigurationYamlConfiguration,所以我认为你不需要使用java.io. *,并且真的不用担心文件写作。

  

FileConfiguration config = YamlConfiguration.loadConfiguration(file);

如果你确定必须每秒多次记录一些东西,我认为你应该尝试使用缓存或只是用Java创建一个变量,而不是每次都写入文件或数据库..我不知道为什么你需要记录哪个玩家按下shift的列表?