Merged with released L2J-Unity files.

This commit is contained in:
mobiusdev
2016-06-12 01:34:09 +00:00
parent e003e87887
commit 635557f5da
18352 changed files with 3245113 additions and 2892959 deletions

View File

@ -1,535 +1,388 @@
/*
* This file is part of the L2J Mobius project.
*
* This program 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.
*
* This program 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.l2jmobius.gameserver.model.entity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.gameserver.ThreadPoolManager;
import com.l2jmobius.gameserver.data.sql.impl.ClanTable;
import com.l2jmobius.gameserver.model.L2Clan;
import com.l2jmobius.gameserver.model.StatsSet;
import com.l2jmobius.gameserver.model.actor.instance.L2DoorInstance;
import com.l2jmobius.gameserver.model.actor.instance.L2PcInstance;
import com.l2jmobius.gameserver.model.itemcontainer.Inventory;
import com.l2jmobius.gameserver.model.zone.type.L2ClanHallZone;
import com.l2jmobius.gameserver.network.serverpackets.PledgeShowInfoUpdate;
public abstract class ClanHall
{
protected static final Logger _log = Logger.getLogger(ClanHall.class.getName());
private final int _clanHallId;
private ArrayList<L2DoorInstance> _doors;
private final String _name;
private int _ownerId;
private final String _desc;
private final String _location;
private L2ClanHallZone _zone;
protected boolean _isFree = true;
private final Map<Integer, ClanHallFunction> _functions;
/** Clan Hall Functions */
public static final int FUNC_TELEPORT = 1;
public static final int FUNC_ITEM_CREATE = 2;
public static final int FUNC_RESTORE_HP = 3;
public static final int FUNC_RESTORE_MP = 4;
public static final int FUNC_RESTORE_EXP = 5;
public static final int FUNC_SUPPORT = 6;
public static final int FUNC_DECO_FRONTPLATEFORM = 7; // Only Auctionable Halls
public static final int FUNC_DECO_CURTAINS = 8; // Only Auctionable Halls
public class ClanHallFunction
{
private final int _type;
private int _lvl;
protected int _fee;
protected int _tempFee;
private final long _rate;
private long _endDate;
protected boolean _inDebt;
public boolean _cwh; // first activating clanhall function is payed from player inventory, any others from clan warehouse
public ClanHallFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
{
_type = type;
_lvl = lvl;
_fee = lease;
_tempFee = tempLease;
_rate = rate;
_endDate = time;
initializeTask(cwh);
}
public int getType()
{
return _type;
}
public int getLvl()
{
return _lvl;
}
public int getLease()
{
return _fee;
}
public long getRate()
{
return _rate;
}
public long getEndTime()
{
return _endDate;
}
public void setLvl(int lvl)
{
_lvl = lvl;
}
public void setLease(int lease)
{
_fee = lease;
}
public void setEndTime(long time)
{
_endDate = time;
}
private void initializeTask(boolean cwh)
{
if (_isFree)
{
return;
}
final long currentTime = System.currentTimeMillis();
if (_endDate > currentTime)
{
ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
}
else
{
ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
}
}
private class FunctionTask implements Runnable
{
public FunctionTask(boolean cwh)
{
_cwh = cwh;
}
@Override
public void run()
{
try
{
if (_isFree)
{
return;
}
if ((ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= _fee) || !_cwh)
{
final int fee = getEndTime() == -1 ? _tempFee : _fee;
setEndTime(System.currentTimeMillis() + getRate());
dbSave();
if (_cwh)
{
ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CH_function_fee", Inventory.ADENA_ID, fee, null, null);
}
ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
}
else
{
removeFunction(getType());
}
}
catch (Exception e)
{
_log.log(Level.SEVERE, "", e);
}
}
}
public void dbSave()
{
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("REPLACE INTO clanhall_functions (hall_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)"))
{
ps.setInt(1, getId());
ps.setInt(2, getType());
ps.setInt(3, getLvl());
ps.setInt(4, getLease());
ps.setLong(5, getRate());
ps.setLong(6, getEndTime());
ps.execute();
}
catch (Exception e)
{
_log.log(Level.SEVERE, "Exception: ClanHall.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): " + e.getMessage(), e);
}
}
}
public ClanHall(StatsSet set)
{
_clanHallId = set.getInt("id");
_name = set.getString("name");
_ownerId = set.getInt("ownerId");
_desc = set.getString("desc");
_location = set.getString("location");
_functions = new ConcurrentHashMap<>();
if (_ownerId <= 0)
{
return;
}
final L2Clan clan = ClanTable.getInstance().getClan(_ownerId);
if (clan != null)
{
clan.setHideoutId(getId());
}
else
{
free();
}
}
/**
* @return Id Of Clan hall
*/
public final int getId()
{
return _clanHallId;
}
/**
* @return the Clan Hall name.
*/
public final String getName()
{
return _name;
}
/**
* @return OwnerId
*/
public final int getOwnerId()
{
return _ownerId;
}
/**
* @return Desc
*/
public final String getDesc()
{
return _desc;
}
/**
* @return Location
*/
public final String getLocation()
{
return _location;
}
/**
* @return all DoorInstance
*/
public final ArrayList<L2DoorInstance> getDoors()
{
if (_doors == null)
{
_doors = new ArrayList<>();
}
return _doors;
}
/**
* @param doorId
* @return Door
*/
public final L2DoorInstance getDoor(int doorId)
{
if (doorId <= 0)
{
return null;
}
for (L2DoorInstance door : getDoors())
{
if (door.getId() == doorId)
{
return door;
}
}
return null;
}
/**
* @param type
* @return function with id
*/
public ClanHallFunction getFunction(int type)
{
return _functions.get(type);
}
/**
* Sets this clan halls zone
* @param zone
*/
public void setZone(L2ClanHallZone zone)
{
_zone = zone;
}
/**
* @param x
* @param y
* @param z
* @return true if object is inside the zone
*/
public boolean checkIfInZone(int x, int y, int z)
{
return getZone().isInsideZone(x, y, z);
}
/**
* @return the zone of this clan hall
*/
public L2ClanHallZone getZone()
{
return _zone;
}
/** Free this clan hall */
public void free()
{
_ownerId = 0;
_isFree = true;
for (Integer fc : _functions.keySet())
{
removeFunction(fc);
}
_functions.clear();
updateDb();
}
/**
* Set owner if clan hall is free
* @param clan
*/
public void setOwner(L2Clan clan)
{
// Verify that this ClanHall is Free and Clan isn't null
if ((_ownerId > 0) || (clan == null))
{
return;
}
_ownerId = clan.getId();
_isFree = false;
clan.setHideoutId(getId());
// Announce to Online member new ClanHall
clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
updateDb();
}
/**
* Open or Close Door
* @param activeChar
* @param doorId
* @param open
*/
public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
{
if ((activeChar != null) && (activeChar.getClanId() == getOwnerId()))
{
openCloseDoor(doorId, open);
}
}
public void openCloseDoor(int doorId, boolean open)
{
openCloseDoor(getDoor(doorId), open);
}
public void openCloseDoor(L2DoorInstance door, boolean open)
{
if (door != null)
{
if (open)
{
door.openMe();
}
else
{
door.closeMe();
}
}
}
public void openCloseDoors(L2PcInstance activeChar, boolean open)
{
if ((activeChar != null) && (activeChar.getClanId() == getOwnerId()))
{
openCloseDoors(open);
}
}
public void openCloseDoors(boolean open)
{
for (L2DoorInstance door : getDoors())
{
if (door != null)
{
if (open)
{
door.openMe();
}
else
{
door.closeMe();
}
}
}
}
/** Banish Foreigner */
public void banishForeigners()
{
if (_zone != null)
{
_zone.banishForeigners(getOwnerId());
}
else
{
_log.log(Level.WARNING, getClass().getSimpleName() + ": Zone is null for clan hall: " + getId() + " " + getName());
}
}
/** Load All Functions */
protected void loadFunctions()
{
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM clanhall_functions WHERE hall_id = ?"))
{
ps.setInt(1, getId());
try (ResultSet rs = ps.executeQuery())
{
while (rs.next())
{
_functions.put(rs.getInt("type"), new ClanHallFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
}
}
}
catch (Exception e)
{
_log.log(Level.SEVERE, "Exception: ClanHall.loadFunctions(): " + e.getMessage(), e);
}
}
/**
* Remove function In List and in DB
* @param functionType
*/
public void removeFunction(int functionType)
{
_functions.remove(functionType);
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM clanhall_functions WHERE hall_id=? AND type=?"))
{
ps.setInt(1, getId());
ps.setInt(2, functionType);
ps.execute();
}
catch (Exception e)
{
_log.log(Level.SEVERE, "Exception: ClanHall.removeFunctions(int functionType): " + e.getMessage(), e);
}
}
public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
{
if ((player == null) || ((lease > 0) && !player.destroyItemByItemId("Consume", Inventory.ADENA_ID, lease, null, true)))
{
return false;
}
if (addNew)
{
_functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, 0, false));
}
else if ((lvl == 0) && (lease == 0))
{
removeFunction(type);
}
else if ((lease - _functions.get(type).getLease()) > 0)
{
_functions.remove(type);
_functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, -1, false));
}
else
{
_functions.get(type).setLease(lease);
_functions.get(type).setLvl(lvl);
_functions.get(type).dbSave();
}
return true;
}
public int getGrade()
{
return 0;
}
public long getPaidUntil()
{
return 0;
}
public int getLease()
{
return 0;
}
public boolean isSiegableHall()
{
return false;
}
public boolean isFree()
{
return _isFree;
}
public abstract void updateDb();
}
/*
* This file is part of the L2J Mobius project.
*
* This program 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.
*
* This program 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.l2jmobius.gameserver.model.entity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.gameserver.ThreadPoolManager;
import com.l2jmobius.gameserver.data.sql.impl.ClanTable;
import com.l2jmobius.gameserver.data.xml.impl.ClanHallData;
import com.l2jmobius.gameserver.enums.ClanHallGrade;
import com.l2jmobius.gameserver.enums.ClanHallType;
import com.l2jmobius.gameserver.instancemanager.ZoneManager;
import com.l2jmobius.gameserver.model.L2Clan;
import com.l2jmobius.gameserver.model.Location;
import com.l2jmobius.gameserver.model.StatsSet;
import com.l2jmobius.gameserver.model.actor.L2Npc;
import com.l2jmobius.gameserver.model.actor.instance.L2DoorInstance;
import com.l2jmobius.gameserver.model.holders.ClanHallTeleportHolder;
import com.l2jmobius.gameserver.model.itemcontainer.Inventory;
import com.l2jmobius.gameserver.model.residences.AbstractResidence;
import com.l2jmobius.gameserver.model.zone.type.L2ClanHallZone;
import com.l2jmobius.gameserver.network.SystemMessageId;
import com.l2jmobius.gameserver.network.serverpackets.PledgeShowInfoUpdate;
import com.l2jmobius.gameserver.network.serverpackets.SystemMessage;
/**
* @author St3eT
*/
public final class ClanHall extends AbstractResidence
{
// Static parameters
private final ClanHallGrade _grade;
private final ClanHallType _type;
private final int _minBid;
private final int _lease;
private final int _deposit;
private final List<Integer> _npcs;
private final List<L2DoorInstance> _doors;
private final List<ClanHallTeleportHolder> _teleports;
private final Location _ownerLocation;
private final Location _banishLocation;
// Dynamic parameters
private L2Clan _owner = null;
private long _paidUntil = 0;
protected ScheduledFuture<?> _checkPaymentTask = null;
// Other
private static final String INSERT_CLANHALL = "INSERT INTO clanhall (id, ownerId, paidUntil) VALUES (?,?,?)";
private static final String LOAD_CLANHALL = "SELECT * FROM clanhall WHERE id=?";
private static final String UPDATE_CLANHALL = "UPDATE clanhall SET ownerId=?,paidUntil=? WHERE id=?";
private static final Logger LOGGER = Logger.getLogger(ClanHallData.class.getName());
public ClanHall(StatsSet params)
{
super(params.getInt("id"));
// Set static parameters
setName(params.getString("name"));
_grade = params.getEnum("grade", ClanHallGrade.class);
_type = params.getEnum("type", ClanHallType.class);
_minBid = params.getInt("minBid");
_lease = params.getInt("lease");
_deposit = params.getInt("deposit");
_npcs = params.getList("npcList", Integer.class);
_doors = params.getList("doorList", L2DoorInstance.class);
_teleports = params.getList("teleportList", ClanHallTeleportHolder.class);
_ownerLocation = params.getLocation("owner_loc");
_banishLocation = params.getLocation("banish_loc");
// Set dynamic parameters (from DB)
load();
// Init Clan Hall zone and Functions
initResidenceZone();
initFunctions();
}
@Override
protected void load()
{
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement loadStatement = con.prepareStatement(LOAD_CLANHALL);
PreparedStatement insertStatement = con.prepareStatement(INSERT_CLANHALL))
{
loadStatement.setInt(1, getResidenceId());
try (ResultSet rset = loadStatement.executeQuery())
{
if (rset.next())
{
setPaidUntil(rset.getLong("paidUntil"));
setOwner(rset.getInt("ownerId"));
}
else
{
insertStatement.setInt(1, getResidenceId());
insertStatement.setInt(2, 0); // New clanhall should not have owner
insertStatement.setInt(3, 0); // New clanhall should not have paid until
if (insertStatement.execute())
{
LOGGER.info("Clan Hall " + getName() + " (" + getResidenceId() + ") was sucessfully created.");
}
}
}
}
catch (SQLException e)
{
LOGGER.log(Level.INFO, "Failed loading clan hall", e);
}
}
public void updateDB()
{
try (Connection con = DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement(UPDATE_CLANHALL))
{
statement.setInt(1, getOwnerId());
statement.setLong(2, getPaidUntil());
statement.setInt(3, getResidenceId());
statement.execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
protected void initResidenceZone()
{
final L2ClanHallZone zone = ZoneManager.getInstance().getAllZones(L2ClanHallZone.class).stream().filter(z -> z.getResidenceId() == getResidenceId()).findFirst().orElse(null);
if (zone != null)
{
setResidenceZone(zone);
}
}
public int getCostFailDay()
{
final Duration failDay = Duration.between(Instant.ofEpochMilli(getPaidUntil()), Instant.now());
return failDay.isNegative() ? 0 : (int) failDay.toDays();
}
/**
* Teleport all non-owner players from {@link L2ClanHallZone} to {@link ClanHall#getBanishLocation()}.
*/
public void banishOthers()
{
getResidenceZone().banishForeigners(getOwnerId());
}
/**
* Open or close all {@link L2DoorInstance} related to this {@link ClanHall}.
* @param open {@code true} means open door, {@code false} means close door
*/
public void openCloseDoors(boolean open)
{
_doors.forEach(door -> door.openCloseMe(open));
}
/**
* Gets the grade of clan hall.
* @return grade of this {@link ClanHall} in {@link ClanHallGrade} enum.
*/
public ClanHallGrade getGrade()
{
return _grade;
}
/**
* Gets all {@link L2DoorInstance} related to this {@link ClanHall}.
* @return all {@link L2DoorInstance} related to this {@link ClanHall}
*/
public List<L2DoorInstance> getDoors()
{
return _doors;
}
/**
* Gets all {@link L2Npc} related to this {@link ClanHall}.
* @return all {@link L2Npc} related to this {@link ClanHall}
*/
public List<Integer> getNpcs()
{
return _npcs;
}
/**
* Gets the {@link ClanHallType} of this {@link ClanHall}.
* @return {@link ClanHallType} of this {@link ClanHall} in {@link ClanHallGrade} enum.
*/
public ClanHallType getType()
{
return _type;
}
/**
* Gets the {@link L2Clan} which own this {@link ClanHall}.
* @return {@link L2Clan} which own this {@link ClanHall}
*/
public L2Clan getOwner()
{
return _owner;
}
/**
* Gets the {@link L2Clan} ID which own this {@link ClanHall}.
* @return the {@link L2Clan} ID which own this {@link ClanHall}
*/
@Override
public int getOwnerId()
{
final L2Clan owner = _owner;
return (owner != null) ? owner.getId() : 0;
}
/**
* Set the owner of clan hall
* @param clanId the Id of the clan
*/
public void setOwner(int clanId)
{
setOwner(ClanTable.getInstance().getClan(clanId));
}
/**
* Set the clan as owner of clan hall
* @param clan the L2Clan object
*/
public void setOwner(L2Clan clan)
{
if (clan != null)
{
_owner = clan;
clan.setHideoutId(getResidenceId());
clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
if (getPaidUntil() == 0)
{
setPaidUntil(Instant.now().plus(Duration.ofDays(7)).toEpochMilli());
}
final int failDays = getCostFailDay();
final long time = failDays > 0 ? (failDays > 8 ? Instant.now().toEpochMilli() : Instant.ofEpochMilli(getPaidUntil()).plus(Duration.ofDays(failDays + 1)).toEpochMilli()) : getPaidUntil();
_checkPaymentTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckPaymentTask(), time - System.currentTimeMillis());
}
else
{
if (_owner != null)
{
_owner.setHideoutId(0);
_owner.broadcastToOnlineMembers(new PledgeShowInfoUpdate(_owner));
removeFunctions();
}
_owner = null;
setPaidUntil(0);
if (_checkPaymentTask != null)
{
_checkPaymentTask.cancel(true);
_checkPaymentTask = null;
}
}
updateDB();
}
/**
* Gets the due date of clan hall payment
* @return the due date of clan hall payment
*/
public long getPaidUntil()
{
return _paidUntil;
}
/**
* Set the due date of clan hall payment
* @param paidUntil the due date of clan hall payment
*/
public void setPaidUntil(long paidUntil)
{
_paidUntil = paidUntil;
}
/**
* Gets the next date of clan hall payment
* @return the next date of clan hall payment
*/
public long getNextPayment()
{
return (_checkPaymentTask != null) ? System.currentTimeMillis() + _checkPaymentTask.getDelay(TimeUnit.MILLISECONDS) : 0;
}
public Location getOwnerLocation()
{
return _ownerLocation;
}
public Location getBanishLocation()
{
return _banishLocation;
}
public List<ClanHallTeleportHolder> getTeleportList()
{
return _teleports;
}
public List<ClanHallTeleportHolder> getTeleportList(int functionLevel)
{
return _teleports.stream().filter(holder -> holder.getMinFunctionLevel() <= functionLevel).collect(Collectors.toList());
}
public int getMinBid()
{
return _minBid;
}
public int getLease()
{
return _lease;
}
public int getDeposit()
{
return _deposit;
}
class CheckPaymentTask implements Runnable
{
@Override
public void run()
{
final L2Clan clan = getOwner();
if (clan != null)
{
if (clan.getWarehouse().getAdena() < getLease())
{
if (getCostFailDay() > 8)
{
setOwner(null);
clan.broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.THE_CLAN_HALL_FEE_IS_ONE_WEEK_OVERDUE_THEREFORE_THE_CLAN_HALL_OWNERSHIP_HAS_BEEN_REVOKED));
}
else
{
_checkPaymentTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckPaymentTask(), 1, TimeUnit.DAYS);
final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PAYMENT_FOR_YOUR_CLAN_HALL_HAS_NOT_BEEN_MADE_PLEASE_MAKE_PAYMENT_TO_YOUR_CLAN_WAREHOUSE_BY_S1_TOMORROW);
sm.addInt(getLease());
clan.broadcastToOnlineMembers(sm);
}
}
else
{
clan.getWarehouse().destroyItem("Clan Hall Lease", Inventory.ADENA_ID, getLease(), null, null);
setPaidUntil(Instant.ofEpochMilli(getPaidUntil()).plus(Duration.ofDays(7)).toEpochMilli());
_checkPaymentTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckPaymentTask(), getPaidUntil() - System.currentTimeMillis());
updateDB();
}
}
}
}
@Override
public String toString()
{
return (getClass().getSimpleName() + ":" + getName() + "[" + getResidenceId() + "]");
}
}