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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • Lineage2 Freya High Five @ Reshade with fog and rain etc @ Gracia final epilogue atmosphere   this reshade will eat lots of GPU power 50% or more of an RTX 3060 so be carefull depending on what effects are activated and their settings will eat even more GPU recomended 60hz monitor settings and via nvidia panel in Lineage2 game profile vsync settings to on effects are set up till film deck and the rest are not used but still working again this can eat alot of GPU Don't overheat GPU this is for freya high five but might work on others too copy in the  System  folder the folder  reshade-shaders  and the files  d3d9.dll  ReShade.ini  ReShadePreset.ini  ReShade.log  CccDddCcc.ini insert opens the menu and delete is on and of some settings need  ctrl + left click  to be changed   making another profile will reset the not activated effects to their default values so just copy the profile  CccDddCcc  and rename if needed also something needs to be closed from settings in game menu, the blur at distance and advanced shaders but keeping the advanced water effects all reflections   for those that don't like the h5 look of the sky and the red fog and rain and ambien red at night on all maps well if we want the cool gracia final epilogue back then we need to do this rename the  Maps  folder to Mapsretail or whatever copy the  Maps  folder from gracia final epilogue to h5 also we need the  L2_Skies.utx  from gracia final epilogue  Textures  folder to be replaced and also we need to do the same to the files  timeenv0.int  timeenv1.int  timeenv2.int  timeenv3.int  found in  system  folder   another setting that will probably be needed but not really tested out is to open file  option.ini  from  system  folder and add cachesize like this   [FirstRun] FirstRun=2   [Engine.GameEngine] CacheSizeMegs=512   also maybe is good to change those to 4.000000   [ClippingRange] Terrain=4.000000 Actor=4.000000 StaticMesh=4.000000 StaticMeshLod=4.000000 Pawn=4.000000       sorry bad english   https://mega.nz/file/aRNXxDrQ#mbxrNERBtW0XEEezK6w8-86oZWuX1k6NgtR6RZWKRVM   the compression on the video is kinda bad but meh    
    • Thanks, tho if possible let's lock the topic. I decided to finish up myself as I only created this to save up my time, but seems that while waiting for prop dev. finished myself.
    • Plus he have Discord where using Celestine, Nightwolf and others names to ensure that he is legit, what a looser, that's why mxc have bad reputation, coming here is like 50/50 get scammed.
    • Greetings, MaxCheaters community! We are the development team behind projects like AdenLand, Classic GvE, TinyEssence, PvPEssence, and several others. Over the years of managing high-traffic servers, we’ve built our own ecosystem of tools for datapack and client editing, simply because standard software often failed to meet our production needs or was too slow. Today, we are opening access to these professional solutions for our colleagues in the dev scene. You can explore our suite of editors and utilities here: 👉 https://la2.tools/ Why choose our tools? Battle-Tested: This software is used daily to maintain and update our own live projects. Efficiency: Designed to automate routine tasks that usually take hours or even days. Stability: Built to handle complex client/server structures where generic tools often crash. We are ready to provide consultations on integrating these tools into your development workflow to make your process faster and more secure.  
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..