NpcPersonalAIData.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2004-2015 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.util.HashMap;
  21. import java.util.Map;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.util.Rnd;
  25. /**
  26. * This class holds parameter, specific to certain NPCs.<br>
  27. * It can be either general parameters overridden for certain NPC instance instead of template parameters(aggro range, for example), or some optional parameters, handled by datapack scripts.<br>
  28. * @author GKR
  29. */
  30. public class NpcPersonalAIData
  31. {
  32. private final Map<String, Map<String, Integer>> _AIData = new HashMap<>();
  33. /**
  34. * Instantiates a new table.
  35. */
  36. protected NpcPersonalAIData()
  37. {
  38. }
  39. /**
  40. * Stores data for given spawn.
  41. * @param spawnDat spawn to process
  42. * @param data Map of AI values
  43. */
  44. public void storeData(L2Spawn spawnDat, Map<String, Integer> data)
  45. {
  46. if ((data != null) && !data.isEmpty())
  47. {
  48. // check for spawn name. Since spawn name is key for AI Data, generate random name, if spawn name isn't specified
  49. if (spawnDat.getName() == null)
  50. {
  51. spawnDat.setName(Long.toString(Rnd.nextLong()));
  52. }
  53. _AIData.put(spawnDat.getName(), data);
  54. }
  55. }
  56. /**
  57. * Gets AI value with given spawnName and paramName
  58. * @param spawnName spawn name to check
  59. * @param paramName parameter to check
  60. * @return value of given parameter for given spawn name
  61. */
  62. public int getAIValue(String spawnName, String paramName)
  63. {
  64. return hasAIValue(spawnName, paramName) ? _AIData.get(spawnName).get(paramName) : -1;
  65. }
  66. /**
  67. * Verifies if there is AI value with given spawnName and paramName
  68. * @param spawnName spawn name to check
  69. * @param paramName parameter name to check
  70. * @return {@code true} if parameter paramName is set for spawn spawnName, {@code false} otherwise
  71. */
  72. public boolean hasAIValue(String spawnName, String paramName)
  73. {
  74. return (spawnName != null) && _AIData.containsKey(spawnName) && _AIData.get(spawnName).containsKey(paramName);
  75. }
  76. /**
  77. * Initializes npc parameters by specified values.
  78. * @param npc NPC to process
  79. * @param spawn link to NPC's spawn
  80. * @param spawnName name of spawn
  81. */
  82. public void initializeNpcParameters(L2Npc npc, L2Spawn spawn, String spawnName)
  83. {
  84. if (_AIData.containsKey(spawnName))
  85. {
  86. Map<String, Integer> map = _AIData.get(spawnName);
  87. try
  88. {
  89. for (String key : map.keySet())
  90. {
  91. switch (key)
  92. {
  93. case "disableRandomAnimation":
  94. npc.setRandomAnimationEnabled((map.get(key) == 0));
  95. break;
  96. case "disableRandomWalk":
  97. npc.setIsNoRndWalk((map.get(key) == 1));
  98. spawn.setIsNoRndWalk((map.get(key) == 1));
  99. break;
  100. }
  101. }
  102. }
  103. catch (Exception e)
  104. {
  105. // Do nothing
  106. }
  107. }
  108. }
  109. /**
  110. * Gets the single instance of NpcTable.
  111. * @return single instance of NpcTable
  112. */
  113. public static NpcPersonalAIData getInstance()
  114. {
  115. return SingletonHolder._instance;
  116. }
  117. private static class SingletonHolder
  118. {
  119. protected static final NpcPersonalAIData _instance = new NpcPersonalAIData();
  120. }
  121. }