/
tofu
/
l2interlude
Обзор
Документация
Войти
/
tofu
/
l2interlude
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/main/java/com/l2jserver/gameserver/instancemanager/SiegeGuardManager.java
187 строк
6 KB
Zoey76
Copyright Update
01 янв 2025, 07:57
01 янв 2025, 07:57
16130a5
Код
Авторство
О чём код?
/* * Copyright © 2004-2025 L2J Server * * This file is part of L2J Server. * * L2J Server is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * L2J Server is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.l2jserver.gameserver.instancemanager; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.l2jserver.commons.database.ConnectionFactory; import com.l2jserver.gameserver.model.L2Spawn; import com.l2jserver.gameserver.model.actor.instance.L2PcInstance; import com.l2jserver.gameserver.model.entity.Castle; public final class SiegeGuardManager { private static final Logger LOG = LoggerFactory.getLogger(SiegeGuardManager.class); private final Castle _castle; private final List<L2Spawn> _siegeGuardSpawn = new ArrayList<>(); public SiegeGuardManager(Castle castle) { _castle = castle; } public void addSiegeGuard(L2PcInstance activeChar, int npcId) { if (activeChar == null) { return; } addSiegeGuard(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId); } private void addSiegeGuard(int x, int y, int z, int heading, int npcId) { saveSiegeGuard(x, y, z, heading, npcId, 0); } public void hireMerc(L2PcInstance activeChar, int npcId) { if (activeChar == null) { return; } hireMerc(activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar.getHeading(), npcId); } public void hireMerc(int x, int y, int z, int heading, int npcId) { saveSiegeGuard(x, y, z, heading, npcId, 1); } /** * Remove a single mercenary, identified by the npcId and location. Presumably, this is used when a castle lord picks up a previously dropped ticket */ public void removeMerc(int npcId, int x, int y, int z) { try (var con = ConnectionFactory.getInstance().getConnection(); var ps = con.prepareStatement("DELETE FROM castle_siege_guards WHERE npcId = ? AND x = ? AND y = ? AND z = ? AND isHired = 1")) { ps.setInt(1, npcId); ps.setInt(2, x); ps.setInt(3, y); ps.setInt(4, z); ps.execute(); } catch (Exception e) { LOG.warn("Error deleting hired siege guard at {},{},{}: {}", x, y, z, e.getMessage(), e); } } /** * Remove mercs. */ public void removeMercs() { try (var con = ConnectionFactory.getInstance().getConnection(); var ps = con.prepareStatement("DELETE FROM castle_siege_guards WHERE castleId = ? AND isHired = 1")) { ps.setInt(1, getCastle().getResidenceId()); ps.execute(); } catch (Exception e) { LOG.warn("Error deleting hired siege guard for castle {}: {}", getCastle().getName(), e.getMessage(), e); } } /** * Spawn guards. */ public void spawnSiegeGuard() { try { int hiredCount = 0, hiredMax = MercTicketManager.getInstance().getMaxAllowedMerc(_castle.getResidenceId()); boolean isHired = getCastle().getOwnerId() > 0; loadSiegeGuard(); for (L2Spawn spawn : _siegeGuardSpawn) { if (spawn != null) { spawn.init(); if (isHired) { spawn.stopRespawn(); if (++hiredCount > hiredMax) { return; } } } } } catch (Exception e) { LOG.error("Error spawning siege guards for castle {}", getCastle().getName(), e); } } /** * Unspawn guards. */ public void unspawnSiegeGuard() { for (L2Spawn spawn : _siegeGuardSpawn) { if ((spawn != null) && (spawn.getLastSpawn() != null)) { spawn.stopRespawn(); spawn.getLastSpawn().doDie(spawn.getLastSpawn()); } } _siegeGuardSpawn.clear(); } /** * Load guards. */ private void loadSiegeGuard() { try (var con = ConnectionFactory.getInstance().getConnection(); var ps = con.prepareStatement("SELECT * FROM castle_siege_guards WHERE castleId = ? AND isHired = ?")) { ps.setInt(1, getCastle().getResidenceId()); if (getCastle().getOwnerId() > 0) { ps.setInt(2, 1); } else { ps.setInt(2, 0); } try (var rs = ps.executeQuery()) { while (rs.next()) { final L2Spawn spawn = new L2Spawn(rs.getInt("npcId")); spawn.setAmount(1); spawn.setX(rs.getInt("x")); spawn.setY(rs.getInt("y")); spawn.setZ(rs.getInt("z")); spawn.setHeading(rs.getInt("heading")); spawn.setRespawnDelay(rs.getInt("respawnDelay")); spawn.setLocationId(0); _siegeGuardSpawn.add(spawn); } } } catch (Exception e) { LOG.warn("Error loading siege guard for castle {}: {}", getCastle().getName(), e.getMessage(), e); } } private void saveSiegeGuard(int x, int y, int z, int heading, int npcId, int isHire) { try (var con = ConnectionFactory.getInstance().getConnection(); var ps = con.prepareStatement("INSERT INTO castle_siege_guards (castleId, npcId, x, y, z, heading, respawnDelay, isHired) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) { ps.setInt(1, getCastle().getResidenceId()); ps.setInt(2, npcId); ps.setInt(3, x); ps.setInt(4, y); ps.setInt(5, z); ps.setInt(6, heading); ps.setInt(7, (isHire == 1 ? 0 : 600)); ps.setInt(8, isHire); ps.execute(); } catch (Exception e) { LOG.warn("Error adding siege guard for castle {}: {}", getCastle().getName(), e.getMessage(), e); } } public Castle getCastle() { return _castle; } public List<L2Spawn> getSiegeGuardSpawn() { return _siegeGuardSpawn; } }