Jump to content
  • 0

My script refuses to spawn the NPC. pls help!


Question

Posted (edited)

I have a problem. They just refuse to spawn automatically...


"INFO Spawning phantom Takeshi[268484832] through spawner. Cur/Max 0/35000"

Could someone tell me what's wrong? I think the problem is on row 540. It calls _loc but this location hasnt been set nor linked to an outside source... i think.

Here is the script:

Spoiler

package wp.gameserver.model;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

import org.apache.log4j.Logger;

import com.google.common.io.Files;

import wp.commons.util.Rnd;
import wp.gameserver.Config;
import wp.gameserver.ThreadPoolManager;
import wp.gameserver.ai.PhantomPlayerAI;
import wp.gameserver.dao.CharacterDAO;
import wp.gameserver.dao.PhantomsDAO;
import wp.gameserver.data.xml.holder.PhantomTemplateData;
import wp.gameserver.data.xml.holder.SkillAcquireHolder;
import wp.gameserver.instancemanager.CursedWeaponsManager;
import wp.gameserver.model.base.AcquireType;
import wp.gameserver.model.base.ClassId;
import wp.gameserver.model.base.Experience;
import wp.gameserver.model.base.InvisibleType;
import wp.gameserver.model.base.RestartType;
import wp.gameserver.model.entity.L2Event;
import wp.gameserver.model.items.ItemInstance;
import wp.gameserver.model.phantom.PhantomTemplate;
import wp.gameserver.tables.SkillTable;
import wp.gameserver.templates.item.CreateItem;
import wp.gameserver.templates.item.ItemTemplate;
import wp.gameserver.utils.Location;
import wp.gameserver.utils.Util;

public class PhantomPlayers
{
	private static final Logger _log = Logger.getLogger(PhantomPlayers.class.getName());
	private static List<Integer> _phantoms;
	private static List<String> _phantomNames;
	private static List<PhantomSpawner> _phantomSpawners;
	
	public static void init()
	{
		_log.info("Loading phantom players...");
		
		try
		{
			File file = Config.findNonCustomResource("config/phantom/player_names.txt");
			_phantomNames = Files.readLines(file, StandardCharsets.UTF_8);
		}
		catch (IOException e)
		{
			_log.warn("PhantomPlayers: Unable to load phantom player names.", e);
			_phantomNames = Collections.emptyList();
		}
		
		_phantoms = new ArrayList<>(PhantomsDAO.getInstance().selectAll().keySet());
		_phantomSpawners = new ArrayList<>();
		PhantomSpawner spawner = new PhantomSpawner();
		ThreadPoolManager.getInstance().execute(spawner);
		_phantomSpawners.add(spawner);
		
		_log.info("Loaded " + _phantoms.size() + " phantom players from database with a maximum of " + Config.PHANTOM_MAX_PLAYERS + " phantoms.");
		_log.info("Loaded " + _phantomNames.size() + " possible phantom names.");
		_log.info("Scheduled spawner with " + (Config.PHANTOM_SPAWN_DELAY / 1000) + " seconds delay.");
	}
	
	public static Player createNewPhantom()
	{
		if (_phantomNames == null)
		{
			return null;
		}
		
		for (int i = 0; i < 25; i++) // 25 tries to make a phantom player. Enough tries to avoid name duplicate and other stuff.
		{
			String name = Rnd.get(_phantomNames);
			Player player = createNewPhantom(name);
			if (player != null)
			{
				return player;
			}
		}
		
		return null;
	}
	
	public static Player createNewPhantom(String name)
	{
		return createNewPhantom(name, PhantomTemplateData.getInstance().getRandomTemplate());
	}
	
	public static Player createNewPhantom(String name, PhantomTemplate template)
	{
		boolean female = Rnd.nextBoolean();
		int hairStyle = Rnd.get(3);
		int hairColor = Rnd.get(3);
		int face = Rnd.get(3);
		return createNewPhantom(name, template, female, hairStyle, hairColor, face);
	}
	
	public static Player createNewPhantom(String name, PhantomTemplate template, boolean female, int hairStyle, int hairColor, int face)
	{
		try
		{
			if (!Util.isMatchingRegexp(name, Config.CNAME_TEMPLATE))
			{
				return null;
			}
			if ((CharacterDAO.getInstance().getObjectIdByName(name) > 0) || Util.contains(Config.FORBIDDEN_CHAR_NAMES, name))
			{
				return null;
			}
			
			if (template == null)
			{
				return null;
			}
			
			final ClassId classId = ClassId.VALUES[template.getClassId()];
			Player newChar = Player.create(classId.getId(), female ? 1 : 0, Config.PHANTOM_ACCOUNT, name, hairStyle, hairColor, face);
			if (newChar == null)
			{
				return null;
			}
			
			newChar.setCreateTime(System.currentTimeMillis());
			try
			{
				switch (classId.getLevel())
				{
					case 2:
						newChar.addExpAndSp(Experience.getExpForLevel(20), 835863);
						break;
					case 3:
						newChar.addExpAndSp(Experience.getExpForLevel(40), 15422930);
						break;
					case 4:
						newChar.addExpAndSp(Experience.getExpForLevel(76), 931275829);
						break;
				}
			}
			catch (ArrayIndexOutOfBoundsException | NullPointerException e)
			{
				_log.warn("PhantomPlayers: Failed to set appropreate level for classId " + classId, e);
			}
			
			Player.restoreCharSubClasses(newChar);
			
			if (Config.STARTING_ADENA > 0)
			{
				newChar.addAdena(Config.STARTING_ADENA);
			}
			
			if (Config.STARTING_LVL > newChar.getLevel())
			{
				newChar.addExpAndSp(Experience.LEVEL[Config.STARTING_LVL] - newChar.getExp(), 0, 0, 0, false, false);
			}
			
			if (Config.SPAWN_CHAR)
			{
				newChar.teleToLocation(Config.SPAWN_X, Config.SPAWN_Y, Config.SPAWN_Z);
			}
			else
			{
				newChar.setLoc(Rnd.get(newChar.getTemplate().getSpawnLocs()));
			}
			
			if (Config.CHAR_TITLE)
			{
				newChar.setTitle(Config.ADD_CHAR_TITLE);
			}
			else
			{
				newChar.setTitle("");
			}
			
			for (CreateItem i : newChar.getTemplate().getItems())
			{
				ItemInstance item = new ItemInstance(i.getItemId());
				newChar.getInventory().addItem(item);
				
				if (i.isEquipable() && item.isEquipable() && ((newChar.getActiveWeaponItem() == null) || (item.getTemplate().getType2() != ItemTemplate.TYPE2_WEAPON)))
				{
					newChar.getInventory().equipItem(item);
				}
			}
			
			if (Config.ALLOW_START_ITEMS)
			{
				if (classId.isMage())
				{
					for (int i = 0; i < Config.START_ITEMS_MAGE.length; i++)
					{
						ItemInstance item = new ItemInstance(Config.START_ITEMS_MAGE[i]);
						item.setCount(Config.START_ITEMS_MAGE_COUNT[i]);
						newChar.getInventory().addItem(item);
					}
					
					if (Config.BIND_NEWBIE_START_ITEMS_TO_CHAR)
					{
						for (int i = 0; i < Config.START_ITEMS_MAGE_BIND_TO_CHAR.length; i++)
						{
							ItemInstance item = new ItemInstance(Config.START_ITEMS_MAGE_BIND_TO_CHAR[i]);
							item.setCount(Config.START_ITEMS_MAGE_COUNT_BIND_TO_CHAR[i]);
							item.setCustomFlags(ItemInstance.FLAG_NO_CRYSTALLIZE | ItemInstance.FLAG_NO_TRADE | ItemInstance.FLAG_NO_TRANSFER | ItemInstance.FLAG_NO_DROP | ItemInstance.FLAG_NO_SELL);
							newChar.getInventory().addItem(item);
						}
					}
				}
				else
				{
					for (int i = 0; i < Config.START_ITEMS_FITHER.length; i++)
					{
						ItemInstance item = new ItemInstance(Config.START_ITEMS_FITHER[i]);
						item.setCount(Config.START_ITEMS_FITHER_COUNT[i]);
						newChar.getInventory().addItem(item);
					}
					
					if (Config.BIND_NEWBIE_START_ITEMS_TO_CHAR)
					{
						for (int i = 0; i < Config.START_ITEMS_FITHER_BIND_TO_CHAR.length; i++)
						{
							ItemInstance item = new ItemInstance(Config.START_ITEMS_FITHER_BIND_TO_CHAR[i]);
							item.setCount(Config.START_ITEMS_FITHER_COUNT_BIND_TO_CHAR[i]);
							item.setCustomFlags(ItemInstance.FLAG_NO_CRYSTALLIZE | ItemInstance.FLAG_NO_TRADE | ItemInstance.FLAG_NO_TRANSFER | ItemInstance.FLAG_NO_DROP | ItemInstance.FLAG_NO_SELL);
							newChar.getInventory().addItem(item);
						}
					}
				}
			}
			
			for (SkillLearn skill : SkillAcquireHolder.getInstance().getAvailableSkills(newChar, AcquireType.NORMAL))
			{
				newChar.addSkill(SkillTable.getInstance().getInfo(skill.getId(), skill.getLevel()), true);
			}
			
			newChar.setCurrentHpMp(newChar.getMaxHp(), newChar.getMaxMp());
			newChar.setCurrentCp(0); // retail
			newChar.setOnlineStatus(true);
			
			newChar.store(true);
			newChar.getInventory().store();
			newChar.deleteMe();
			
			PhantomsDAO.getInstance().insert(newChar.getObjectId(), template);
			
			_phantoms.add(newChar.getObjectId());
			return newChar;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static PhantomSpawner spawnPhantoms(int numSpawns, long delayInMilis, boolean generateNewPhantoms, Location loc)
	{
		PhantomSpawner spawner = new PhantomSpawner(numSpawns, delayInMilis, generateNewPhantoms).setLocation(loc);
		ThreadPoolManager.getInstance().execute(spawner);
		_phantomSpawners.add(spawner);
		return spawner;
	}
	
	/**
	 * Gets the next unspawned phantom.
	 * @return random free (unspawned) phantom object id or -1 if all are taken.
	 */
	private static int getUnspawnedPhantomObjId()
	{
		List<Integer> _unspawnedPhantoms = new ArrayList<>();
		_unspawnedPhantoms.addAll(_phantoms);
		
		for (Player player : L2ObjectsStorage.getAllPlayersForIterate())
		{
			if (_unspawnedPhantoms.contains(Integer.valueOf(player.getObjectId())))
			{
				_unspawnedPhantoms.remove(Integer.valueOf(player.getObjectId()));
			}
		}
		
		if (!_unspawnedPhantoms.isEmpty())
		{
			return Rnd.get(_unspawnedPhantoms);
		}
		
		return -1;
	}
	
	public static class PhantomSpawn implements Runnable
	{
		private final int _objId;
		private Location _loc;
		
		public PhantomSpawn()
		{
			_objId = getUnspawnedPhantomObjId();
		}
		
		public PhantomSpawn(int objId)
		{
			_objId = objId;
			_loc = null;
		}
		
		public PhantomSpawn setLocation(Location loc)
		{
			_loc = new Location(loc.getX() + Rnd.get(200), loc.getY() + Rnd.get(200), loc.getZ());
			return this;
		}
		
		@Override
		public void run()
		{
			
			Player player = World.getPlayer(_objId);
			if (player == null)
			{
				player = Player.restore(_objId);
			}
			if (player == null)
			{
				return;
			}
			
			player.setOfflineMode(false);
			player.setIsOnline(true);
			player.updateOnlineStatus();
			
			player.setOnlineStatus(true);
			player.setInvisibleType(InvisibleType.NONE);
			player.setNonAggroTime(Long.MAX_VALUE);
			player.spawnMe();
			
			player.setHero(Config.NEW_CHAR_IS_HERO);
			player.setNoble(Config.NEW_CHAR_IS_NOBLE);
			
			player.getListeners().onEnter();
			
			// Backup to set default name color every time on login.
			if ((player.getNameColor() != 0xFFFFFF) && ((player.getKarma() == 0) || (player.getRecomHave() == 0)) && !player.isGM())
			{
				player.setNameColor(0xFFFFFF);
			}
			
			if ((player.getTitleColor() != Player.DEFAULT_TITLE_COLOR) && !player.isGM())
			{
				player.setTitleColor(Player.DEFAULT_TITLE_COLOR);
			}
			
			// Restore after nocarrier title, title color.
			if (player.getVar("NoCarrierTitle") != null)
			{
				player.setTitle(player.getVar("NoCarrierTitle"));
				
				if (player.getVar("NoCarrierTitleColor") != null)
				{
					player.setTitleColor(Integer.parseInt(player.getVar("NoCarrierTitleColor")));
				}
				
				player.broadcastCharInfo();
				
				player.unsetVar("NoCarrierTitle");
				player.unsetVar("NoCarrierTitleColor");
			}
			
			if (player.isCursedWeaponEquipped())
			{
				CursedWeaponsManager.getInstance().showUsageTime(player, player.getCursedWeaponEquippedId());
			}
			
			player.setCurrentHpMp(player.getMaxHp(), player.getMaxMp());
			player.setCurrentCp(player.getMaxCp());
			
			player.setIsPhantom(true);
			
			if (player.getAI().isPhantomPlayerAI())
			{
				((PhantomPlayerAI) player.getAI()).startAITask();
			}
			
			if (L2Event.isParticipant(player))
			{
				L2Event.restorePlayerEventStatus(player);
			}
			
			player.setRunning();
			player.standUp();
			player.startTimers();
			
			player.broadcastCharInfo();
			player.setHeading(Rnd.get(65535));
			if (player.isDead())
			{
				player.teleToLocation(Location.getRestartLocation(player, RestartType.TO_VILLAGE));
				player.doRevive(100);
			}
			else
			{
				player.teleToLocation(_loc == null ? player.getLoc() : _loc);
			}
		}
	}
	
	/**
	 * Checks if the given player is in the world, then logs out when he is out of combat.
	 */
	private static class PhantomDespawn implements Runnable
	{
		private final int _objId;
		private final boolean _force;
		
		public PhantomDespawn(int objId, boolean force)
		{
			_objId = objId;
			_force = force;
		}
		
		@Override
		public void run()
		{
			Player phantom = L2ObjectsStorage.getPlayer(_objId);
			if (phantom == null)
			{
				return;
			}
			
			if (!_force)
			{
				// Continue when phantom is out of combat.
				if (phantom.isInCombat())
				{
					ThreadPoolManager.getInstance().schedule(this, 1000);
					return;
				}
				
				// When phantom is out of combat, stop moving.
				if (phantom.isMoving)
				{
					phantom.stopMove();
				}
			}
			
			phantom.getAI().stopAITask();
			phantom.kick();
		}
	}
	
	public static class PhantomSpawner implements Runnable
	{
		private final int _numSpawns;
		private final long _delayInMilis;
		private final boolean _generateNewPhantoms;
		private int _curSpawns = 0;
		private Location _loc = null;
		private ScheduledFuture<?> _task = null;
		
		public PhantomSpawner()
		{
			_numSpawns = Config.PHANTOM_SPAWN_MAX;
			_delayInMilis = Config.PHANTOM_SPAWN_DELAY;
			_generateNewPhantoms = true;
		}
		
		public PhantomSpawner(int numSpawns)
		{
			_numSpawns = numSpawns;
			_delayInMilis = Config.PHANTOM_SPAWN_DELAY;
			_generateNewPhantoms = true;
		}
		
		public PhantomSpawner(int numSpawns, long delayInMilis)
		{
			_numSpawns = numSpawns;
			_delayInMilis = delayInMilis;
			_generateNewPhantoms = true;
		}
		
		public PhantomSpawner(int numSpawns, long delayInMilis, boolean generateNewPhantoms)
		{
			_numSpawns = numSpawns;
			_delayInMilis = delayInMilis;
			_generateNewPhantoms = generateNewPhantoms;
		}
		
		public PhantomSpawner setLocation(Location loc)
		{
			_loc = loc;
			return this;
		}
		
		@Override
		public void run()
		{
			if (_numSpawns == 0)
			{
				return;
			}
			
			_task = ThreadPoolManager.getInstance().scheduleAtFixedRate(() ->
			{
				if (_curSpawns < _numSpawns)
				{
					// Do not spawn more than max phantoms.
					if (L2ObjectsStorage.getAllPlayersStream().filter(Player::isPhantom).count() >= Config.PHANTOM_MAX_PLAYERS)
					{
						return;
					}
					
					int objId = getUnspawnedPhantomObjId();
					if (objId > 0)
					{
						if (_generateNewPhantoms)
						{
							try
							{
								Player phantom = createNewPhantom();
								if (phantom != null)
								{
									objId = phantom.getObjectId();
									_log.info("Spawning phantom " + phantom + " through spawner. Cur/Max " + _curSpawns + "/" + _numSpawns);
								}
							}
							catch (Exception e)
							{
								_log.error("ERROR: Spawning phantom  through spawner. Cur/Max " + _curSpawns + "/" + _numSpawns, e);
							}
						}
						else
						{
							return;
						}
					}
					
					ThreadPoolManager.getInstance().execute(new PhantomSpawn(objId).setLocation(_loc));
					_curSpawns++;
				}
			}, 0, _delayInMilis);
		}
		
		public void cancel()
		{
			if (_task != null)
			{
				_task.cancel(true);
				System.out.println("Canceling phantom scheduler");
			}
		}
	}
	
	public static void stopSpawners()
	{
		if (_phantomSpawners != null)
		{
			for (PhantomSpawner thread : _phantomSpawners)
			{
				if (thread != null)
				{
					thread.cancel();
				}
			}
		}
	}
	
	public static void terminatePhantoms(boolean force)
	{
		stopSpawners();
		
		for (int objId : _phantoms)
		{
			new PhantomDespawn(objId, force).run();
		}
	}
	
	public static void terminatePhantom(int objId, boolean disableFromReenter)
	{
		if (disableFromReenter && (_phantoms != null))
		{
			_phantoms.remove(Integer.valueOf(objId));
		}
		
		new PhantomDespawn(objId, true).run();
	}
}

 

 



The following is a row I've taken from the admin commands java and it spawns the Phantoms.
 

ThreadPoolManager.getInstance().execute(new PhantomPlayers.PhantomSpawn(phantom.getObjectId()).setLocation(activeChar.getLoc()));
 

Edited by bru7al

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Posts

    • L2Lusty 50x Essence High Version Open day 27/06  12:00 GMT London WebSite: https://l2lusty.com/ General Information If you are looking for a High Five retail server, this project is not for you, our gameplay and farming system is based on the Essence version. If you are looking for something new and different to play, this server is for you. Our server is using its own version that mixes High Five and the latest Essence version. With this, we have a good part of the items from the Lineage2 Essence version, with classes and bosses from High Five. An auto-balance system in onlympiad games, which will bring greater equality in combats. We added the Chaos Zone and Peace Zone events, for a better LCoin farm, these events are 24h and can be accessed with the commands .chaos or .peace. All essence items are purchased in our alt+b and cost an average of 300k LCoins. LCoins are obtained by killing any mob, with better drops in the Chaos zone and the Peace Zone. Server Version High Five + Essence Game Play Experience 50x Spoil 8x Drop 8x Adena 8x Normal Enchant 50% (+3 to +12) (40% 12 to 20) Blessed Enchant 50% (+3 to +12) (40% 12 to 20) Safe Enchant +3 Max Enchant +20 Max Windows / IP 6 Accounts Anti-Bot system ON Champions System ON TerritoryWar Saturday 20:00 Siege Every Sunday Olympiads 18:00 / 23:50 Oly End Days 1, 11 and 22 Max Register 1 For IP Minimum Players for Start 4 Players Subclass FREE MAX LVL 85 SHOP GRADE-S Party Diff 30 Level 55/85   Essence Items Price   Items Lvl 1 300k / 1.200kk LCoins Upgrade to Lvl 2 2 items Lvl 1 + 2b Adenas Upgrade to Lvl 3 2 items Lvl 2 + 2b Adenas Upgrade to Lvl 4 2 items Lvl 3 + 2b Adenas Upgrade Chance 25%   Exchange Items   As in other MMORPGs, we have a system of exchanging items for better items.       GrandBoss All Grand Boss Time Fixed Raids Status 50% Change Drop Queen Ant / Core / Orfen / Baium Queen ant Level: 80 Every day 18:30 / Drop Jewel 40% Core Level: 80 Every day 18:40 / Drop Jewel 90% Orfem Level: 80 Every day 18:50 / Drop Jewel 90% Baium Level: 80 Every Friday 18:00 / Drop Jewel 100% Beleth Every Friday 22:00 / Drop Jewel 100% Valakas Every Saturday 18:00 / Drop Jewel 100% Antharas Every Sunday 17:00 / Drop Jewel 100% Max Character in Zone Boss 1 For IP   Instances Party All Intances 5 Players Raids Status 50% Zaken Day 61 5 Players / Jewel Chance 10% Zaken Day Hard 83 5 Players / Jewel Chance Normal / 10% / Blessed 1.9% Zaken Nightmare 61 5 Players / Jewel Chance 90% Frintezza 5 Players / Jewel Chance 40% Freya Normal 5 Players / Jewel Chance 40% Freya Hard 5 Players / Jewel Chance 40% Tiat 5 Players / Weapon Chance 10%   Instances Solo   All Intances Drop S84 Up Crystal Level 10 / 17 Baylor Solo Drop Moirai Set / Vesper Weapons Darion Solo Weapon Chance 1% Tiat Solo Weapon Chance 1% Frintezza Solo Jewel Chance 2% Freya Solo Jewel Chance 2% Zaken Solo Jewel Chance 2% Core Solo Jewel Chance 2% Orfen Solo Jewel Chance 2% Beleth Solo Jewel Chance 1%   Special Events Event Boss Lindvior Every day 16:40 - 22:40 Event Pig Invazion Every day 15:40 - 23:40 Event Dragon Invazion Every day 18:40 - 01:40 Event City War Every day 19:40 - 02:40   Commands .ach .achievements Opens the achievements interface .buffshield (anti-buff) .away .back (stay away / back) .menu .cfg (Character control panel) .combine .talisman (Combine all Talismans) .dressme (Visual Armor Added +1000 HP) .party .invite .partylist Our custom party creation system .offline (off line shop) .repair (repair character) .siege (See the siege time or register your clan) .stats (character status)   .offbuff (Open store buffs) .autofarm (Open Auto Farm Interface) .report (Report a suspicious bot player) .buffshop (Invoke a summon to sell your buffs.) .vote (Opens our vote system.) .aa (Exchange your seal stones for AA automatically.) .oly (Shows all players who are first in the oly ranking.) .status (See a player's status, he has to be in your target) .seeres (Look at a player's resistance, he has to be in his target) .equip (See all of a player's items, they have to be in their target) .regoly (Register with oly wherever you are.) .bagclean Delete all items from your inventory, use it knowing that you will lose everything that is not equipped!   WebSite: https://l2lusty.com/
    • Our sales are ongoing. Bump. 22 June 2025 Telegram: ContactDiscordAccS
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock