2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers ;
import engine.Enum ;
import engine.Enum.DbObjectType ;
import engine.Enum.ProtectionState ;
import engine.Enum.TaxType ;
import engine.gameManager.DbManager ;
import engine.math.Vector3fImmutable ;
import engine.objects.* ;
import org.joda.time.DateTime ;
import org.pmw.tinylog.Logger ;
2023-05-20 19:58:47 -04:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
2022-04-30 09:41:17 -04:00
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.time.LocalDateTime ;
import java.time.ZoneId ;
import java.util.ArrayList ;
import java.util.HashMap ;
public class dbBuildingHandler extends dbHandlerBase {
2023-05-21 08:16:27 -04:00
public dbBuildingHandler () {
this . localClass = Building . class ;
this . localObjectType = Enum . GameObjectType . valueOf ( this . localClass . getSimpleName ());
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public Building CREATE_BUILDING ( int parentZoneID , int OwnerUUID , String name , int meshUUID ,
Vector3fImmutable location , float meshScale , int currentHP ,
ProtectionState protectionState , int currentGold , int rank ,
DateTime upgradeDate , int blueprintUUID , float w , float rotY ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
Building toCreate = null ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL `building_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , parentZoneID );
preparedStatement . setInt ( 2 , OwnerUUID );
preparedStatement . setString ( 3 , name );
preparedStatement . setInt ( 4 , meshUUID );
preparedStatement . setFloat ( 5 , location . x );
preparedStatement . setFloat ( 6 , location . y );
preparedStatement . setFloat ( 7 , location . z );
preparedStatement . setFloat ( 8 , meshScale );
preparedStatement . setInt ( 9 , currentHP );
preparedStatement . setString ( 10 , protectionState . name ());
preparedStatement . setInt ( 11 , currentGold );
preparedStatement . setInt ( 12 , rank );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( upgradeDate != null )
preparedStatement . setTimestamp ( 13 , new java . sql . Timestamp ( upgradeDate . getMillis ()));
else
setNULL ( 13 , java . sql . Types . DATE );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
setInt ( 14 , blueprintUUID );
setFloat ( 15 , w );
setFloat ( 16 , rotY );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
int objectUUID = ( int ) rs . getLong ( "UID" );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( objectUUID > 0 )
toCreate = GET_BUILDINGBYUUID ( objectUUID );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
throw new RuntimeException ( e );
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return toCreate ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean DELETE_FROM_DATABASE ( final Building b ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return removeFromBuildings ( b );
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public ArrayList < Building > GET_ALL_BUILDINGS_FOR_ZONE ( Zone zone ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
ArrayList < Building > buildings = new ArrayList <> ();
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_building`.*, `object`.`parent` FROM `object` INNER JOIN `obj_building` ON `obj_building`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , zone . getObjectUUID ());
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2023-05-21 15:28:50 -04:00
buildings = getObjectsFromRs ( rs , 1000 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return buildings ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public Building GET_BUILDINGBYUUID ( int uuid ) {
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
if ( uuid == 0 )
return null ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
Building building = ( Building ) DbManager . getFromCache ( Enum . GameObjectType . Building , uuid );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( building != null )
return building ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_building`.*, `object`.`parent` FROM `object` INNER JOIN `obj_building` ON `obj_building`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , uuid );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
building = ( Building ) getObjectFromRs ( rs );
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
return building ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public Building GET_BUILDING_BY_MESH ( final int meshID ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
Building building = null ;
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL building_GETBYMESH(?)" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , meshID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
if ( rs . next ())
building = new Building ( rs );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( "Building" , e );
return null ;
}
return building ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public String SET_PROPERTY ( final Building b , String name , Object new_value ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
String result = "" ;
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL building_SETPROP(?,?,?)" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , b . getObjectUUID ());
preparedStatement . setString ( 2 , name );
preparedStatement . setString ( 3 , String . valueOf ( new_value ));
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
result = rs . getString ( "result" );
2023-05-20 21:18:32 -04:00
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return result ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public int MOVE_BUILDING ( long buildingID , long parentID , float locX , float locY , float locZ ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
int rowCount ;
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `object` INNER JOIN `obj_building` On `object`.`UID` = `obj_building`.`UID` SET `object`.`parent`=?, `obj_building`.`locationX`=?, `obj_building`.`locationY`=?, `obj_building`.`locationZ`=? WHERE `obj_building`.`UID`=?;" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , parentID );
preparedStatement . setFloat ( 2 , locX );
preparedStatement . setFloat ( 3 , locY );
preparedStatement . setFloat ( 4 , locZ );
preparedStatement . setLong ( 5 , buildingID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
rowCount = preparedStatement . executeUpdate ();
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return 0 ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return rowCount ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
private boolean removeFromBuildings ( final Building b ) {
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `object` WHERE `UID` = ?" )) {
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , b . getObjectUUID ());
preparedStatement . execute ();
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 19:58:47 -04:00
2023-05-21 08:16:27 -04:00
return true ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean CHANGE_NAME ( Building b , String newName ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `name`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setString ( 1 , newName );
preparedStatement . setLong ( 2 , b . getObjectUUID ());
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean SET_RESERVE ( Building b , int reserveAmount ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `reserve`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , reserveAmount );
preparedStatement . setLong ( 2 , b . getObjectUUID ());
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
//CAS update to rank
public boolean CHANGE_RANK ( final long buildingID , int newRank ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `rank`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , newRank );
preparedStatement . setLong ( 2 , buildingID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean UPDATE_BUILDING_HEALTH ( final long buildingID , int NewHealth ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `currentHP`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , NewHealth );
preparedStatement . setLong ( 2 , buildingID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean UPDATE_BUILDING_ALTITUDE ( final long buildingID , float newAltitude ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `locationY`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setFloat ( 1 , newAltitude );
preparedStatement . setLong ( 2 , buildingID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean UPDATE_PROTECTIONSTATE ( final long buildingUUID , ProtectionState protectionState ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `protectionState`=? WHERE `UID`=?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setString ( 1 , protectionState . name ());
preparedStatement . setLong ( 2 , buildingUUID );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean UPDATE_DOOR_LOCK ( final int buildingUUID , int doorFlags ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET doorState = ? WHERE UID = ?" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , doorFlags );
preparedStatement . setInt ( 2 , buildingUUID );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean ADD_TO_FRIENDS_LIST ( final long buildingID , final long friendID , final long guildID , final int friendType ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_building_friends` (`buildingUID`, `playerUID`,`guildUID`, `friendType`) VALUES (?,?,?,?)" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
preparedStatement . setLong ( 2 , friendID );
preparedStatement . setLong ( 3 , guildID );
preparedStatement . setInt ( 4 , friendType );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean REMOVE_FROM_FRIENDS_LIST ( final long buildingID , long friendID , long guildID , int friendType ) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_building_friends` WHERE `buildingUID`=? AND `playerUID`=? AND `guildUID` =? AND `friendType` = ?" )) {
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
preparedStatement . setLong ( 2 , friendID );
preparedStatement . setLong ( 3 , guildID );
preparedStatement . setInt ( 4 , friendType );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 20:40:52 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean REMOVE_FROM_CONDEMNED_LIST ( final long buildingID , long friendID , long guildID , int friendType ) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_building_condemned` WHERE `buildingUID`=? AND `playerUID`=? AND `guildUID` =? AND `friendType` = ?" )) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
preparedStatement . setLong ( 2 , friendID );
preparedStatement . setLong ( 3 , guildID );
preparedStatement . setInt ( 4 , friendType );
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public void CLEAR_FRIENDS_LIST ( final long buildingID ) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_building_friends` WHERE `buildingUID`=?" )) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
preparedStatement . execute ();
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public void CLEAR_CONDEMNED_LIST ( final long buildingID ) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_building_condemned` WHERE `buildingUID`=?" )) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
preparedStatement . execute ();
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean CLEAR_PATROL ( final long buildingID ) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_building_patrol_points` WHERE `buildingUID`=?" )) {
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , buildingID );
return ( preparedStatement . executeUpdate () > 0 );
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-20 21:18:32 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public void LOAD_ALL_FRIENDS_FOR_BUILDING ( Building building ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( building == null )
return ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_building_friends` WHERE `buildingUID` = ?" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , building . getObjectUUID ());
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
while ( rs . next ()) {
BuildingFriends friend = new BuildingFriends ( rs );
switch ( friend . getFriendType ()) {
case 7 :
building . getFriends (). put ( friend . getPlayerUID (), friend );
break ;
case 8 :
case 9 :
building . getFriends (). put ( friend . getGuildUID (), friend );
break ;
}
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public void LOAD_ALL_CONDEMNED_FOR_BUILDING ( Building building ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( building == null )
return ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_building_condemned` WHERE `buildingUID` = ?" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , building . getObjectUUID ());
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
while ( rs . next ()) {
Condemned condemned = new Condemned ( rs );
switch ( condemned . getFriendType ()) {
case 2 :
building . getCondemned (). put ( condemned . getPlayerUID (), condemned );
break ;
case 4 :
case 5 :
building . getCondemned (). put ( condemned . getGuildUID (), condemned );
break ;
}
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public ArrayList < Vector3fImmutable > LOAD_PATROL_POINTS ( Building building ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( building == null )
return null ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
ArrayList < Vector3fImmutable > patrolPoints = new ArrayList <> ();
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_building_patrol_points` WHERE `buildingUID` = ?" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setInt ( 1 , building . getObjectUUID ());
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
while ( rs . next ()) {
float x1 = rs . getFloat ( "patrolX" );
float y1 = rs . getFloat ( "patrolY" );
float z1 = rs . getFloat ( "patrolZ" );
Vector3fImmutable patrolPoint = new Vector3fImmutable ( x1 , y1 , z1 );
patrolPoints . add ( patrolPoint );
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return patrolPoints ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean ADD_TO_CONDEMNLIST ( final long parentUID , final long playerUID , final long guildID , final int friendType ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_building_condemned` (`buildingUID`, `playerUID`,`guildUID`, `friendType`) VALUES (?,?,?,?)" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , parentUID );
preparedStatement . setLong ( 2 , playerUID );
preparedStatement . setLong ( 3 , guildID );
preparedStatement . setInt ( 4 , friendType );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public boolean ADD_TO_PATROL ( final long parentUID , final Vector3fImmutable patrolPoint ) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_building_patrol_points` (`buildingUID`, `patrolX`,`patrolY`, `patrolZ`) VALUES (?,?,?,?)" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
preparedStatement . setLong ( 1 , parentUID );
preparedStatement . setFloat ( 2 , ( int ) patrolPoint . x );
preparedStatement . setFloat ( 3 , ( int ) patrolPoint . y );
preparedStatement . setFloat ( 4 , ( int ) patrolPoint . z );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public HashMap < Integer , ArrayList < BuildingRegions >> LOAD_BUILDING_REGIONS () {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
HashMap < Integer , ArrayList < BuildingRegions >> regionList = new HashMap <> ();
BuildingRegions buildingRegions ;
int recordsRead = 0 ;
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM static_building_regions" )) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
while ( rs . next ()) {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
recordsRead ++ ;
buildingRegions = new BuildingRegions ( rs );
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
if ( regionList . get ( buildingRegions . getBuildingID ()) == null ) {
ArrayList < BuildingRegions > regionsList = new ArrayList <> ();
regionsList . add ( buildingRegions );
regionList . put ( buildingRegions . getBuildingID (), regionsList );
} else {
ArrayList < BuildingRegions > regionsList = regionList . get ( buildingRegions . getBuildingID ());
regionsList . add ( buildingRegions );
regionList . put ( buildingRegions . getBuildingID (), regionsList );
}
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
return null ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
Logger . info ( "read: " + recordsRead + " cached: " + regionList . size ());
return regionList ;
}
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
public HashMap < Integer , MeshBounds > LOAD_MESH_BOUNDS () {
2022-04-30 09:41:17 -04:00
2023-05-21 08:16:27 -04:00
HashMap < Integer , MeshBounds > meshBoundsMap = new HashMap <> ();
MeshBounds meshBounds ;
int recordsRead = 0 ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM static_mesh_bounds" )) {
ResultSet rs = preparedStatement . executeQuery ();
while ( rs . next ()) {
recordsRead ++ ;
meshBounds = new MeshBounds ( rs );
meshBoundsMap . put ( meshBounds . meshID , meshBounds );
}
} catch ( SQLException e ) {
Logger . error ( e );
}
Logger . info ( "read: " + recordsRead + " cached: " + meshBoundsMap . size ());
return meshBoundsMap ;
}
public HashMap < Integer , ArrayList < StaticColliders >> LOAD_ALL_STATIC_COLLIDERS () {
HashMap < Integer , ArrayList < StaticColliders >> colliders = new HashMap <> ();
StaticColliders thisColliders ;
int recordsRead = 0 ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM static_building_colliders" )) {
ResultSet rs = preparedStatement . executeQuery ();
while ( rs . next ()) {
recordsRead ++ ;
thisColliders = new StaticColliders ( rs );
if ( colliders . get ( thisColliders . getMeshID ()) == null ) {
ArrayList < StaticColliders > colliderList = new ArrayList <> ();
colliderList . add ( thisColliders );
colliders . put ( thisColliders . getMeshID (), colliderList );
} else {
ArrayList < StaticColliders > colliderList = colliders . get ( thisColliders . getMeshID ());
colliderList . add ( thisColliders );
colliders . put ( thisColliders . getMeshID (), colliderList );
}
}
} catch ( SQLException e ) {
Logger . error ( e );
}
Logger . info ( "read: " + recordsRead + " cached: " + colliders . size ());
return colliders ;
}
// This public class inserted here as it's a generic utility function
// with no other good place for it. If you find a good home for it
// feel free to move it. -
public final DbObjectType GET_UID_ENUM ( long object_UID ) {
DbObjectType storedEnum = DbObjectType . INVALID ;
if ( object_UID == 0 )
return storedEnum ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `type` FROM `object` WHERE `object`.`UID` = n LIMIT 1;" )) {
preparedStatement . setLong ( 1 , object_UID );
ResultSet rs = preparedStatement . executeQuery ();
if ( rs . next ())
storedEnum = DbObjectType . valueOf ( getString ( "type" ). toUpperCase ());
} catch ( SQLException e ) {
Logger . error ( "Building" , e );
return DbObjectType . INVALID ;
}
return storedEnum ;
}
public boolean updateBuildingRank ( final Building b , int Rank ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `rank`=?,"
+ "`upgradeDate`=?, `meshUUID`=?, `currentHP`=? "
+ "WHERE `UID` = ?" )) {
preparedStatement . setInt ( 1 , Rank );
preparedStatement . setNull ( 2 , java . sql . Types . DATE );
preparedStatement . setInt ( 3 , b . getBlueprint (). getMeshForRank ( Rank ));
preparedStatement . setInt ( 4 , b . getBlueprint (). getMaxHealth ( Rank ));
preparedStatement . setInt ( 5 , b . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateReverseKOS ( final Building b , boolean reverse ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `reverseKOS`=? WHERE `UID` = ?" )) {
preparedStatement . setBoolean ( 1 , reverse );
preparedStatement . setInt ( 2 , b . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateActiveCondemn ( final Condemned condemn , boolean active ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_building_condemned` SET `active`=? "
+ "WHERE`buildingUID` = ? AND `playerUID` = ? AND `guildUID` = ? AND `friendType` = ?" )) {
preparedStatement . setBoolean ( 1 , active );
preparedStatement . setInt ( 2 , condemn . getParent ());
preparedStatement . setInt ( 3 , condemn . getPlayerUID ());
preparedStatement . setInt ( 4 , condemn . getGuildUID ());
preparedStatement . setInt ( 5 , condemn . getFriendType ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateBuildingOwner ( final Building building , int ownerUUID ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_building` SET `ownerUUID`=? "
+ " WHERE `UID` = ?" )) {
preparedStatement . setInt ( 1 , ownerUUID );
preparedStatement . setInt ( 2 , building . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateBuildingUpgradeTime ( LocalDateTime upgradeDateTime , Building toUpgrade , int costToUpgrade ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET upgradeDate=?, currentGold=? "
+ "WHERE UID = ?" )) {
if ( upgradeDateTime == null )
preparedStatement . setNull ( 1 , java . sql . Types . DATE );
else
preparedStatement . setTimestamp ( 1 , new java . sql . Timestamp ( upgradeDateTime . atZone ( ZoneId . systemDefault ())
. toInstant (). toEpochMilli ()));
preparedStatement . setInt ( 2 , toUpgrade . getStrongboxValue () - costToUpgrade );
preparedStatement . setInt ( 3 , toUpgrade . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean updateMaintDate ( Building building ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET maintDate=? "
+ "WHERE UID = ?" )) {
if ( building . maintDateTime == null )
preparedStatement . setNull ( 1 , java . sql . Types . DATE );
else
preparedStatement . setTimestamp ( 1 , new java . sql . Timestamp ( building . maintDateTime . atZone ( ZoneId . systemDefault ())
. toInstant (). toEpochMilli ()));
preparedStatement . setInt ( 2 , building . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean addTaxes ( Building building , TaxType taxType , int amount , boolean enforceKOS ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET taxType=?, taxAmount = ?, enforceKOS = ? "
+ "WHERE UID = ?" )) {
preparedStatement . setString ( 1 , taxType . name ());
preparedStatement . setInt ( 2 , amount );
preparedStatement . setBoolean ( 3 , enforceKOS );
preparedStatement . setInt ( 4 , building . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean removeTaxes ( Building building ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET taxType=?, taxAmount = ?, enforceKOS = ?, taxDate = ? "
+ "WHERE UID = ?" )) {
preparedStatement . setString ( 1 , TaxType . NONE . name ());
preparedStatement . setInt ( 2 , 0 );
preparedStatement . setBoolean ( 3 , false );
preparedStatement . setNull ( 4 , java . sql . Types . DATE );
preparedStatement . setInt ( 5 , building . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
public boolean acceptTaxes ( Building building ) {
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_building SET taxDate=? "
+ "WHERE UID = ?" )) {
preparedStatement . setTimestamp ( 1 , new java . sql . Timestamp ( DateTime . now (). plusDays ( 7 ). getMillis ()));
preparedStatement . setInt ( 2 , building . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
2022-04-30 09:41:17 -04:00
}