ProtectionBank.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. package me.km.plots;
  2. import com.mojang.authlib.GameProfile;
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.stream.Collectors;
  6. import me.km.KajetansMod;
  7. import me.km.api.GlobalText;
  8. import me.km.api.Module;
  9. import me.km.databank.DataBank;
  10. import me.km.databank.SimpleDataBank;
  11. import me.km.dimensions.ModDimensions;
  12. import me.km.playerbank.PlayerBank;
  13. import net.minecraft.command.ICommandSender;
  14. import net.minecraft.entity.Entity;
  15. import net.minecraft.entity.EntityLeashKnot;
  16. import net.minecraft.entity.item.EntityBoat;
  17. import net.minecraft.entity.item.EntityItemFrame;
  18. import net.minecraft.entity.item.EntityMinecart;
  19. import net.minecraft.entity.item.EntityPainting;
  20. import net.minecraft.entity.monster.EntitySnowman;
  21. import net.minecraft.entity.passive.EntityChicken;
  22. import net.minecraft.entity.passive.EntityCow;
  23. import net.minecraft.entity.passive.EntityHorse;
  24. import net.minecraft.entity.passive.EntityMooshroom;
  25. import net.minecraft.entity.passive.EntityOcelot;
  26. import net.minecraft.entity.passive.EntityPig;
  27. import net.minecraft.entity.passive.EntityRabbit;
  28. import net.minecraft.entity.passive.EntitySheep;
  29. import net.minecraft.entity.passive.EntitySquid;
  30. import net.minecraft.entity.passive.EntityVillager;
  31. import net.minecraft.entity.player.EntityPlayer;
  32. import net.minecraft.util.math.BlockPos;
  33. import net.minecraft.world.World;
  34. public class ProtectionBank extends SimpleDataBank
  35. {
  36. private final HashSet<Class<? extends Entity>> protectetEntities;
  37. public boolean isProtected(Class<? extends Entity> et)
  38. {
  39. return protectetEntities.contains(et);
  40. }
  41. public ProtectionBank(Module m, DataBank c)
  42. {
  43. super(m, c);
  44. protectetEntities = new HashSet();
  45. protectetEntities.add(EntityBoat.class);
  46. protectetEntities.add(EntityChicken.class);
  47. protectetEntities.add(EntityCow.class);
  48. protectetEntities.add(EntityHorse.class);
  49. protectetEntities.add(EntityItemFrame.class);
  50. protectetEntities.add(EntityLeashKnot.class);
  51. protectetEntities.add(EntityMinecart.class);
  52. protectetEntities.add(EntityMooshroom.class);
  53. protectetEntities.add(EntityPainting.class);
  54. protectetEntities.add(EntityPig.class);
  55. protectetEntities.add(EntityRabbit.class);
  56. protectetEntities.add(EntitySheep.class);
  57. protectetEntities.add(EntitySnowman.class);
  58. protectetEntities.add(EntitySquid.class);
  59. protectetEntities.add(EntityOcelot.class);
  60. protectetEntities.add(EntityVillager.class);
  61. }
  62. @Override
  63. protected void init()
  64. {
  65. // PlotBank
  66. if(!this.doesTableExist("plots"))
  67. {
  68. this.getModule().sendToConsole("Die Plot-Bank wurde nicht gefunden, erstelle ...");
  69. if(this.update("CREATE TABLE IF NOT EXISTS plots ("
  70. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  71. + "x1 int(11) NOT NULL, "
  72. + "y1 int(11) NOT NULL, "
  73. + "z1 int(11) NOT NULL, "
  74. + "x2 int(11) NOT NULL, "
  75. + "y2 int(11) NOT NULL, "
  76. + "z2 int(11) NOT NULL, "
  77. + "world_name varchar(20) NOT NULL, "
  78. + "name varchar(50) NOT NULL, "
  79. + "INDEX (x1, x2), "
  80. + "INDEX (y1, y2), "
  81. + "INDEX (z1, z2), "
  82. + "INDEX (world_name));", false))
  83. {
  84. this.getModule().sendToConsole("Die Plot-Bank wurde erstellt.");
  85. }
  86. }
  87. else
  88. {
  89. this.getModule().sendToConsole("Die Plot-Bank wurde gefunden.");
  90. }
  91. // PlotBank - Owners
  92. if(!this.doesTableExist("plot_grant"))
  93. {
  94. this.getModule().sendToConsole("Die Plot-Owner-Bank wurde nicht gefunden, erstelle ...");
  95. if(this.update("CREATE TABLE plot_grant ("
  96. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  97. + "plot_id int(11) NOT NULL, "
  98. + "player_id int(11) NOT NULL, "
  99. + "INDEX plot_id (plot_id), "
  100. + "UNIQUE KEY (plot_id, player_id), "
  101. + "CONSTRAINT plot_grant_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);", false))
  102. {
  103. this.getModule().sendToConsole("Die Plot-Owner-Bank wurde erstellt.");
  104. }
  105. }
  106. else
  107. {
  108. this.getModule().sendToConsole("Die Plot-Owner-Bank wurde gefunden.");
  109. }
  110. // PlotBank - Tags
  111. if(!this.doesTableExist("plot_tags"))
  112. {
  113. this.getModule().sendToConsole("Die Plot-Tag-Bank wurde nicht gefunden, erstelle ...");
  114. if(this.update("CREATE TABLE plot_tags ("
  115. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  116. + "plot_id int(11) NOT NULL, "
  117. + "tag VARCHAR(20) NOT NULL, "
  118. + "INDEX plot_id (plot_id), "
  119. + "UNIQUE KEY (plot_id, tag), "
  120. + "CONSTRAINT plot_tags_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);", false))
  121. {
  122. this.getModule().sendToConsole("Die Plot-Tag-Bank wurde erstellt.");
  123. }
  124. }
  125. else
  126. {
  127. this.getModule().sendToConsole("Die Plot-Tag-Bank wurde gefunden.");
  128. }
  129. }
  130. //--------------------------------------------------------------------------
  131. // Allgemeine Texte
  132. //--------------------------------------------------------------------------
  133. private void printError(ICommandSender cs)
  134. {
  135. this.getModule().sendWarning(cs, "Es ist ein Fehler aufgetreten.");
  136. }
  137. private void noPlot(ICommandSender cs)
  138. {
  139. this.getModule().send(cs, "Du befindest dich auf keinem Plot.");
  140. }
  141. private void doesNotExist(ICommandSender cs, int id)
  142. {
  143. this.getModule().send(cs, "Der Plot mit der ID '" + id + "' ist nicht vorhanden.");
  144. }
  145. //--------------------------------------------------------------------------
  146. // Plot-Methoden
  147. //--------------------------------------------------------------------------
  148. public void addPlot(int x1, int y1, int z1, int x2, int y2, int z2, String world, ICommandSender sender, String pname)
  149. {
  150. Integer id;
  151. pname = makeStringSafe(pname);
  152. if(pname != null)
  153. {
  154. id = KajetansMod.playerbank.getDataBank().getIdByName(pname);
  155. if(id == null)
  156. {
  157. this.getModule().send(sender, GlobalText.cantFindPlayer(pname));
  158. return;
  159. }
  160. }
  161. else
  162. {
  163. if(!(sender instanceof EntityPlayer))
  164. {
  165. return;
  166. }
  167. EntityPlayer p = (EntityPlayer) sender;
  168. id = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getUniqueID().toString());
  169. pname = p.getName();
  170. }
  171. if(this.update("INSERT INTO plots (x1,y1,z1,x2,y2,z2,world_name,name) "
  172. + "VALUES (" + x1 + "," + y1 + "," + z1 + "," + x2 + "," + y2 + "," + z2 +
  173. ",'" + world + "','" + pname + "');", false) &&
  174. this.update("INSERT INTO plot_grant (plot_id,player_id) VALUES (LAST_INSERT_ID(), " + id + ");", false))
  175. {
  176. this.getModule().send(sender, "Der Plot wurde hinzugefügt.");
  177. return;
  178. }
  179. printError(sender);
  180. }
  181. public void changeName(Integer id, String name, ICommandSender sender)
  182. {
  183. if(id == null)
  184. {
  185. noPlot(sender);
  186. return;
  187. }
  188. if(!doesPlotExist(id))
  189. {
  190. doesNotExist(sender, id);
  191. return;
  192. }
  193. name = makeStringSafe(name);
  194. if(this.update("UPDATE plots SET name='" + name + "' WHERE id=" + id + ";", false))
  195. {
  196. this.getModule().send(sender, "Der Name wurde auf '" + name + "§r' geändert.");
  197. return;
  198. }
  199. printError(sender);
  200. }
  201. public void addTag(Integer id, String tag, ICommandSender cs)
  202. {
  203. if(id == null)
  204. {
  205. noPlot(cs);
  206. return;
  207. }
  208. if(!doesPlotExist(id))
  209. {
  210. doesNotExist(cs, id);
  211. return;
  212. }
  213. tag = makeStringSafe(tag);
  214. String tags = this.getFirst("SELECT tag FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", String.class);
  215. if(tags != null)
  216. {
  217. this.getModule().send(cs, "Der Tag '" + tag + "' ist bereits vorhanden.");
  218. return;
  219. }
  220. if(this.update("INSERT INTO plot_tags (plot_id,tag) VALUES(" + id + ",'" + tag + "');", false))
  221. {
  222. this.getModule().send(cs, "Der Tag '" + tag + "' wurde hinzugefügt.");
  223. return;
  224. }
  225. printError(cs);
  226. }
  227. public void removeTag(Integer id, String tag, ICommandSender sender)
  228. {
  229. if(id == null)
  230. {
  231. noPlot(sender);
  232. return;
  233. }
  234. if(!doesPlotExist(id))
  235. {
  236. doesNotExist(sender, id);
  237. return;
  238. }
  239. tag = makeStringSafe(tag);
  240. String tags = this.getFirst("SELECT tag FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", String.class);
  241. if(tags == null)
  242. {
  243. this.getModule().send(sender, "Der Tag '" + tag + "' ist nicht vorhanden.");
  244. return;
  245. }
  246. if(this.update("DELETE FROM plot_tags WHERE plot_id=" + id + " AND tag='" + tag + "';", false))
  247. {
  248. this.getModule().send(sender, "Der Tag '" + tag + "' wurde entfernt.");
  249. return;
  250. }
  251. printError(sender);
  252. }
  253. public boolean hasTag(World w, BlockPos pos, String tag)
  254. {
  255. return this.getFirst(
  256. "SELECT tag FROM plot_tags " +
  257. "LEFT JOIN plots ON plots.id = plot_tags.plot_id " +
  258. "WHERE x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  259. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  260. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  261. " AND world_name='" + ModDimensions.getWorldName(w) + "' AND tag='" + tag + "';", String.class) != null;
  262. }
  263. public void removePlayer(Integer id, GameProfile p, ICommandSender sender)
  264. {
  265. if(id == null)
  266. {
  267. noPlot(sender);
  268. return;
  269. }
  270. if(!doesPlotExist(id))
  271. {
  272. doesNotExist(sender, id);
  273. return;
  274. }
  275. Integer playerID = this.getFirst("SELECT player_id FROM plot_grant " +
  276. "LEFT JOIN players ON players.id = plot_grant.player_id " +
  277. "WHERE plot_grant.plot_id=" + id + " AND players.uuid='" + p.getId().toString() + "';", Integer.class);
  278. if(playerID == null)
  279. {
  280. this.getModule().send(sender, "Der Spieler '" + p.getName() + "' ist nicht vorhanden.");
  281. return;
  282. }
  283. playerID = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getId().toString());
  284. if(playerID == null)
  285. {
  286. this.getModule().send(sender, GlobalText.cantFindPlayer(p.getName()));
  287. return;
  288. }
  289. if(this.update("DELETE FROM plot_grant WHERE plot_id=" + id + " AND player_id=" + playerID + ";", false))
  290. {
  291. this.getModule().send(sender, "Der Spieler '" + p.getName() + "' wurde entfernt.");
  292. return;
  293. }
  294. printError(sender);
  295. }
  296. public void addPlayer(Integer id, GameProfile p, ICommandSender sender)
  297. {
  298. if(id == null)
  299. {
  300. noPlot(sender);
  301. return;
  302. }
  303. if(!doesPlotExist(id))
  304. {
  305. doesNotExist(sender, id);
  306. return;
  307. }
  308. Integer playerID = this.getFirst("SELECT player_id FROM plot_grant " +
  309. "LEFT JOIN players ON players.id = plot_grant.player_id " +
  310. "WHERE plot_grant.plot_id=" + id + " AND players.uuid='" + p.getId().toString() + "';", Integer.class);
  311. if(playerID != null)
  312. {
  313. this.getModule().send(sender, "Der Spieler '" + p.getName() + "' ist bereits vorhanden.");
  314. return;
  315. }
  316. playerID = KajetansMod.playerbank.getDataBank().getIdByUUID(p.getId().toString());
  317. if(playerID == null)
  318. {
  319. this.getModule().send(sender, GlobalText.cantFindPlayer(p.getName()));
  320. return;
  321. }
  322. if(this.update("INSERT INTO plot_grant (plot_id,player_id) VALUES(" + id + "," + playerID + ");", false))
  323. {
  324. this.getModule().send(sender, "Der Spieler '" + p.getName() + "' wurde hinzugefügt.");
  325. return;
  326. }
  327. printError(sender);
  328. }
  329. public boolean isPlotOverlapping(int x1, int y1, int z1, int x2, int y2, int z2, World w)
  330. {
  331. ArrayList<Object> list = this.getFirstColumn(
  332. "SELECT id FROM plots WHERE " +
  333. "(x1<=" + x1 + " AND x2>=" + x1 + " OR " +
  334. "x1<=" + x2 + " AND x2>=" + x2 + " OR " +
  335. "x1>=" + x1 + " AND x1<=" + x2 + " OR " +
  336. "x2>=" + x1 + " AND x2<=" + x2 + ") AND " +
  337. "(y1<=" + y1 + " AND y2>=" + y1 + " OR " +
  338. "y1<=" + y2 + " AND y2>=" + y2 + " OR " +
  339. "y1>=" + y1 + " AND y1<=" + y2 + " OR " +
  340. "y2>=" + y1 + " AND y2<=" + y2 + ") AND " +
  341. "(z1<=" + z1 + " AND z2>=" + z1 + " OR " +
  342. "z1<=" + z2 + " AND z2>=" + z2 + " OR " +
  343. "z1>=" + z1 + " AND z1<=" + z2 + " OR " +
  344. "z2>=" + z1 + " AND z2<=" + z2 + ") AND " +
  345. "world_name='" + ModDimensions.getWorldName(w) + "';");
  346. return !list.isEmpty();
  347. }
  348. private ProtectionStatus getProtectionStatus(World w, BlockPos pos, EntityPlayer p)
  349. {
  350. ArrayList<Object> list = this.getFirstColumn(
  351. "SELECT id FROM plots WHERE " +
  352. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  353. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  354. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  355. " AND world_name='" + ModDimensions.getWorldName(w) + "';");
  356. if(list == null)
  357. {
  358. return ProtectionStatus.ERROR;
  359. }
  360. if(list.isEmpty())
  361. {
  362. return ProtectionStatus.NOTHING;
  363. }
  364. if(list.stream().anyMatch(li ->
  365. {
  366. return this.getFirst("SELECT plot_grant.id FROM plot_grant " +
  367. "LEFT JOIN players ON players.id = plot_grant.player_id " +
  368. "WHERE plot_grant.plot_id=" + li + " AND " +
  369. "players.uuid='" + p.getUniqueID().toString() + "';", Integer.class) != null;
  370. }))
  371. {
  372. return ProtectionStatus.OWNER;
  373. }
  374. return ProtectionStatus.STRANGER;
  375. }
  376. public boolean hasProtection(World w, BlockPos pos)
  377. {
  378. ArrayList<Object> list = this.getFirstColumn(
  379. "SELECT id FROM plots WHERE " +
  380. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  381. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  382. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  383. " AND world_name='" + ModDimensions.getWorldName(w) + "';");
  384. if(list == null)
  385. {
  386. return true;
  387. }
  388. return !list.isEmpty();
  389. }
  390. public boolean canBuild(World w, BlockPos pos, EntityPlayer p)
  391. {
  392. ProtectionStatus ps = getProtectionStatus(w, pos, p);
  393. return !(ps == ProtectionStatus.STRANGER || ps == ProtectionStatus.ERROR);
  394. }
  395. public void removePlot(Integer id, ICommandSender sender)
  396. {
  397. if(id == null)
  398. {
  399. noPlot(sender);
  400. return;
  401. }
  402. if(!doesPlotExist(id))
  403. {
  404. doesNotExist(sender, id);
  405. return;
  406. }
  407. if(this.update("DELETE FROM plots WHERE id=" + id + ";", false))
  408. {
  409. this.getModule().send(sender, "Der Plot mit der ID '" + id + "' wurde gelöscht.");
  410. return;
  411. }
  412. printError(sender);
  413. }
  414. public void printInfo(EntityPlayer p)
  415. {
  416. Module m = this.getModule();
  417. BlockPos pos = p.getPosition();
  418. ArrayList<ArrayList<Object>> list = this.get("SELECT id,name FROM plots WHERE " +
  419. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  420. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  421. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  422. " AND world_name='" + ModDimensions.getWorldName(p.world) + "';");
  423. if(list == null)
  424. {
  425. printError(p);
  426. return;
  427. }
  428. if(list.isEmpty())
  429. {
  430. noPlot(p);
  431. return;
  432. }
  433. m.send(p, "Du befindest dich auf folgenden Plots:");
  434. PlayerBank pb = KajetansMod.playerbank.getDataBank();
  435. list.stream().forEach(li ->
  436. {
  437. String owner = this.getFirstColumn("SELECT player_id FROM plot_grant WHERE plot_id=" + (int) li.get(0) + ";").stream().map(id -> pb.getNameById((int) id)).collect(Collectors.joining(", "));
  438. String tags = this.getFirstColumn("SELECT tag FROM plot_tags WHERE plot_id=" + (int) li.get(0) + ";").stream().map(tag -> tag.toString()).collect(Collectors.joining(", "));
  439. m.sendHelpListElement(p, li.get(1).toString(), "(" + li.get(0).toString() + ") §7" + owner + " §7" + tags);
  440. });
  441. }
  442. public ArrayList<Object> getRegionIds(World w, BlockPos pos)
  443. {
  444. return this.getFirstColumn("SELECT id FROM plots WHERE " +
  445. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  446. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  447. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  448. " AND world_name='" + ModDimensions.getWorldName(w) + "';");
  449. }
  450. public Integer getFirstRegionId(World w, BlockPos pos)
  451. {
  452. return this.getFirst("SELECT id FROM plots WHERE " +
  453. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  454. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  455. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  456. " AND world_name='" + ModDimensions.getWorldName(w) + "';", Integer.class);
  457. }
  458. public Integer getFirstRegionId(EntityPlayer p)
  459. {
  460. return getFirstRegionId(p.world, p.getPosition());
  461. }
  462. public String getFirstRegionName(World w, BlockPos pos)
  463. {
  464. return this.getFirst("SELECT name FROM plots WHERE " +
  465. "x1<=" + pos.getX() + " AND x2>=" + pos.getX() +
  466. " AND y1<=" + pos.getY() + " AND y2>=" + pos.getY() +
  467. " AND z1<=" + pos.getZ() + " AND z2>=" + pos.getZ() +
  468. " AND world_name='" + ModDimensions.getWorldName(w) + "';", String.class);
  469. }
  470. public boolean doesPlotExist(int id)
  471. {
  472. return this.getFirst("SELECT id FROM plots WHERE id=" + id + ";", Integer.class) != null;
  473. }
  474. private String makeStringSafe(String s)
  475. {
  476. if(s == null)
  477. {
  478. return null;
  479. }
  480. return s.replace("'", "").replace("\"", "");
  481. }
  482. }