Jump to content
  • 0

LF l2s code adapt for l2j


Majkl
 Share

Question

Hello, lf developer for adapting this code for l2jserver Hi5.

Payment via paypal. Thank you!

package l2s.gameserver.taskmanager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import l2s.commons.dbutils.DbUtils;
import l2s.commons.threading.RunnableImpl;
import l2s.gameserver.ThreadPoolManager;
import l2s.gameserver.data.xml.holder.ItemHolder;
import l2s.gameserver.database.DatabaseFactory;
import l2s.gameserver.model.GameObjectsStorage;
import l2s.gameserver.model.Player;
import l2s.gameserver.model.items.ItemInstance;
import l2s.gameserver.model.items.ItemInstance.ItemLocation;
import l2s.gameserver.model.items.PcInventory;
import l2s.gameserver.network.l2.s2c.SystemMessagePacket;
import l2s.gameserver.templates.item.ItemTemplate;
import l2s.gameserver.utils.ItemFunctions;
import l2s.gameserver.utils.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DelayedItemsManager extends RunnableImpl
{
	private static final Logger _log = LoggerFactory.getLogger(DelayedItemsManager.class);
	private static DelayedItemsManager _instance;

	private static final Object _lock = new Object();
	private int last_payment_id = 0;

	public static DelayedItemsManager getInstance()
	{
		if(_instance == null)
			_instance = new DelayedItemsManager();
		return _instance;
	}

	public DelayedItemsManager()
	{
		Connection con = null;
		try
		{
			con = DatabaseFactory.getInstance().getConnection();
			last_payment_id = get_last_payment_id(con);
		}
		catch(Exception e)
		{
			_log.error("", e);
		}
		finally
		{
			DbUtils.closeQuietly(con);
		}

		ThreadPoolManager.getInstance().schedule(this, 10000L);
	}

	private int get_last_payment_id(Connection con)
	{
		PreparedStatement st = null;
		ResultSet rset = null;
		int result = last_payment_id;
		try
		{
			st = con.prepareStatement("SELECT MAX(payment_id) AS last FROM items_delayed");
			rset = st.executeQuery();
			if(rset.next())
				result = rset.getInt("last");
		}
		catch(Exception e)
		{
			_log.error("", e);
		}
		finally
		{
			DbUtils.closeQuietly(st, rset);
		}
		return result;
	}

	@Override
	public void runImpl() throws Exception
	{
		Player player = null;

		Connection con = null;
		PreparedStatement st = null;
		ResultSet rset = null;
		try
		{
			con = DatabaseFactory.getInstance().getConnection();
			int last_payment_id_temp = get_last_payment_id(con);
			if(last_payment_id_temp != last_payment_id)
				synchronized (_lock)
				{
					st = con.prepareStatement("SELECT DISTINCT owner_id FROM items_delayed WHERE payment_status=0 AND payment_id > ?");
					st.setInt(1, last_payment_id);
					rset = st.executeQuery();
					while(rset.next())
						if((player = GameObjectsStorage.getPlayer(rset.getInt("owner_id"))) != null)
							loadDelayed(player, true);
					last_payment_id = last_payment_id_temp;
				}
		}
		catch(Exception e)
		{
			_log.error("", e);
		}
		finally
		{
			DbUtils.closeQuietly(con, st, rset);
		}

		ThreadPoolManager.getInstance().schedule(this, 10000L);
	}

	public static void addDelayed(int objectId, int itemId, long itemCount, int enchant, String desc)
	{
		Connection con = null;
		PreparedStatement statement = null;
		try
		{
			con = DatabaseFactory.getInstance().getConnection();
			statement = con.prepareStatement("INSERT INTO items_delayed (owner_id, item_id, count, enchant_level, description) VALUES (?, ?, ?, ?, ?)");
			statement.setInt(1, objectId);
			statement.setInt(2, itemId);
			statement.setLong(3, itemCount);
			statement.setInt(4, enchant);
			statement.setString(5, desc);
			statement.execute();
		}
		catch(Exception e)
		{
			_log.info("DelayedItemsManager.addDelayed(int, int, long): " + e, e);
		}
		finally
		{
			DbUtils.closeQuietly(con, statement);
		}
	}

	public int loadDelayed(Player player, boolean notify)
	{
		if(player == null)
			return 0;
		final int player_id = player.getObjectId();
		final PcInventory inv = player.getInventory();
		if(inv == null)
			return 0;

		int restored_counter = 0;

		Connection con = null;
		PreparedStatement st = null, st_delete = null;
		ResultSet rset = null;
		synchronized (_lock)
		{
			try
			{
				con = DatabaseFactory.getInstance().getConnection();
				st = con.prepareStatement("SELECT * FROM items_delayed WHERE owner_id=? AND payment_status=0");
				st.setInt(1, player_id);
				rset = st.executeQuery();

				ItemInstance item, newItem;
				st_delete = con.prepareStatement("UPDATE items_delayed SET payment_status=1 WHERE payment_id=?");

				while(rset.next())
				{
					final int ITEM_ID = rset.getInt("item_id");
					final int PAYMENT_ID = rset.getInt("payment_id");

					final ItemTemplate ITEM_TEMPLATE = ItemHolder.getInstance().getTemplate(ITEM_ID);
					if(ITEM_TEMPLATE != null)
					{
						final long ITEM_COUNT = rset.getLong("count");
						final int ITEM_ENCHANT = rset.getInt("enchant_level");
						final int FLAGS = rset.getInt("flags");
						final int ATTRIBUTE = rset.getInt("attribute");
						final int ATTRIBUTE_LEVEL = rset.getInt("attribute_level");
						final String DESCRIPTION = rset.getString("description");
						boolean stackable = ITEM_TEMPLATE.isStackable();
						boolean success = false;

						for(int i = 0; i < (stackable ? 1 : ITEM_COUNT); i++)
						{
							if(ITEM_COUNT > 0)
							{
								item = ItemFunctions.createItem(ITEM_ID);
								if(item.isStackable())
									item.setCount(ITEM_COUNT);
								else
									item.setEnchantLevel(ITEM_ENCHANT);
								//FIXME [G1ta0] item-API
								//item.setAttributeElement(ATTRIBUTE, ATTRIBUTE_LEVEL, true);
								item.setLocation(ItemLocation.INVENTORY);
								item.setCustomFlags(FLAGS);

								newItem = inv.addItem(item, "Delayed Item");
								if(newItem == null)
								{
									_log.warn("Unable to delayed create item " + ITEM_ID + " request " + PAYMENT_ID);
									continue;
								}

								if(notify)
									player.sendPacket(SystemMessagePacket.obtainItems(ITEM_ID, stackable ? ITEM_COUNT : 1, ITEM_ENCHANT));

							}

							success = true;
							restored_counter++;
						}

						if(!success)
							continue;
					}
					st_delete.setInt(1, PAYMENT_ID);
					st_delete.execute();
				}
			}
			catch(Exception e)
			{
				_log.error("Could not load delayed items for player " + player + "!", e);
			}
			finally
			{
				DbUtils.closeQuietly(st_delete);
				DbUtils.closeQuietly(con, st, rset);
			}
		}
		return restored_counter;
	}
}

 

Link to comment
Share on other sites

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.

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.

 Share



  • Posts

    • Good luck sky been always decent server 🙂
    • Dear friends, We promised you an Interlude server towards the end of the year, now we are here to deliver.  The entire L2mid team is proud to present you, a returning legend! L2Mid Interlude Dynamic Rates X25 A traditional Interlude server most fitting for the upcoming Christmas Season!   Grand Opening: 26th December 2021  Website: https://l2mid.com   Let’s send off this year in a fitting way, by playing on the top Interlude mid-rate server!  You can seat tight and expect in the near future, when we will announce more information about other upcoming projects with whom we will be collaborating.       Merry Christmas, and may all your Christmases be white!     May the true spirit of Christmas shine in your heart and light your path.         Dynamic Rates x25 is a classic game with simplified development and with maximum approximation to massive PVP battles. Сlassic, but simplistic gameplay maintains a constant competition in all its manifestations. Here you will not achieve everything in just one day and you will not quietly develop because your competitors will disturb you all time. Dynamic Rates Experience and Skill Points from Monsters: 1-61 Level - x25   Dynamic Rates Experience and Skill Points from Monsters: 61-76 LEVEL - X15   Dynamic Rates Experience and Skill Points from Monsters: 76-80 LEVEL - X5 ADENA x15   SEAL STONES x10   DROP x15   SPOIL x15   MANOR x1   Raid Boss Experience, Skill Points and Drop x7   Epic Boss Experience, Skill Points and Drop x1   Quest Item Drop x3   Quest Item Reward x2   Daily Quest Every 24 hours
    • Moved to request help board.
    • Hello, @DumanisT regarding the rules your topic must include prices.
  • 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 Disbaled AdBlock