Advertisement
lLuffy

Untitled

Jul 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /**
  2. * Copyright (C) 2016 Chikachi
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as
  6. * published by the Free Software Foundation, either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see http://www.gnu.org/licenses.
  16. */
  17.  
  18. package chikachi.discord.command.discord;
  19.  
  20. import chikachi.discord.command.DiscordCommandSender;
  21. import com.google.common.base.Joiner;
  22. import com.google.gson.stream.JsonReader;
  23. import com.google.gson.stream.JsonToken;
  24. import net.dv8tion.jda.entities.User;
  25. import net.minecraft.server.MinecraftServer;
  26.  
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30.  
  31. import org.bukkit.Bukkit;
  32. import org.bukkit.command.defaults.BukkitCommand;
  33.  
  34. public class CustomCommandConfig extends CommandConfig {
  35. private final String command;
  36.  
  37. private CustomCommandConfig(String name, String command, String... defaultRoles) {
  38. super(name, true, defaultRoles);
  39.  
  40. this.command = command;
  41. }
  42.  
  43. public static CustomCommandConfig createFromConfig(JsonReader reader) throws IOException {
  44. String name = null;
  45. String command = null;
  46. List<String> roles = new ArrayList<>();
  47.  
  48. if (reader.peek() == JsonToken.BEGIN_OBJECT) {
  49. reader.beginObject();
  50. while (reader.hasNext()) {
  51. String _name = reader.nextName();
  52. if (_name.equalsIgnoreCase("name") && reader.peek() == JsonToken.STRING) {
  53. name = reader.nextString();
  54. } else if ((_name.equalsIgnoreCase("cmd") || _name.equalsIgnoreCase("command")) && reader.peek() == JsonToken.STRING) {
  55. command = reader.nextString();
  56. } else if (_name.equalsIgnoreCase("roles") && reader.peek() == JsonToken.BEGIN_ARRAY) {
  57. reader.beginArray();
  58. while (reader.hasNext()) {
  59. if (reader.peek() == JsonToken.STRING) {
  60. roles.add(reader.nextString().toLowerCase());
  61. }
  62. }
  63. reader.endArray();
  64. } else {
  65. reader.skipValue();
  66. }
  67. }
  68. reader.endObject();
  69. if (name != null && command != null) {
  70. return new CustomCommandConfig(name, command, roles.toArray(new String[roles.size()]));
  71. }
  72. }
  73. return null;
  74. }
  75.  
  76. @Override
  77. public void execute(User user, List<String> args) {
  78. String cmd = this.command;
  79.  
  80. int argsCount = args.size();
  81. if (argsCount > 0) {
  82. for (int i = 0; i < argsCount; i++) {
  83. cmd = cmd.replace("%" + (i + 1) + "%", args.get(i));
  84. }
  85. cmd = cmd.replace("%args%", Joiner.on(' ').join(args));
  86. }
  87. cmd = cmd.replaceAll("/%([0-9]+|args)%/", "");
  88.  
  89. MinecraftServer.getServer().getCommandManager().executeCommand(
  90.  
  91. new DiscordCommandSender(user),
  92. cmd
  93. );
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement