Don't rely too much on other libraries & add commands in a list format.

This commit is contained in:
filoghost 2014-12-11 16:08:42 +01:00
parent a10bedc473
commit 6e9bb32480
9 changed files with 74 additions and 26 deletions

View File

@ -37,8 +37,6 @@ import com.gmail.filoghost.chestcommands.task.ErrorLoggerTask;
import com.gmail.filoghost.chestcommands.util.CaseInsensitiveMap;
import com.gmail.filoghost.chestcommands.util.ErrorLogger;
import com.gmail.filoghost.chestcommands.util.Utils;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public class ChestCommands extends JavaPlugin {
@ -68,7 +66,7 @@ public class ChestCommands extends JavaPlugin {
instance = this;
fileNameToMenuMap = CaseInsensitiveMap.create();
commandsToMenuMap = CaseInsensitiveMap.create();
boundItems = Sets.newHashSet();
boundItems = Utils.newHashSet();
settings = new Settings(new PluginConfig(this, "config.yml"));
lang = new Lang(new PluginConfig(this, "lang.yml"));
@ -244,7 +242,7 @@ public class ChestCommands extends JavaPlugin {
* Loads all the configuration files recursively into a list.
*/
private List<PluginConfig> loadMenus(File file) {
List<PluginConfig> list = Lists.newArrayList();
List<PluginConfig> list = Utils.newArrayList();
if (file.isDirectory()) {
for (File subFile : file.listFiles()) {
list.addAll(loadMenus(subFile));

View File

@ -16,7 +16,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import com.google.common.collect.Lists;
import com.gmail.filoghost.chestcommands.util.Utils;
public class Icon {
@ -170,7 +170,7 @@ public class Icon {
if (hasLore()) {
output = Lists.newArrayList();
output = Utils.newArrayList();
// TODO some magic
for (String line : lore) {
output.add(line);
@ -179,7 +179,7 @@ public class Icon {
if (material == null) {
if (output == null) output = Lists.newArrayList();
if (output == null) output = Utils.newArrayList();
// Add an error message.
output.add(ChatColor.RED + "(Invalid material)");

View File

@ -11,12 +11,12 @@ import org.apache.commons.lang.StringEscapeUtils;
import com.gmail.filoghost.chestcommands.ChestCommands;
import com.gmail.filoghost.chestcommands.util.ErrorLogger;
import com.gmail.filoghost.chestcommands.util.Utils;
import com.google.common.collect.Maps;
// This is not a real YAML file ;)
public class AsciiPlaceholders {
private static Map<String, String> placeholders = Maps.newHashMap();
private static Map<String, String> placeholders = Utils.newHashMap();
public static void load(ErrorLogger errorLogger) throws IOException, Exception {

View File

@ -1,10 +1,11 @@
package com.gmail.filoghost.chestcommands.internal;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.gmail.filoghost.chestcommands.util.Validate;
public class RequiredItem {
private Material material;

View File

@ -18,11 +18,11 @@ import com.gmail.filoghost.chestcommands.api.IconMenu;
import com.gmail.filoghost.chestcommands.internal.BoundItem;
import com.gmail.filoghost.chestcommands.internal.MenuInventoryHolder;
import com.gmail.filoghost.chestcommands.task.ExecuteCommandsTask;
import com.google.common.collect.Maps;
import com.gmail.filoghost.chestcommands.util.Utils;
public class InventoryListener implements Listener {
private static Map<Player, Long> antiClickSpam = Maps.newHashMap();
private static Map<Player, Long> antiClickSpam = Utils.newHashMap();
@EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = false)
public void onInteract(PlayerInteractEvent event) {

View File

@ -1,6 +1,5 @@
package com.gmail.filoghost.chestcommands.serializer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -22,11 +21,11 @@ import com.gmail.filoghost.chestcommands.internal.icon.command.ServerIconCommand
import com.gmail.filoghost.chestcommands.internal.icon.command.SoundIconCommand;
import com.gmail.filoghost.chestcommands.internal.icon.command.TellIconCommand;
import com.gmail.filoghost.chestcommands.util.ErrorLogger;
import com.google.common.collect.Lists;
import com.gmail.filoghost.chestcommands.util.Utils;
public class CommandSerializer {
private static Map<Pattern, Class<? extends IconCommand>> commandTypesMap = new HashMap<Pattern, Class<? extends IconCommand>>();
private static Map<Pattern, Class<? extends IconCommand>> commandTypesMap = Utils.newHashMap();
static {
commandTypesMap.put(commandPattern("console:"), ConsoleIconCommand.class);
commandTypesMap.put(commandPattern("op:"), OpIconCommand.class);
@ -64,7 +63,7 @@ public class CommandSerializer {
}
String[] split = input.split(Pattern.quote(separator));
List<IconCommand> iconCommands = Lists.newArrayList();
List<IconCommand> iconCommands = Utils.newArrayList();
for (String command : split) {
String trim = command.trim();

View File

@ -119,7 +119,23 @@ public class IconSerializer {
icon.setCloseOnClick(closeOnClick);
if (section.isSet(Nodes.COMMAND)) {
List<IconCommand> commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND));
List<IconCommand> commands;
if (section.isList(Nodes.COMMAND)) {
commands = Utils.newArrayList();
for (String commandString : section.getStringList(Nodes.COMMAND)) {
if (commandString.isEmpty()) {
continue;
}
commands.add(CommandSerializer.matchCommand(commandString));
}
} else {
commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND));
}
icon.setClickHandler(new CommandsClickHandler(commands, closeOnClick));
}

View File

@ -7,8 +7,7 @@ import org.bukkit.ChatColor;
import com.gmail.filoghost.chestcommands.ChestCommands;
import com.gmail.filoghost.chestcommands.util.ErrorLogger;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.gmail.filoghost.chestcommands.util.Utils;
public class ErrorLoggerTask implements Runnable {
@ -21,7 +20,7 @@ public class ErrorLoggerTask implements Runnable {
@Override
public void run() {
List<String> lines = Lists.newArrayList();
List<String> lines = Utils.newArrayList();
lines.add(" ");
lines.add(ChatColor.RED + "#------------------- Chest Commands Errors -------------------#");
@ -31,7 +30,7 @@ public class ErrorLoggerTask implements Runnable {
}
lines.add(ChatColor.RED + "#-------------------------------------------------------------#");
String output = Joiner.on('\n').join(lines);
String output = Utils.join(lines, "\n");
if (ChestCommands.getSettings().use_console_colors) {
Bukkit.getConsoleSender().sendMessage(output);

View File

@ -6,9 +6,14 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -19,19 +24,17 @@ import org.bukkit.plugin.Plugin;
import com.gmail.filoghost.chestcommands.ChestCommands;
import com.gmail.filoghost.chestcommands.exception.FormatException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class Utils {
// Default material names are ugly.
private static Map<String, Material> materialMap = Maps.newHashMap();
private static Map<String, Material> materialMap = newHashMap();
static {
for (Material mat : Material.values()) {
materialMap.put(StringUtils.stripChars(mat.toString(), "_").toLowerCase(), mat);
}
Map<String, Material> tempMap = Maps.newHashMap();
Map<String, Material> tempMap = newHashMap();
tempMap.put("iron bar", Material.IRON_FENCE);
tempMap.put("iron bars", Material.IRON_FENCE);
@ -215,7 +218,7 @@ public class Utils {
BufferedReader br = null;
try {
List<String> lines = Lists.newArrayList();
List<String> lines = newArrayList();
if (!file.exists()) {
throw new FileNotFoundException();
@ -264,6 +267,7 @@ public class Utils {
return Color.fromRGB(red, green, blue);
}
public static void saveResourceSafe(Plugin plugin, String name) {
try {
plugin.saveResource(name, false);
@ -271,4 +275,35 @@ public class Utils {
// Shhh...
}
}
public static <T> Set<T> newHashSet() {
return new HashSet<T>();
}
public static <T, V> Map<T, V> newHashMap() {
return new HashMap<T, V>();
}
public static <T> List<T> newArrayList() {
return new ArrayList<T>();
}
public static String join(Iterable<?> iterable, String separator) {
StringBuilder builder = new StringBuilder();
Iterator<?> iter = iterable.iterator();
boolean first = true;
while (iter.hasNext()) {
if (first) {
first = false;
} else {
builder.append(separator);
}
builder.append(iter.next());
}
return builder.toString();
}
}