BlockProtectionBank.java 9.8 KB

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