123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server 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.
- *
- * L2J Server 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.l2jserver.gameserver.data.sql.impl;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- 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.commons.database.pool.impl.ConnectionFactory;
- import com.l2jserver.gameserver.data.xml.impl.PetDataTable;
- public class PetNameTable
- {
- private static Logger LOGGER = Logger.getLogger(PetNameTable.class.getName());
-
- public static PetNameTable getInstance()
- {
- return SingletonHolder._instance;
- }
-
- public boolean doesPetNameExist(String name, int petNpcId)
- {
- boolean result = true;
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT name FROM pets p, items i WHERE p.item_obj_id = i.object_id AND name=? AND i.item_id IN (?)"))
- {
- ps.setString(1, name);
- StringBuilder cond = new StringBuilder();
- if (!cond.toString().isEmpty())
- {
- cond.append(", ");
- }
-
- cond.append(PetDataTable.getInstance().getPetItemsByNpc(petNpcId));
- ps.setString(2, cond.toString());
- try (ResultSet rs = ps.executeQuery())
- {
- result = rs.next();
- }
- }
- catch (SQLException e)
- {
- LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing petname:" + e.getMessage(), e);
- }
- return result;
- }
-
- public boolean isValidPetName(String name)
- {
- boolean result = true;
-
- if (!isAlphaNumeric(name))
- {
- return result;
- }
-
- Pattern pattern;
- try
- {
- pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
- }
- catch (PatternSyntaxException e) // case of illegal pattern
- {
- LOGGER.warning(getClass().getSimpleName() + ": Pet name pattern of config is wrong!");
- pattern = Pattern.compile(".*");
- }
- Matcher regexp = pattern.matcher(name);
- if (!regexp.matches())
- {
- result = false;
- }
- return result;
- }
-
- private boolean isAlphaNumeric(String text)
- {
- boolean result = true;
- char[] chars = text.toCharArray();
- for (int i = 0; i < chars.length; i++)
- {
- if (!Character.isLetterOrDigit(chars[i]))
- {
- result = false;
- break;
- }
- }
- return result;
- }
-
- private static class SingletonHolder
- {
- protected static final PetNameTable _instance = new PetNameTable();
- }
- }
|