我想在我的systemmanager(设置管理器)中使用默认设置创建多配置文件,我尝试使用默认设置(如插件中的配置文件)创建badwords文件。但它不起作用,我想这是不可能的,我需要设置我尝试在系统管理器中设置它们的默认值,但它给出了一个错误。 Soo如何为我的自定义配置设置像配置这样的默认值?我希望你明白。如果自定义配置文件不存在,则将使用默认值创建自定义配置文件。这是我的系统管理员:
package me.herobrine112211.ulticmdpack;
import java.io.File;
import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
public class SystemManager {
private SystemManager() {}
static SystemManager instance = new SystemManager();
public static SystemManager getInstance(){
return instance;
}
Plugin p;
// Config
FileConfiguration config;
File cfile;
// BadWords
FileConfiguration badwordsdata;
File badwordsfile;
public void setup(Plugin p){
// Config
cfile = new File(p.getDataFolder(), "config.yml");
config = p.getConfig();
if(!cfile.exists()){
config.options().copyDefaults(true);
saveConfig();
Bukkit.getServer().getLogger().severe(ChatColor.RED + "UltimateCommandPack CONFIG does not exist! Creating new config!");
}
// BadWords
badwordsfile = new File(p.getDataFolder(), "badwords.yml");
badwordsdata = YamlConfiguration.loadConfiguration(badwordsfile);
if(!badwordsfile.exists()){
badwordsdata.options().copyDefaults(true);
savebadwordsdata();
Bukkit.getServer().getLogger().severe(ChatColor.RED + "UltimateCommandPack BADWORDS does not exist! Creating new badwords!");
}
}
// Config
public FileConfiguration getConfig(){
return config;
}
public void saveConfig(){
try {
config.save(cfile);
} catch (IOException e) {
Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
}
}
public void reloadConfig(){
config = YamlConfiguration.loadConfiguration(cfile);
}
// BadWords
public FileConfiguration getbadwordsdata(){
return badwordsdata;
}
public void savebadwordsdata(){
try {
badwordsdata.save(badwordsfile);
} catch (IOException e) {
Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save badwords.yml!");
}
}
public void reloadbadwordsdata(){
badwordsdata = YamlConfiguration.loadConfiguration(badwordsfile);
}
}
如果您需要,这是我的项目(插件)完整代码:
package me.herobrine112211.ulticmdpack;
import java.util.ArrayList;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class System extends JavaPlugin implements Listener{
SystemManager SysMng = SystemManager.getInstance();
ArrayList<String> frozen = new ArrayList<String>();
ArrayList<Player> cooldown = new ArrayList<Player>();
public static Economy econ = null;
public void onEnable() {
SysMng.setup(this);
if (!setupEconomy()){
getLogger().severe(String.format("[%s] - Disabled because vault was not found!", getDescription().getName()));
getServer().getPluginManager().disablePlugin(this);
return;
}
Bukkit.getServer().getPluginManager().registerEvents(this, this);
Bukkit.getServer().getLogger().info(ChatColor.GREEN + "Ultimate Command Pack Plugin has been actived!");
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "ANNOUNCEMENT" + ChatColor.WHITE + "]" + ChatColor.YELLOW + "Server creator: Herobrine112211!");
}
}, 100, 1400);
}
private boolean setupEconomy(){
if (getServer().getPluginManager().getPlugin("Vault") == null){
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if(rsp == null){
return false;
}
econ = rsp.getProvider();
return econ !=null;
}
public void onDisable() {
Bukkit.getServer().getLogger().info(ChatColor.GREEN + "All files have been saved!");
Bukkit.getServer().getLogger().info(ChatColor.GREEN + "Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent joinevent){
Player getplayer = joinevent.getPlayer();
getplayer.sendMessage(ChatColor.AQUA + "Hey " + getplayer.getName() + "! Welcome to the Ultimate Prison server!");
// Spawning player in spawn location
if(SysMng.getConfig().getConfigurationSection("spawn") == null){
getplayer.sendMessage(ChatColor.RED + "Spawn is not set!. Report this problem to owner INSTANTLY!");
}
World w = Bukkit.getServer().getWorld(SysMng.getConfig().getString("spawn.world"));
double x = SysMng.getConfig().getDouble("spawn.x");
double y = SysMng.getConfig().getDouble("spawn.y");
double z = SysMng.getConfig().getDouble("spawn.z");
getplayer.teleport(new Location(w, x, y, z));
// ----------------------------------------------------------------
if(getplayer.getName().equals("Herobrine112211")){
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.GOLD + "Server Creator Herobrine112211 has joined the game!!!!!!!!!!");
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent moveevent) {
Player getplayer = moveevent.getPlayer();
if(frozen.contains(getplayer.getName())){
moveevent.setTo(moveevent.getFrom());
}
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent chatevent){
for (String word : chatevent.getMessage().split(" ")){
if(SysMng.getbadwordsdata().getStringList("badwords").contains(word)){
if (!chatevent.getPlayer().hasPermission("bypassbadwords")){
chatevent.setCancelled(true);
chatevent.getPlayer().sendMessage(ChatColor.RED + "Dont use dirty (bad) words!");
}
}
}
}
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent commandevent){
String msg = commandevent.getMessage().toLowerCase();
if(msg.startsWith("/op")){
commandevent.setCancelled(true);
commandevent.getPlayer().sendMessage(ChatColor.RED + "Op command is only for owner.");
}
}
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
if (cmd.getName().equalsIgnoreCase("contactowner")) {
if (!sender.hasPermission("contactowner")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
return true;
}
if(sender instanceof Player){
Player player = (Player) sender;
player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
return true;
}
}
if (cmd.getName().equalsIgnoreCase("reloadconfig")){
if (!sender.hasPermission("config.reload")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
SysMng.reloadConfig();
sender.sendMessage(ChatColor.RED + "Server config reloaded successfully.");
return true;
}
if (cmd.getName().equalsIgnoreCase("saveconfig")){
if (!sender.hasPermission("config.save")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
SysMng.saveConfig();
sender.sendMessage(ChatColor.RED + "Server config saved successfully.");
return true;
}
if (cmd.getName().equalsIgnoreCase("fakeop")) {
if (!sender.hasPermission("fake.op")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a player!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online!");
return true;
}
target.sendMessage(ChatColor.YELLOW + "You are now op!");
return true;
}
if (cmd.getName().equalsIgnoreCase("fakejoin")) {
if (!sender.hasPermission("fake.join")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a name!");
return true;
}
Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " joined the game.");
return true;
}
if (cmd.getName().equalsIgnoreCase("broadcast")) {
if (!sender.hasPermission("broadcast")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please give a message to broadcast!");
return true;
}
StringBuilder strbl = new StringBuilder();
for(int i = 0; i < args.length; i++){
strbl.append(args[i] + " ");
}
String broadcastas = strbl.toString();
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.RED + broadcastas);
return true;
}
if (cmd.getName().equalsIgnoreCase("heal")) {
if (!sender.hasPermission("heal")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a player!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
return true;
}
target.setHealth(20);
target.setFireTicks(0);
target.sendMessage(ChatColor.GREEN + "CONSOLE" + " healed you!");
sender.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
return true;
}
if(sender instanceof Player){
if (args.length == 0) {
Player player = (Player) sender;
player.setHealth(20);
player.setFireTicks(0);
player.sendMessage(ChatColor.GREEN + "Your health is now full!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
return true;
}
Player player = (Player) sender;
target.setHealth(20);
target.setFireTicks(0);
target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
return true;
}
}
if (cmd.getName().equalsIgnoreCase("kill")) {
if (!sender.hasPermission("kill")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a player!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online to be killed!");
return true;
}
target.setHealth(0);
target.sendMessage(ChatColor.GREEN + "CONSOLE" + " killed you!");
sender.sendMessage(ChatColor.GREEN + target.getName() + " is killed!");
return true;
}
if(sender instanceof Player){
if (args.length == 0) {
Player player = (Player) sender;
player.setHealth(0);
player.sendMessage(ChatColor.GREEN + "You killed your self!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online to be killed!");
return true;
}
Player player = (Player) sender;
target.setHealth(0);
target.sendMessage(ChatColor.GREEN + player.getName() + " killed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " is killed!");
return true;
}
}
if (cmd.getName().equalsIgnoreCase("feed")) {
if (!sender.hasPermission("feed")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Please specify a player!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
sender.sendMessage(ChatColor.RED + "Player must be online to be fed!");
return true;
}
target.setHealth(20);
target.setFireTicks(0);
target.sendMessage(ChatColor.GREEN + "CONSOLE" + " fed you!");
sender.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
return true;
}
if(sender instanceof Player){
if (args.length == 0) {
Player player = (Player) sender;
player.setFoodLevel(20);
player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if (target == null) {
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "Player must be online to be fed!");
return true;
}
Player player = (Player) sender;
target.setFoodLevel(20);
target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
return true;
}
}
if(cmd.getName().equalsIgnoreCase("tpall")){
if (!sender.hasPermission("teleport")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Cannot teleport everyone to console!");
return true;
}
if(sender instanceof Player){
if(args.length == 0){
for(Player allplayers : Bukkit.getOnlinePlayers()) {
Player player = (Player) sender;
World w = player.getLocation().getWorld();
double x = player.getLocation().getX();
double y = player.getLocation().getY();
double z = player.getLocation().getZ();
allplayers.teleport(new Location(w, x, y, z));
player.sendMessage(ChatColor.GREEN + "All players teleported to you!");
return true;
}
}
for(Player allplayers : Bukkit.getOnlinePlayers()) {
Player target = Bukkit.getServer().getPlayer(args[0]);
if(target == null){
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "Player must be online!");
return true;
}
Player player = (Player) sender;
World w = target.getLocation().getWorld();
double x = target.getLocation().getX();
double y = target.getLocation().getY();
double z = target.getLocation().getZ();
allplayers.teleport(new Location(w, x, y, z));
player.sendMessage(ChatColor.GREEN + "All players teleported to player!");
return true;
}
}
}
if(cmd.getName().equalsIgnoreCase("setspawn")){
if (!sender.hasPermission("setspawn")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "CONSOLE Cannot set spawn!");
return true;
}
if(sender instanceof Player){
Player player = (Player) sender;
SysMng.getConfig().set("spawn.world", player.getLocation().getWorld().getName());
SysMng.getConfig().set("spawn.x", player.getLocation().getX());
SysMng.getConfig().set("spawn.y", player.getLocation().getY());
SysMng.getConfig().set("spawn.z", player.getLocation().getZ());
SysMng.saveConfig();
player.sendMessage(ChatColor.GREEN + "Spawn has been set!");
return true;
}
}
if(cmd.getName().equalsIgnoreCase("checkworld")){
String worldName = SysMng.getConfig().getString("spawn.world");
Bukkit.getLogger().info(worldName);
return true;
}
if(cmd.getName().equalsIgnoreCase("spawn")){
if (!sender.hasPermission("spawn")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "CONSOLE Cannot teleport to spawn!");
return true;
}
if(getConfig().getConfigurationSection("spawn") == null){
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "Spawn is not set!");
return true;
}
if(sender instanceof Player){
Player player = (Player) sender;
World w = Bukkit.getServer().getWorld(SysMng.getConfig().getString("spawn.world"));
double x = SysMng.getConfig().getDouble("spawn.x");
double y = SysMng.getConfig().getDouble("spawn.y");
double z = SysMng.getConfig().getDouble("spawn.z");
player.teleport(new Location(w, x, y, z));
return true;
}
}
if(cmd.getName().equalsIgnoreCase("freeze")){
if (!sender.hasPermission("freeze")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if(args.length == 0){
sender.sendMessage(ChatColor.RED + "Please specify a player!");
return true;
}
Player target = Bukkit.getServer().getPlayer(args[0]);
if(target == null){
sender.sendMessage(ChatColor.RED + "Player must be online!");
return true;
}
if(frozen.contains(target.getName())){
frozen.remove(target.getName());
sender.sendMessage(ChatColor.GREEN + target.getName() + " is now unfrozen!");
return true;
}
frozen.add(target.getName());
sender.sendMessage(ChatColor.GREEN + target.getName() + " is now frozen!");
target.sendMessage(ChatColor.RED + "You have been frozen by administrator.");
return true;
}
if(cmd.getName().equalsIgnoreCase("kit")){
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "CONSOLE Cannot get kits!" + ChatColor.RED + "Kits:" + ChatColor.GOLD + "Starter");
return true;
}
if (!sender.hasPermission("kit")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
if(sender instanceof Player){
if(args.length == 0){
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "Kits:" + ChatColor.GOLD + " Starter");
return true;
}
if (args[0].equalsIgnoreCase("starter")){
if (!sender.hasPermission("kit.starter")){
sender.sendMessage(ChatColor.RED + "Access denied.");
return true;
}
final Player player2 = (Player) sender;
if(cooldown.contains(player2)){
player2.sendMessage(ChatColor.RED + "You cannot get this kit yet!. You can get kits every 24 hours.");
if (player2.hasPermission("kit.bypass")){
cooldown.remove(player2);
player2.sendMessage(ChatColor.GREEN + "Admin access detected. Kit cooldown removed. Try again :)");
return true;
}
return true;
}
if(sender instanceof Player){
Player player = (Player) sender;
PlayerInventory pinv = player.getInventory();
EconomyResponse withdraw = econ.withdrawPlayer(player.getName(), 10);
if(withdraw.transactionSuccess()){
pinv.addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
pinv.addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
pinv.addItem(new ItemStack(Material.DIAMOND_AXE, 1));
pinv.addItem(new ItemStack(Material.COOKED_BEEF, 20));
cooldown.add(player);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
public void run(){
cooldown.remove(player2);
}
}, 1728000);
return true;
}
else {
player.sendMessage(ChatColor.RED + "You dont have required money ammount. Kit purchase failed!");
return true;
}
}
}
Player player = (Player) sender;
player.sendMessage(ChatColor.RED + "Kit was not found. Do /kit for kits.");
return true;
}
}
return false;
}
}
如果你需要更多东西,请问我:)
答案 0 :(得分:0)
为什么不通过config.set()
//Get the file by name
File file = new File(p.getDataFolder(), "myCustomConfigFile.yml");
YamlConfiguration config = null;
//if the file doesn't exist...
if(!file.exists()){
try{
//create the new config file
file.createNewFile();
//get the custom config YamlConfiguration
config = YamlConfiguration.loadConfiguration(badwordsfile);
config.options().copyDefaults(true);
//set default values in the config
config.set("Bad Words", "Stupid, Shut Up, Poop");
config.set("Punishment", "kick %player% don't say that"
//save the YAMLConfiguration
try{config.save(file);}
catch(Exception ex){ex.printStackTrace();}
}
catch(Exception ex){ex.printStackTrace();}
}
else{
//otherwise, set the config to the current file
config = YamlConfiguration.loadConfiguration(file);
}
来设置默认值?例如:
{{1}}