PetNameTable.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.datatables;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.logging.Logger;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import java.util.regex.PatternSyntaxException;
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.L2DatabaseFactory;
  25. import net.sf.l2j.gameserver.model.L2PetDataTable;
  26. public class PetNameTable
  27. {
  28. private static Logger _log = Logger.getLogger(PetNameTable.class.getName());
  29. private static PetNameTable _instance;
  30. public static PetNameTable getInstance()
  31. {
  32. if (_instance == null)
  33. {
  34. _instance = new PetNameTable();
  35. }
  36. return _instance;
  37. }
  38. public boolean doesPetNameExist(String name, int petNpcId)
  39. {
  40. boolean result = true;
  41. java.sql.Connection con = null;
  42. try
  43. {
  44. con = L2DatabaseFactory.getInstance().getConnection();
  45. PreparedStatement statement = con.prepareStatement("SELECT name FROM pets p, items i WHERE p.item_obj_id = i.object_id AND name=? AND i.item_id IN (?)");
  46. statement.setString(1, name);
  47. String cond = "";
  48. for (int it : L2PetDataTable.getPetItemsByNpc(petNpcId))
  49. {
  50. if (cond != "") cond += ", ";
  51. cond += it;
  52. }
  53. statement.setString(2, cond);
  54. ResultSet rset = statement.executeQuery();
  55. result = rset.next();
  56. rset.close();
  57. statement.close();
  58. }
  59. catch (SQLException e)
  60. {
  61. _log.warning("could not check existing petname:"+e.getMessage());
  62. }
  63. finally
  64. {
  65. try { con.close(); } catch (Exception e) {}
  66. }
  67. return result;
  68. }
  69. public boolean isValidPetName(String name)
  70. {
  71. boolean result = true;
  72. if (!isAlphaNumeric(name)) return result;
  73. Pattern pattern;
  74. try
  75. {
  76. pattern = Pattern.compile(Config.PET_NAME_TEMPLATE);
  77. }
  78. catch (PatternSyntaxException e) // case of illegal pattern
  79. {
  80. _log.warning("ERROR : Pet name pattern of config is wrong!");
  81. pattern = Pattern.compile(".*");
  82. }
  83. Matcher regexp = pattern.matcher(name);
  84. if (!regexp.matches())
  85. {
  86. result = false;
  87. }
  88. return result;
  89. }
  90. private boolean isAlphaNumeric(String text)
  91. {
  92. boolean result = true;
  93. char[] chars = text.toCharArray();
  94. for (int i = 0; i < chars.length; i++)
  95. {
  96. if (!Character.isLetterOrDigit(chars[i]))
  97. {
  98. result = false;
  99. break;
  100. }
  101. }
  102. return result;
  103. }
  104. }