2
0

PetNameTable.java 3.1 KB

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