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 getIds(int x, int y, int z, String worldName) { ArrayList 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 hasPlots(int x, int y, int z, String worldName) { 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()) { return rs.next(); } catch(SQLException ex) { ex.printStackTrace(); } } catch(SQLException ex) { ex.printStackTrace(); } return false; } @Override public boolean hasPlots(IWorld w, BlockPos pos) { return hasPlots(pos.getX(), pos.getY(), pos.getZ(), WorldManager.getName(w)); } @Override public boolean canBuild(int x, int y, int z, String worldName, int playerId) { ArrayList 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()); } }