Noticias:

No tienes permiso para ver los enlaces. Para poder verlos Registrate o Conectate.

Menú Principal

Cambiar contraseña del jugador con comandos

Iniciado por Swarlog, Jun 25, 2025, 09:53 PM

Tema anterior - Siguiente tema

Swarlog

Se trata de un comando a añadir en vuestros servidores con el que un jugador desde dentro del servidor, podrá cambiar su contraseña de cuenta mediante el uso de un comando. By Jing~Jang, espero que os guste!

/*
 * 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 com.l2jfrozen.gameserver.handler.voicedcommandhandlers;

import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.StringTokenizer;

import com.l2jfrozen.crypt.Base64;
import com.l2jfrozen.util.database.L2DatabaseFactory;
import com.l2jfrozen.gameserver.handler.IVoicedCommandHandler;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.network.serverpackets.ExShowScreenMessage;

/* 
 * @Author: Unknow
 * @Adaptation: |-Jing~Jang-|
 */

public class ChangePassword implements IVoicedCommandHandler
{
	private static final String[] _voicedCommands =
	{
		"changepass"
	};

	@Override
	public boolean useVoicedCommand(String command, L2PcInstance activeChar, String target)
	{
		if(command.equalsIgnoreCase("changepass") && target != null)
		{
			StringTokenizer st = new StringTokenizer(target);
			try
			{
				String curpass = null, newpass = null, repeatnewpass = null;

				if(st.hasMoreTokens())
					curpass = st.nextToken();
				if(st.hasMoreTokens())
					newpass = st.nextToken();
				if(st.hasMoreTokens())
					repeatnewpass = st.nextToken();

				if(!(curpass == null || newpass == null || repeatnewpass == null))
				{
					if(!newpass.equals(repeatnewpass))
					{
						activeChar.sendMessage("The new password doesn't match with the repeated one!");
						return false;
					}
					if(newpass.length() < 4)
					{
						activeChar.sendMessage("The new password is shorter than 4 characters! Please try with a longer one.");
						return false;
					}
					if(newpass.length() > 25)
					{
						activeChar.sendMessage("The new password is longer than 25 characters! Please try with a shorter one.");
						return false;
					}

					MessageDigest md = MessageDigest.getInstance("SHA");

					byte[] raw = curpass.getBytes("UTF-8");
					raw = md.digest(raw);
					String curpassEnc = Base64.encodeBytes(raw);
					String pass = null;
					int passUpdated = 0;

					Connection con = null;
					con = L2DatabaseFactory.getInstance().getConnection();
					PreparedStatement statement = con.prepareStatement("SELECT password FROM accounts WHERE login=?");
					statement.setString(1, activeChar.getAccountName());
					ResultSet rset = statement.executeQuery();

					if(rset.next())
					{
						pass = rset.getString("password");
					}

					rset.close();
					statement.close();

					if(curpassEnc.equals(pass))
					{
						byte[] password = newpass.getBytes("UTF-8");
						password = md.digest(password);

						PreparedStatement ps = con.prepareStatement("UPDATE accounts SET password=? WHERE login=?");
						ps.setString(1, Base64.encodeBytes(password));
						ps.setString(2, activeChar.getAccountName());
						passUpdated = ps.executeUpdate();
						ps.close();
						con.close();
						
						if(passUpdated > 0)
						{
							activeChar.sendMessage("You have successfully changed your password!");
							activeChar.sendPacket(new ExShowScreenMessage("You have successfully changed your password!#Enjoy :)", 6000));
						}
						else
						{
								activeChar.sendMessage("The password change was unsuccessful!");
								activeChar.sendPacket(new ExShowScreenMessage("The password change was unsuccessful!!#U_u)", 6000));
						}
					}
					else
					{
						activeChar.sendMessage("CurrentPass doesn't match with your current one.");
						return false;
					}
				}
				else
				{
					activeChar.sendMessage("Invalid pass data! Format: .changepass CurrentPass NewPass NewPass");
					return false;
				}
			}
			catch(Exception e)
			{
				activeChar.sendMessage("A problem occured while changing password!");
			}
		}
		else
		{
			activeChar.sendMessage("To change your current password, you have to type the command in the following format(without the brackets []): [.changepass CurrentPass NewPass NewPass]. You should also know that the password is case sensitive.");
			activeChar.sendPacket(new ExShowScreenMessage("To change your current password, you have to type the command in the following format(without the brackets []):#[.changepass CurrentPass NewPass NewPass]. You should also know that the password is case sensitive.)", 7000));
			return false;
		}

		return true;
	}

	@Override
	public String[] getVoicedCommandList()
	{
		return _voicedCommands;
	}

}