package me.km.plots; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import me.km.KajetansMod; import me.km.databank.IStatement; import me.km.module.Module; import me.km.world.ModDimensions; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; public class ProtectionBank extends Module { public ProtectionBank() { super("Plots", TextFormatting.GOLD); KajetansMod.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));"); KajetansMod.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);"); registerEvent(new Protection()); } //-------------------------------------------------------------------------- // Plot-Methoden //-------------------------------------------------------------------------- private final IStatement add = KajetansMod.databank.createStatement( "INSERT INTO plots (x1,y1,z1,x2,y2,z2,world_name,name) VALUES (?,?,?,?,?,?,?,?);"); public void add(int x1, int y1, int z1, int x2, int y2, int z2, String worldName, String plotName) { int minX = Math.min(x1, x2); int minY = Math.min(y1, y2); int minZ = Math.min(z1, z2); int maxX = Math.max(x1, x2); int maxY = Math.max(y1, y2); int maxZ = Math.max(z1, z2); try { add.validate(); add.setInt(1, minX); add.setInt(2, minY); add.setInt(3, minZ); add.setInt(4, maxX); add.setInt(5, maxY); add.setInt(6, maxZ); add.setString(7, worldName); add.setString(8, plotName); add.executeUpdate(); } catch(SQLException ex) { ex.printStackTrace(); } } private final IStatement removePlayer = KajetansMod.databank.createStatement( "DELETE FROM plot_grant WHERE plot_id = ? AND player_id = ?;"); public void removePlayer(int plotId, int playerId) { try { removePlayer.validate(); removePlayer.setInt(1, plotId); removePlayer.setInt(2, playerId); removePlayer.executeUpdate(); } catch(SQLException ex) { ex.printStackTrace(); } } private final IStatement addPlayer = KajetansMod.databank.createStatement( "INSERT IGNORE INTO plot_grant (plot_id, player_id) VALUES(?,?);"); public void addPlayer(int plotId, int playerId) { try { addPlayer.validate(); addPlayer.setInt(1, plotId); addPlayer.setInt(2, playerId); addPlayer.executeUpdate(); } catch(SQLException ex) { ex.printStackTrace(); } } private final IStatement isOverlapping = KajetansMod.databank.createStatement( "SELECT id FROM plots WHERE x2 >= ? AND ? >= x1 AND y2 >= ? AND ? >= y1 AND z2 >= ? AND ? >= z1 AND world_name=?;"); public boolean isOverlapping(int x1, int y1, int z1, int x2, int y2, int z2, World w) { int minX = Math.min(x1, x2); int minY = Math.min(y1, y2); int minZ = Math.min(z1, z2); int maxX = Math.max(x1, x2); int maxY = Math.max(y1, y2); int maxZ = Math.max(z1, z2); try { isOverlapping.validate(); isOverlapping.setInt(1, minX); isOverlapping.setInt(2, maxX); isOverlapping.setInt(3, minY); isOverlapping.setInt(4, maxY); isOverlapping.setInt(5, minZ); isOverlapping.setInt(6, maxZ); try(ResultSet rs = isOverlapping.executeQuery()) { return rs.next(); } catch(SQLException ex) { ex.printStackTrace(); } } catch(SQLException ex) { ex.printStackTrace(); } return false; } private final IStatement getIds = KajetansMod.databank.createStatement( "SELECT id FROM plots WHERE x1<=? AND x2>=? AND y1<=? AND y2>=? AND z1<=? AND z2>=? AND world_name=?;"); public ArrayList getIds(int x, int y, int z, String worldName) { ArrayList list = new ArrayList<>(); try { getIds.validate(); 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; } private final IStatement canBuild = KajetansMod.databank.createStatement( "SELECT id FROM plot_grant WHERE plot_id=? AND player_id=?;"); 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.validate(); 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; } public boolean canBuild(World w, BlockPos pos, EntityPlayer p) { return canBuild(pos.getX(), pos.getY(), pos.getZ(), ModDimensions.getWorldName(w), KajetansMod.playerbank.getPlayerId(p.getUniqueID())); } private final IStatement remove = KajetansMod.databank.createStatement( "DELETE FROM plots WHERE id=?;"); public void remove(int plotId) { try { remove.validate(); remove.setInt(1, plotId); remove.executeUpdate(); } catch(SQLException ex) { ex.printStackTrace(); } } private final IStatement getName = KajetansMod.databank.createStatement( "SELECT name FROM plots WHERE id=?;"); public String getName(int plotId) { try { getName.validate(); getName.setInt(1, plotId); try(ResultSet rs = getName.executeQuery()) { if(rs.next()) { return rs.getString(1); } return "-error-"; } catch(SQLException ex) { ex.printStackTrace(); } } catch(SQLException ex) { ex.printStackTrace(); } return "-error-"; } }