2026-07-26 19:08:44 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2026
// https://www.magicbane.com
2022-04-30 09:41:17 -04:00
package engine.db.handlers ;
2023-05-22 07:44:34 -04:00
import engine.gameManager.DbManager ;
2023-08-21 14:00:00 -04:00
import engine.math.Vector3fImmutable ;
2024-04-14 14:48:59 -04:00
import engine.mbEnums ;
import engine.mbEnums.ProfitType ;
2023-05-22 08:41:25 -04:00
import engine.objects.NPC ;
import engine.objects.NPCProfits ;
2022-04-30 09:41:17 -04:00
import org.joda.time.DateTime ;
import org.pmw.tinylog.Logger ;
2023-05-22 07:44:34 -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.util.ArrayList ;
2024-03-15 10:58:01 -04:00
import java.util.HashMap ;
2022-04-30 09:41:17 -04:00
public class dbNPCHandler extends dbHandlerBase {
2023-05-22 07:44:34 -04:00
public dbNPCHandler () {
this . localClass = NPC . class ;
2024-04-05 07:59:44 -04:00
this . localObjectType = mbEnums . GameObjectType . valueOf ( this . localClass . getSimpleName ());
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2024-03-15 10:58:01 -04:00
public static HashMap < Integer , ArrayList < Integer >> LOAD_RUNES_FOR_NPC_AND_MOBS () {
HashMap < Integer , ArrayList < Integer >> runeSets = new HashMap <> ();
int runeSetID ;
int runeBaseID ;
int recordsRead = 0 ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM static_npc_runeSet" )) {
ResultSet rs = preparedStatement . executeQuery ();
while ( rs . next ()) {
recordsRead ++ ;
runeSetID = rs . getInt ( "runeSet" );
runeBaseID = rs . getInt ( "runeBase" );
if ( runeSets . get ( runeSetID ) == null ) {
ArrayList < Integer > runeList = new ArrayList <> ();
runeList . add ( runeBaseID );
runeSets . put ( runeSetID , runeList );
} else {
ArrayList < Integer > runeList = runeSets . get ( runeSetID );
2026-10-17 14:06:10 -04:00
runeList . add ( runeBaseID );
2024-03-15 10:58:01 -04:00
runeSets . put ( runeSetID , runeList );
}
}
} catch ( SQLException e ) {
Logger . error ( e );
return runeSets ;
}
Logger . info ( "read: " + recordsRead + " cached: " + runeSets . size ());
return runeSets ;
}
2023-08-21 13:26:13 -04:00
public NPC PERSIST ( NPC toAdd ) {
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
NPC npc = null ;
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL `npc_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" )) {
2022-04-30 09:41:17 -04:00
2023-08-21 14:00:00 -04:00
Vector3fImmutable bindLocation ;
if ( toAdd . buildingUUID == 0 )
bindLocation = toAdd . bindLoc ;
else
bindLocation = Vector3fImmutable . ZERO ;
2023-08-21 13:47:47 -04:00
preparedStatement . setLong ( 1 , toAdd . parentZoneUUID );
2023-05-22 07:44:34 -04:00
preparedStatement . setString ( 2 , toAdd . getName ());
2023-08-21 13:47:47 -04:00
preparedStatement . setInt ( 3 , toAdd . contractUUID );
preparedStatement . setInt ( 4 , toAdd . guildUUID );
2023-08-21 14:00:00 -04:00
preparedStatement . setFloat ( 5 , bindLocation . x );
preparedStatement . setFloat ( 6 , bindLocation . y );
preparedStatement . setFloat ( 7 , bindLocation . z );
2023-08-21 13:47:47 -04:00
preparedStatement . setInt ( 8 , toAdd . level );
preparedStatement . setFloat ( 9 , toAdd . buyPercent );
preparedStatement . setFloat ( 10 , toAdd . sellPercent );
2022-04-30 09:41:17 -04:00
2023-08-21 14:01:28 -04:00
preparedStatement . setInt ( 11 , Math . max ( toAdd . buildingUUID , 0 ));
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-23 12:24:11 -04:00
if ( rs . next ()) {
int objectUUID = ( int ) rs . getLong ( "UID" );
2023-05-22 07:44:34 -04:00
2023-05-23 12:24:11 -04:00
if ( objectUUID > 0 )
npc = GET_NPC ( objectUUID );
}
2023-05-23 14:29:08 -04:00
2023-05-22 07:44:34 -04:00
} catch ( SQLException e ) {
throw new RuntimeException ( e );
}
return npc ;
}
public int DELETE_NPC ( final NPC npc ) {
int row_count = 0 ;
npc . removeFromZone ();
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `object` WHERE `UID` = ?" )) {
preparedStatement . setLong ( 1 , npc . getDBID ());
row_count = preparedStatement . executeUpdate ();
} catch ( SQLException e ) {
Logger . error ( e );
}
return row_count ;
}
2023-10-17 08:45:33 -04:00
public ArrayList < NPC > GET_ALL_NPCS () {
ArrayList < NPC > npcList = new ArrayList <> ();
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` ORDER BY `object`.`UID` ASC;" )) {
ResultSet rs = preparedStatement . executeQuery ();
npcList = getObjectsFromRs ( rs , 1000 );
} catch ( SQLException e ) {
Logger . error ( e );
}
return npcList ;
}
2023-05-22 07:44:34 -04:00
public NPC GET_NPC ( final int objectUUID ) {
2023-05-22 07:57:08 -04:00
NPC npc = null ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;" )) {
preparedStatement . setLong ( 1 , objectUUID );
ResultSet rs = preparedStatement . executeQuery ();
npc = ( NPC ) getObjectFromRs ( rs );
} catch ( SQLException e ) {
Logger . error ( e );
}
return npc ;
2023-05-22 07:44:34 -04:00
}
public int MOVE_NPC ( long npcID , long parentID , float locX , float locY , float locZ ) {
2023-05-22 07:57:08 -04:00
int rowCount ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `object` INNER JOIN `obj_npc` On `object`.`UID` = `obj_npc`.`UID` SET `object`.`parent`=?, `obj_npc`.`npc_spawnX`=?, `obj_npc`.`npc_spawnY`=?, `obj_npc`.`npc_spawnZ`=? WHERE `obj_npc`.`UID`=?;" )) {
preparedStatement . setLong ( 1 , parentID );
preparedStatement . setFloat ( 2 , locX );
preparedStatement . setFloat ( 3 , locY );
preparedStatement . setFloat ( 4 , locZ );
preparedStatement . setLong ( 5 , npcID );
rowCount = preparedStatement . executeUpdate ();
} catch ( SQLException e ) {
Logger . error ( e );
return 0 ;
}
return rowCount ;
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public String SET_PROPERTY ( final NPC n , String name , Object new_value ) {
2022-04-30 09:41:17 -04:00
2023-05-22 07:57:08 -04:00
String result = "" ;
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "CALL npc_SETPROP(?,?,?)" )) {
preparedStatement . setLong ( 1 , n . getDBID ());
preparedStatement . setString ( 2 , name );
preparedStatement . setString ( 3 , String . valueOf ( new_value ));
ResultSet rs = preparedStatement . executeQuery ();
2023-05-23 14:29:08 -04:00
if ( rs . next ())
result = rs . getString ( "result" );
2023-05-22 07:57:08 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
return result ;
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public void updateDatabase ( final NPC npc ) {
2023-05-22 07:57:08 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_npc SET npc_name=?, npc_contractID=?, npc_typeID=?, npc_guildID=?,"
+ " npc_spawnX=?, npc_spawnY=?, npc_spawnZ=?, npc_level=? ,"
+ " npc_buyPercent=?, npc_sellPercent=?, npc_buildingID=? WHERE UID = ?" )) {
preparedStatement . setString ( 1 , npc . getName ());
preparedStatement . setInt ( 2 , ( npc . getContract () != null ) ? npc . getContract (). getObjectUUID () : 0 );
preparedStatement . setInt ( 3 , 0 );
preparedStatement . setInt ( 4 , ( npc . getGuild () != null ) ? npc . getGuild (). getObjectUUID () : 0 );
preparedStatement . setFloat ( 5 , npc . getBindLoc (). x );
preparedStatement . setFloat ( 6 , npc . getBindLoc (). y );
preparedStatement . setFloat ( 7 , npc . getBindLoc (). z );
preparedStatement . setShort ( 8 , npc . getLevel ());
preparedStatement . setFloat ( 9 , npc . getBuyPercent ());
preparedStatement . setFloat ( 10 , npc . getSellPercent ());
preparedStatement . setInt ( 11 , ( npc . getBuilding () != null ) ? npc . getBuilding (). getObjectUUID () : 0 );
preparedStatement . setInt ( 12 , npc . getDBID ());
preparedStatement . executeUpdate ();
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public boolean updateUpgradeTime ( NPC npc , DateTime upgradeDateTime ) {
2022-04-30 09:41:17 -04:00
2023-05-22 07:57:08 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE obj_npc SET upgradeDate=? "
+ "WHERE UID = ?" )) {
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
if ( upgradeDateTime == null )
2023-05-22 07:57:08 -04:00
preparedStatement . setNull ( 1 , java . sql . Types . DATE );
2023-05-22 07:44:34 -04:00
else
2023-05-22 07:57:08 -04:00
preparedStatement . setTimestamp ( 1 , new java . sql . Timestamp ( upgradeDateTime . getMillis ()));
2022-04-30 09:41:17 -04:00
2023-05-22 07:57:08 -04:00
preparedStatement . setInt ( 2 , npc . getObjectUUID ());
preparedStatement . execute ();
return true ;
} catch ( SQLException e ) {
Logger . error ( e );
2023-05-22 07:44:34 -04:00
}
2023-05-22 07:57:08 -04:00
return false ;
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public boolean UPDATE_MOBBASE ( NPC npc , int mobBaseID ) {
2023-05-22 08:28:58 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_npc` SET `npc_raceID`=? WHERE `UID`=?" )) {
preparedStatement . setLong ( 1 , mobBaseID );
preparedStatement . setLong ( 2 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public boolean UPDATE_EQUIPSET ( NPC npc , int equipSetID ) {
2023-05-22 08:28:58 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_npc` SET `equipsetID`=? WHERE `UID`=?" )) {
preparedStatement . setInt ( 1 , equipSetID );
preparedStatement . setLong ( 2 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public boolean UPDATE_NAME ( NPC npc , String name ) {
2023-05-22 08:28:58 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_npc` SET `npc_name`=? WHERE `UID`=?" )) {
preparedStatement . setString ( 1 , name );
preparedStatement . setLong ( 2 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-22 07:44:34 -04:00
}
2024-05-12 13:36:47 -04:00
2024-05-04 21:38:06 -05:00
public boolean UPDATE_RACE ( NPC npc , int value ) {
2022-04-30 09:41:17 -04:00
2024-05-04 21:38:06 -05:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `obj_npc` SET `npc_raceID`=? WHERE `UID`=?" )) {
preparedStatement . setInt ( 1 , value );
preparedStatement . setLong ( 2 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
}
2024-05-12 13:36:47 -04:00
2023-05-22 07:44:34 -04:00
public void LOAD_PIRATE_NAMES () {
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
String pirateName ;
int mobBase ;
int recordsRead = 0 ;
2022-04-30 09:41:17 -04:00
2023-05-22 08:28:58 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM static_piratenames" )) {
2022-04-30 09:41:17 -04:00
2023-05-22 08:28:58 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
while ( rs . next ()) {
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
recordsRead ++ ;
mobBase = rs . getInt ( "mobbase" );
pirateName = rs . getString ( "first_name" );
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
// Handle new mobbbase entries
2022-04-30 09:41:17 -04:00
2023-05-22 08:28:58 -04:00
if ( NPC . _pirateNames . get ( mobBase ) == null )
2023-05-22 07:44:34 -04:00
NPC . _pirateNames . putIfAbsent ( mobBase , new ArrayList <> ());
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
// Insert name into proper arraylist
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
NPC . _pirateNames . get ( mobBase ). add ( pirateName );
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
} catch ( SQLException e ) {
2023-05-22 08:28:58 -04:00
Logger . error ( e );
2023-05-22 07:44:34 -04:00
}
2023-05-22 08:28:58 -04:00
Logger . info ( "names read: " + recordsRead + " for "
+ NPC . _pirateNames . size () + " mobBases" );
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
public boolean UPDATE_PROFITS ( NPC npc , ProfitType profitType , float value ) {
2023-05-22 08:41:25 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_npc_profits` SET `" + profitType . dbField + "` = ? WHERE `npcUID`=?" )) {
preparedStatement . setFloat ( 1 , value );
preparedStatement . setInt ( 2 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-22 07:44:34 -04:00
}
public void LOAD_NPC_PROFITS () {
NPCProfits npcProfit ;
2022-04-30 09:41:17 -04:00
2023-05-22 08:41:25 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM dyn_npc_profits" )) {
2022-04-30 09:41:17 -04:00
2023-05-22 08:41:25 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-05-22 07:44:34 -04:00
while ( rs . next ()) {
npcProfit = new NPCProfits ( rs );
NPCProfits . ProfitCache . put ( npcProfit . npcUID , npcProfit );
}
} catch ( SQLException e ) {
2023-05-22 08:41:25 -04:00
Logger . error ( e );
2023-05-22 07:44:34 -04:00
}
}
public boolean CREATE_PROFITS ( NPC npc ) {
2023-05-22 08:41:25 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_npc_profits` (`npcUID`) VALUES (?)" )) {
preparedStatement . setLong ( 1 , npc . getObjectUUID ());
return ( preparedStatement . executeUpdate () > 0 );
} catch ( SQLException e ) {
Logger . error ( e );
return false ;
}
2023-05-22 07:44:34 -04:00
}
2022-04-30 09:41:17 -04:00
}