* Owner of the crystal may manage crystal functions, such as:
*
defence abilitiy
*
warning ability
*
stats upgrades
*
special items/buffs/teleports (TODO)
*/
public class L2CrystalInstance extends L2Npc
{
private final int DEFENDERS_COUNT = 5;
private int _ownerClanId = 1;
private String _locationName = "";
private boolean _defenceAbilityEnabled = false;
private boolean _warningAbilityEnabled = false;
private boolean _statsUpgradeEnabled = false;
private boolean _canWarn = true;
private boolean _canSpawnDefenders = true;
private int _warningCount = 0;
private int _defenceSpawnCount = 0;
public static FastSet _defenders = new FastSet();
public L2CrystalInstance(int objectId, L2NpcTemplate template)
{
super(objectId, template);
setIsInvul(false);
}
@Override
public void onSpawn()
{
super.onSpawn();
loadData(this.getNpcId());
}
/**
* Load Data about crystal from database.
* Clan owner Id, location name, enabled avilities etc.
* @param crystalId
*/
private void loadData(int crystalId)
{
Connection con = null;
try
{
PreparedStatement statement;
ResultSet rs;
con = L2DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("Select * from crystal_list where mapId =" + this.getNpcId());
rs = statement.executeQuery();
while (rs.next())
{
_ownerClanId = rs.getInt("clanId");
_locationName = rs.getString("locationName");
_defenceAbilityEnabled = rs.getBoolean("defenceEnabled");
_warningAbilityEnabled = rs.getBoolean("warningEnabled");
_statsUpgradeEnabled = rs.getBoolean("statsEnabled");
_warningCount = rs.getInt("warningCount");
_defenceSpawnCount = rs.getInt("defenceCount");
}
rs.close();
statement.close();
}
catch (Exception e)
{
_log.info("Crystal Manager [Load Data]: " + e);
}
finally
{
L2DatabaseFactory.close(con);
}
}
/**
* Save Data about crystal into database.
* @param crystalId
*/
public void saveData(int crystalId)
{
Connection con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
Statement statement = con.createStatement();
statement.executeUpdate("UPDATE crystal_list SET clanId ='" + _ownerClanId + "', defenceEnabled ='" + _defenceAbilityEnabled + "', warningEnabled ='" + _warningAbilityEnabled + "', statsEnabled ='" + _statsUpgradeEnabled + "', warningCount ='" + _warningCount + "', defenceCount ='" + _defenceSpawnCount + "' WHERE mapId ='" + crystalId + "'");
statement.close();
}
catch (SQLException e)
{
_log.info("Crystal Manager [Save Data]: " + e);
}
finally
{
L2DatabaseFactory.close(con);
}
}
@Override
public void onBypassFeedback(L2PcInstance player, String command)
{
if (player == null || player.getLastFolkNPC() == null || player.getLastFolkNPC().getObjectId() != this.getObjectId())
{
return;
}
if (command.startsWith("manageCrystal"))
{
if (player.isClanLeader())
{
try
{
showManageCrystalWindow(player);
}
catch (Exception e)
{
System.out.println("MANAGE CRYSTAL ERROR: " + e);
}
}
}
else if (command.startsWith("disableDefence"))
{
setDefenceAbilityEnabled(false);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("enableDefence"))
{
setDefenceAbilityEnabled(true);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("disableWarning"))
{
setWarningAbilityEnabled(false);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("enableWarning"))
{
setWarningAbilityEnabled(true);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("disableStats"))
{
setStatsEnabled(false);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("enableStats"))
{
setStatsEnabled(true);
saveDataAndShowManageWindow(player);
}
else if (command.startsWith("defenceInfo"))
{
showCrystalWindow(player, "defence.htm");
}
else if (command.startsWith("warningInfo"))
{
showCrystalWindow(player, "warning.htm");
}
else if (command.startsWith("statsInfo"))
{
showCrystalWindow(player, "stats.htm");
}
else if (command.startsWith("mainWindow"))
{
showChatWindow(player, 0);
}
else if (command.startsWith("ownerInfo"))
{
showOwnerInfoWindow(player);
}
else if (command.startsWith("crystalGuide"))
{
showCrystalWindow(player, "guide.htm");
}
else if (command.startsWith("abilitiesWindow"))
{
showAbilitiesShopWindow(player);
}
else if (command.startsWith("buyWarning"))
{
try
{
String val = command.substring(10);
StringTokenizer st = new StringTokenizer(val);
String id = st.nextToken();
int idval = Integer.parseInt(id);
buyAbility(player, idval, 1);
showAbilitiesShopWindow(player);
saveData(this.getNpcId());
}
catch (StringIndexOutOfBoundsException e)
{
player.sendMessage("Error!");
}
catch (NumberFormatException nfe)
{
player.sendMessage("Specify a valid number.");
}
}
else if (command.startsWith("buyDefence"))
{
try
{
String val = command.substring(10);
StringTokenizer st = new StringTokenizer(val);
String id = st.nextToken();
int idval = Integer.parseInt(id);
buyAbility(player, idval, 2);
showAbilitiesShopWindow(player);
saveData(this.getNpcId());
}
catch (StringIndexOutOfBoundsException e)
{
player.sendMessage("Error!");
}
catch (NumberFormatException nfe)
{
player.sendMessage("Specify a valid number.");
}
}
else if (command.startsWith("locStatus"))
{
showLocationsStatusWindow(player);
}
else if (command.startsWith("crystalFunctions"))
{
showCrystalFunctionsWindow(player);
}
}
@Override
public void showChatWindow(L2PcInstance player, int val)
{
TextBuilder tb = new TextBuilder();
tb.append("Crystal
");
tb.append("Conqerable " + getLocationName() + " Location: ");
tb.append("Welcome " + player.getName() + ", Im Occupation Crystal! ");
if (player.getClanId() == getOwnerClanId())
{
if (player.isClanLeader())
{
tb.append("How may I help you my owner? ");
tb.append("