Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.osroyale.model.entity.mob.player;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Objects;
- import java.util.Optional;
- import java.util.Queue;
- import java.util.logging.Logger;
- import com.osroyale.Configuration;
- import com.osroyale.engine.network.channel.PlayerChannel;
- import com.osroyale.engine.network.packet.outgoing.OutgoingPacket;
- import com.osroyale.engine.network.packet.outgoing.impl.SendExpCounter;
- import com.osroyale.engine.network.packet.outgoing.impl.SendLogout;
- import com.osroyale.engine.network.packet.outgoing.impl.SendMessage;
- import com.osroyale.engine.network.packet.outgoing.impl.SendPlayerDetails;
- import com.osroyale.engine.network.packet.outgoing.impl.SendPlayerOption;
- import com.osroyale.engine.network.packet.outgoing.impl.SendResetCameraPosition;
- import com.osroyale.engine.network.packet.outgoing.impl.SendRunEnergy;
- import com.osroyale.engine.network.packet.outgoing.impl.SendWalkableInterface;
- import com.osroyale.model.entity.EntityType;
- import com.osroyale.model.entity.mob.Mob;
- import com.osroyale.model.entity.mob.UpdateFlag;
- import com.osroyale.model.entity.mob.npc.Npc;
- import com.osroyale.model.entity.mob.player.appearance.Appearance;
- import com.osroyale.model.entity.mob.player.content.PreloadingGear;
- import com.osroyale.model.entity.mob.player.content.achievement.AchievementList;
- import com.osroyale.model.entity.mob.player.content.clan.ClanManager;
- import com.osroyale.model.entity.mob.player.content.combat.BountyHunter;
- import com.osroyale.model.entity.mob.player.content.combat.Skulling;
- import com.osroyale.model.entity.mob.player.content.dialogue.ChatBoxItemDialogue;
- import com.osroyale.model.entity.mob.player.content.dialogue.Dialogue;
- import com.osroyale.model.entity.mob.player.content.dialogue.DialogueFactory;
- import com.osroyale.model.entity.mob.player.content.dialogue.OptionDialogue;
- import com.osroyale.model.entity.mob.player.content.dialogue.input.InputAmount;
- import com.osroyale.model.entity.mob.player.content.dialogue.input.InputString;
- import com.osroyale.model.entity.mob.player.content.quest.QuestManager;
- import com.osroyale.model.entity.mob.player.content.skill.construction.House;
- import com.osroyale.model.entity.mob.player.content.skill.magic.Spellbook;
- import com.osroyale.model.entity.mob.player.content.skill.magic.spell.SpellCasting;
- import com.osroyale.model.entity.mob.player.content.skill.slayer.Slayer;
- import com.osroyale.model.entity.mob.player.content.teleport.Teleport;
- import com.osroyale.model.entity.mob.player.content.tittle.PlayerTitle;
- import com.osroyale.model.entity.mob.player.content.trade.Trade;
- import com.osroyale.model.entity.mob.player.content.trade.TradeContainer;
- import com.osroyale.model.entity.mob.player.profile.ProfileSave;
- import com.osroyale.model.entity.mob.player.relations.ChatMessage;
- import com.osroyale.model.entity.mob.player.relations.PlayerRelation;
- import com.osroyale.model.entity.mob.player.requests.RequestManager;
- import com.osroyale.model.world.World;
- import com.osroyale.model.world.items.containers.bank.Bank;
- import com.osroyale.model.world.items.containers.equipment.Equipment;
- import com.osroyale.model.world.items.containers.inventory.Inventory;
- import com.osroyale.model.world.items.containers.pricechecker.PriceChecker;
- import com.osroyale.model.world.location.Area;
- import com.osroyale.model.world.location.Location;
- import com.osroyale.model.world.region.Region;
- import com.osroyale.utility.Utility;
- import com.osroyale.utility.stopwatch.Stopwatch;
- import io.netty.buffer.ByteBuf;
- /**
- * This class represents a character controlled by a player.
- *
- * @author Daniel
- * @author Michael | Chex
- */
- public class Player extends Mob {
- /** A logger instance for the {@code Player} class. */
- public static final Logger logger = Logger.getLogger(Player.class.getName());
- /** The {@code PlayerChannel}. */
- public final Optional<PlayerChannel> channel;
- /** The player details. */
- public PlayerDetails playerDetails;
- /** The name. */
- public String username;
- /** The password. */
- public String password;
- /** The name expressed as a long. */
- public long usernameLong;
- /** The host IP address. */
- public String lastHost;
- /** All registered IP addresses. */
- public List<String> hostList = new LinkedList<>();
- /** The uuid. */
- public String uuid;
- /** The created date. */
- public String created;
- /** The player's appearance. */
- public Appearance appearance = Configuration.DEFAULT_APPEARANCE;
- /** The rights level. */
- public PlayerRight right = PlayerRight.PLAYER;
- /** The player title. */
- public PlayerTitle playerTitle = PlayerTitle.create("", 0);
- /** The spellbook. */
- public Spellbook spellbook = Spellbook.MODERN;
- /** The shop. */
- public int shop;
- /** The wilderness level. */
- public int wilderness;
- /** Royal Point. */
- public int royalPoint;
- /** Run Energy. */
- public int runEnergy;
- /** Experience rate. */
- public double experienceRate;
- /** Experience counter. */
- public int experienceCounter;
- /** Ring of recoil. */
- public int recoilCharge;
- /** The Pet. */
- public Npc pet;
- /** Idle. */
- public boolean idle;
- /** If player is a bot element. */
- public final boolean bot;
- /** If player is locked. */
- public boolean locked;
- /** Clan channel. */
- public String clan;
- /** The cached update block. */
- public ByteBuf cachedUpdateBlock;
- /** The input string. */
- public InputString inputString;
- /** The input amount. */
- public InputAmount inputAmount;
- /** Chatbox Item listener. */
- public ChatBoxItemDialogue chatBoxItemDialogue;
- /** The current chat message. */
- public ChatMessage currentChatMessage;
- /** A queue of pending chat messages. */
- public final Queue<ChatMessage> chatMessages = new LinkedList<ChatMessage>();
- /** The dialogue. */
- public Optional<Dialogue> dialogue = Optional.empty();
- /** The dialogue factory. */
- public DialogueFactory dialogueFactory = new DialogueFactory(this);
- /** The option dialogue. */
- public Optional<OptionDialogue> optionDialogue = Optional.empty();
- /** The animation timer. */
- public Stopwatch emoteDelay = new Stopwatch();
- /** The generic action delay - used for clicking objects, or npcs . */
- public Stopwatch actionDelay = new Stopwatch();
- /** The generic click delay - used for clicking interfaces. */
- public Stopwatch clickDelay = new Stopwatch();
- /** The generic food delay - used for consuming food. */
- public Stopwatch foodDelay = new Stopwatch();
- /** The player assistant. */
- public final PlayerAssistant playerAssistant = new PlayerAssistant(this);
- /** The interface state. */
- public final InterfaceState interfaceState = new InterfaceState(this);
- /** The request manager which manages trading and dueling requests. */
- public final RequestManager requestManager = new RequestManager(this);
- /** The player's settings. */
- public final Settings settings = new Settings(this);
- /** The player relations. */
- public final PlayerRelation relation = new PlayerRelation(this);
- /** The account security. */
- public final AccountSecurity security = new AccountSecurity(this);
- /** The inventory. */
- public final Inventory inventory = new Inventory(this);
- /** The bank. */
- public final Bank bank = new Bank(this);
- /** The equipment. */
- public final Equipment equipment = new Equipment(this);
- /** The pre-loading gear. */
- public final PreloadingGear preloadingGear = new PreloadingGear(this);
- /** The price checker. */
- public final PriceChecker priceChecker = new PriceChecker(this);
- public final House house = new House(this);
- /** The trade container. */
- public final Trade trade = new Trade(this);
- /** The trade container. */
- public final TradeContainer tradeContainer = new TradeContainer(this);
- /** The slayer instance. */
- public Slayer slayer = new Slayer(this);
- /** The skulling. */
- public Skulling skulling = new Skulling(this);
- public QuestManager quest = new QuestManager(this);
- /** The spell casting. */
- public SpellCasting spellCasting = new SpellCasting(this);
- /** The bounty hunter. */
- public BountyHunter bountyHunter = new BountyHunter(this);
- /** The favorite teleports. */
- public List<Teleport> favoriteTeleport = new ArrayList<Teleport>();
- /** The achievement. */
- public HashMap<AchievementList, Integer> playerAchievements = new HashMap<AchievementList, Integer>(AchievementList.values().length) {
- private static final long serialVersionUID = 1842952445111093360L;
- {
- for (final AchievementList achievement : AchievementList.values()) {
- put(achievement, 0);
- }
- }
- };
- /** The player. */
- public Player(Optional<PlayerChannel> channel) {
- super(Configuration.DEFAULT_LOCATION);
- this.channel = channel;
- bot = false;
- }
- /** The player. */
- public Player(String username, Location location) {
- super(Configuration.DEFAULT_LOCATION);
- setLocation(location);
- setUsername(username);
- password = "";
- attr().set("NEW_PLAYER", false);
- this.channel = Optional.empty();
- bot = true;
- }
- /**
- * Sets the player's username. The username long will also be recalculated.
- *
- * @param username
- * The username to set.
- */
- public void setUsername(String username) {
- this.username = username;
- this.usernameLong = Utility.nameToLong(username);
- }
- /**
- * Checks if there is an update block.
- *
- * @return {@code True} if there is an update block.
- */
- public boolean hasCacheUpdateBlock() {
- return cachedUpdateBlock != null;
- }
- /**
- * Sends a packet to the client.
- *
- * @param out
- * The outgoing packet to send.
- */
- public void send(OutgoingPacket out) {
- out.encode(this);
- }
- /**
- * Resets the cache update block.
- */
- public void resetCachedUpdateBlock() {
- cachedUpdateBlock = null;
- }
- /**
- * Handles the player logging in of the server.
- */
- private final void login() {
- setId(-1);
- setTeleporting(true);
- setRegionChange(true);
- flag(UpdateFlag.APPEARANCE);
- getSkills().login();
- getAnimations().setPlayerAnimations();
- send(new SendRunEnergy());
- send(new SendPlayerDetails());
- send(new SendResetCameraPosition());
- send(new SendExpCounter(experienceCounter));
- send(new SendMessage(String.format("Welcome to %s.", Configuration.SERVER_NAME)));
- security.login();
- relation.login();
- settings.login();
- equipment.login();
- inventory.refresh();
- bountyHunter.login();
- playerAssistant.login();
- ClanManager.login(this);
- }
- /**
- * Handles the player logging out of the server.
- */
- public final void logout() {
- bountyHunter.logout();
- send(new SendLogout());
- }
- @Override
- public void register() {
- if (!isRegistered() && !World.PLAYERS.contains(this)) {
- setRegistered(World.PLAYERS.add(this));
- setLocation(getLocation());
- login();
- logger.info(String.format("[REGISTERED]: " + Utility.formatName(getName()) + " [" + lastHost + "]"));
- }
- }
- @Override
- public void unregister() {
- if (isRegistered()) {
- interfaceState.close();
- house.leave();
- ProfileSave.save(this);
- ClanManager.leave(this);
- relation.updateLists(false);
- World.PLAYERS.remove(destroy());
- logger.info(String.format("[UNREGISTERED]: " + Utility.formatName(getName()) + " [" + lastHost + "]"));
- }
- }
- @Override
- public void addToRegion(Region region) {
- region.addPlayer(this);
- }
- @Override
- public void removeFromRegion(Region region) {
- region.removePlayer(this);
- }
- @Override
- public void onStep() {
- if (Area.inWilderness(this)) {
- int modY = getLocation().getY() > 6400 ? getLocation().getY() - 6400 : getLocation().getY();
- wilderness = (((modY - 3521) / 8) + 1);
- send(new SendPlayerOption(PlayerOption.ATTACK, true));
- send(new SendPlayerOption(PlayerOption.DUEL_REQUEST, false, true));
- bountyHunter.enter();
- /* Bounty Hunter timer */
- } else if (!Area.inWilderness(this) && bountyHunter.target != null) {
- bountyHunter.leave();
- /* Bounty Hunter timer */
- } else if (bountyHunter.timer != -1) {
- bountyHunter.update();
- /* Duel Arena */
- } else if (Area.inDuelArena(this)) {
- send(new SendPlayerOption(PlayerOption.ATTACK, true, true));
- send(new SendPlayerOption(PlayerOption.DUEL_REQUEST, false));
- send(new SendWalkableInterface(201));
- /* Clear screen */
- } else if (!interfaceState.isClear()) {
- interfaceState.close();
- }
- }
- @Override
- public void onDeath() {
- move(Configuration.DEFAULT_LOCATION);
- send(new SendMessage("Oh dear, you are dead!"));
- skulling.unskull();
- bountyHunter.death();
- }
- @Override
- public String getName() {
- return Utility.formatName(username);
- }
- @Override
- public EntityType getType() {
- return EntityType.PLAYER;
- }
- @Override
- public boolean isValid() {
- if (!bot && !channel.isPresent()) {
- return false;
- }
- return super.isValid();
- }
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof Player) {
- Player other = (Player) obj;
- return (other.username == username && other.password == password) || (other.username.equalsIgnoreCase(username) && other.password.equalsIgnoreCase(password));
- }
- return obj == this;
- }
- @Override
- public int hashCode() {
- return Objects.hash(Utility.nameToLong(username));
- }
- @Override
- public String toString() {
- return "Player[index=" + getIndex() + ", username=" + getName() + ", location=" + getLocation() + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement