CommandBlock.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package me.km.blockprotections;
  2. import com.mojang.authlib.GameProfile;
  3. import me.km.KajetansMod;
  4. import me.km.api.GlobalText;
  5. import me.km.api.Module;
  6. import me.km.api.ModuleCommand;
  7. import me.km.api.Utils;
  8. import me.km.permissions.Permissions;
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockDoor;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.command.ICommandSender;
  13. import net.minecraft.entity.player.EntityPlayerMP;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.world.World;
  17. public class CommandBlock extends ModuleCommand
  18. {
  19. private final BlockProtectionBank bank;
  20. //private int counter;
  21. //private List<Location> locs;
  22. //private final ArrayList<BlockState> blocks;
  23. //private boolean checker;
  24. public CommandBlock(Module m)
  25. {
  26. super("block", m);
  27. super.setDescription("Zeigt alles für die Block-Verwaltung");
  28. super.setUsage("/block für die Hilfe");
  29. super.setPermission(Permissions.BLOCK);
  30. super.addAlias("blocks");
  31. super.addAlias("pro");
  32. super.addAlias("protect");
  33. bank = KajetansMod.blocks.getDataBank(BlockProtectionBank.class);
  34. //locs = new ArrayList<>();
  35. //blocks = new ArrayList<>();
  36. }
  37. @Override
  38. public boolean execute(ICommandSender cs, String[] arg)
  39. {
  40. if(!(cs instanceof EntityPlayerMP))
  41. {
  42. this.getModule().send(cs, GlobalText.onlyPlayer());
  43. return true;
  44. }
  45. Module m = this.getModule();
  46. EntityPlayerMP p = (EntityPlayerMP) cs;
  47. World w = p.getEntityWorld();
  48. if(!KajetansMod.worldManager.getWorldPreferences(w).blockProtection && !KajetansMod.perms.has(cs, Permissions.BLOCK_BYPASS))
  49. {
  50. m.send(cs, "Du darfst hier keine Blöcke schützen.");
  51. return true;
  52. }
  53. if(arg.length > 0)
  54. {
  55. BlockPos pos = Utils.getPlayerTarget(p);
  56. IBlockState state = w.getBlockState(pos);
  57. Block b = state.getBlock();
  58. if(!Utils.shouldBeProtected(b) && !KajetansMod.perms.has(cs, Permissions.BLOCK_ALL))
  59. {
  60. m.send(cs, "Du kannst diesen Block nicht bearbeiten.");
  61. m.send(cs, "Du bist auf folgenden Block gerichtet: " + b.toString());
  62. return true;
  63. }
  64. if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
  65. {
  66. pos = pos.add(0, -1, 0);
  67. }
  68. switch(arg[0])
  69. {
  70. case "protect":
  71. {
  72. String name = null;
  73. if(arg.length >= 2 && KajetansMod.perms.has(cs, Permissions.BLOCK_OTHER))
  74. {
  75. name = arg[1];
  76. }
  77. bank.addBlock(pos, w, p, name);
  78. if(b != Blocks.CHEST && b != Blocks.TRAPPED_CHEST)
  79. {
  80. return true;
  81. }
  82. BlockPos otherChest = Utils.getSameNearbyBlock(w, pos, b);
  83. if(otherChest != null)
  84. {
  85. bank.addBlock(otherChest, w, p, name);
  86. }
  87. return true;
  88. }
  89. case "remove":
  90. {
  91. if(!bank.doesExist(pos, w))
  92. {
  93. this.getModule().send(p, "Der Block ist nicht gesichert.");
  94. return true;
  95. }
  96. if(!bank.hasAccess(pos, w, p, false) && !KajetansMod.perms.has(cs, Permissions.BLOCK_BYPASS))
  97. {
  98. m.send(cs, "Du hast keinen Zugriff auf diesen Block.");
  99. return true;
  100. }
  101. bank.removeBlock(pos, w, p);
  102. if(b != Blocks.CHEST && b != Blocks.TRAPPED_CHEST)
  103. {
  104. return true;
  105. }
  106. BlockPos otherChest = Utils.getSameNearbyBlock(w, pos, b);
  107. if(otherChest != null)
  108. {
  109. bank.removeBlock(otherChest, w, p);
  110. }
  111. return true;
  112. }
  113. case "info":
  114. {
  115. bank.printInfo(pos, w, p);
  116. return true;
  117. }
  118. case "share":
  119. {
  120. if(arg.length >= 2)
  121. {
  122. if(!bank.doesExist(pos, w))
  123. {
  124. this.getModule().send(p, "Der Block ist nicht gesichert.");
  125. return true;
  126. }
  127. if(!bank.hasAccess(pos, w, p, false) && !KajetansMod.perms.has(cs, Permissions.BLOCK_BYPASS))
  128. {
  129. m.send(cs, "Du hast keinen Zugriff auf diesen Block.");
  130. return true;
  131. }
  132. GameProfile player = KajetansMod.playerbank.getDataBank().getOfflinePlayer(arg[1]);
  133. if(player == null)
  134. {
  135. m.send(cs, GlobalText.cantFindPlayer(arg[1]));
  136. return true;
  137. }
  138. bank.addPlayer(pos, w, player, p);
  139. if(b != Blocks.CHEST && b != Blocks.TRAPPED_CHEST)
  140. {
  141. return true;
  142. }
  143. BlockPos otherChest = Utils.getSameNearbyBlock(w, pos, b);
  144. if(otherChest != null)
  145. {
  146. bank.addPlayer(otherChest, w, player, p);
  147. }
  148. return true;
  149. }
  150. }
  151. case "kick":
  152. {
  153. if(arg.length >= 2)
  154. {
  155. if(!bank.doesExist(pos, w))
  156. {
  157. this.getModule().send(p, "Der Block ist nicht gesichert.");
  158. return true;
  159. }
  160. if(!bank.hasAccess(pos, w, p, true) && !KajetansMod.perms.has(cs, Permissions.BLOCK_BYPASS))
  161. {
  162. m.send(cs, "Du hast keinen Zugriff auf diesen Block.");
  163. return true;
  164. }
  165. GameProfile player = KajetansMod.playerbank.getDataBank().getOfflinePlayer(arg[1]);
  166. if(player == null)
  167. {
  168. m.send(cs, GlobalText.cantFindPlayer(arg[1]));
  169. return true;
  170. }
  171. bank.removePlayer(pos, w, player, p);
  172. if(b != Blocks.CHEST && b != Blocks.TRAPPED_CHEST)
  173. {
  174. return true;
  175. }
  176. BlockPos otherChest = Utils.getSameNearbyBlock(w, pos, b);
  177. if(otherChest != null)
  178. {
  179. bank.removePlayer(otherChest, w, player, p);
  180. }
  181. return true;
  182. }
  183. }
  184. case "clear":
  185. {
  186. if(KajetansMod.perms.has(cs, Permissions.BLOCK_CLEAR))
  187. {
  188. //TODO
  189. this.getModule().send(cs, GlobalText.notImplementedYet());
  190. /*checker = false;
  191. Bukkit.getScheduler().runTaskAsynchronously(this.getModule().getPlugin(), () ->
  192. {
  193. counter = 0;
  194. locs = bank.getAll();
  195. checker = true;
  196. });
  197. Bukkit.getScheduler().scheduleSyncDelayedTask(this.getModule().getPlugin(), () ->
  198. {
  199. if(!checker)
  200. {
  201. this.getModule().send(cs, "Das Abrufen aller Einträge hat zu lange gedauert.");
  202. locs.clear();
  203. blocks.clear();
  204. return;
  205. }
  206. locs.stream().forEach((l) ->
  207. {
  208. Bukkit.getScheduler().scheduleSyncDelayedTask(this.getModule().getPlugin(), () ->
  209. {
  210. try
  211. {
  212. blocks.add(l.getBlock().getState());
  213. }
  214. catch(NullPointerException ex)
  215. {
  216. }
  217. });
  218. });
  219. }, 200);
  220. Bukkit.getScheduler().runTaskLaterAsynchronously(this.getModule().getPlugin(), () ->
  221. {
  222. blocks.stream().filter(bl -> !Utils.shouldBeProtected(bl)).forEach(bl ->
  223. {
  224. bank.removeBlock(bl.getLocation(), p);
  225. counter++;
  226. });
  227. this.getModule().send(cs, "Alle ungültigen Protections wurden entfernt. (" + counter + ")");
  228. locs.clear();
  229. blocks.clear();
  230. }, 400);*/
  231. return true;
  232. }
  233. break;
  234. }
  235. }
  236. }
  237. m.send(cs, "/block ...");
  238. m.sendHelpListElement(cs, "protect [player]", "Erstellt eine Block-Protection");
  239. m.sendHelpListElement(cs, "remove", "Löscht eine Block-Protection");
  240. m.sendHelpListElement(cs, "info", "Gibt Information über den Block");
  241. m.sendHelpListElement(cs, "share <player>", "Teilt eine Block-Protection mit einem Spieler");
  242. m.sendHelpListElement(cs, "kick <player>", "Kickt einen Spieler von einer Block-Protection");
  243. if(KajetansMod.perms.has(cs, Permissions.BLOCK_CLEAR))
  244. {
  245. m.sendHelpListElement(cs, "clear", "Entfernt ungültige Block-Protections auf jeden Block");
  246. }
  247. return true;
  248. }
  249. }