Jump to content
  • 0

[HELP] Buffer


Question

Posted

hi everyone i need help to call this script onLogout

 

but i dont know how to call this script on log out, can someone help me?

	public void save(L2PcInstance owner)
	{
		if (isModified)
		{
			saveToDatabase(owner);
		}
	}

 

this is my scheme buffer table

package com.l2jserver.gameserver.communitybbs.BB;

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

import javolution.util.FastList;
import javolution.util.FastMap;

import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;

import gnu.trove.map.hash.TIntIntHashMap;

/**
* @author BiggBoss Buffer database tables: - scheme_buffs: contains buffname, buffid and bufflevel to handle - scheme_buffer_profiles: contains each char id with his scheme profile - scheme_char_buffs: contains the player objectid, his profile and buff name, buff id and buff level for each profile
*/

public class SchemeBufferManager
{
/* FastMap to hold the avaliable buffs, FastList to properly iterate showing the buffs */
public static FastMap<String, Integer[]> _buffs = new FastMap<>();
public static TIntIntHashMap _skillLevels = new TIntIntHashMap();

/* Pages to show the buffs. Each page should have 15 buffs */
public static int _pages;

public SchemeBufferManager()
{
	loadData();
}

/*
 * Load all buffs into a FastMap at server start up
 */
private void loadData()
{
	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("SELECT * FROM scheme_buffs");
		ResultSet rset = statement.executeQuery();

		while (rset.next())
		{
			Integer[] buffs = new Integer[2];
			String name = rset.getString("buff_name");
			buffs[0] = rset.getInt("buff_id");
			buffs[1] = rset.getInt("buff_lvl");
			_buffs.put(name, buffs);
		}
		statement.close();
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}

	_pages = Math.round(_buffs.size() /* / (CustomConfig.IS_X50_SERVER ? 22 : 15) */);

	for (Integer[] i : _buffs.values())
	{
		_skillLevels.put(i[0], i[1]);
	}

	// All was correct, print out a message
	System.out.println("Loaded " + _buffs.size() + " buffs for the Scheme Buffer. " + _pages + " pages available.");
}

/*
 * Load all player profiles into a FastList
 */
public FastList<PlayerBuffProfile> loadPlayerProfiles(L2PcInstance player)
{
	FastList<PlayerBuffProfile> profiles = new FastList<>();

	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("SELECT profile, buffs FROM scheme_buffer_profiles WHERE charId = ?");
		statement.setInt(1, player.getObjectId());
		ResultSet rset = statement.executeQuery();

		if (rset == null)
		{
			return null;
		}

		while (rset.next())
		{
			PlayerBuffProfile profile = SchemeBufferManager.getInstance().new PlayerBuffProfile(rset.getString("profile"));
			String[] buffs = rset.getString("buffs").split(";");
			for (String buff : buffs)
			{
				profile.buffs.add(buff);
			}

			profiles.add(profile);
		}

		statement.close();
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}

	return profiles;
}

public void saveProfile(L2PcInstance player, String profile) 
{	
	try(Connection con = L2DatabaseFactory.getInstance().getConnection()) 
	{
		PreparedStatement statement = con.prepareStatement("INSERT INTO scheme_buffer_profiles VALUES ( ? , ? )");
		statement.setInt(1, player.getObjectId());
		statement.setString(2, profile);
		statement.execute();
		con.close();
		statement.close();
	}
	catch(SQLException e) 
	{
		e.printStackTrace();
	}
}	

public void deleteFromDatabase(L2PcInstance owner, String profile)
{
	try (Connection con = L2DatabaseFactory.getInstance().getConnection())
	{
		PreparedStatement statement = con.prepareStatement("DELETE FROM scheme_buffer_profiles WHERE charId = ? AND profile = ?");
		statement.setInt(1, owner.getObjectId());
		statement.setString(2, profile);
		statement.execute();
		statement.close();
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}
	finally
	{
		owner.removeProfile(profile);
	}
}

public class PlayerBuffProfile
{
	public final String profileName;
	public final FastList<String> buffs;
	private boolean isModified = false;

	public PlayerBuffProfile(String $profileName)
	{
		profileName = $profileName;
		buffs = new FastList<>();
	}

	public void addBuff(String buffName)
	{
		isModified = true;
		buffs.add(buffName);
	}

	public void removeBuff(String buffName)
	{
		isModified = true;
		buffs.remove(buffName);
	}

	public void save(L2PcInstance owner)
	{
		if (isModified)
		{
			saveToDatabase(owner);
		}
	}

	private void saveToDatabase(L2PcInstance owner)
	{
		try (Connection con = L2DatabaseFactory.getInstance().getConnection())
		{
			PreparedStatement statement = con.prepareStatement("REPLACE INTO scheme_buffer_profiles VALUES ( ? , ? , ? )");
			statement.setInt(1, owner.getObjectId());
			statement.setString(2, profileName);
			statement.setString(3, getBuffsAsString());
			statement.execute();
			statement.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			isModified = false;
		}
	}

	private String getBuffsAsString()
	{
		StringBuilder sb = new StringBuilder();

		for (String buff : buffs)
		{
			sb.append(buff);
			sb.append(";");
		}

		return sb.toString();
	}
}

public static final SchemeBufferManager getInstance()
{
	return SingletonHolder._instance;
}

private static class SingletonHolder
{
	protected static final SchemeBufferManager _instance = new SchemeBufferManager();
}
}

1 answer to this question

Recommended Posts

  • 0
Posted

SchemeBufferManager.getInstance().save(this);

on cleanup(), deleteMe() or store().

 

Edit : my bad, you use a class PlayerBuffProfile. I guess each player got his own PlayerBuffProfile, so use "_profile".save(this). On cleanup(), deleteMe() or store().

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


×
×
  • 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 Disabled AdBlock