danielbarion / JBlueHeart-Source

Lineage 2 Java 8 emulator based on JSunrise
5 stars 10 forks source link

Sistema de entrega automatizada de itens #7

Closed AnibalDuarte closed 5 years ago

AnibalDuarte commented 5 years ago

Existe essa funcionalidade no servidor?

Se não, eu desenvolvi ele pra L2jServer, ele só precisa ser adaptado. O sistema visa enviar para a bag do jogador itens de forma segura sem a interferência de um GM online. Isso serve para sistemas de doação, entrega de itens, entregas de recompensas e outros para que não seja mais necessário fazer "gambiarras" nos obj_id dos itens quando eles são entregues.

/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package custom.AutoDelivery;

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

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import java.util.logging.Logger;
import java.util.Random;

/**
 * @author PaiPlayer (Anderuimm) based on AutoReward from Debian @ L2jServer Forums
 *
 */
public class AutoDelivery
{

    protected static final Logger _log = Logger.getLogger(AutoDelivery.class.getName());

    static public void main(String[] args)
        {
                _log.info("AutoDelivering System Enabled");  
                ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable()
                {
                       @Override
                        public void run()
                        {
                                AutoDeliver();                                                              
                        }

                        private void AutoDeliver()
                        {                     
                                try (Connection con = ConnectionFactory.getInstance().getConnection()){
                                        // GETTING THE ITEMS DELAYED
                                        PreparedStatement rss = con.prepareStatement("SELECT * FROM items_delayed WHERE delivered<1");
                                        ResultSet action = rss.executeQuery();

                                        for (L2PcInstance player : L2World.getInstance().getPlayers())
                                        {                     
                                                int PlayerObjectId = player.getObjectId();
                                                while (action.next())
                                                {
                                                        if(PlayerObjectId==action.getInt("charId")){
                                                                PreparedStatement rssb = con.prepareStatement("UPDATE items_delayed SET delivered=1 WHERE id=?");
                                                                rssb.setInt(1, action.getInt("id"));                                                
                                                                rssb.executeUpdate();
                                                                _log.info("=== ITEMS DELAYED SYSTEM: Player [ "+player.getName()+" ] received "+action.getInt("itemId")+" x"+action.getInt("itemCount"));
                                                                player.addItem("IDS ", action.getInt("itemId"), action.getInt("itemCount"), player, true);
                                                        } 
                                                }

                                        }

                                } 
                                catch (SQLException e)
                                {
                                        print(e);
                                }                             
                        }
                }, 0, 120 * 1000); // SCHEDULED EACH 2 MINUTES
        }

        private static void print(Exception e)
    {
        e.printStackTrace();
    }

}

Ele basicamente abre uma tabela de items_delayed e entrega diretamente na bag do jogador um item, desde que ele esteja online. Se ele não estiver online o item é entregue assim que ele efetua login.

A rotina está rodando como um script custom à cada 2 minutos, mas o tempo pode ser alterado à gosto.

A estrutura da tabela é:

CREATE TABLE `items_delayed`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `itemId` int(11) NOT NULL,
  `itemCount` int(11) NOT NULL,
  `charId` int(11) NOT NULL,
  `delivered` int(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
);
danielbarion commented 5 years ago

Ainda não há entrega de items automatizada dessa forma no projeto;

Código bem clean e organizado, sem mencionar os comentários.

Poderia fazer um pull request por gentileza ?

Obrigado @AnibalDuarte

AnibalDuarte commented 5 years ago

Tentei aqui fazer o push mas deu acesso negado.

Sobre a adaptação do código, já fiz.

danielbarion commented 5 years ago

Tentei aqui fazer o push mas deu acesso negado.

Sobre a adaptação do código, já fiz.

Na master ou em uma branch nova ?

AnibalDuarte commented 5 years ago

Tentei aqui fazer o push mas deu acesso negado. Sobre a adaptação do código, já fiz.

Na master ou em uma branch nova ?

Criei uma nova branch e ao tentar fazer o push ele retorna o erro:

image

danielbarion commented 5 years ago

Te adicionei como colaborador no projeto, vou verificar sobre as permissões para não colaboradores, talvez eu precise configurar algo...

AnibalDuarte commented 5 years ago

Já fiz o pull request: https://github.com/danielbarion/JBlueHeart-Source/pull/8 :)

danielbarion commented 5 years ago

Beleza, vou ver o código e aceitar;

Obrigado