Advertisement
MarGaZeaS

Untitled

Oct 7th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. package margazeas.gold.gameserver.model.zone.type;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.concurrent.ConcurrentHashMap;
  10.  
  11. import margazeas.gold.commons.pool.ConnectionPool;
  12.  
  13. import margazeas.gold.gameserver.data.xml.MapRegionData.TeleportType;
  14. import margazeas.gold.gameserver.enums.ZoneId;
  15. import margazeas.gold.gameserver.model.actor.Attackable;
  16. import margazeas.gold.gameserver.model.actor.Creature;
  17. import margazeas.gold.gameserver.model.actor.Playable;
  18. import margazeas.gold.gameserver.model.actor.Player;
  19. import margazeas.gold.gameserver.model.actor.Summon;
  20. import margazeas.gold.gameserver.model.zone.type.subtype.ZoneType;
  21.  
  22. /**
  23. * A Boss zone, extending {@link ZoneType}. It holds a {@link List} and a {@link Map} of allowed {@link Player}s.<br>
  24. * <br>
  25. * The Map is used for Players disconnections, while the List is used for Players to re-enter the zone after server downtime/restart.
  26. */
  27. public class BossZone extends ZoneType
  28. {
  29. private static final String SELECT_GRAND_BOSS_LIST = "SELECT * FROM grandboss_list WHERE zone = ?";
  30.  
  31. // Track the times that players got disconnected. Players are allowed to log back into the zone as long as their log-out was within _timeInvade time...
  32. private final Map<Integer, Long> _allowedPlayersEntryTime = new ConcurrentHashMap<>();
  33.  
  34. // Track players admitted to the zone who should be allowed back in after reboot/server downtime, within 30min of server restart
  35. private final Set<Integer> _allowedPlayers = ConcurrentHashMap.newKeySet();
  36.  
  37. private final int[] _oustLoc = new int[3];
  38.  
  39. private int _invadeTime;
  40.  
  41. public BossZone(int id)
  42. {
  43. super(id);
  44.  
  45. try (Connection con = ConnectionPool.getConnection();
  46. PreparedStatement ps = con.prepareStatement(SELECT_GRAND_BOSS_LIST))
  47. {
  48. ps.setInt(1, id);
  49.  
  50. try (ResultSet rs = ps.executeQuery())
  51. {
  52. while (rs.next())
  53. allowPlayerEntry(rs.getInt("player_id"));
  54. }
  55. }
  56. catch (Exception e)
  57. {
  58. LOGGER.error("Couldn't load players for {}.", e, toString());
  59. }
  60. }
  61.  
  62. @Override
  63. public void setParameter(String name, String value)
  64. {
  65. if (name.equals("InvadeTime"))
  66. _invadeTime = Integer.parseInt(value);
  67. else if (name.equals("oustX"))
  68. _oustLoc[0] = Integer.parseInt(value);
  69. else if (name.equals("oustY"))
  70. _oustLoc[1] = Integer.parseInt(value);
  71. else if (name.equals("oustZ"))
  72. _oustLoc[2] = Integer.parseInt(value);
  73. else
  74. super.setParameter(name, value);
  75. }
  76.  
  77. @Override
  78. protected void onEnter(Creature character)
  79. {
  80. character.setInsideZone(ZoneId.BOSS, true);
  81.  
  82. if (character instanceof Player)
  83. {
  84. // Get player and set zone info.
  85. final Player player = (Player) character;
  86. player.setInsideZone(ZoneId.NO_SUMMON_FRIEND, true);
  87.  
  88. // Skip other checks for GM or if no invade time is set.
  89. if (player.isGM() || _invadeTime == 0)
  90. return;
  91.  
  92. // Get player object id.
  93. final int id = player.getObjectId();
  94.  
  95. if (_allowedPlayers.contains(id))
  96. {
  97. // Get and remove the entry expiration time (once entered, can not enter enymore, unless specified).
  98. final long entryTime = _allowedPlayersEntryTime.remove(id);
  99. if (entryTime > System.currentTimeMillis())
  100. return;
  101.  
  102. // Player trying to join after expiration, remove from allowed list.
  103. _allowedPlayers.remove(Integer.valueOf(id));
  104. }
  105.  
  106. // Teleport out player, who attempt "illegal" (re-)entry.
  107. if (_oustLoc[0] != 0 && _oustLoc[1] != 0 && _oustLoc[2] != 0)
  108. player.teleportTo(_oustLoc[0], _oustLoc[1], _oustLoc[2], 0);
  109. else
  110. player.teleportTo(TeleportType.TOWN);
  111. }
  112. else if (character instanceof Summon)
  113. {
  114. final Player player = ((Summon) character).getOwner();
  115. if (player != null)
  116. {
  117. if (_allowedPlayers.contains(player.getObjectId()) || player.isGM() || _invadeTime == 0)
  118. return;
  119.  
  120. // Remove summon.
  121. ((Summon) character).unSummon(player);
  122. }
  123. }
  124. }
  125.  
  126. @Override
  127. protected void onExit(Creature character)
  128. {
  129. character.setInsideZone(ZoneId.BOSS, false);
  130.  
  131. if (character instanceof Playable)
  132. {
  133. if (character instanceof Player)
  134. {
  135. // Get player and set zone info.
  136. final Player player = (Player) character;
  137. player.setInsideZone(ZoneId.NO_SUMMON_FRIEND, false);
  138.  
  139. // Skip other checks for GM or if no invade time is set.
  140. if (player.isGM() || _invadeTime == 0)
  141. return;
  142.  
  143. // Get player object id.
  144. final int id = player.getObjectId();
  145.  
  146. if (_allowedPlayers.contains(id))
  147. {
  148. if (!player.isOnline())
  149. {
  150. // Player disconnected.
  151. _allowedPlayersEntryTime.put(id, System.currentTimeMillis() + _invadeTime);
  152. }
  153. else
  154. {
  155. // Player has allowed entry, do not delete from allowed list.
  156. if (_allowedPlayersEntryTime.containsKey(id))
  157. return;
  158.  
  159. // Remove player allowed list.
  160. _allowedPlayers.remove(Integer.valueOf(id));
  161. }
  162. }
  163. }
  164.  
  165. // If playables aren't found, force all bosses to return to spawnpoint.
  166. if (!_characters.isEmpty())
  167. {
  168. if (!getKnownTypeInside(Playable.class).isEmpty())
  169. return;
  170.  
  171. for (Attackable raid : getKnownTypeInside(Attackable.class))
  172. {
  173. if (!raid.isRaidRelated())
  174. continue;
  175.  
  176. raid.returnHome();
  177. }
  178. }
  179. }
  180. else if (character instanceof Attackable && character.isRaidRelated())
  181. ((Attackable) character).returnHome();
  182. }
  183.  
  184. /**
  185. * Enables the entry of a {@link Player} to this {@link BossZone} for next "duration" seconds. If the Player tries to enter the zone after this period, he will be teleported out.
  186. * @param player : The allowed player to entry.
  187. * @param duration : The entry permission period (in seconds).
  188. */
  189. public void allowPlayerEntry(Player player, int duration)
  190. {
  191. // Get player object id.
  192. final int playerId = player.getObjectId();
  193.  
  194. // Allow player entry.
  195. if (!_allowedPlayers.contains(playerId))
  196. _allowedPlayers.add(playerId);
  197.  
  198. // For the given duration.
  199. _allowedPlayersEntryTime.put(playerId, System.currentTimeMillis() + duration * 1000);
  200. }
  201.  
  202. /**
  203. * Enables the entry of a {@link Player} to this {@link BossZone} after server shutdown/restart. The time limit is specified by each zone via "InvadeTime" parameter. If the player tries to enter the zone after this period, he will be teleported out.
  204. * @param playerId : The objectid of the allowed player to entry.
  205. */
  206. public void allowPlayerEntry(int playerId)
  207. {
  208. // Allow player entry.
  209. if (!_allowedPlayers.contains(playerId))
  210. _allowedPlayers.add(playerId);
  211.  
  212. // For the given duration.
  213. _allowedPlayersEntryTime.put(playerId, System.currentTimeMillis() + _invadeTime);
  214. }
  215.  
  216. /**
  217. * Removes the {@link Player} from allowed list and cancel the entry permition.
  218. * @param player : Player to remove from the zone.
  219. */
  220. public void removePlayer(Player player)
  221. {
  222. // Get player object id.
  223. final int id = player.getObjectId();
  224.  
  225. // Remove player from allowed list.
  226. _allowedPlayers.remove(Integer.valueOf(id));
  227.  
  228. // Remove player permission.
  229. _allowedPlayersEntryTime.remove(id);
  230. }
  231.  
  232. /**
  233. * @return the list of all allowed {@link Player}s objectIds.
  234. */
  235. public Set<Integer> getAllowedPlayers()
  236. {
  237. return _allowedPlayers;
  238. }
  239.  
  240. /**
  241. * Teleport all {@link Player}s located in this {@link BossZone} to a specific location, as listed on {@link #_oustLoc}. Clear both containers holding Players informations.
  242. * @return the List of all Players who have been forced to teleport.
  243. */
  244. public List<Player> oustAllPlayers()
  245. {
  246. final List<Player> players = getKnownTypeInside(Player.class);
  247. if (players.isEmpty())
  248. return players;
  249.  
  250. for (Player player : players)
  251. {
  252. if (player.isOnline())
  253. {
  254. if (_oustLoc[0] != 0 && _oustLoc[1] != 0 && _oustLoc[2] != 0)
  255. player.teleportTo(_oustLoc[0], _oustLoc[1], _oustLoc[2], 0);
  256. else
  257. player.teleportTo(TeleportType.TOWN);
  258. }
  259. }
  260. _allowedPlayersEntryTime.clear();
  261. _allowedPlayers.clear();
  262.  
  263. return players;
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement