Jump to content
  • 0

Java script


janiko
 Share

Question

Hope someone can help

I wrote script to restore buffs here is the function.

the problem is that it restores only first buff from sql record

Example of Sql Record

-- ----------------------------
-- Table structure for `community_skillsave`
-- ----------------------------
DROP TABLE IF EXISTS `community_skillsave`;
CREATE TABLE `community_skillsave` (
  `charId` int(10) DEFAULT NULL,
  `skills` text,
  `pet` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of community_skillsave
-- ----------------------------
INSERT INTO `community_skillsave` VALUES ('268480452', '1240;1048;', null);

 

Restore Java Script

private void RESTOR(L2PcInstance activeChar)
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		PreparedStatement stat = con.prepareStatement("SELECT * FROM community_skillsave WHERE charId=?;");
		stat.setInt(1, activeChar.getObjectId());
		ResultSet rset = stat.executeQuery();
		rset.next();

		String allskills = rset.getString(2);
		StringTokenizer stBuff = new StringTokenizer(allskills, ";");
		while (stBuff.hasMoreTokens())
		{
			int skilltoresatore = Integer.parseInt(stBuff.nextToken());
			int skilllevel = SkillTable.getInstance().getMaxLevel(skilltoresatore);
			L2Skill skill = SkillTable.getInstance().getInfo(skilltoresatore, skilllevel);

			PreparedStatement st = con.prepareStatement("SELECT COUNT(*) FROM community_skillsave WHERE skills=?;");
			st.setInt(1, skilltoresatore);
			ResultSet rs = st.executeQuery();
			rs.next();

			if (rs.getInt(1) != 0)
			{
				skill.getEffects(activeChar, activeChar);
			}
			else
			{
				activeChar.sendMessage("Buff: " + skill.getName() + " (" + skill.getId() + "), can't be restored!");
			}

			rs.close();
			st.close();
		}
		rset.close();
		stat.close();
	}
	catch (SQLException e)
	{
	}
	finally
	{
		L2DatabaseFactory.close(con);
	}
}

 

thanks to everyone

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I don't understand at all the use of your StringTokenizer stBuff.

 

If you want to retrieve from a DB, then use :

while (rset.next())
{

For a simple DB load you don't need to use StringTokenizer AT ALL. Eventually to split a String from a config file, but that's clearly not what you try to do.

 


 

PS : I understood lol. What you try to do is ugly. Make one line per skillId. It will be faster than to try to retrieve a string, split it and then doing the same action.

 

INSERT INTO `community_skillsave` VALUES ('268480452', '1240', null);
INSERT INTO `community_skillsave` VALUES ('268480452', '1048', null);

Link to comment
Share on other sites

  • 0

I reworked it, but main goal that i want to achieve is to get skills from sql with 1024;1035; etc

Because i save buff with bypass :  bypass -h _bbs_buff_Player;264;266;267;268;270;304;529;364;

 

thats why i want to restore with the same method, reworked Restore function restores only first one again :(

 

private void SAVE(L2PcInstance activeChar)
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		PreparedStatement stat = con.prepareStatement("SELECT *FROM community_skillsave WHERE charId=?;");
		stat.setInt(1, activeChar.getObjectId());
		ResultSet rset = stat.executeQuery();
		rset.next();

		String allbuff = "";
		int charId = rset.getInt("charId");
		L2Effect[] skill = activeChar.getAllEffects();//TODO Check Buff Skills to not bug with other effects

		for (int j = 0; j < skill.length; ++j)
		{
			allbuff = allbuff + new StringBuilder().append(skill[j].getSkill().getId()).append(";").toString();
		}

		if (charId == 0)
		{
			PreparedStatement statement1 = con.prepareStatement("INSERT INTO community_skillsave (charId,skills) values (?,?)");
			statement1.setInt(1, activeChar.getObjectId());
			statement1.setString(2, allbuff);
				activeChar.sendMessage("Buff Saved Successfully!");
			statement1.execute();
			statement1.close();
		}
		else
		{
			PreparedStatement statement = con.prepareStatement("UPDATE community_skillsave SET skills=? WHERE charId=?;");
			statement.setString(1, allbuff);
			statement.setInt(2, activeChar.getObjectId());
				activeChar.sendMessage("Buff Updated Successfully!");				
			statement.execute();
			statement.close();
		}
		rset.close();
		stat.close();
	}
	catch (SQLException e)
	{
	}
	finally
	{
		L2DatabaseFactory.close(con);
	}
}

private void RESTOR(L2PcInstance activeChar)
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		PreparedStatement stat = con.prepareStatement("SELECT * FROM community_skillsave WHERE charId=?;");
		stat.setInt(1, activeChar.getObjectId());
		ResultSet rset = stat.executeQuery();

	    while (rset.next())
	    {			
	    	String allskills = rset.getString("skills");
	    	int charId = rset.getInt("charId");
	    	StringTokenizer BuffId = new StringTokenizer(allskills, ";");
			int skillid = Integer.parseInt(BuffId.nextToken());
			int skilllevel = SkillTable.getInstance().getMaxLevel(skillid);
			L2Skill skill = SkillTable.getInstance().getInfo(skillid, skilllevel);		    	

			if (charId != 0)
			{
				skill.getEffects(activeChar, activeChar);
			}
			else
			{
				activeChar.sendMessage("Buff: " + skill.getName() + " (" + skill.getId() + "), can't be restored!");
			}
	    }
		rset.close();
		stat.close();
	}
	catch (SQLException e)
	{
	}
	finally
	{
		L2DatabaseFactory.close(con);
	}
}

Link to comment
Share on other sites

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



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