CommandBlock.java 11 KB

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