Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.47 KB | None | 0 0
  1. package com.osroyale.model.entity.mob.player;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Objects;
  8. import java.util.Optional;
  9. import java.util.Queue;
  10. import java.util.logging.Logger;
  11.  
  12. import com.osroyale.Configuration;
  13. import com.osroyale.engine.network.channel.PlayerChannel;
  14. import com.osroyale.engine.network.packet.outgoing.OutgoingPacket;
  15. import com.osroyale.engine.network.packet.outgoing.impl.SendExpCounter;
  16. import com.osroyale.engine.network.packet.outgoing.impl.SendLogout;
  17. import com.osroyale.engine.network.packet.outgoing.impl.SendMessage;
  18. import com.osroyale.engine.network.packet.outgoing.impl.SendPlayerDetails;
  19. import com.osroyale.engine.network.packet.outgoing.impl.SendPlayerOption;
  20. import com.osroyale.engine.network.packet.outgoing.impl.SendResetCameraPosition;
  21. import com.osroyale.engine.network.packet.outgoing.impl.SendRunEnergy;
  22. import com.osroyale.engine.network.packet.outgoing.impl.SendWalkableInterface;
  23. import com.osroyale.model.entity.EntityType;
  24. import com.osroyale.model.entity.mob.Mob;
  25. import com.osroyale.model.entity.mob.UpdateFlag;
  26. import com.osroyale.model.entity.mob.npc.Npc;
  27. import com.osroyale.model.entity.mob.player.appearance.Appearance;
  28. import com.osroyale.model.entity.mob.player.content.PreloadingGear;
  29. import com.osroyale.model.entity.mob.player.content.achievement.AchievementList;
  30. import com.osroyale.model.entity.mob.player.content.clan.ClanManager;
  31. import com.osroyale.model.entity.mob.player.content.combat.BountyHunter;
  32. import com.osroyale.model.entity.mob.player.content.combat.Skulling;
  33. import com.osroyale.model.entity.mob.player.content.dialogue.ChatBoxItemDialogue;
  34. import com.osroyale.model.entity.mob.player.content.dialogue.Dialogue;
  35. import com.osroyale.model.entity.mob.player.content.dialogue.DialogueFactory;
  36. import com.osroyale.model.entity.mob.player.content.dialogue.OptionDialogue;
  37. import com.osroyale.model.entity.mob.player.content.dialogue.input.InputAmount;
  38. import com.osroyale.model.entity.mob.player.content.dialogue.input.InputString;
  39. import com.osroyale.model.entity.mob.player.content.quest.QuestManager;
  40. import com.osroyale.model.entity.mob.player.content.skill.construction.House;
  41. import com.osroyale.model.entity.mob.player.content.skill.magic.Spellbook;
  42. import com.osroyale.model.entity.mob.player.content.skill.magic.spell.SpellCasting;
  43. import com.osroyale.model.entity.mob.player.content.skill.slayer.Slayer;
  44. import com.osroyale.model.entity.mob.player.content.teleport.Teleport;
  45. import com.osroyale.model.entity.mob.player.content.tittle.PlayerTitle;
  46. import com.osroyale.model.entity.mob.player.content.trade.Trade;
  47. import com.osroyale.model.entity.mob.player.content.trade.TradeContainer;
  48. import com.osroyale.model.entity.mob.player.profile.ProfileSave;
  49. import com.osroyale.model.entity.mob.player.relations.ChatMessage;
  50. import com.osroyale.model.entity.mob.player.relations.PlayerRelation;
  51. import com.osroyale.model.entity.mob.player.requests.RequestManager;
  52. import com.osroyale.model.world.World;
  53. import com.osroyale.model.world.items.containers.bank.Bank;
  54. import com.osroyale.model.world.items.containers.equipment.Equipment;
  55. import com.osroyale.model.world.items.containers.inventory.Inventory;
  56. import com.osroyale.model.world.items.containers.pricechecker.PriceChecker;
  57. import com.osroyale.model.world.location.Area;
  58. import com.osroyale.model.world.location.Location;
  59. import com.osroyale.model.world.region.Region;
  60. import com.osroyale.utility.Utility;
  61. import com.osroyale.utility.stopwatch.Stopwatch;
  62.  
  63. import io.netty.buffer.ByteBuf;
  64.  
  65. /**
  66. * This class represents a character controlled by a player.
  67. *
  68. * @author Daniel
  69. * @author Michael | Chex
  70. */
  71. public class Player extends Mob {
  72.  
  73. /** A logger instance for the {@code Player} class. */
  74. public static final Logger logger = Logger.getLogger(Player.class.getName());
  75.  
  76. /** The {@code PlayerChannel}. */
  77. public final Optional<PlayerChannel> channel;
  78.  
  79. /** The player details. */
  80. public PlayerDetails playerDetails;
  81.  
  82. /** The name. */
  83. public String username;
  84.  
  85. /** The password. */
  86. public String password;
  87.  
  88. /** The name expressed as a long. */
  89. public long usernameLong;
  90.  
  91. /** The host IP address. */
  92. public String lastHost;
  93.  
  94. /** All registered IP addresses. */
  95. public List<String> hostList = new LinkedList<>();
  96.  
  97. /** The uuid. */
  98. public String uuid;
  99.  
  100. /** The created date. */
  101. public String created;
  102.  
  103. /** The player's appearance. */
  104. public Appearance appearance = Configuration.DEFAULT_APPEARANCE;
  105.  
  106. /** The rights level. */
  107. public PlayerRight right = PlayerRight.PLAYER;
  108.  
  109. /** The player title. */
  110. public PlayerTitle playerTitle = PlayerTitle.create("", 0);
  111.  
  112. /** The spellbook. */
  113. public Spellbook spellbook = Spellbook.MODERN;
  114.  
  115. /** The shop. */
  116. public int shop;
  117.  
  118. /** The wilderness level. */
  119. public int wilderness;
  120.  
  121. /** Royal Point. */
  122. public int royalPoint;
  123.  
  124. /** Run Energy. */
  125. public int runEnergy;
  126.  
  127. /** Experience rate. */
  128. public double experienceRate;
  129.  
  130. /** Experience counter. */
  131. public int experienceCounter;
  132.  
  133. /** Ring of recoil. */
  134. public int recoilCharge;
  135.  
  136. /** The Pet. */
  137. public Npc pet;
  138.  
  139. /** Idle. */
  140. public boolean idle;
  141.  
  142. /** If player is a bot element. */
  143. public final boolean bot;
  144.  
  145. /** If player is locked. */
  146. public boolean locked;
  147.  
  148. /** Clan channel. */
  149. public String clan;
  150.  
  151. /** The cached update block. */
  152. public ByteBuf cachedUpdateBlock;
  153.  
  154. /** The input string. */
  155. public InputString inputString;
  156.  
  157. /** The input amount. */
  158. public InputAmount inputAmount;
  159.  
  160. /** Chatbox Item listener. */
  161. public ChatBoxItemDialogue chatBoxItemDialogue;
  162.  
  163. /** The current chat message. */
  164. public ChatMessage currentChatMessage;
  165.  
  166. /** A queue of pending chat messages. */
  167. public final Queue<ChatMessage> chatMessages = new LinkedList<ChatMessage>();
  168.  
  169. /** The dialogue. */
  170. public Optional<Dialogue> dialogue = Optional.empty();
  171.  
  172. /** The dialogue factory. */
  173. public DialogueFactory dialogueFactory = new DialogueFactory(this);
  174.  
  175. /** The option dialogue. */
  176. public Optional<OptionDialogue> optionDialogue = Optional.empty();
  177.  
  178. /** The animation timer. */
  179. public Stopwatch emoteDelay = new Stopwatch();
  180.  
  181. /** The generic action delay - used for clicking objects, or npcs . */
  182. public Stopwatch actionDelay = new Stopwatch();
  183.  
  184. /** The generic click delay - used for clicking interfaces. */
  185. public Stopwatch clickDelay = new Stopwatch();
  186.  
  187. /** The generic food delay - used for consuming food. */
  188. public Stopwatch foodDelay = new Stopwatch();
  189.  
  190. /** The player assistant. */
  191. public final PlayerAssistant playerAssistant = new PlayerAssistant(this);
  192.  
  193. /** The interface state. */
  194. public final InterfaceState interfaceState = new InterfaceState(this);
  195.  
  196. /** The request manager which manages trading and dueling requests. */
  197. public final RequestManager requestManager = new RequestManager(this);
  198.  
  199. /** The player's settings. */
  200. public final Settings settings = new Settings(this);
  201.  
  202. /** The player relations. */
  203. public final PlayerRelation relation = new PlayerRelation(this);
  204.  
  205. /** The account security. */
  206. public final AccountSecurity security = new AccountSecurity(this);
  207.  
  208. /** The inventory. */
  209. public final Inventory inventory = new Inventory(this);
  210.  
  211. /** The bank. */
  212. public final Bank bank = new Bank(this);
  213.  
  214. /** The equipment. */
  215. public final Equipment equipment = new Equipment(this);
  216.  
  217. /** The pre-loading gear. */
  218. public final PreloadingGear preloadingGear = new PreloadingGear(this);
  219.  
  220. /** The price checker. */
  221. public final PriceChecker priceChecker = new PriceChecker(this);
  222.  
  223. public final House house = new House(this);
  224.  
  225. /** The trade container. */
  226. public final Trade trade = new Trade(this);
  227.  
  228. /** The trade container. */
  229. public final TradeContainer tradeContainer = new TradeContainer(this);
  230.  
  231. /** The slayer instance. */
  232. public Slayer slayer = new Slayer(this);
  233.  
  234. /** The skulling. */
  235. public Skulling skulling = new Skulling(this);
  236.  
  237. public QuestManager quest = new QuestManager(this);
  238.  
  239. /** The spell casting. */
  240. public SpellCasting spellCasting = new SpellCasting(this);
  241.  
  242. /** The bounty hunter. */
  243. public BountyHunter bountyHunter = new BountyHunter(this);
  244.  
  245. /** The favorite teleports. */
  246. public List<Teleport> favoriteTeleport = new ArrayList<Teleport>();
  247.  
  248. /** The achievement. */
  249. public HashMap<AchievementList, Integer> playerAchievements = new HashMap<AchievementList, Integer>(AchievementList.values().length) {
  250. private static final long serialVersionUID = 1842952445111093360L;
  251. {
  252. for (final AchievementList achievement : AchievementList.values()) {
  253. put(achievement, 0);
  254. }
  255. }
  256. };
  257.  
  258. /** The player. */
  259. public Player(Optional<PlayerChannel> channel) {
  260. super(Configuration.DEFAULT_LOCATION);
  261. this.channel = channel;
  262. bot = false;
  263. }
  264.  
  265. /** The player. */
  266. public Player(String username, Location location) {
  267. super(Configuration.DEFAULT_LOCATION);
  268. setLocation(location);
  269. setUsername(username);
  270. password = "";
  271. attr().set("NEW_PLAYER", false);
  272. this.channel = Optional.empty();
  273. bot = true;
  274. }
  275.  
  276. /**
  277. * Sets the player's username. The username long will also be recalculated.
  278. *
  279. * @param username
  280. * The username to set.
  281. */
  282. public void setUsername(String username) {
  283. this.username = username;
  284. this.usernameLong = Utility.nameToLong(username);
  285. }
  286.  
  287. /**
  288. * Checks if there is an update block.
  289. *
  290. * @return {@code True} if there is an update block.
  291. */
  292. public boolean hasCacheUpdateBlock() {
  293. return cachedUpdateBlock != null;
  294. }
  295.  
  296. /**
  297. * Sends a packet to the client.
  298. *
  299. * @param out
  300. * The outgoing packet to send.
  301. */
  302. public void send(OutgoingPacket out) {
  303. out.encode(this);
  304. }
  305.  
  306. /**
  307. * Resets the cache update block.
  308. */
  309. public void resetCachedUpdateBlock() {
  310. cachedUpdateBlock = null;
  311. }
  312.  
  313. /**
  314. * Handles the player logging in of the server.
  315. */
  316. private final void login() {
  317. setId(-1);
  318. setTeleporting(true);
  319. setRegionChange(true);
  320. flag(UpdateFlag.APPEARANCE);
  321. getSkills().login();
  322. getAnimations().setPlayerAnimations();
  323. send(new SendRunEnergy());
  324. send(new SendPlayerDetails());
  325. send(new SendResetCameraPosition());
  326. send(new SendExpCounter(experienceCounter));
  327. send(new SendMessage(String.format("Welcome to %s.", Configuration.SERVER_NAME)));
  328. security.login();
  329. relation.login();
  330. settings.login();
  331. equipment.login();
  332. inventory.refresh();
  333. bountyHunter.login();
  334. playerAssistant.login();
  335. ClanManager.login(this);
  336. }
  337.  
  338. /**
  339. * Handles the player logging out of the server.
  340. */
  341. public final void logout() {
  342. bountyHunter.logout();
  343. send(new SendLogout());
  344. }
  345.  
  346. @Override
  347. public void register() {
  348. if (!isRegistered() && !World.PLAYERS.contains(this)) {
  349. setRegistered(World.PLAYERS.add(this));
  350. setLocation(getLocation());
  351. login();
  352. logger.info(String.format("[REGISTERED]: " + Utility.formatName(getName()) + " [" + lastHost + "]"));
  353. }
  354. }
  355.  
  356. @Override
  357. public void unregister() {
  358. if (isRegistered()) {
  359. interfaceState.close();
  360. house.leave();
  361. ProfileSave.save(this);
  362. ClanManager.leave(this);
  363. relation.updateLists(false);
  364. World.PLAYERS.remove(destroy());
  365. logger.info(String.format("[UNREGISTERED]: " + Utility.formatName(getName()) + " [" + lastHost + "]"));
  366. }
  367. }
  368.  
  369. @Override
  370. public void addToRegion(Region region) {
  371. region.addPlayer(this);
  372. }
  373.  
  374. @Override
  375. public void removeFromRegion(Region region) {
  376. region.removePlayer(this);
  377. }
  378.  
  379. @Override
  380. public void onStep() {
  381. if (Area.inWilderness(this)) {
  382. int modY = getLocation().getY() > 6400 ? getLocation().getY() - 6400 : getLocation().getY();
  383. wilderness = (((modY - 3521) / 8) + 1);
  384. send(new SendPlayerOption(PlayerOption.ATTACK, true));
  385. send(new SendPlayerOption(PlayerOption.DUEL_REQUEST, false, true));
  386. bountyHunter.enter();
  387.  
  388. /* Bounty Hunter timer */
  389. } else if (!Area.inWilderness(this) && bountyHunter.target != null) {
  390. bountyHunter.leave();
  391.  
  392. /* Bounty Hunter timer */
  393. } else if (bountyHunter.timer != -1) {
  394. bountyHunter.update();
  395.  
  396. /* Duel Arena */
  397. } else if (Area.inDuelArena(this)) {
  398. send(new SendPlayerOption(PlayerOption.ATTACK, true, true));
  399. send(new SendPlayerOption(PlayerOption.DUEL_REQUEST, false));
  400. send(new SendWalkableInterface(201));
  401.  
  402. /* Clear screen */
  403. } else if (!interfaceState.isClear()) {
  404. interfaceState.close();
  405. }
  406. }
  407.  
  408. @Override
  409. public void onDeath() {
  410. move(Configuration.DEFAULT_LOCATION);
  411. send(new SendMessage("Oh dear, you are dead!"));
  412. skulling.unskull();
  413. bountyHunter.death();
  414. }
  415.  
  416. @Override
  417. public String getName() {
  418. return Utility.formatName(username);
  419. }
  420.  
  421. @Override
  422. public EntityType getType() {
  423. return EntityType.PLAYER;
  424. }
  425.  
  426. @Override
  427. public boolean isValid() {
  428. if (!bot && !channel.isPresent()) {
  429. return false;
  430. }
  431.  
  432. return super.isValid();
  433. }
  434.  
  435. @Override
  436. public boolean equals(Object obj) {
  437. if (obj instanceof Player) {
  438. Player other = (Player) obj;
  439. return (other.username == username && other.password == password) || (other.username.equalsIgnoreCase(username) && other.password.equalsIgnoreCase(password));
  440. }
  441. return obj == this;
  442. }
  443.  
  444. @Override
  445. public int hashCode() {
  446. return Objects.hash(Utility.nameToLong(username));
  447. }
  448.  
  449. @Override
  450. public String toString() {
  451. return "Player[index=" + getIndex() + ", username=" + getName() + ", location=" + getLocation() + "]";
  452. }
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement