Jump to content
  • 0

Help With Community Board Services


Question

Posted (edited)

Hello I have I section on my community board that have Services, like Change Gender, Pk Cleaner, Noblesse, etc. I want to add a option to change the name. I add the code but when I enter the name on the box, instead of change the name to what I enter in the box I get the bypass as my name like:

;Name;$newName

How can I fix that?

 

Here is my code.

package com.l2jserver.gameserver.communitybbs.Managers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.l2jserver.Config;
import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.communitybbs.Manager.BaseBBSManager;
import com.l2jserver.gameserver.communitybbs.Manager.RegionBBSManager;
import com.l2jserver.gameserver.datatables.CharNameTable;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowAll;
import com.l2jserver.gameserver.network.serverpackets.PartySmallWindowDeleteAll;
import com.l2jserver.gameserver.network.serverpackets.PledgeShowMemberListAll;
import com.l2jserver.gameserver.network.serverpackets.PledgeShowMemberListUpdate;
import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
import com.l2jserver.gameserver.network.serverpackets.UserInfo;
import com.l2jserver.gameserver.util.Util;

public class ServiceBBSManager extends BaseBBSManager
{
	public static final Logger _log = Logger.getLogger(ServiceBBSManager.class.getName());
	
	public ServiceBBSManager()
	{
	}
	
	@Override
	public void parsecmd(String command, L2PcInstance activeChar)
	{
		if (command.equals("_bbsservice"))
		{
			sendHtm(activeChar, "data/html/CommunityBoard/42.htm");
		}
		else if (command.startsWith("_bbsservice;"))
		{
			StringTokenizer st = new StringTokenizer(command, ";");
			st.nextToken();
			String param = st.nextToken();
			if (param.equalsIgnoreCase("Noobles"))
			{
				if (activeChar.getInventory().getItemByItemId(Config.NoblItemId) == null)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				if (activeChar.getInventory().getItemByItemId(Config.NoblItemId).getCount() < Config.NoblItemCount)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				if (activeChar.getClassId().level() < 3)
				{
					activeChar.sendMessage("You must have third class.");
					return;
				}
				if (activeChar.isNoble())
				{
					activeChar.sendMessage("You already have nobless.");
					return;
				}
				activeChar.destroyItemByItemId("ShopBBS", Config.NoblItemId, Config.NoblItemCount, activeChar, true);
				activeChar.setNoble(true);
				activeChar.setTarget(activeChar);
				activeChar.broadcastPacket(new MagicSkillUse(activeChar, 5103, 1, 1000, 0));
				activeChar.broadcastUserInfo();
				activeChar.sendMessage("You are now a Noblesse Character.");
			}
			else if (param.equalsIgnoreCase("Gender"))
			{
				if (activeChar.getInventory().getItemByItemId(Config.GenderItemId) == null)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				if (activeChar.getInventory().getItemByItemId(Config.GenderItemId).getCount() < Config.GenderItemCount)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				activeChar.destroyItemByItemId("ShopBBS", Config.GenderItemId, Config.GenderItemCount, activeChar, true);
				activeChar.getAppearance().setSex(activeChar.getAppearance().getSex() ? false : true);
				activeChar.setTarget(activeChar);
				activeChar.broadcastPacket(new MagicSkillUse(activeChar, 5103, 1, 1000, 0));
				activeChar.broadcastUserInfo();
				activeChar.sendMessage("You have change your Gender successfully.");
			}
			else if (param.equalsIgnoreCase("RecoveryPK"))
			{
				if (activeChar.getInventory().getItemByItemId(Config.RecoveryPKItemId) == null)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				if (activeChar.getInventory().getItemByItemId(Config.RecoveryPKItemId).getCount() < Config.RecoveryPKItemCount)
				{
					activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.NOT_ENOUGH_ITEMS));
					return;
				}
				activeChar.destroyItemByItemId("ShopBBS", Config.RecoveryPKItemId, Config.RecoveryPKItemCount, activeChar, true);
				activeChar.setKarma(0);
				activeChar.setPkKills(0);
				activeChar.broadcastUserInfo();
				activeChar.sendPacket(new UserInfo(activeChar));
				activeChar.sendPacket(new ExBrExtraUserInfo(activeChar));
				activeChar.sendMessage("You have clean your Pks.");
				
			}
			else if (command.equalsIgnoreCase("Name"))
			{
				try
				{
					String _name = command.substring(11);
					String errorMsg = null;
					boolean proceed = true;
					if (_name.length() <= 2)
					{
						errorMsg = "Names have to be at least 3 characters";
						activeChar.sendMessage("Names have to be at least 3 characters");
						proceed = false;
					}
					if (_name.length() >= 17)
					{
						errorMsg = "Names cannot be longer than 16 characters";
						activeChar.sendMessage("Names cannot be longer than 16 characters");
						proceed = false;
					}
					if ((!Util.isAlphaNumeric(_name)) || (!isValidName(_name)))
					{
						errorMsg = "Invalid name";
						activeChar.sendMessage("Names can Only have letters and numbers");
						proceed = false;
					}
					if (CharNameTable.getInstance().getIdByName(_name) > 0)
					{
						errorMsg = "Name already exists";
						activeChar.sendMessage((new StringBuilder()).append("Warning, name ").append(_name).append(" already exists.").toString());
						proceed = false;
					}
					if (!proceed)
					{
						activeChar.sendMessage(errorMsg);
					}
					activeChar.destroyItemByItemId("ShopBBS", Config.NameItemId, Config.NameItemCount, activeChar, true);
					L2World.getInstance().removeFromAllPlayers(activeChar);
					activeChar.setName(_name);
					activeChar.store();
					L2World.getInstance().addToAllPlayers(activeChar);
					activeChar.sendMessage("Your new character name is " + _name);
					activeChar.broadcastUserInfo();
					try (Connection con = L2DatabaseFactory.getInstance().getConnection();
						PreparedStatement statement = con.prepareStatement("UPDATE characters SET char_name=? WHERE charId=?"))
					{
						statement.setString(1, _name);
						statement.setInt(2, activeChar.getObjectId());
						statement.execute();
						statement.close();
					}
					catch (Exception e)
					{
						_log.info("Error updating name for player " + activeChar.getName() + ". Error: " + e);
					}
					if (activeChar.isInParty())
					{
						// Delete party window for other party members
						activeChar.getParty().broadcastToPartyMembers(activeChar, new PartySmallWindowDeleteAll());
						for (final L2PcInstance member : activeChar.getParty().getMembers())
						{
							// And re-add
							if (member != activeChar)
							{
								member.sendPacket(new PartySmallWindowAll(activeChar, activeChar.getParty()));
							}
						}
					}
					
					if (activeChar.getClan() != null)
					{
						activeChar.getClan().updateClanMember(activeChar);
						activeChar.getClan().broadcastToOnlineMembers(new PledgeShowMemberListUpdate(activeChar));
						activeChar.sendPacket(new PledgeShowMemberListAll(activeChar.getClan(), activeChar));
					}
					
					RegionBBSManager.getInstance().changeCommunityBoard();
				}
				catch (StringIndexOutOfBoundsException e)
				{ // Case of empty character name
					activeChar.sendMessage("error: Something went wrong");
				}
			}
			else
			{
				ShowBoard sb = new ShowBoard((new StringBuilder()).append("<html><body><br><br><center>the command: ").append(command).append(" is not implemented yet</center><br><br></body></html>").toString(), "101");
				activeChar.sendPacket(sb);
				activeChar.sendPacket(new ShowBoard(null, "102"));
				activeChar.sendPacket(new ShowBoard(null, "103"));
			}
		}
	}
	
	private boolean sendHtm(L2PcInstance player, String path)
	{
		String oriPath = path;
		if ((player.getLang() != null) && !player.getLang().equalsIgnoreCase("en") && path.contains("html/"))
		{
			path = path.replace("html/", (new StringBuilder()).append("html-").append(player.getLang()).append("/").toString());
		}
		String content = HtmCache.getInstance().getHtm(path);
		if ((content == null) && !oriPath.equals(path))
		{
			content = HtmCache.getInstance().getHtm(oriPath);
		}
		if (content == null)
		{
			return false;
		}
		separateAndSend(content, player);
		return true;
	}
	
	private boolean isValidName(final String text)
	{
		boolean result = true;
		final String test = text;
		Pattern pattern;
		
		try
		{
			pattern = Pattern.compile(Config.CNAME_TEMPLATE);
		}
		catch (final PatternSyntaxException e) // case of illegal pattern
		{
			e.printStackTrace();
			pattern = Pattern.compile(".*");
		}
		
		final Matcher regexp = pattern.matcher(test);
		if (!regexp.matches())
		{
			result = false;
		}
		
		return result;
	}
	
	@Override
	public void parsewrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
	{
	}
	
	public static ServiceBBSManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	public static class SingletonHolder
	{
		protected static final ServiceBBSManager _instance = new ServiceBBSManager();
	}
}

and the bypass

<td align=center width=130><edit var="newName" width=100><button value="Change Name" action="bypass -h _bbsservice;Name;$newName" width=120 height=21 back="L2UI_CT1.Button_DF_Down" fore="L2UI_CT1.Button_DF"></td>
						
Edited by disorder25

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

this is because the bypass is string... so it reads 

"bypass -h _bbsservice;Name,$newName"

cant convert from $newName to the given name... if for example ur button bypass will be as :

bypass -h admin_setname $newName

will be fine... i think u can understand what i mean... $newName must be as argument and not as $newName (string)

 

Edit: 

try to change 

String _name = command.substring(11);

to (17)

Edited by melron
  • 0
Posted (edited)

this is because the bypass is string... so it reads 

"bypass -h _bbsservice;Name,$newName"

cant convert from $newName to the given name... if for example ur button bypass will be as :

bypass -h admin_setname $newName

will be fine... i think u can understand what i mean... $newName must be as argument and not as $newName (string)

 

Edit: 

try to change 

String _name = command.substring(11);

to (17)

 

 

I think I kind of understood what you were saying.

I did the changes and now look like the code is working but I still have one problem, if I use this bypass:

bypass -h _bbsservice;Name $newName

I get this error on the community board.

the command: _bbsservice;Name Paul is not implemented yet.

If I use this bypass

bypass -h _bbsservice;Name;$newName

I get the char named $newName

 

 

 

Do I need to create a bypass on CommunityBoard.java for the command for the first bypass to work?

 

 

Thank you.

Edited by disorder25
  • 0
Posted (edited)
String _name = st.nextToken();

and change your bypass to

bypass -h _bbsservice;Name;$newName;

 

 

Didn't work. I made all the changes you show me but still not working. Now when I enter the name and click on the button I get the message from:

catch (StringIndexOutOfBoundsException e)
Edited by disorder25

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...