/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java

https://github.com/tobias1222/Essentials · Java · 117 lines · 103 code · 14 blank · 0 comment · 16 complexity · 9b9f9c8832cb12995efec975523cb344 MD5 · raw file

  1. package com.earth2me.essentials.commands;
  2. import com.earth2me.essentials.ChargeException;
  3. import com.earth2me.essentials.Trade;
  4. import java.util.List;
  5. import org.bukkit.Server;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandSender;
  8. import com.earth2me.essentials.IEssentials;
  9. import com.earth2me.essentials.OfflinePlayer;
  10. import org.bukkit.entity.Player;
  11. import com.earth2me.essentials.User;
  12. import com.earth2me.essentials.Util;
  13. import java.util.logging.Logger;
  14. public abstract class EssentialsCommand implements IEssentialsCommand
  15. {
  16. private final transient String name;
  17. protected transient IEssentials ess;
  18. protected final static Logger logger = Logger.getLogger("Minecraft");
  19. protected EssentialsCommand(final String name)
  20. {
  21. this.name = name;
  22. }
  23. public void setEssentials(final IEssentials ess)
  24. {
  25. this.ess = ess;
  26. }
  27. public String getName()
  28. {
  29. return name;
  30. }
  31. protected User getPlayer(final Server server, final String[] args, final int pos) throws NoSuchFieldException, NotEnoughArgumentsException
  32. {
  33. return getPlayer(server, args, pos, false);
  34. }
  35. protected User getPlayer(final Server server, final String[] args, final int pos, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
  36. {
  37. if (args.length <= pos)
  38. {
  39. throw new NotEnoughArgumentsException();
  40. }
  41. final User user = ess.getUser(args[pos]);
  42. if (user != null)
  43. {
  44. if (!getOffline && (user.getBase() instanceof OfflinePlayer || user.isHidden()))
  45. {
  46. throw new NoSuchFieldException(Util.i18n("playerNotFound"));
  47. }
  48. return user;
  49. }
  50. final List<Player> matches = server.matchPlayer(args[pos]);
  51. if (!matches.isEmpty())
  52. {
  53. for (Player player : matches)
  54. {
  55. final User userMatch = ess.getUser(player);
  56. if (userMatch.getDisplayName().startsWith(args[pos]) && (getOffline || !userMatch.isHidden()))
  57. {
  58. return userMatch;
  59. }
  60. }
  61. final User userMatch = ess.getUser(matches.get(0));
  62. if (getOffline || !userMatch.isHidden())
  63. {
  64. return userMatch;
  65. }
  66. }
  67. throw new NoSuchFieldException(Util.i18n("playerNotFound"));
  68. }
  69. @Override
  70. public final void run(final Server server, final User user, final String commandLabel, final Command cmd, final String[] args) throws Exception
  71. {
  72. final Trade charge = new Trade(this.getName(), ess);
  73. charge.isAffordableFor(user);
  74. run(server, user, commandLabel, args);
  75. charge.charge(user);
  76. }
  77. protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
  78. {
  79. run(server, (CommandSender)user.getBase(), commandLabel, args);
  80. }
  81. @Override
  82. public final void run(final Server server, final CommandSender sender, final String commandLabel, final Command cmd, final String[] args) throws Exception
  83. {
  84. run(server, sender, commandLabel, args);
  85. }
  86. protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
  87. {
  88. throw new Exception(Util.format("onlyPlayers", commandLabel));
  89. }
  90. public static String getFinalArg(final String[] args, final int start)
  91. {
  92. final StringBuilder bldr = new StringBuilder();
  93. for (int i = start; i < args.length; i++)
  94. {
  95. if (i != start)
  96. {
  97. bldr.append(" ");
  98. }
  99. bldr.append(args[i]);
  100. }
  101. return bldr.toString();
  102. }
  103. }