BlockProtectionBank.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.playerbank.PlayerBank;
  7. import me.km.databank.SimpleDataBank;
  8. import java.sql.Connection;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.stream.Collectors;
  12. import me.km.api.Location;
  13. import me.km.dimensions.ModDimensions;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.util.math.Vec3d;
  17. import net.minecraft.world.World;
  18. public class BlockProtectionBank extends SimpleDataBank
  19. {
  20. public BlockProtectionBank(Module m, Connection c)
  21. {
  22. super(m, c);
  23. }
  24. @Override
  25. protected void init()
  26. {
  27. // BlockProtectionBank - Coords und ID
  28. if(!this.doesTableExist("block"))
  29. {
  30. this.getModule().sendToConsole("Die Block-Protection-Bank wurde nicht gefunden, erstelle ...");
  31. if(this.update("CREATE TABLE IF NOT EXISTS block ("
  32. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  33. + "x int(11) NOT NULL, "
  34. + "y int(11) NOT NULL, "
  35. + "z int(11) NOT NULL, "
  36. + "world_name varchar(20) NOT NULL, "
  37. + "UNIQUE KEY uniq_block_pos (world_name,x,y,z));", false))
  38. {
  39. this.getModule().sendToConsole("Die Block-Protection-Bank wurde erstellt.");
  40. }
  41. }
  42. else
  43. {
  44. this.getModule().sendToConsole("Die Block-Protection-Bank wurde gefunden.");
  45. }
  46. // BlockProtectionDataBank - Coords und ID
  47. if(!this.doesTableExist("block_grant"))
  48. {
  49. this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde nicht gefunden, erstelle ...");
  50. if(this.update("CREATE TABLE block_grant ("
  51. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  52. + "block_id int(11) NOT NULL, "
  53. + "player_id int(11) NOT NULL, "
  54. + "INDEX block_id (block_id), "
  55. + "UNIQUE KEY (block_id, player_id), "
  56. + "CONSTRAINT block_grant_ibfk_1 FOREIGN KEY (block_id) REFERENCES block (id) ON DELETE CASCADE);", false))
  57. {
  58. this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde erstellt.");
  59. }
  60. }
  61. else
  62. {
  63. this.getModule().sendToConsole("Die Block-Protection-Data-Bank wurde gefunden.");
  64. }
  65. }
  66. //--------------------------------------------------------------------------
  67. // Allgemeine Texte
  68. //--------------------------------------------------------------------------
  69. private void printError(EntityPlayer p)
  70. {
  71. this.getModule().sendWarning(p, "Es ist ein Fehler aufgetreten.");
  72. }
  73. //--------------------------------------------------------------------------
  74. // Block-Methoden
  75. //--------------------------------------------------------------------------
  76. public void addBlock(BlockPos pos, World w, EntityPlayer p, String pname)
  77. {
  78. if(doesExist(pos, w))
  79. {
  80. this.getModule().send(p, "Der Block ist bereits gesichert.");
  81. return;
  82. }
  83. Integer id;
  84. pname = makeStringSafe(pname);
  85. if(pname != null)
  86. {
  87. id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByName(pname);
  88. if(id == null)
  89. {
  90. this.getModule().send(p, GlobalText.cantFindPlayer(pname));
  91. return;
  92. }
  93. }
  94. else
  95. {
  96. id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getUniqueID().toString());
  97. if(id == null)
  98. {
  99. this.getModule().send(p, GlobalText.cantFindPlayer(pname));
  100. return;
  101. }
  102. }
  103. if(this.update("INSERT INTO block (x,y,z,world_name) "
  104. + "VALUES (" + pos.getX() + "," + pos.getY() + "," + pos.getZ() + ",'" + ModDimensions.getWorldName(w) + "');", false) &&
  105. this.update("INSERT INTO block_grant (block_id,player_id) VALUES (LAST_INSERT_ID(), " + id + ");", false))
  106. {
  107. this.getModule().send(p, "Der Block wurde gesichert.");
  108. return;
  109. }
  110. printError(p);
  111. }
  112. public void removeBlock(BlockPos pos, World w, EntityPlayer p)
  113. {
  114. if(this.update("DELETE FROM block WHERE x=" + pos.getX() +
  115. " AND y=" + pos.getY() + " AND z=" + pos.getZ() +
  116. " AND world_name='" + ModDimensions.getWorldName(w) + "';", false))
  117. {
  118. this.getModule().send(p, "Die Block-Sicherung wurde gelöscht.");
  119. return;
  120. }
  121. printError(p);
  122. }
  123. public void addPlayer(BlockPos pos, World w, GameProfile p, EntityPlayer executor)
  124. {
  125. if(hasAccess(pos, w, p, false))
  126. {
  127. this.getModule().send(executor, "Der Spieler '" + p.getName() + "' ist bereits vorhanden.");
  128. return;
  129. }
  130. Integer id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getId().toString());
  131. if(id == null)
  132. {
  133. this.getModule().send(executor, GlobalText.cantFindPlayer(p.getName()));
  134. return;
  135. }
  136. if(this.update("INSERT INTO block_grant (block_id,player_id) "
  137. + "SELECT id, " + id
  138. + " FROM block WHERE world_name='" + ModDimensions.getWorldName(w) + "' AND x=" + pos.getX() + " AND y=" + pos.getY()
  139. + " AND z=" + pos.getZ() + ";", false))
  140. {
  141. this.getModule().send(executor, "Der Spieler '" + p.getName() + "' wurde hinzugefügt.");
  142. return;
  143. }
  144. printError(executor);
  145. }
  146. public void removePlayer(BlockPos pos, World w, GameProfile p, EntityPlayer executor)
  147. {
  148. if(!hasAccess(pos, w, p, false))
  149. {
  150. this.getModule().send(executor, "Der Spieler '" + p.getName() + "' ist nicht vorhanden.");
  151. return;
  152. }
  153. Integer id = KajetansMod.playerbank.getDataBank(PlayerBank.class).getIdByUUID(p.getId().toString());
  154. if(id == null)
  155. {
  156. this.getModule().send(executor, GlobalText.cantFindPlayer(p.getName()));
  157. return;
  158. }
  159. if(this.update(
  160. "DELETE block_grant FROM block_grant " +
  161. "LEFT JOIN block ON block.id = block_grant.block_id " +
  162. "WHERE block_grant.player_id = " + id + " AND " +
  163. "block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
  164. " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";", false))
  165. {
  166. this.getModule().send(executor, "Der Spieler '" + p.getName() + "' wurde entfernt.");
  167. return;
  168. }
  169. printError(executor);
  170. }
  171. public boolean hasAccess(BlockPos pos, World w, GameProfile p, boolean check)
  172. {
  173. if(check && !doesExist(pos, w))
  174. {
  175. return true;
  176. }
  177. return this.getFirst(
  178. "Select block_grant.id from block_grant " +
  179. "LEFT JOIN players ON players.id = block_grant.player_id LEFT JOIN block ON block.id = block_grant.block_id " +
  180. "WHERE players.uuid = '" + p.getId().toString() + "' AND " +
  181. "block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
  182. " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";", Integer.class) != null;
  183. }
  184. public boolean hasAccess(BlockPos pos, World w, EntityPlayer p, boolean check)
  185. {
  186. return hasAccess(pos, w, p.getGameProfile(), check);
  187. }
  188. public boolean doesExist(BlockPos pos, World w)
  189. {
  190. return this.getFirst(
  191. "SELECT id FROM block WHERE world_name='" + ModDimensions.getWorldName(w) +
  192. "' AND x=" + pos.getX() + " AND y=" + pos.getY() +
  193. " AND z=" + pos.getZ() + ";", Integer.class) != null;
  194. }
  195. public void printInfo(BlockPos pos, World w, EntityPlayer p)
  196. {
  197. Module m = this.getModule();
  198. if(!doesExist(pos, w))
  199. {
  200. m.send(p, "Dieser Block ist nicht gesichert.");
  201. return;
  202. }
  203. ArrayList<Object> list = this.getFirstColumn(
  204. "Select block_grant.player_id from block_grant " +
  205. "LEFT JOIN block ON block.id = block_grant.block_id " +
  206. "WHERE block.world_name='" + ModDimensions.getWorldName(w) + "' AND block.x=" + pos.getX() +
  207. " AND block.y=" + pos.getY() + " AND block.z=" + pos.getZ() + ";");
  208. if(list == null)
  209. {
  210. printError(p);
  211. return;
  212. }
  213. if(list.isEmpty())
  214. {
  215. m.send(p, "Dieser Block ist gesichert, gehört aber niemanden.");
  216. return;
  217. }
  218. m.send(p, "Dieser Block gehört den folgenden Spielern:");
  219. PlayerBank pb = KajetansMod.playerbank.getDataBank(PlayerBank.class);
  220. list.stream().map(id -> pb.getNameById((int) id)).filter(name -> name != null).forEach(name ->
  221. {
  222. m.sendListElement(p, name);
  223. });
  224. }
  225. public List<Location> getAll()
  226. {
  227. return this.get("Select x,y,z,world_name from block;").stream().map(li ->
  228. {
  229. try
  230. {
  231. return new Location(ModDimensions.getWorldFromName(li.get(3).toString()), new Vec3d((int) li.get(0), (int) li.get(1), (int) li.get(2)));
  232. }
  233. catch(Exception ex)
  234. {
  235. return null;
  236. }
  237. }).filter(l -> l != null && l.getWorld() != null).collect(Collectors.toList());
  238. }
  239. private String makeStringSafe(String s)
  240. {
  241. if(s == null)
  242. {
  243. return null;
  244. }
  245. return s.replace("'", "").replace("\"", "");
  246. }
  247. }