Code: Select all
package instances.Training; import java.util.Calendar; import com.l2jserver.gameserver.instancemanager.InstanceManager;import com.l2jserver.gameserver.model.L2World;import com.l2jserver.gameserver.model.Location;import com.l2jserver.gameserver.model.actor.L2Npc;import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;import com.l2jserver.gameserver.model.entity.Instance;import com.l2jserver.gameserver.model.instancezone.InstanceWorld;import com.l2jserver.gameserver.model.quest.Quest;import com.l2jserver.gameserver.network.SystemMessageId;import com.l2jserver.gameserver.network.serverpackets.SystemMessage; public class TrainingGround extends Quest{ private static final int INSTANCE_ID = 999; // NPC private static final int ENTER_NPC = 150; private static final int EXIT_NPC = 151; private static final int BOSS_NPC = 152; // Item private static final int PASS = 3470; // item entrance ID // Initialization at 6:30 am everyday private static final int RESET_HOUR = 6; private static final int RESET_MIN = 30; private static final Location ENTER_TELEPORT = new Location(-76435, -185543, -11008, 0); private static final Location EXIT_TELEPORT = new Location(42781, -48086, 796, 0); protected class TrainingWorld extends InstanceWorld { public L2Npc enter_npc; public L2Npc exit_npc; public L2MonsterInstance boss_npc; } public TrainingGround(int questId, String name, String descr) { super(questId, name, descr); addFirstTalkId(ENTER_NPC, EXIT_NPC); addSpawnId(BOSS_NPC); addKillId(BOSS_NPC); addAttackId(BOSS_NPC); } @Override public String onTalk(L2Npc npc, L2PcInstance player) { String htmltext = null; if (npc.getId() == ENTER_NPC) { htmltext = checkConditions(player); if (htmltext == null) { enterInstance(player, "TrainingGround.xml"); } } else if (npc.getId() == EXIT_NPC) { InstanceWorld world = InstanceManager.getInstance().getWorld(npc.getInstanceId()); if ((world != null) && (world.getInstanceId() == INSTANCE_ID)) { world.removeAllowed(player.getObjectId()); teleportPlayer(player, EXIT_TELEPORT, 0); } } return htmltext; } /** * @param player * @return */ private String checkConditions(L2PcInstance player) { if (player.getInventory().getInventoryItemCount(PASS, -1, false) <= 1) { return "no-item.htm"; } return null; } @Override public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon) { int instanceId = npc.getInstanceId(); if (instanceId > 0) { Instance inst = InstanceManager.getInstance().getInstance(instanceId); InstanceWorld world = InstanceManager.getInstance().getWorld(npc.getInstanceId()); inst.setSpawnLoc(EXIT_TELEPORT); // Terminate instance in 5 min if ((inst.getInstanceEndTime() - System.currentTimeMillis()) > 300000) { inst.setDuration(300000); } inst.setEmptyDestroyTime(0); if ((world != null) && (world.getInstanceId() == INSTANCE_ID)) { setReenterTime(world); } addSpawn(EXIT_NPC, -22144, 278744, -8239, 0, false, 0, false, instanceId); return super.onKill(npc, killer, isSummon); } return null; } private boolean checkTeleport(L2PcInstance player) { Long reentertime = InstanceManager.getInstance().getInstanceTime(player.getObjectId(), INSTANCE_ID); if (System.currentTimeMillis() < reentertime) { SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.C1_MAY_NOT_REENTER_YET); player.broadcastPacket(sm); return false; } return true; } private int enterInstance(L2PcInstance player, String template) { int instanceId = 0; // check for existing instances for this player InstanceWorld world = InstanceManager.getInstance().getPlayerWorld(player); // existing instance if (world != null) { if ((world.getInstanceId() != INSTANCE_ID)) { player.sendPacket(SystemMessageId.ALREADY_ENTERED_ANOTHER_INSTANCE_CANT_ENTER); return 0; } teleportPlayer(player, ENTER_TELEPORT, world.getInstanceId()); return world.getInstanceId(); } if (!checkTeleport(player)) { return 0; } instanceId = InstanceManager.getInstance().createDynamicInstance(template); world = new InstanceWorld(); world.setInstanceId(instanceId); world.setTemplateId(INSTANCE_ID); world.setStatus(0); InstanceManager.getInstance().addWorld(world); _log.info("Tower of Infinitum - Demon Prince floor started " + template + " Instance: " + instanceId + " created by player: " + player.getName()); for (L2PcInstance partyMember : player.getParty().getMembers()) { teleportPlayer(partyMember, ENTER_TELEPORT, instanceId); player.destroyItemByItemId("Quest", PASS, 1, null, true); world.addAllowed(player.getObjectId()); } return instanceId; } public void setReenterTime(InstanceWorld world) { if (world.getInstanceId() == INSTANCE_ID) { // Reenter time should be cleared every Wed and Sat at 6:30 AM, so we set next suitable Calendar reenter; Calendar now = Calendar.getInstance(); Calendar reenterPointWed = (Calendar) now.clone(); reenterPointWed.set(Calendar.AM_PM, Calendar.AM); reenterPointWed.set(Calendar.MINUTE, RESET_MIN); reenterPointWed.set(Calendar.HOUR_OF_DAY, RESET_HOUR); reenterPointWed.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY); Calendar reenterPointSat = (Calendar) reenterPointWed.clone(); reenterPointSat.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); if (now.after(reenterPointSat)) { reenterPointWed.add(Calendar.WEEK_OF_MONTH, 1); reenter = (Calendar) reenterPointWed.clone(); } else { reenter = (Calendar) reenterPointSat.clone(); } SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.INSTANT_ZONE_S1_RESTRICTED); sm.addInstanceName(world.getTemplateId()); // set instance reenter time for all allowed players for (int objectId : world.getAllowed()) { L2PcInstance player = L2World.getInstance().getPlayer(objectId); if ((player != null) && player.isOnline()) { InstanceManager.getInstance().setInstanceTime(objectId, world.getTemplateId(), reenter.getTimeInMillis()); player.sendPacket(sm); } } } }