Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package com.zodiacmc.tlm.commands;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.function.Consumer;
  6.  
  7. import org.bukkit.event.Event;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  11. import org.bukkit.event.server.ServerCommandEvent;
  12.  
  13. import com.zodiacmc.tlm.users.Rank;
  14.  
  15. public class BukkitCommand implements Runnable, Listener {
  16.  
  17. private boolean async = false;
  18. private List<CommandArgument> arguments;
  19.  
  20. private String name, description;
  21. private boolean console, player, isChild;
  22. private Thread thread;
  23. private Rank rank;
  24. private Runnable runnable;
  25. private Consumer<BukkitCommand> consumer;
  26. private List<BukkitCommand> childCommands;
  27. private Event e;
  28. private static List<BukkitCommand> commands = new ArrayList<BukkitCommand>();
  29.  
  30. private BukkitCommand(BukkitCommand.Builder builder) {
  31. this.arguments = new ArrayList<CommandArgument>();
  32. commands.add(this);
  33. }
  34.  
  35. public static BukkitCommand.Builder builder(){
  36. return new BukkitCommand.Builder();
  37. }
  38.  
  39. public boolean hasAnyArguments() {
  40. return arguments.size() > 1;
  41. }
  42.  
  43. public List<CommandArgument> getArguments(){
  44. return arguments;
  45. }
  46.  
  47. public String getName() {
  48. return name;
  49. }
  50.  
  51. public void execute() {
  52. if (consumer == null && runnable == null) {
  53. new CommandException("Consumer & Runnable are null");
  54. return;
  55. }
  56.  
  57. if (thread != null) {
  58. if (!Thread.currentThread().equals(thread)) {
  59. thread.start();
  60. return;
  61. }
  62. }
  63. if (consumer != null) {
  64. consumer.accept(this);
  65. } else {
  66. runnable.run();
  67. }
  68. }
  69.  
  70. public static class Builder {
  71.  
  72. public BukkitCommand.Builder builder;
  73. public String description;
  74. public boolean async;
  75. public Consumer<BukkitCommand> consumer;
  76. public Runnable runnable;
  77. public Rank rank;
  78. public List<BukkitCommand> childCommands = new ArrayList<BukkitCommand>();
  79.  
  80. private Builder() {
  81. this.builder = this;
  82. }
  83.  
  84. public BukkitCommand.Builder description(String s){
  85. this.description = s;
  86. return this;
  87. }
  88.  
  89. public BukkitCommand.Builder async() {
  90. this.async = true;
  91. return this;
  92. }
  93.  
  94. public BukkitCommand.Builder child(BukkitCommand command){
  95. childCommands.add(command);
  96. return this;
  97. }
  98.  
  99. public BukkitCommand.Builder execute(Consumer<BukkitCommand> consumer) {
  100. this.consumer = consumer;
  101. return this;
  102. }
  103.  
  104. public BukkitCommand.Builder execute(Runnable runnable) {
  105. this.runnable = runnable;
  106. return this;
  107. }
  108.  
  109. public BukkitCommand.Builder rank(Rank rank){
  110. this.rank = rank;
  111. return this;
  112. }
  113.  
  114. public BukkitCommand build() {
  115. return new BukkitCommand(this);
  116. }
  117.  
  118. }
  119.  
  120. @Override
  121. public void run() {
  122. execute();
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement