Skip to content

Commit

Permalink
Made tab-completion smarter + fixed problems with the config
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfirecodes committed Nov 7, 2016
1 parent 94e6943 commit 7c28dbb
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 43 deletions.
56 changes: 40 additions & 16 deletions src/main/java/com/rayzr522/decoheads/DecoHeads.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import java.io.IOException;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import com.rayzr522.decoheads.command.CommandDecoHeads;
import com.rayzr522.decoheads.gui.GuiListener;
Expand Down Expand Up @@ -42,29 +44,44 @@ public void onEnable() {
// Create the config handler
configHandler = new ConfigHandler(this);
// Load all config stuff
reload();
if (!reload()) {
err("The config failed to load", true);
return;
}

setupCommands();

try {
Metrics metrics = new Metrics(this);
metrics.start();
} catch (IOException e) {
// Ignore this, metrics failed to start
}
new BukkitRunnable() {
public void run() {
try {
Metrics metrics = new Metrics(instance);
metrics.start();
} catch (IOException e) {
instance.log("Failed to start Metrics!");
}
}
}.runTaskLater(this, 1L);

}

public void reload() {
reloadConfig();
// Just ensure that the config.yml file exists
configHandler.getConfig("config.yml");
// Load the localization from the config
localization = Localization.load(configHandler.getConfig("messages.yml"));

logger.setPrefix(localization.getMessagePrefix());
@Override
public void onDisable() {
instance = null;
}

InventoryManager.loadHeads(this);
public boolean reload() {
try {
// Load the localization from the config
localization = Localization.load(configHandler.getConfig("messages.yml"));
logger.setPrefix(localization.getMessagePrefix());

// Load all the heads
return InventoryManager.loadHeads(this);
} catch (Exception e) {
// What the heck!? Who knows...
e.printStackTrace();
return false;
}
}

private void setupCommands() {
Expand Down Expand Up @@ -121,4 +138,11 @@ public static DecoHeads getInstance() {
return instance;
}

/**
* @return
*/
public ConfigHandler getConfigHandler() {
return configHandler;
}

}
16 changes: 13 additions & 3 deletions src/main/java/com/rayzr522/decoheads/command/CommandDecoHeads.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public boolean onCommand(CommandSender sender, Command command, String cmd, Stri
return true;
}

plugin.reload();
// Load all config stuff
if (!plugin.reload()) {
plugin.err("The config failed to load", true);
return true;
}
plugin.msg(p, plugin.tr("command.decoheads.reloaded"));

} else if (arg.equals("search") || arg.equals("find")) {
Expand Down Expand Up @@ -101,11 +105,17 @@ public boolean onCommand(CommandSender sender, Command command, String cmd, Stri
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 1) {
return Arrays.asList("reload", "rl", "search", "find", "page#");
return sort(Arrays.asList("reload", "rl", "search", "find", "#"), args[0]);
} else if (args.length > 1 && (args[0].equalsIgnoreCase("search") || args[0].equalsIgnoreCase("find"))) {
return InventoryManager.headsList(args);
String filter = ArrayUtils.concat(Arrays.copyOfRange(args, 1, args.length), " ");
return sort(InventoryManager.headsList(filter), filter);
}
return Collections.emptyList();
}

private List<String> sort(List<String> list, String filter) {
Collections.sort(list, new MatchComparator(filter));
return list;
}

}
27 changes: 27 additions & 0 deletions src/main/java/com/rayzr522/decoheads/command/MatchComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
*
*/
package com.rayzr522.decoheads.command;

import java.util.Comparator;

/**
* @author Rayzr
*
*/
public class MatchComparator implements Comparator<String> {
private String filter;

public MatchComparator(String filter) {
this.filter = filter;
}

public int compare(String o1, String o2) {
boolean s1 = o1.startsWith(filter);
boolean s2 = o2.startsWith(filter);
if (s1 && s2) {
return Integer.compare(o1.replace(filter, "").length(), o2.replace(filter, "").length());
}
return Boolean.compare(s2, s1);
}
}
24 changes: 8 additions & 16 deletions src/main/java/com/rayzr522/decoheads/gui/InventoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package com.rayzr522.decoheads.gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.bukkit.ChatColor;
Expand All @@ -15,7 +13,6 @@
import org.bukkit.inventory.ItemStack;

import com.rayzr522.decoheads.DecoHeads;
import com.rayzr522.decoheads.util.ArrayUtils;
import com.rayzr522.decoheads.util.CustomHead;
import com.rayzr522.decoheads.util.ItemUtils;
import com.rayzr522.decoheads.util.TextUtils;
Expand All @@ -30,8 +27,8 @@ public class InventoryManager {

public static final int SIZE = WIDTH * HEIGHT;

public static void loadHeads(DecoHeads plugin) {
FileConfiguration config = plugin.getConfig();
public static boolean loadHeads(DecoHeads plugin) {
FileConfiguration config = plugin.getConfigHandler().getConfig("heads.yml");

heads = new ArrayList<ItemStack>();

Expand All @@ -55,8 +52,10 @@ public static void loadHeads(DecoHeads plugin) {
}

if (heads.size() < 1) {
plugin.logger.err("No heads were found in the config!", true);
plugin.log("No heads were found in config.yml");
return false;
}
return true;
}

private static final ItemStack EMPTY = ItemUtils.setName(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15), " ");
Expand Down Expand Up @@ -121,8 +120,8 @@ public static Inventory getInventory(Player player, String filter, int page) {

}

setItem(items, BUTTON("Previous Page", page > 1), 2, 5);
setItem(items, BUTTON("Next Page", page < maxPages(filteredHeads)), 6, 5);
setItem(items, BUTTON(DecoHeads.getInstance().tr("button.previous-page"), page > 1), 2, 5);
setItem(items, BUTTON(DecoHeads.getInstance().tr("button.next-page"), page < maxPages(filteredHeads)), 6, 5);

int offset = (page - 1) * SIZE;

Expand Down Expand Up @@ -175,17 +174,10 @@ public static int maxPages(List<ItemStack> heads) {
* @param args the current args
* @return a list of all the names of the heads
*/
public static List<String> headsList(String[] args) {
if (args.length < 2) {
return Collections.emptyList();
}
String filter = ArrayUtils.concat(Arrays.copyOfRange(args, 1, args.length), " ");
public static List<String> headsList(String filter) {
List<String> headsList = new ArrayList<String>();
for (ItemStack item : heads) {
String name = ChatColor.stripColor(item.getItemMeta().getDisplayName());
if (name.equalsIgnoreCase(filter)) {
return Collections.emptyList();
}
if (!name.startsWith(filter)) {
continue;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/rayzr522/decoheads/util/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ public class ConfigHandler {

public ConfigHandler(DecoHeads plugin) {
this.plugin = plugin;
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
}

public File getFile(String name) {
return new File(plugin.getDataFolder(), name.replace("/", File.pathSeparator));
}

public YamlConfiguration getConfig(String name) {
File file = getFile(name);

if (!file.exists()) {
if (!getFile(name).exists()) {
if (plugin.getResource(name) == null) {
plugin.log("Failed to load config file '" + name + "'");
return null;
}
saveDefaultFile(name);
file = getFile(name);
}

return YamlConfiguration.loadConfiguration(file);
return YamlConfiguration.loadConfiguration(getFile(name));
}

public void saveDefaultFile(String name) {
plugin.saveResource(name, false);
plugin.saveResource(name, true);
plugin.log("Loaded default file for " + name);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/rayzr522/decoheads/util/DHMessenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public void err(String err, boolean disable) {
if (disable) {

info("DecoHeads will now be disabled.");
info("---------------------------");
info("-----------------------------------------------");
Bukkit.getPluginManager().disablePlugin(plugin);

} else {

info("---------------------------");
info("-----------------------------------------------");

}
}
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ gui:
item:
name: "&e&n{0}"
lore: "\n&c&oMade with DecoHeads!\n"


button:
next-page: "Next Page"
previous-page: "Previous Page"

0 comments on commit 7c28dbb

Please sign in to comment.