L2JMobius

Classic Interlude Items dalayed Donate items will be given out when the character is in the game

NeoGaming · 1 · 2214

Offline NeoGaming

  • Vassal
  • *
    • Posts: 5
Did it for my server, maybe someone will be useful
Add the class /java/org/l2jmobius/gameserver/taskmanager/DelayedItemsManager.java
Code: [Select]
package org.l2jmobius.gameserver.taskmanager;

import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.model.World;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Logger;

public class DelayedItemsManager implements Runnable
{
    private static final Logger _log = Logger.getLogger( DelayedItemsManager.class.getName() );
    private static DelayedItemsManager _instance;

    private static final String SELECT = "SELECT * FROM items_delayed WHERE payment_status = 0";
    private static final String UPDATE = "UPDATE items_delayed SET payment_status = 1 WHERE payment_id = ?";

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

    private DelayedItemsManager()
    {
        ThreadPool.scheduleAtFixedRate( this, 60000L, 60000L );
        _log.info( "DelayedItemsManager initialization" );
    }

    @Override
    public void run()
    {
        try ( Connection connection = DatabaseFactory.getConnection() )
        {
            try ( PreparedStatement statementSelect = connection.prepareStatement( SELECT );
                 ResultSet resultSet = statementSelect.executeQuery() )
            {
                while ( resultSet.next() )
                {
                    PlayerInstance player = World.getInstance().getPlayer( resultSet.getInt( "owner_id" ) );
                    if ( player == null )
                        continue;

                    if ( player.addItem( "", resultSet.getInt( "item_id" ),
                            resultSet.getInt( "count" ), null, true ) != null )
                    {
                        try ( PreparedStatement statementUpdate = connection.prepareStatement( UPDATE ) )
                        {
                            statementUpdate.setInt( 1, resultSet.getInt( "payment_id" ) );
                            statementUpdate.execute();
                        }
                    }
                }
            }
        }
        catch ( Exception e )
        {
            _log.warning( "DelayedItemsManager: " + e );
        }
    }
}
well, do not forget to add the initialization of the manager when starting the server,
for example in /java/org/l2jmobius/gameserver/GameServer.java
Code: [Select]
public GameServer() throws Exception
{
   ...
   DelayedItemsManager.getInstance();
   ...
}
a request to add information about the issue of the subject sql requests are made for our donation script
Code: [Select]
$query = "
    INSERT INTO `items_delayed` (
        `owner_id`,
        `item_id`,
        `count`,
        `payment_status`,
        `description`
    ) VALUES (
        '" . $ownerId . "',
        '" . $itemTypeId . "',
        '" . $itemsAmount . "',
        '0',
        'payment system name'
    )
";
items_delayed table, create in the database
Code: [Select]
CREATE TABLE `items_delayed` (
  `payment_id` int(11) NOT NULL AUTO_INCREMENT,
  `owner_id` int(11) NOT NULL,
  `item_id` smallint(5) unsigned NOT NULL,
  `count` int(10) unsigned NOT NULL DEFAULT '1',
  `payment_status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`payment_id`),
  KEY `key_owner_id` (`owner_id`),
  KEY `key_item_id` (`item_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;