fix: 修复配置更新 修复无参命令未显示

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-10-27 01:13:51 +08:00
parent 571c270248
commit ec5cb657cc
4 changed files with 112 additions and 109 deletions

View File

@ -1,13 +1,6 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands;
import java.util.Arrays; import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -155,9 +148,9 @@ public class CommandHelp {
helpList.add(formatCommand(cmdlist.get(i), label)); helpList.add(formatCommand(cmdlist.get(i), label));
} }
} }
}
// 帮助结尾 // 帮助结尾
helpList.add(String.format(helpFooter, label, HELPPAGECOUNT)); helpList.add(String.format(helpFooter, label, HELPPAGECOUNT));
}
cacheHelp.put(helpkey, helpList.toArray(new String[0])); cacheHelp.put(helpkey, helpList.toArray(new String[0]));
} }
sender.sendMessage(cacheHelp.get(helpkey)); sender.sendMessage(cacheHelp.get(helpkey));

View File

@ -4,21 +4,10 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.*;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@ -116,8 +105,7 @@ public class CommandManager implements TabExecutor {
lookupNames.put(name, plugin); lookupNames.put(name, plugin);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) { } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) {
} }
if ((cmd = plugin.getCommand(name)) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " if ((cmd = plugin.getCommand(name)) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + name + " 命令!"); }
+ name + " 命令!"); }
} }
cmd.setExecutor(this); cmd.setExecutor(this);
cmd.setTabCompleter(this); cmd.setTabCompleter(this);
@ -189,8 +177,7 @@ public class CommandManager implements TabExecutor {
List<String> matchedPlayers = new ArrayList<>(); List<String> matchedPlayers = new ArrayList<>();
for (Player player : C.Player.getOnlinePlayers()) { for (Player player : C.Player.getOnlinePlayers()) {
String name = player.getName(); String name = player.getName();
if ((senderPlayer == null || senderPlayer.canSee(player)) if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
&& StringUtil.startsWithIgnoreCase(name, lastWord)) {
matchedPlayers.add(name); matchedPlayers.add(name);
} }
} }
@ -254,7 +241,7 @@ public class CommandManager implements TabExecutor {
registerTab(method, clazz); registerTab(method, clazz);
} }
} }
help = new CommandHelp(cmds); help = new CommandHelp(defCmd, cmds);
buildCmdNameCache(); buildCmdNameCache();
} }

View File

@ -74,7 +74,7 @@ public class CommandTabInfo {
try { try {
return (List<String>) method.invoke(origin, cmdArgs); return (List<String>) method.invoke(origin, cmdArgs);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new CommandException("调用Tab自动补全发生错误 请反馈给开发者 " + Arrays.toString(P.instance.getDescription().getAuthors().toArray()) + " !", e); throw new CommandException("调用Tab自动补全发生错误 请反馈给开发者 " + Arrays.toString(P.getDescription().getAuthors().toArray()) + " !", e);
} }
} }

View File

@ -1,17 +1,8 @@
package pw.yumc.YumCore.config; package pw.yumc.YumCore.config;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -67,7 +58,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param file * @param file
*
*/ */
public FileConfig(File file) { public FileConfig(File file) {
Validate.notNull(file, FILE_NOT_BE_NULL); Validate.notNull(file, FILE_NOT_BE_NULL);
@ -77,8 +69,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param parent * @param parent
* @param filename *
* @param filename
*
*/ */
public FileConfig(File parent, String filename) { public FileConfig(File parent, String filename) {
init(new File(parent, filename), true); init(new File(parent, filename), true);
@ -87,7 +81,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param stream * @param stream
*
*/ */
public FileConfig(InputStream stream) { public FileConfig(InputStream stream) {
init(stream); init(stream);
@ -96,7 +91,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param filename * @param filename
*
*/ */
public FileConfig(String filename) { public FileConfig(String filename) {
init(new File(plugin.getDataFolder(), filename), true); init(new File(plugin.getDataFolder(), filename), true);
@ -105,8 +101,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param parent * @param parent
* @param filename *
* @param filename
*
*/ */
public FileConfig(String parent, String filename) { public FileConfig(String parent, String filename) {
init(new File(parent, filename), true); init(new File(parent, filename), true);
@ -115,9 +113,12 @@ public class FileConfig extends AbstractConfig {
/** /**
* List * List
* *
* @param <E> List * @param <E>
* @param path * List
* @param obj * @param path
*
* @param obj
*
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public <E> FileConfig addToList(String path, E obj) { public <E> FileConfig addToList(String path, E obj) {
@ -132,8 +133,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* StringList * StringList
* *
* @param path * @param path
* @param obj *
* @param obj
*
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public FileConfig addToStringList(String path, String obj) { public FileConfig addToStringList(String path, String obj) {
@ -144,9 +147,12 @@ public class FileConfig extends AbstractConfig {
/** /**
* StringList * StringList
* *
* @param path * @param path
* @param obj *
* @param allowrepeat * @param obj
*
* @param allowrepeat
*
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public FileConfig addToStringList(String path, String obj, boolean allowrepeat) { public FileConfig addToStringList(String path, String obj, boolean allowrepeat) {
@ -164,14 +170,13 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param cfgmsg List * @param cfgmsg
* List
* @return * @return
*/ */
public List<String> getColorList(List<String> cfgmsg) { public List<String> getColorList(List<String> cfgmsg) {
List<String> message = new ArrayList<>(); List<String> message = new ArrayList<>();
if (cfgmsg == null) { if (cfgmsg == null) { return Collections.emptyList(); }
return Collections.emptyList();
}
for (String msg : cfgmsg) { for (String msg : cfgmsg) {
message.add(ChatColor.translateAlternateColorCodes('&', msg)); message.add(ChatColor.translateAlternateColorCodes('&', msg));
} }
@ -190,7 +195,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* Location * Location
* *
* @param key * @param key
*
* @return {@link Location} * @return {@link Location}
*/ */
public Location getLocation(String key) { public Location getLocation(String key) {
@ -200,8 +206,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* Location * Location
* *
* @param path * @param path
* @param def *
* @param def
*
* @return {@link Location} * @return {@link Location}
*/ */
public Location getLocation(String path, Location def) { public Location getLocation(String path, Location def) {
@ -212,7 +220,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param path * @param path
*
* @return * @return
*/ */
public String getMessage(String path) { public String getMessage(String path) {
@ -222,8 +231,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param path * @param path
* @param def *
* @param def
*
* @return * @return
*/ */
public String getMessage(String path, String def) { public String getMessage(String path, String def) {
@ -237,14 +248,13 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param path * @param path
*
* @return * @return
*/ */
public List<String> getMessageList(String path) { public List<String> getMessageList(String path) {
List<String> cfgmsg = this.getStringList(path); List<String> cfgmsg = this.getStringList(path);
if (cfgmsg == null) { if (cfgmsg == null) { return Collections.emptyList(); }
return Collections.emptyList();
}
for (int i = 0; i < cfgmsg.size(); i++) { for (int i = 0; i < cfgmsg.size(); i++) {
cfgmsg.set(i, ChatColor.translateAlternateColorCodes(ALT_COLOR_CHAR, cfgmsg.get(i))); cfgmsg.set(i, ChatColor.translateAlternateColorCodes(ALT_COLOR_CHAR, cfgmsg.get(i)));
} }
@ -254,7 +264,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param path * @param path
*
* @return * @return
*/ */
public String[] getStringArray(String path) { public String[] getStringArray(String path) {
@ -276,24 +287,21 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param newver * @param newver
* @param oldver *
* @param oldver
*
* @return * @return
*/ */
public boolean needUpdate(String newver, String oldver) { public boolean needUpdate(String newver, String oldver) {
if (newver == null) { if (newver == null) { return false; }
return false; if (oldver == null) { return true; }
}
if (oldver == null) {
return true;
}
String[] va1 = newver.split("\\.");// 注意此处为正则匹配,不能用"." String[] va1 = newver.split("\\.");// 注意此处为正则匹配,不能用"."
String[] va2 = oldver.split("\\."); String[] va2 = oldver.split("\\.");
int idx = 0; int idx = 0;
int minLength = Math.min(va1.length, va2.length);// 取最小长度值 int minLength = Math.min(va1.length, va2.length);// 取最小长度值
int diff = 0; int diff = 0;
while (idx < minLength while (idx < minLength && (diff = va1[idx].length() - va2[idx].length()) == 0// 先比较长度
&& (diff = va1[idx].length() - va2[idx].length()) == 0// 先比较长度
&& (diff = va1[idx].compareTo(va2[idx])) == 0) {// 再比较字符 && (diff = va1[idx].compareTo(va2[idx])) == 0) {// 再比较字符
++idx; ++idx;
} }
@ -314,9 +322,12 @@ public class FileConfig extends AbstractConfig {
/** /**
* List * List
* *
* @param <E> List * @param <E>
* @param path * List
* @param obj * @param path
*
* @param obj
*
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public <E> FileConfig removeFromList(String path, E obj) { public <E> FileConfig removeFromList(String path, E obj) {
@ -330,8 +341,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* StringList * StringList
* *
* @param path * @param path
* @param obj *
* @param obj
*
* @return {@link FileConfig} * @return {@link FileConfig}
*/ */
public FileConfig removeFromStringList(String path, String obj) { public FileConfig removeFromStringList(String path, String obj) {
@ -405,7 +418,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param oldcfg * @param oldcfg
*
*/ */
protected void backupConfig(FileConfig oldcfg) { protected void backupConfig(FileConfig oldcfg) {
String filename = oldcfg.getConfigName(); String filename = oldcfg.getConfigName();
@ -423,7 +437,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param file * @param file
*
*/ */
protected void check(File file) { protected void check(File file) {
String filename = file.getName(); String filename = file.getName();
@ -437,9 +452,7 @@ public class FileConfig extends AbstractConfig {
plugin.saveResource(filename, true); plugin.saveResource(filename, true);
} }
} else { } else {
if (stream == null) { if (stream == null) { return; }
return;
}
FileConfig newcfg = new FileConfig(stream); FileConfig newcfg = new FileConfig(stream);
FileConfig oldcfg = new FileConfig(file); FileConfig oldcfg = new FileConfig(file);
if (needUpdate(newcfg, oldcfg)) { if (needUpdate(newcfg, oldcfg)) {
@ -476,7 +489,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* FileConfig * FileConfig
* *
* @param file * @param file
*
* @return FileConfig * @return FileConfig
*/ */
protected FileConfig init(File file) { protected FileConfig init(File file) {
@ -487,8 +501,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* FileConfig * FileConfig
* *
* @param file * @param file
* @param check *
* @param check
*
* @return FileConfig * @return FileConfig
*/ */
protected FileConfig init(File file, boolean check) { protected FileConfig init(File file, boolean check) {
@ -508,7 +524,8 @@ public class FileConfig extends AbstractConfig {
/** /**
* FileConfig * FileConfig
* *
* @param stream * @param stream
*
* @return FileConfig * @return FileConfig
*/ */
protected FileConfig init(InputStream stream) { protected FileConfig init(InputStream stream) {
@ -516,16 +533,12 @@ public class FileConfig extends AbstractConfig {
try { try {
this.load(new InputStreamReader(stream, UTF_8)); this.load(new InputStreamReader(stream, UTF_8));
} catch (InvalidConfigurationException | IllegalArgumentException ex) { } catch (InvalidConfigurationException | IllegalArgumentException ex) {
if (file == null) { if (file == null) { throw new IllegalArgumentException(ex); }
throw new IllegalArgumentException(ex);
}
Log.warning(String.format(CONFIG_FORMAT_ERROR, file.getName())); Log.warning(String.format(CONFIG_FORMAT_ERROR, file.getName()));
Log.warning(ex.getMessage()); Log.warning(ex.getMessage());
saveFromJar(); saveFromJar();
} catch (IOException ex) { } catch (IOException ex) {
if (file == null) { if (file == null) { throw new IllegalStateException(ex); }
throw new IllegalStateException(ex);
}
Log.warning(String.format(CONFIG_READ_ERROR, file.getName())); Log.warning(String.format(CONFIG_READ_ERROR, file.getName()));
} }
return this; return this;
@ -534,8 +547,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param newcfg * @param newcfg
* @param oldcfg *
* @param oldcfg
*
* @return * @return
*/ */
protected boolean needUpdate(FileConfig newcfg, FileConfig oldcfg) { protected boolean needUpdate(FileConfig newcfg, FileConfig oldcfg) {
@ -545,8 +560,10 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param newcfg * @param newcfg
* @param oldcfg *
* @param oldcfg
*
* @return * @return
*/ */
protected FileConfig updateConfig(FileConfig newCfg, FileConfig oldCfg) { protected FileConfig updateConfig(FileConfig newCfg, FileConfig oldCfg) {
@ -556,9 +573,12 @@ public class FileConfig extends AbstractConfig {
/** /**
* *
* *
* @param newCfg * @param newCfg
* @param oldCfg *
* @param force * @param oldCfg
*
* @param force
*
* @return * @return
*/ */
protected FileConfig updateConfig(FileConfig newCfg, FileConfig oldCfg, boolean force) { protected FileConfig updateConfig(FileConfig newCfg, FileConfig oldCfg, boolean force) {
@ -578,7 +598,10 @@ public class FileConfig extends AbstractConfig {
for (String string : oldConfigKeys) { for (String string : oldConfigKeys) {
Object var = oldCfg.get(string); Object var = oldCfg.get(string);
// 需要进行节点检查 还有类型检查 不同类型情况下 使用新配置 // 需要进行节点检查 还有类型检查 不同类型情况下 使用新配置
if (var != null && !(var instanceof MemorySection) && newCfg.get(string).getClass().equals(var.getClass())) { if (var != null && !(var instanceof MemorySection)) {
if (!newCfg.get(string).getClass().equals(var.getClass())) {
Log.w("警告! 旧数据类型与新配置类型不匹配!");
}
Log.debug(String.format(CONFIG_UPDATE_VALUE, string, var)); Log.debug(String.format(CONFIG_UPDATE_VALUE, string, var));
newCfg.set(string, var); newCfg.set(string, var);
} }