123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package me.km.plots;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import me.km.databank.DataBank;
- import me.km.overrides.ModEntityPlayerMP;
- import me.km.world.WorldManager;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.IWorld;
- public class ProtectionBank implements IProtection
- {
- private final PreparedStatement getIds;
- private final PreparedStatement canBuild;
-
- public ProtectionBank(DataBank databank)
- {
- databank.execute("CREATE TABLE IF NOT EXISTS plots ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "x1 int(11) NOT NULL, "
- + "y1 int(11) NOT NULL, "
- + "z1 int(11) NOT NULL, "
- + "x2 int(11) NOT NULL, "
- + "y2 int(11) NOT NULL, "
- + "z2 int(11) NOT NULL, "
- + "world_name varchar(20) NOT NULL, "
- + "name varchar(50) NOT NULL, "
- + "INDEX (x1, x2), "
- + "INDEX (y1, y2), "
- + "INDEX (z1, z2), "
- + "INDEX (world_name));");
-
- databank.execute("CREATE TABLE IF NOT EXISTS plot_grant ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "plot_id int(11) NOT NULL, "
- + "player_id int(11) NOT NULL, "
- + "INDEX plot_id (plot_id), "
- + "UNIQUE KEY (plot_id, player_id), "
- + "CONSTRAINT plot_grant_ibfk_1 FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE);");
-
- getIds = databank.prepareStatement("SELECT id FROM plots WHERE x1<=? AND x2>=? AND y1<=? AND y2>=? AND z1<=? AND z2>=? AND world_name=?;");
- canBuild = databank.prepareStatement("SELECT id FROM plot_grant WHERE plot_id=? AND player_id=?;");
- }
-
- @Override
- public ArrayList<Double> getIds(int x, int y, int z, String worldName)
- {
- ArrayList<Double> list = new ArrayList<>();
- try
- {
- getIds.setInt(1, x);
- getIds.setInt(2, x);
- getIds.setInt(3, y);
- getIds.setInt(4, y);
- getIds.setInt(5, z);
- getIds.setInt(6, z);
- getIds.setString(7, worldName);
- try(ResultSet rs = getIds.executeQuery())
- {
- while(rs.next())
- {
- list.add((double) rs.getInt(1));
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return list;
- }
-
- @Override
- public boolean canBuild(int x, int y, int z, String worldName, int playerId)
- {
- ArrayList<Double> ids = getIds(x, y, z, worldName);
- if(ids.isEmpty())
- {
- return true;
- }
- for(double plotId : ids)
- {
- try
- {
- canBuild.setInt(1, (int) plotId);
- canBuild.setInt(2, playerId);
- try(ResultSet rs = canBuild.executeQuery())
- {
- if(rs.next())
- {
- return true;
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- return false;
- }
-
- @Override
- public boolean canBuild(IWorld w, BlockPos pos, ModEntityPlayerMP p)
- {
- return canBuild(pos.getX(), pos.getY(), pos.getZ(), WorldManager.getName(w), p.getId());
- }
- }
|