Bukkit如何更改配置文件中的int,然后能够再次更改它而无需重新加载(自定义配置文件类.) [英] Bukkit How to change an int in the config file then be able to change it again without reloading (Custom config file class.))

查看:77
本文介绍了Bukkit如何更改配置文件中的int,然后能够再次更改它而无需重新加载(自定义配置文件类.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在为OP-Prison服务器创建自定义功能,我需要做的一件事情就是从players.yml文件中获取一个整数,检查它是否> =一个,如果是删除一个,保存它,然后如果它仍然高于1,则他们可以重复该操作,直到它为0. 问题在于我必须重新启动服务器才能更改文件,即使我这样做,它一次也只会下降一个整数,而不必再次重新加载.

Okay so I am making a custom feature for my OP-Prison server, one of the things that I need to do is get an integer from the players.yml file, check if it is >= one, if it is take away one, save it and then if it is still above one then they can repeat the action untill it's 0. The issue comes with the fact that I have to restart the server for the file to change, and even when I do, it will only go down by one integer at a time, before having to reload it again.

GUI创建代码:

Main main = Main.getPlugin(Main.class);

@SuppressWarnings("unused")
private FileControl fc;

@SuppressWarnings("unused")
private FileControl playerfc;

public static String inventoryname = Utils.chat(Main.pl.getFileControl().getConfig().getString("Backpacks.White.InventoryName"));

public List<Player> WhiteOpened = new ArrayList<>();

public static Inventory whiteBackpack(Player player) {
    Inventory whiteBackpack = Bukkit.createInventory(null, 27, (inventoryname));
    UUID uuid = player.getUniqueId();

    whiteBackpack.setItem(10,
            new ItemCreator(Material.INK_SACK).setData(8)
                    .setDisplayname(Utils.chat("&fCommon Packages &8» &f&l" + Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common")))
                    .getItem());

    return whiteBackpack;
}

单击Commonpackage时更新config +项目的代码:

Code for updating the config + item when the Commonpackage is clicked:

@EventHandler
public void whiteBackpackInteract(InventoryClickEvent event) {

    Player player = (Player) event.getWhoClicked();
    UUID uuid = player.getUniqueId();
    ItemStack clicked = event.getCurrentItem();
    String title = event.getInventory().getName();

    if (title.equals(inventoryname)) {
        // Making it so that the item cannot be moved
        event.setCancelled(true); 

        if (clicked != null) {

            if (event.getSlot() == 10) {

                // Getting the user's common packages section in the config and checking if it is greater than or equal to 1.
                if (Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common") >= 1) { 

                    // Saving the user's common package section to 'currentCommon'
                    Integer currentCommon = Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common");

                    // Taking away one from 'currentCommon' and saving it to 'newCommon'
                    Integer newCommon = currentCommon - 1;

                    // Getting the 'players.yml' file
                    File file = new File(main.getDataFolder(), "players.yml");
                    FileConfiguration config = YamlConfiguration.loadConfiguration(file);

                    // Checking if the current common keys is greater than or equal to 1
                    if (currentCommon >= 1) {
                        try {

                            //Now, Here's where the error lies.
                            //Gets the player's common package count and sets it to the 'newCommon' count
                            config.set("Users." + uuid + ".Packages.Common", newCommon);
                            //Saves the players.yml file
                            config.save(file);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // Updates the inventory they're currently in (Atleast it's meant to...)
                        player.updateInventory();
                        // Sends them a message (This is just for testing purposes, making sure it's working.)
                        player.sendMessage(Utils.chat("&8(&9Vexil&8) &fCommon Package"));
                    }
                }
            }
        }
    }
}

如果您需要任何其他代码,请问我会很乐意为您提供.

If there is any other code that you need, just ask I'll happily provide it for you.

推荐答案

您正在使用Main.getPlugin();

You are using Main.getPlugin();

现在,虽然这看起来似乎不是一件坏事,但是您得到了一个未分配的变量,我不知道它是如何工作的,但是您正在将Main分配给Main.实际获得主类有两种正确的方法.

Now while that doesn't seem like such a bad thing, your getting an unassigned variable, I have no idea how it is working but you're assigning Main to Main. There are 2 proper ways to actually get the main class.

第一种也是通常最好的方法是使用依赖项注入.

The first, and generally best way, is to use dependency injection.

所以基本上,

public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
        BackpackListener listener new Backpacklistener(this);
        getServer().getPluginManager().registerEvents(listener, this);
    }
}

public class BackpackListener implements Listener {
     private Main instance;
     private BackpackUtil util;

     public BackpackListener(Main instance) {
         this.instance = instance;
         util = new BackpackUtil();
     }

     @EventHandler
     public void onClick(InventoryClickEvent event) {
         //code
         util.whiteBackpack(instance);
     }
public class BackpackUtil {
    public Inventory whiteBackpack(Main instance) {
        FileConfiguration config = instance.getConfig();
        //Do things
        instance.saveConfig();
    }
}

您可以通过另一种方法来达到最佳效果,并对此表示反对,但仍然是一个更简单的选择.

The next way you can do it is less optimal, and frowned upon, but still an easier option.

public class Main() {
    public static Main instance;
    @Override
    public void onEnable() {
        instance = this;
    }
}

public class ConfigHelper() {
    Main instance = Main.instance;
    FileConfiguration config = instance.getConfig();
    //Do things
    instance.saveConfig();
}

摆脱使用第二种方法(称为单例)的习惯是一件好事,因为通常主类会更改,或者具有多个实例,等等.但是在Spigot中,只能有一个主实例和一个线程.

It's good to get out of the habit of using the second method (It's called a singleton), because normally the main class will change, or have multiple instances, etc... but with Spigot there can only be one main instance and one thread.

这篇关于Bukkit如何更改配置文件中的int,然后能够再次更改它而无需重新加载(自定义配置文件类.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆