Files
Server/src/engine/objects/PowerGrantedTrains.java
T

166 lines
5.9 KiB
Java
Raw Normal View History

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.objects;
2024-03-14 15:22:38 -04:00
import engine.gameManager.DbManager;
import engine.parsers.objects.RuneTemplate;
2022-04-30 09:41:17 -04:00
import engine.server.MBServerStatics;
import engine.util.Hasher;
2022-04-30 09:41:17 -04:00
import org.pmw.tinylog.Logger;
2024-03-14 15:22:38 -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;
import java.util.Map;
2022-04-30 09:41:17 -04:00
import java.util.concurrent.ConcurrentHashMap;
public class PowerGrantedTrains {
2022-04-30 09:41:17 -04:00
private static ConcurrentHashMap<Integer, PowerGrantedTrains> grantedPowers = null;
2023-07-15 09:23:48 -04:00
private int token;
private ConcurrentHashMap<Integer, Short> runeGrants = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
/**
* ResultSet Constructor
*/
public PowerGrantedTrains(ResultSet rs) throws SQLException {
2023-07-15 09:23:48 -04:00
this.token = rs.getInt("powerToken");
runeGrants.put(rs.getInt("runeID"), rs.getShort("grantAmount"));
}
2022-04-30 09:41:17 -04:00
public PowerGrantedTrains(int token) {
this.token = token;
}
2023-07-15 09:23:48 -04:00
/*
* Getters
*/
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public static Short getGrantedTrains(int token, PlayerCharacter pc) {
if (pc == null)
return (short) 0;
2022-04-30 09:41:17 -04:00
if (PowerGrantedTrains.grantedPowers == null)
loadGrantedPowers();
2022-04-30 09:41:17 -04:00
if (PowerGrantedTrains.grantedPowers.containsKey(token)) {
PowerGrantedTrains pg = PowerGrantedTrains.grantedPowers.get(token);
2022-04-30 09:41:17 -04:00
ConcurrentHashMap<Integer, Short> runeGrants = pg.runeGrants;
2023-07-15 09:23:48 -04:00
ArrayList<Integer> toks = new ArrayList<>();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//get race ID
2024-03-27 17:17:34 -05:00
Race race = pc.race;
2023-07-15 09:23:48 -04:00
if (race != null)
toks.add(race.getRaceRuneID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//get baseClass ID
2024-03-27 17:17:34 -05:00
BaseClass bc = pc.baseClass;
2023-07-15 09:23:48 -04:00
if (bc != null)
toks.add(bc.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//get promoClass ID
PromotionClass pcc = pc.getPromotionClass();
if (pcc != null)
toks.add(pcc.getObjectUUID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//get promotion and base class combined ID
if (bc != null && pcc != null)
toks.add(((pcc.getObjectUUID() * 10) + bc.getObjectUUID()));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//get any other rune IDs
ArrayList<CharacterRune> runes = pc.getRunes();
for (CharacterRune rune : runes)
toks.add(rune.getRuneBaseID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//Add any power bonuses granted from runes up
short amount = (short) 0;
for (Integer tok : toks) {
if (runeGrants.containsKey(tok))
amount += runeGrants.get(tok);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return amount;
} else
return (short) 0;
}
2022-04-30 09:41:17 -04:00
// Trains are power_arguments[0] of the first action; only mob race runes carry more than one.
public static void loadGrantedPowers() {
ConcurrentHashMap<Integer, PowerGrantedTrains> powerGrants = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
int token;
int grantCount = 0;
for (RuneTemplate rune : RuneTemplate._runes.values())
for (Map.Entry<String, ArrayList<int[]>> powerAction : rune.item_power_action.entrySet()) {
if (powerAction.getValue().isEmpty())
continue;
token = Hasher.SBStringHash(powerAction.getKey());
powerGrants.computeIfAbsent(token, PowerGrantedTrains::new)
.addRuneGrant(rune.template_id, (short) powerAction.getValue().get(0)[0]);
grantCount++;
}
PowerGrantedTrains.grantedPowers = powerGrants;
Logger.info("Loaded " + grantCount + " power train grants for " + powerGrants.size() + " powers");
}
2023-07-15 09:23:48 -04:00
public static void fillGrantedPowers() {
2024-03-14 15:22:38 -04:00
PowerGrantedTrains.grantedPowers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
2024-03-14 15:22:38 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powergrant")) {
ResultSet rs = preparedStatement.executeQuery();
if (PowerGrantedTrains.grantedPowers.size() > 0) {
2023-07-15 09:23:48 -04:00
rs.close();
return;
}
2024-03-14 15:22:38 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2024-03-14 15:22:38 -04:00
2023-07-15 09:23:48 -04:00
int token = rs.getInt("powerToken");
2024-03-14 15:22:38 -04:00
PowerGrantedTrains pg;
2024-03-14 15:22:38 -04:00
if (PowerGrantedTrains.grantedPowers.containsKey(token)) {
pg = PowerGrantedTrains.grantedPowers.get(token);
2023-07-15 09:23:48 -04:00
pg.addRuneGrant(rs.getInt("runeID"), rs.getShort("grantAmount"));
} else {
pg = new PowerGrantedTrains(rs);
PowerGrantedTrains.grantedPowers.put(token, pg);
2023-07-15 09:23:48 -04:00
}
}
rs.close();
} catch (SQLException e) {
Logger.error("SQL Error number: " + e.getErrorCode(), e);
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
/*
* Database
*/
public int getToken() {
return this.token;
}
private void addRuneGrant(int runeID, short amount) {
this.runeGrants.put(runeID, amount);
}
2022-04-30 09:41:17 -04:00
}