09-06-2014, 04:44 PM
Помогите пожалуйста, как сделать чтобы если гварды живые(arch1,arch2...), то flag.setIsInvul(true);
Сам инвул работает, но если гварды мертвые то инвул не снимается, вот код:
[SRC="java"]package l2ft.gameserver.instancemanager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import gnu.trove.map.hash.TIntObjectHashMap;
import l2ft.commons.dbutils.DbUtils;
import l2ft.commons.geometry.Circle;
import l2ft.gameserver.Announcements;
import l2ft.gameserver.ThreadPoolManager;
import l2ft.gameserver.ai.CtrlEvent;
import l2ft.gameserver.ai.TownNPC;
import l2ft.gameserver.database.DatabaseFactory;
import l2ft.gameserver.database.mysql;
import l2ft.gameserver.geodata.GeoEngine;
import l2ft.gameserver.model.*;
import l2ft.gameserver.model.instances.AvanpostFlagInstance;
import l2ft.gameserver.model.instances.NpcInstance;
import l2ft.gameserver.model.instances.TownNpcInstance;
import l2ft.gameserver.network.l2.components.ChatType;
import l2ft.gameserver.network.l2.components.SystemMsg;
import l2ft.gameserver.network.l2.s2c.Say2;
import l2ft.gameserver.utils.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.java2d.pipe.SpanShapeRenderer;
public class TownManager
{
private final Logger _log = LoggerFactory.getLogger(TownManager.class);
private static TownManager _instance;
public int factionId;
private TIntObjectHashMap<Town> _towns;
private static final Location spawnGuard1ID1 = new Location(-83080, 150152, -3154);
private static final Location spawnGuard2ID1 = new Location(-83016, 151688, -3154);
private static final Location spawnGuard3ID1 = new Location(-81688, 150824, -3154);
private static final Location spawnGuard4ID1 = new Location(-83080, 152760, -3203);
private static final Location spawnGuard5ID1 = new Location(-84520, 150888, -3154);
private static final Location spawnGuard6ID1 = new Location(-80843, 150829, -3064);
public static TownManager getInstance()
{
return _instance == null ? (_instance = new TownManager()) : _instance;
}
public TownManager()
{
_towns = new TIntObjectHashMap<Town>();
_log.info("[GVE_TOWN]: Town NPC spawn...");
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("SELECT * FROM gve_town");
rset = statement.executeQuery();
while (rset.next())
{
// создаем флаг
SimpleSpawner spawn = new SimpleSpawner(rset.getInt("npcId"));
spawn.setAmount(1);
spawn.setLoc(new Location(rset.getInt("x"), rset.getInt("y"), rset.getInt("z")));
spawn.setRespawnDelay(240);
factionId = rset.getInt("factionId");
Town t = new Town(rset.getInt("id"), spawn, rset.getString("name"), rset.getBoolean("capturable"), getFactionId(rset.getInt("factionId")));
t.setFactionId(getFactionId((rset.getInt("factionId"))));
_towns.put(rset.getInt("id"), t);
_log.info("[GVE_TOWN]: Town ID: " + t.ID + ", Faction: " + t.getFactionId());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, statement, rset);
}
}
public int getFactionId(int factionId)
{
return this.factionId;
}
public void setFactionId(int i)
{
factionId=i;
}
public Town getTownById(int id)
{
return _towns.get(id);
}
public Town[] getTown()
{
return _towns.values(new Town[]{});
}
public class Town
{
private String _name;
public final int ID;
private boolean _capturable;
public final int _factionId;
private SimpleSpawner _baseSpawn;
private Map<Player, Integer> rewards = new HashMap<Player, Integer>();
// guards
private List<SimpleSpawner> _guards;
public int factionId;
public Town(int id, SimpleSpawner spawn, String name, boolean canCapture, int factionId)
{
ID = id;
_factionId = factionId;
factionId = getFactionId();
_name = name;
_capturable = canCapture;
_baseSpawn = spawn;
_baseSpawn.startRespawn();
for(int i = 0; i < _baseSpawn.getAmount(); i++)
{
TownNpcInstance flag = (TownNpcInstance)_baseSpawn.doSpawn(true);
flag.setFactionId(getFactionId());
flag.setTown(this);
flag.setTitle("Город " + getName());
if(_guards != null)
{
flag.setIsInvul(true);
}
if(_guards == null)
{
flag.setIsInvul(false);
}
}
ThreadPoolManager.getInstance().schedule(new Runnable()
{
@Override
public void run()
{
// спавним охрану
spawnGuards();
}
}, 1000);
}
public void spawnGuards() {
// уже есть гварды
if (_guards != null) {
for (SimpleSpawner spawn : _guards) {
spawn.stopRespawn();
for (NpcInstance npc : spawn.getAllSpawned()) {
npc.deleteMe();
}
}
_guards = null;
}
_guards = new ArrayList<SimpleSpawner>(6);
int zMin = GeoEngine.getHeight(_baseSpawn.getLoc(), _baseSpawn.getReflection().getGeoIndex()) - 50;
int zMax = zMin + 150;
if (ID == 1)
{
if (getFactionId() == 0)
{
SimpleSpawner arch1 = new SimpleSpawner(37000);
SimpleSpawner arch2 = new SimpleSpawner(37000);
SimpleSpawner arch3 = new SimpleSpawner(37000);
SimpleSpawner arch4 = new SimpleSpawner(37000);
SimpleSpawner arch5 = new SimpleSpawner(37000);
SimpleSpawner arch6 = new SimpleSpawner(37000);
arch1.setAmount(1);
arch2.setAmount(1);
arch3.setAmount(1);
arch4.setAmount(1);
arch5.setAmount(1);
arch6.setAmount(1);
arch1.setLoc(spawnGuard1ID1);
arch2.setLoc(spawnGuard2ID1);
arch3.setLoc(spawnGuard3ID1);
arch4.setLoc(spawnGuard4ID1);
arch5.setLoc(spawnGuard5ID1);
arch6.setLoc(spawnGuard6ID1);
arch1.setRespawnTime(0);
arch2.setRespawnTime(0);
arch3.setRespawnTime(0);
arch4.setRespawnTime(0);
arch5.setRespawnTime(0);
arch6.setRespawnTime(0);
arch1.setRespawnDelay(3600);
arch2.setRespawnDelay(3600);
arch3.setRespawnDelay(3600);
arch4.setRespawnDelay(3600);
arch5.setRespawnDelay(3600);
arch6.setRespawnDelay(3600);
arch1.startRespawn();
arch2.startRespawn();
arch3.startRespawn();
arch4.startRespawn();
arch5.startRespawn();
arch6.startRespawn();
arch1.init();
arch2.init();
arch3.init();
arch4.init();
arch5.init();
arch6.init();
_guards.add(arch1);
_guards.add(arch2);
_guards.add(arch3);
_guards.add(arch4);
_guards.add(arch5);
_guards.add(arch6);
}
}
}
public String getName()
{
return _name;
}
public boolean isCapturable()
{
return _capturable;
}
public Location getLocation()
{
return _baseSpawn.getLoc();
}
public int getFactionId()
{
return this.factionId;
}
public void setFactionId(int i)
{
factionId=i;
}
public boolean canTeleport(Player player)
{
return getFactionId() == player.getFactionId();
}
private long _nextAttackNotify;
public void onEvtAttacked(Creature attacker, int damage)
{
if(_guards != null)
{
attacker.sendMessage("Командир города неуязвим пока живы его охраники!");
}
long temp = System.currentTimeMillis();
if(_nextAttackNotify <= temp)
{
_nextAttackNotify = temp + (60 * 2 * 1000);
if(attacker.getFactionId() == 1)
{
Say2 cs = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), "Атакован фракцией добра!");
for(Player player : GameObjectsStorage.getAllPlayersForIterate())
player.sendPacket(cs);
}
if(attacker.getFactionId() == 2)
{
Say2 cs1 = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), "Атакован фракцией зла!");
for(Player player : GameObjectsStorage.getAllPlayersForIterate())
player.sendPacket(cs1);
}
}
//Добавляем игрока и его урон складываем с предыдущем значением
if (rewards.get(attacker.getPlayer()) != null) {
rewards.put(attacker.getPlayer(), rewards.get(attacker.getPlayer()) + damage);
}
else
{
rewards.put(attacker.getPlayer(), damage);
}
/*for (NpcInstance flag : _baseSpawn.getAllSpawned())
{
for (NpcInstance npc : flag.getAroundNpc(1000, 200))
{
if(npc.isDead())
continue;
if(npc.getFactionId() == flag.getFactionId())
{
npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, attacker, damage);
}
}
}*/
}
public void onEvtDead(Creature killer)
{
Player player = killer.getPlayer();
if(player == null)
return;
{
StringBuilder sb = new StringBuilder();
if(_capturable)
{
sb.append("захвачен");
if(player.getFactionId() == 1)
{
sb.append(" фракцией добра.");
}
else if(player.getFactionId() == 2)
{
sb.append(" фракцией зла.");
}
}
else
{
sb.append("Уничтожен.");
}
if(!_capturable)
{
sb.append(" Начаты восстановительные работы!");
}
Player[] attackers = rewards.keySet().toArray(new Player[rewards.size()]);
for(Player attacker : attackers)
{
attacker.getInventory().addItem(22335, 50);
attacker.sendPacket(SystemMsg.YOU_CAPTURE);
attacker.sendPacket(SystemMsg.YOU_HAVE_REWARD);
}
rewards.clear();
Say2 cs = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), sb.toString());
for(Player player1 : GameObjectsStorage.getAllPlayersForIterate())
player1.sendPacket(cs);
}
if(_capturable)
{
factionId = player.getFactionId();
mysql.set("UPDATE `gve_town` SET `factionId` = " + factionId + " WHERE `id` = " + ID);
ThreadPoolManager.getInstance().schedule(new Runnable()
{
@Override
public void run()
{
// спавним охрану
spawnGuards();
}
}, 2000);
}
}
}
}[/SRC]
На счет гавно-кода можете не напоминать, знаю
Сам инвул работает, но если гварды мертвые то инвул не снимается, вот код:
[SRC="java"]package l2ft.gameserver.instancemanager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import gnu.trove.map.hash.TIntObjectHashMap;
import l2ft.commons.dbutils.DbUtils;
import l2ft.commons.geometry.Circle;
import l2ft.gameserver.Announcements;
import l2ft.gameserver.ThreadPoolManager;
import l2ft.gameserver.ai.CtrlEvent;
import l2ft.gameserver.ai.TownNPC;
import l2ft.gameserver.database.DatabaseFactory;
import l2ft.gameserver.database.mysql;
import l2ft.gameserver.geodata.GeoEngine;
import l2ft.gameserver.model.*;
import l2ft.gameserver.model.instances.AvanpostFlagInstance;
import l2ft.gameserver.model.instances.NpcInstance;
import l2ft.gameserver.model.instances.TownNpcInstance;
import l2ft.gameserver.network.l2.components.ChatType;
import l2ft.gameserver.network.l2.components.SystemMsg;
import l2ft.gameserver.network.l2.s2c.Say2;
import l2ft.gameserver.utils.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.java2d.pipe.SpanShapeRenderer;
public class TownManager
{
private final Logger _log = LoggerFactory.getLogger(TownManager.class);
private static TownManager _instance;
public int factionId;
private TIntObjectHashMap<Town> _towns;
private static final Location spawnGuard1ID1 = new Location(-83080, 150152, -3154);
private static final Location spawnGuard2ID1 = new Location(-83016, 151688, -3154);
private static final Location spawnGuard3ID1 = new Location(-81688, 150824, -3154);
private static final Location spawnGuard4ID1 = new Location(-83080, 152760, -3203);
private static final Location spawnGuard5ID1 = new Location(-84520, 150888, -3154);
private static final Location spawnGuard6ID1 = new Location(-80843, 150829, -3064);
public static TownManager getInstance()
{
return _instance == null ? (_instance = new TownManager()) : _instance;
}
public TownManager()
{
_towns = new TIntObjectHashMap<Town>();
_log.info("[GVE_TOWN]: Town NPC spawn...");
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("SELECT * FROM gve_town");
rset = statement.executeQuery();
while (rset.next())
{
// создаем флаг
SimpleSpawner spawn = new SimpleSpawner(rset.getInt("npcId"));
spawn.setAmount(1);
spawn.setLoc(new Location(rset.getInt("x"), rset.getInt("y"), rset.getInt("z")));
spawn.setRespawnDelay(240);
factionId = rset.getInt("factionId");
Town t = new Town(rset.getInt("id"), spawn, rset.getString("name"), rset.getBoolean("capturable"), getFactionId(rset.getInt("factionId")));
t.setFactionId(getFactionId((rset.getInt("factionId"))));
_towns.put(rset.getInt("id"), t);
_log.info("[GVE_TOWN]: Town ID: " + t.ID + ", Faction: " + t.getFactionId());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, statement, rset);
}
}
public int getFactionId(int factionId)
{
return this.factionId;
}
public void setFactionId(int i)
{
factionId=i;
}
public Town getTownById(int id)
{
return _towns.get(id);
}
public Town[] getTown()
{
return _towns.values(new Town[]{});
}
public class Town
{
private String _name;
public final int ID;
private boolean _capturable;
public final int _factionId;
private SimpleSpawner _baseSpawn;
private Map<Player, Integer> rewards = new HashMap<Player, Integer>();
// guards
private List<SimpleSpawner> _guards;
public int factionId;
public Town(int id, SimpleSpawner spawn, String name, boolean canCapture, int factionId)
{
ID = id;
_factionId = factionId;
factionId = getFactionId();
_name = name;
_capturable = canCapture;
_baseSpawn = spawn;
_baseSpawn.startRespawn();
for(int i = 0; i < _baseSpawn.getAmount(); i++)
{
TownNpcInstance flag = (TownNpcInstance)_baseSpawn.doSpawn(true);
flag.setFactionId(getFactionId());
flag.setTown(this);
flag.setTitle("Город " + getName());
if(_guards != null)
{
flag.setIsInvul(true);
}
if(_guards == null)
{
flag.setIsInvul(false);
}
}
ThreadPoolManager.getInstance().schedule(new Runnable()
{
@Override
public void run()
{
// спавним охрану
spawnGuards();
}
}, 1000);
}
public void spawnGuards() {
// уже есть гварды
if (_guards != null) {
for (SimpleSpawner spawn : _guards) {
spawn.stopRespawn();
for (NpcInstance npc : spawn.getAllSpawned()) {
npc.deleteMe();
}
}
_guards = null;
}
_guards = new ArrayList<SimpleSpawner>(6);
int zMin = GeoEngine.getHeight(_baseSpawn.getLoc(), _baseSpawn.getReflection().getGeoIndex()) - 50;
int zMax = zMin + 150;
if (ID == 1)
{
if (getFactionId() == 0)
{
SimpleSpawner arch1 = new SimpleSpawner(37000);
SimpleSpawner arch2 = new SimpleSpawner(37000);
SimpleSpawner arch3 = new SimpleSpawner(37000);
SimpleSpawner arch4 = new SimpleSpawner(37000);
SimpleSpawner arch5 = new SimpleSpawner(37000);
SimpleSpawner arch6 = new SimpleSpawner(37000);
arch1.setAmount(1);
arch2.setAmount(1);
arch3.setAmount(1);
arch4.setAmount(1);
arch5.setAmount(1);
arch6.setAmount(1);
arch1.setLoc(spawnGuard1ID1);
arch2.setLoc(spawnGuard2ID1);
arch3.setLoc(spawnGuard3ID1);
arch4.setLoc(spawnGuard4ID1);
arch5.setLoc(spawnGuard5ID1);
arch6.setLoc(spawnGuard6ID1);
arch1.setRespawnTime(0);
arch2.setRespawnTime(0);
arch3.setRespawnTime(0);
arch4.setRespawnTime(0);
arch5.setRespawnTime(0);
arch6.setRespawnTime(0);
arch1.setRespawnDelay(3600);
arch2.setRespawnDelay(3600);
arch3.setRespawnDelay(3600);
arch4.setRespawnDelay(3600);
arch5.setRespawnDelay(3600);
arch6.setRespawnDelay(3600);
arch1.startRespawn();
arch2.startRespawn();
arch3.startRespawn();
arch4.startRespawn();
arch5.startRespawn();
arch6.startRespawn();
arch1.init();
arch2.init();
arch3.init();
arch4.init();
arch5.init();
arch6.init();
_guards.add(arch1);
_guards.add(arch2);
_guards.add(arch3);
_guards.add(arch4);
_guards.add(arch5);
_guards.add(arch6);
}
}
}
public String getName()
{
return _name;
}
public boolean isCapturable()
{
return _capturable;
}
public Location getLocation()
{
return _baseSpawn.getLoc();
}
public int getFactionId()
{
return this.factionId;
}
public void setFactionId(int i)
{
factionId=i;
}
public boolean canTeleport(Player player)
{
return getFactionId() == player.getFactionId();
}
private long _nextAttackNotify;
public void onEvtAttacked(Creature attacker, int damage)
{
if(_guards != null)
{
attacker.sendMessage("Командир города неуязвим пока живы его охраники!");
}
long temp = System.currentTimeMillis();
if(_nextAttackNotify <= temp)
{
_nextAttackNotify = temp + (60 * 2 * 1000);
if(attacker.getFactionId() == 1)
{
Say2 cs = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), "Атакован фракцией добра!");
for(Player player : GameObjectsStorage.getAllPlayersForIterate())
player.sendPacket(cs);
}
if(attacker.getFactionId() == 2)
{
Say2 cs1 = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), "Атакован фракцией зла!");
for(Player player : GameObjectsStorage.getAllPlayersForIterate())
player.sendPacket(cs1);
}
}
//Добавляем игрока и его урон складываем с предыдущем значением
if (rewards.get(attacker.getPlayer()) != null) {
rewards.put(attacker.getPlayer(), rewards.get(attacker.getPlayer()) + damage);
}
else
{
rewards.put(attacker.getPlayer(), damage);
}
/*for (NpcInstance flag : _baseSpawn.getAllSpawned())
{
for (NpcInstance npc : flag.getAroundNpc(1000, 200))
{
if(npc.isDead())
continue;
if(npc.getFactionId() == flag.getFactionId())
{
npc.getAI().notifyEvent(CtrlEvent.EVT_AGGRESSION, attacker, damage);
}
}
}*/
}
public void onEvtDead(Creature killer)
{
Player player = killer.getPlayer();
if(player == null)
return;
{
StringBuilder sb = new StringBuilder();
if(_capturable)
{
sb.append("захвачен");
if(player.getFactionId() == 1)
{
sb.append(" фракцией добра.");
}
else if(player.getFactionId() == 2)
{
sb.append(" фракцией зла.");
}
}
else
{
sb.append("Уничтожен.");
}
if(!_capturable)
{
sb.append(" Начаты восстановительные работы!");
}
Player[] attackers = rewards.keySet().toArray(new Player[rewards.size()]);
for(Player attacker : attackers)
{
attacker.getInventory().addItem(22335, 50);
attacker.sendPacket(SystemMsg.YOU_CAPTURE);
attacker.sendPacket(SystemMsg.YOU_HAVE_REWARD);
}
rewards.clear();
Say2 cs = new Say2(0, ChatType.HERO_VOICE, "Город " +getName(), sb.toString());
for(Player player1 : GameObjectsStorage.getAllPlayersForIterate())
player1.sendPacket(cs);
}
if(_capturable)
{
factionId = player.getFactionId();
mysql.set("UPDATE `gve_town` SET `factionId` = " + factionId + " WHERE `id` = " + ID);
ThreadPoolManager.getInstance().schedule(new Runnable()
{
@Override
public void run()
{
// спавним охрану
spawnGuards();
}
}, 2000);
}
}
}
}[/SRC]
На счет гавно-кода можете не напоминать, знаю