ProtectionBank.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package me.km.plots;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import me.km.databank.DataBank;
  7. import me.km.overrides.ModEntityPlayerMP;
  8. import me.km.world.WorldManager;
  9. import net.minecraft.util.math.BlockPos;
  10. import net.minecraft.world.IWorld;
  11. public class ProtectionBank implements IProtection
  12. {
  13. private final PreparedStatement getIds;
  14. private final PreparedStatement canBuild;
  15. public ProtectionBank(DataBank databank)
  16. {
  17. databank.execute("CREATE TABLE IF NOT EXISTS plots ("
  18. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  19. + "x1 int(11) NOT NULL, "
  20. + "y1 int(11) NOT NULL, "
  21. + "z1 int(11) NOT NULL, "
  22. + "x2 int(11) NOT NULL, "
  23. + "y2 int(11) NOT NULL, "
  24. + "z2 int(11) NOT NULL, "
  25. + "world_name varchar(20) NOT NULL, "
  26. + "name varchar(50) NOT NULL, "
  27. + "INDEX (x1, x2), "
  28. + "INDEX (y1, y2), "
  29. + "INDEX (z1, z2), "
  30. + "INDEX (world_name));");
  31. databank.execute("CREATE TABLE IF NOT EXISTS plot_grant ("
  32. + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
  33. + "plot_id int(11) NOT NULL, "
  34. + "player_id int(11) NOT NULL, "
  35. + "INDEX plot_id (plot_id), "
  36. + "UNIQUE KEY (plot_id, player_id), "
  37. + "CONSTRAINT plot_grant_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);");
  38. getIds = databank.prepareStatement("SELECT id FROM plots WHERE x1<=? AND x2>=? AND y1<=? AND y2>=? AND z1<=? AND z2>=? AND world_name=?;");
  39. canBuild = databank.prepareStatement("SELECT id FROM plot_grant WHERE plot_id=? AND player_id=?;");
  40. }
  41. @Override
  42. public ArrayList<Double> getIds(int x, int y, int z, String worldName)
  43. {
  44. ArrayList<Double> list = new ArrayList<>();
  45. try
  46. {
  47. getIds.setInt(1, x);
  48. getIds.setInt(2, x);
  49. getIds.setInt(3, y);
  50. getIds.setInt(4, y);
  51. getIds.setInt(5, z);
  52. getIds.setInt(6, z);
  53. getIds.setString(7, worldName);
  54. try(ResultSet rs = getIds.executeQuery())
  55. {
  56. while(rs.next())
  57. {
  58. list.add((double) rs.getInt(1));
  59. }
  60. }
  61. catch(SQLException ex)
  62. {
  63. ex.printStackTrace();
  64. }
  65. }
  66. catch(SQLException ex)
  67. {
  68. ex.printStackTrace();
  69. }
  70. return list;
  71. }
  72. @Override
  73. public boolean canBuild(int x, int y, int z, String worldName, int playerId)
  74. {
  75. ArrayList<Double> ids = getIds(x, y, z, worldName);
  76. if(ids.isEmpty())
  77. {
  78. return true;
  79. }
  80. for(double plotId : ids)
  81. {
  82. try
  83. {
  84. canBuild.setInt(1, (int) plotId);
  85. canBuild.setInt(2, playerId);
  86. try(ResultSet rs = canBuild.executeQuery())
  87. {
  88. if(rs.next())
  89. {
  90. return true;
  91. }
  92. }
  93. catch(SQLException ex)
  94. {
  95. ex.printStackTrace();
  96. }
  97. }
  98. catch(SQLException ex)
  99. {
  100. ex.printStackTrace();
  101. }
  102. }
  103. return false;
  104. }
  105. @Override
  106. public boolean canBuild(IWorld w, BlockPos pos, ModEntityPlayerMP p)
  107. {
  108. return canBuild(pos.getX(), pos.getY(), pos.getZ(), WorldManager.getName(w), p.getId());
  109. }
  110. }