Jump to content
  • 0

Timed Items aCis


Question

Posted

Hello, I'm trying to put some time in some items, however the items added in the config are not saving in my database, would anyone help me solve this?

 

code complete : https://pastebin.com/raw/y3B7Cvfk

 

Class responsible for saving and removing from database

 

package net.sf.l2j.gameserver.data.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.model.World;
import net.sf.l2j.gameserver.model.WorldObject;
import net.sf.l2j.gameserver.model.actor.instance.Player;
import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
import net.sf.l2j.gameserver.network.SystemMessageId;
import net.sf.l2j.gameserver.network.serverpackets.ItemList;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
import net.sf.l2j.gameserver.taskmanager.ItemsTaskManager;

public class TimedItemTable
	public final Map<Integer, Info> _timedItems = new ConcurrentHashMap<>();
{
	private static Logger _log = Logger.getLogger(TimedItemTable.class.getName());
	
	public class Info
	{
		int _charId;
		int _itemId;
		long _activationTime;
	}
	
	public static final TimedItemTable getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final TimedItemTable _instance = new TimedItemTable();
	}
	
	public TimedItemTable()
	{
		restore();
		_startControlTask.schedule(60000);
	}
	
	public boolean getActiveTimed(Player player, boolean trade)
	{
		for (Info i : _timedItems.values())
		{
			if ((i != null) && (i._charId == player.getObjectId()))
			{
				ItemInstance item = player.getInventory().getItemByObjectId(i._itemId);
				if (item != null)
				{
					if (System.currentTimeMillis() < i._activationTime)
						return true;
				}
			}
		}
		return false;
	}
	
	public synchronized void destroy(ItemInstance item)
	{
		Info inf = _timedItems.get(item.getObjectId());
		if (inf != null)
		{
			_timedItems.remove(inf._itemId);
			try (Connection con = L2DatabaseFactory.getInstance().getConnection())
			{
				PreparedStatement statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId = ? AND itemId = ?");
				statement.setInt(1, inf._charId);
				statement.setInt(2, inf._itemId);
				statement.execute();
				statement.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void setTimed(ItemInstance item)
	{
		Info inf = _timedItems.get(item.getObjectId());
		if (inf != null)
			inf._charId = item.getOwnerId();
		else
		{
			inf = new Info();
			inf._activationTime = (System.currentTimeMillis() / 1000) + (Config.TIMED_ITEM_TIME * 60);
			inf._charId = item.getOwnerId();
			inf._itemId = item.getObjectId();
			_timedItems.put(inf._itemId, inf);
		}
		saveToDb(inf);
	}
	
	public boolean isActive(ItemInstance item)
	{
		for (Info i : _timedItems.values())
		{
			if (i._itemId == item.getObjectId())
				return true;
		}
		return false;
	}
	
	private void restore()
	{
		try (Connection con = L2DatabaseFactory.getInstance().getConnection()) 
		{
			PreparedStatement statement = con.prepareStatement("SELECT charId, itemId, time FROM character_timed_items");
			ResultSet rs = statement.executeQuery();
			
			while (rs.next())
			{
				Info inf = new Info();
				inf._activationTime = rs.getLong("time");
				inf._charId = rs.getInt("charId");
				inf._itemId = rs.getInt("itemId");
				_timedItems.put(inf._itemId, inf);
			}
			rs.close();
			statement.close();
			
			_log.info("loaded " + _timedItems.size() + " Timed Items ");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("resource")
	private static void saveToDb(Info temp)
	{
		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
		{
			PreparedStatement statement = con.prepareStatement("UPDATE character_timed_items set charId = ? where itemId = ?");
			statement.setInt(1, temp._charId);
			statement.setInt(2, temp._itemId);
			
			if (statement.executeUpdate() == 0)
			{
				statement = con.prepareStatement("INSERT INTO character_timed_items (charId, itemId, time) VALUES (?, ?, ?)");
				statement.setInt(1, temp._charId);
				statement.setInt(2, temp._itemId);
				statement.setLong(3, temp._activationTime);
				statement.execute();
				statement.close();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("resource")
	public void delete(Info temp)
	{
		_timedItems.remove(temp._itemId);
		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
		{
			PreparedStatement statement = con.prepareStatement("DELETE FROM character_timed_items WHERE charId =? AND itemId =?");
			statement.setInt(1, temp._charId);
			statement.setInt(2, temp._itemId);
			statement.execute();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		Player player = World.getInstance().getPlayer(temp._charId);
		if (player != null)
		{
			ItemInstance item = player.getInventory().getItemByObjectId(temp._itemId);
			
			if (item.isEquipped())
				player.getInventory().unEquipItemInSlot(item.getLocationSlot());
			
			player.getInventory().destroyItem("timeLost", item, player, player);
			player.sendPacket(new ItemList(player, false));
			
			player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED).addItemName(item.getItemId()));          
		}
		else
		{
			try (Connection con = L2DatabaseFactory.getInstance().getConnection();)
			{
				if (temp._charId != 0)
				{
					try (PreparedStatement statement = con.prepareStatement("DELETE FROM items WHERE owner_id = ? AND object_id = ?");)
					{
						statement.setInt(1, temp._charId);
						statement.setInt(2, temp._itemId);
						statement.execute();
						statement.close();
					}
				}
				else
				{
					for (WorldObject o : World.getInstance().getObjects())
					{
						if (o.getObjectId() == temp._itemId)
						{
							World.getInstance().removeObject(o);
							break;
						}
					}
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private final ItemsTaskManager _startControlTask = new ItemsTaskManager()
	{
		@Override
		protected void onElapsed()
		{
			for (Info temp : _timedItems.values())
			{
				if (temp._activationTime < (System.currentTimeMillis() / 1000))                  
					delete(temp);
			}
			schedule(60000);
		}
	};      
}

 

 

 

 

3 answers to this question

Recommended Posts

  • 0
Posted

In Acis just put duration in item.

If I'm not mistaken in the new versions already have that same code.

	<!-- Timed Jewels -->
	<item id="9610" type="Armor" name="Timed Earring of Antharas - 5 Hours">
		<set name="default_action" val="equip" />
		<set name="bodypart" val="rear;lear" />
		<set name="material" val="GOLD" />
		<set name="weight" val="150" />
		<set name="price" val="3700000" />
		<set name="is_oly_restricted" val="true" />	
		<set name="is_tradable" val="false" />
		<set name="is_dropable" val="false" />
		<set name="is_sellable" val="false" />
		<set name="is_depositable" val="false" />
	    <set name="is_oly_restricted" val="true" />
		<set name="item_skill" val="3558-1" />
		<set name="duration" val="300" />  <!-- DURATION -->
		<for>			
			<add order="0x10" stat="mDef" val="71" />
			<add order="0x40" stat="maxMp" val="31" />
			<enchant order="0x0C" stat="mDef" val="0" />
		</for>
	</item>

 

  • 0
Posted
12 hours ago, Tryskell said:

Use Shadow Weapon concept (cf previous post), everything is running ok and all checks already exist. No reason to reinvent the wheel.

 

the count is in hours?

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.

×
×
  • Create New...