Advertisement
Guest User

CommandProcessor

a guest
Dec 30th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.68 KB | None | 0 0
  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License version 3
  9. as published by the Free Software Foundation. You may not use, modify
  10. or distribute this program under any other version of the
  11. GNU Affero General Public License.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU Affero General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Affero General Public License
  19. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  */
  21. package client.messages;
  22.  
  23. import java.util.ArrayList;
  24. import client.MapleCharacter;
  25. import client.MapleClient;
  26. import client.messages.commands.*;
  27. import client.messages.commands.AdminCommand;
  28. import client.messages.commands.GMCommand;
  29. import client.messages.commands.InternCommand;
  30. import client.messages.commands.PlayerCommand;
  31. import constants.ServerConstants.CommandType;
  32. import constants.ServerConstants.PlayerGMRank;
  33. import database.DatabaseConnection;
  34. import java.lang.reflect.Modifier;
  35. import java.sql.PreparedStatement;
  36. import java.sql.SQLException;
  37. import java.util.Collections;
  38. import java.util.HashMap;
  39. import tools.FileoutputUtil;
  40. import constants.ServerConstants;
  41.  
  42.  
  43. public class CommandProcessor {
  44.  
  45.     private final static HashMap<String, CommandObject> commands = new HashMap<String, CommandObject>();
  46.     private final static HashMap<Integer, ArrayList<String>> commandList = new HashMap<Integer, ArrayList<String>>();
  47.  
  48.     static {
  49.  
  50.         Class<?>[] CommandFiles = {
  51.             PlayerCommand.class, InternCommand.class, GMCommand.class, AdminCommand.class, DonatorCommand.class, SuperDonatorCommand.class, SuperGMCommand.class
  52.         };
  53.  
  54.         for (Class<?> clasz : CommandFiles) {
  55.             try {
  56.                 PlayerGMRank rankNeeded = (PlayerGMRank) clasz.getMethod("getPlayerLevelRequired", new Class<?>[]{}).invoke(null, null);
  57.                 Class<?>[] a = clasz.getDeclaredClasses();
  58.                 ArrayList<String> cL = new ArrayList<String>();
  59.                 for (Class<?> c : a) {
  60.                     try {
  61.                         if (!Modifier.isAbstract(c.getModifiers()) && !c.isSynthetic()) {
  62.                             Object o = c.newInstance();
  63.                             boolean enabled;
  64.                             try {
  65.                                 enabled = c.getDeclaredField("enabled").getBoolean(c.getDeclaredField("enabled"));
  66.                             } catch (NoSuchFieldException ex) {
  67.                                 enabled = true; //Enable all coded commands by default.
  68.                             }
  69.                             if (o instanceof CommandExecute && enabled) {
  70.                                 cL.add(rankNeeded.getCommandPrefix() + c.getSimpleName().toLowerCase());
  71.                                 commands.put(rankNeeded.getCommandPrefix() + c.getSimpleName().toLowerCase(), new CommandObject((CommandExecute) o, rankNeeded.getLevel()));
  72.                 if (rankNeeded.getCommandPrefix() != PlayerGMRank.GM.getCommandPrefix() && rankNeeded.getCommandPrefix() != PlayerGMRank.NORMAL.getCommandPrefix()) { //add it again for GM
  73.                                     commands.put("!" + c.getSimpleName().toLowerCase(), new CommandObject((CommandExecute) o, PlayerGMRank.GM.getLevel()));
  74.                 }
  75.                             }
  76.                         }
  77.                     } catch (Exception ex) {
  78.                         ex.printStackTrace();
  79.                         FileoutputUtil.outputFileError(FileoutputUtil.ScriptEx_Log, ex);
  80.                     }
  81.                 }
  82.                 Collections.sort(cL);
  83.                 commandList.put(rankNeeded.getLevel(), cL);
  84.             } catch (Exception ex) {
  85.                 ex.printStackTrace();
  86.                 FileoutputUtil.outputFileError(FileoutputUtil.ScriptEx_Log, ex);
  87.             }
  88.         }
  89.     }
  90.  
  91.     private static void sendDisplayMessage(MapleClient c, String msg, CommandType type) {
  92.     if (c.getPlayer() == null) {
  93.         return;
  94.     }
  95.         switch (type) {
  96.             case NORMAL:
  97.                 c.getPlayer().dropMessage(6, msg);
  98.                 break;
  99.             case TRADE:
  100.                 c.getPlayer().dropMessage(-2, "Error : " + msg);
  101.                 break;
  102.         case POKEMON:
  103.         c.getPlayer().dropMessage(-3, "(..." + msg + "..)");
  104.         break;
  105.         }
  106.  
  107.     }
  108.  
  109.     public static void dropHelp(MapleClient c) {
  110.     final StringBuilder sb = new StringBuilder("Command list: ");
  111.     for (int i = 0; i <= c.getPlayer().getGMLevel(); i++) {
  112.         if (commandList.containsKey(i)) {
  113.             for (String s : commandList.get(i)) {
  114.             sb.append(s);
  115.             sb.append(" ");
  116.             }
  117.         }
  118.     }
  119.     c.getPlayer().dropMessage(6, sb.toString());
  120.     }
  121.  
  122.     public static boolean processCommand(MapleClient c, String line, CommandType type) {
  123.         if (line.charAt(0) == PlayerGMRank.NORMAL.getCommandPrefix() || (c.getPlayer().getGMLevel() > PlayerGMRank.NORMAL.getLevel() && line.charAt(0) == PlayerGMRank.DONATOR.getCommandPrefix())) {
  124.             String[] splitted = line.split(" ");
  125.             splitted[0] = splitted[0].toLowerCase();
  126.  
  127.             CommandObject co = commands.get(splitted[0]);
  128.             if (co == null || co.getType() != type) {
  129.                 sendDisplayMessage(c, "That player command does not exist.", type);
  130.                 return true;
  131.             }
  132.             try {
  133.                 int ret = co.execute(c, splitted); //Don't really care about the return value. ;D
  134.             } catch (Exception e) {
  135.                 sendDisplayMessage(c, "There was an error.", type);
  136.                 if (c.getPlayer().isGM()) {
  137.                     sendDisplayMessage(c, "Error: " + e, type);
  138.             e.printStackTrace();
  139.             FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, e);
  140.                 }
  141.             }
  142.             return true;
  143.         }
  144.  
  145.         if (c.getPlayer().getGMLevel() > PlayerGMRank.NORMAL.getLevel()) {
  146.             if (line.charAt(0) == PlayerGMRank.SUPERGM.getCommandPrefix() || line.charAt(0) == PlayerGMRank.INTERN.getCommandPrefix() || line.charAt(0) == PlayerGMRank.GM.getCommandPrefix() || line.charAt(0) == PlayerGMRank.ADMIN.getCommandPrefix()) { //Redundant for now, but in case we change symbols later. This will become extensible.
  147.                 String[] splitted = line.split(" ");
  148.                 splitted[0] = splitted[0].toLowerCase();
  149.  
  150.                 CommandObject co = commands.get(splitted[0]);
  151.                 if (co == null) {
  152.             if (splitted[0].equals(line.charAt(0) + "help")) {
  153.                 dropHelp(c);
  154.                 return true;
  155.             }
  156.                     sendDisplayMessage(c, "That command does not exist.", type);
  157.                     return true;
  158.                 }
  159.                 if (c.getPlayer().getGMLevel() >= co.getReqGMLevel()) {
  160.                     int ret = 0;
  161.             try {
  162.             ret = co.execute(c, splitted);
  163.             } catch (ArrayIndexOutOfBoundsException x) {
  164.             sendDisplayMessage(c, "The command was not used properly: " + x, type);
  165.             } catch (Exception e) {
  166.             FileoutputUtil.outputFileError(FileoutputUtil.CommandEx_Log, e);
  167.             }
  168.                     if (ret > 0 && c.getPlayer() != null) { //incase d/c after command or something
  169.             if (c.getPlayer().isGM()) {
  170.                             logCommandToDB(c.getPlayer(), line, "gmlog");
  171.             } else {
  172.                 logCommandToDB(c.getPlayer(), line, "internlog");
  173.             }
  174.                     }
  175.                 } else {
  176.                     sendDisplayMessage(c, "You do not have the privileges to use that command.", type);
  177.                 }
  178.                 return true;
  179.             }
  180.         }
  181.         return false;
  182.     }
  183.  
  184.     private static void logCommandToDB(MapleCharacter player, String command, String table) {
  185.         PreparedStatement ps = null;
  186.         try {
  187.             ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO " + table + " (cid, command, mapid) VALUES (?, ?, ?)");
  188.             ps.setInt(1, player.getId());
  189.             ps.setString(2, command);
  190.             ps.setInt(3, player.getMap().getId());
  191.             ps.executeUpdate();
  192.         } catch (SQLException ex) {
  193.             FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, ex);
  194.             ex.printStackTrace();
  195.         } finally {
  196.             try {
  197.                 ps.close();
  198.             } catch (SQLException e) {/*Err.. Fuck?*/
  199.  
  200.             }
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement