我想创建自定义配置文件,因为我正在制作非常大的插件,问题是我想在我的自定义badwords.yml中复制像config.yml这样的默认设置。我怎么做?我尝试过像config.yml一样使用copydefault,但是没有用。这是代码:
package me.herobrine112211.ulticmdpack;
import java.io.File;
import java.io.IOException;
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.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
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{
public File BadWordsFile = new File(getDataFolder()+"/Data/badwords.yml");
public FileConfiguration badwordsdata = YamlConfiguration.loadConfiguration(BadWordsFile);
ArrayList<String> frozen = new ArrayList<String>();
ArrayList<Player> cooldown = new ArrayList<Player>();
public static Economy econ = null;
public void saveFiles(){
try {
badwordsdata.save(BadWordsFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFiles(){
if(BadWordsFile.exists()){
try {
badwordsdata.load(BadWordsFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}else{
try {
badwordsdata.options().copyDefaults(true);
badwordsdata.save(BadWordsFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void onEnable() {
if (!setupEconomy()){
getLogger().severe(String.format("[%s] - Disabled because vault was not found!", getDescription().getName()));
getServer().getPluginManager().disablePlugin(this);
return;
}
getConfig().options().copyDefaults(true);
saveConfig();
loadFiles();
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() {
saveFiles();
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(getConfig().getConfigurationSection("spawn") == null){
getplayer.sendMessage(ChatColor.RED + "Spawn is not set!. Report this problem to owner INSTANTLY!");
}
World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
double x = getConfig().getDouble("spawn.x");
double y = getConfig().getDouble("spawn.y");
double z = 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(badwordsdata.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;
}
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;
}
reloadConfig();
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;
getConfig().set("spawn.world", player.getLocation().getWorld().getName());
getConfig().set("spawn.x", player.getLocation().getX());
getConfig().set("spawn.y", player.getLocation().getY());
getConfig().set("spawn.z", player.getLocation().getZ());
saveConfig();
player.sendMessage(ChatColor.GREEN + "Spawn has been set!");
return true;
}
}
if(cmd.getName().equalsIgnoreCase("checkworld")){
String worldName = 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(getConfig().getString("spawn.world"));
double x = getConfig().getDouble("spawn.x");
double y = getConfig().getDouble("spawn.y");
double z = 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;
}
}
如果您不明白,我的意思是我想从插件中复制badwords.yml文件,如果它不存在的话。我希望更清楚。
更新的代码:
答案 0 :(得分:1)
首先,如果我是你,我会坚持使用Java Naming Conventions
除变量外,所有实例,类和类常量都是大小写混合的小写首字母。内部单词以大写字母开头。变量名称不应以下划线_或美元符号$字符开头,即使两者都允许。 http://www.oracle.com/technetwork/java/codeconventions-135099.html
public File BadWordsFile = new File(getDataFolder()+"/Data/badwords.yml");
public FileConfiguration badwordsdata = YamlConfiguration.loadConfiguration(BadWordsFile);
变为
public File badWordsFile = new File(getDataFolder()+"/Data/badwords.yml");
public FileConfiguration badWordsData = YamlConfiguration.loadConfiguration(badWordsFile);
至于你的问题,只需检查它是否存在,如果不存在则创建它然后复制默认的'badwords'。
if (!badWordsFile.exists()) {
badWordsFile.createNewFile();
//copy in default 'badwords'
List<String> listOfBadWords = Arrays.asList("BadWord", "AnotherBadWord", "AlsoABadWord");
badWordsData.set("badwords", listOfBadWords);
}
我也会运行上面的内容并初始化badWordsFile
&amp; badwordsdata
方法中的onEnable()
。
public void reloadCustomConfig() {
if (customConfigFile == null) {
customConfigFile = new File(getDataFolder(), "customConfig.yml");
}
customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
// Look for defaults in the jar
Reader defConfigStream = new InputStreamReader(this.class.getResource("customConfig.yml"), "UTF-8");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
customConfig.setDefaults(defConfig);
}
}
您可以按照以下方式进行调整
public File badWordsFile;
public FileConfiguration badWordsData;
public void saveDefaultConfigValues() {
badWordsFile = new File(getDataFolder() + "/Data/badwords.yml");
badWordsData = YAMLConfiguration.loadConfiguration(badWordsFile);
//Defaults in jar
Reader defConfigStream;
try {
defConfigStream = new InputStreamReader(this.class.getResource("badwords.yml"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
badWordsData.setDefaults(defConfig);
//Copy default values
badWordsData.options().copyDefaults(true);
this.saveConfig();
//OR use this to copy default values
//this.saveDefaultConfig();
}
}
现在您可以在onEnable()
方法中调用此方法。