diff --git a/src/client/command/CommandsExecutor.java b/src/client/command/CommandsExecutor.java index 98be594909..196853b703 100644 --- a/src/client/command/CommandsExecutor.java +++ b/src/client/command/CommandsExecutor.java @@ -258,6 +258,7 @@ public class CommandsExecutor { addCommand("unjail", 2, UnJailCommand.class); addCommand("job", 2, JobCommand.class); addCommand("unbug", 2, UnBugCommand.class); + addCommand("id", 2, IdCommand.class); commandsNameDesc.add(levelCommandsCursor); } diff --git a/src/client/command/commands/gm2/IdCommand.java b/src/client/command/commands/gm2/IdCommand.java new file mode 100644 index 0000000000..91ecace67a --- /dev/null +++ b/src/client/command/commands/gm2/IdCommand.java @@ -0,0 +1,101 @@ +package client.command.commands.gm2; + +import client.MapleCharacter; +import client.MapleClient; +import client.command.Command; +import tools.exceptions.IdTypeNotSupportedException; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.*; + +public class IdCommand extends Command { + { + setDescription(""); + } + + private final Map handbookDirectory = new HashMap<>(); + private final Map> itemMap = new HashMap<>(); + + public IdCommand() { + handbookDirectory.put("map", "handbook/Map.txt"); + handbookDirectory.put("etc", "handbook/Etc.txt"); + handbookDirectory.put("npc", "handbook/NPC.txt"); + handbookDirectory.put("use", "handbook/Use.txt"); + handbookDirectory.put("weapon", "handbook/Equip/Weapon.txt"); // TODO add more into this + } + + @Override + public void execute(MapleClient client, final String[] params) { + final MapleCharacter player = client.getPlayer(); + if (params.length < 2) { + player.yellowMessage("Syntax: !id "); + return; + } + final String queryItem = joinStringArr(Arrays.copyOfRange(params, 1, params.length), " "); + player.yellowMessage("Querying for entry... May take some time... Please try to refine your search"); + Runnable queryRunnable = new Runnable() { + @Override + public void run() { + try { + populateIdMap(params[0].toLowerCase()); + + Map resultList = fetchResults(itemMap.get(params[0]), queryItem); + + if (resultList.size() > 0) { + int count = 0; + for (Map.Entry entry: resultList.entrySet()) { + player.yellowMessage(String.format("Id for %s is: %s", entry.getKey(), entry.getValue())); + if (++count > 100) { + break; + } + } + player.yellowMessage(String.format("Results found: %d | Returned: %d/100 | Refine search query to improve time.", resultList.size(), count - 1)); + } else { + player.yellowMessage(String.format("Id not found for item: %s, of type: %s", queryItem, params[0])); + } + } catch (IdTypeNotSupportedException e) { + player.yellowMessage("Your query type is not supported"); + } catch (IOException e) { + player.yellowMessage("Error reading file, please contact your administrator"); + } + } + }; + Thread thread = new Thread(queryRunnable); + thread.start(); + } + + private void populateIdMap(String type) throws IdTypeNotSupportedException, IOException { + if (!handbookDirectory.containsKey(type)) { + throw new IdTypeNotSupportedException(); + } + itemMap.put(type, new HashMap()); + BufferedReader reader = new BufferedReader(new FileReader(handbookDirectory.get(type))); + String line; + while ((line = reader.readLine()) != null) { + String[] row = line.split(" - ", 2); + if (row.length == 2) { + itemMap.get(type).put(row[1].toLowerCase(), row[0]); + } + } + } + + private String joinStringArr(String[] arr, String separator) { + if (null == arr || 0 == arr.length) return ""; + StringBuilder sb = new StringBuilder(256); + sb.append(arr[0]); + for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]); + return sb.toString(); + } + + private Map fetchResults(Map queryMap, String queryItem) { + Map results = new HashMap<>(); + for (String item: queryMap.keySet()) { + if (item.indexOf(queryItem) != -1) { + results.put(item, queryMap.get(item)); + } + } + return results; + } +} diff --git a/src/tools/exceptions/IdTypeNotSupportedException.java b/src/tools/exceptions/IdTypeNotSupportedException.java new file mode 100644 index 0000000000..26f2f66c6f --- /dev/null +++ b/src/tools/exceptions/IdTypeNotSupportedException.java @@ -0,0 +1,11 @@ +package tools.exceptions; + +public class IdTypeNotSupportedException extends Exception { + public IdTypeNotSupportedException() { + super("The given ID type is not supported"); + } + + public IdTypeNotSupportedException(String message) { + super(message); + } +}