1package com.runehive.game.world;
3import com.runehive.Config;
4import com.runehive.content.activity.impl.pestcontrol.PestControlLobby;
5import com.runehive.content.bot.PlayerBot;
6import com.runehive.content.clanchannel.ClanRepository;
7import com.runehive.content.tradingpost.TradingPost;
8import com.runehive.game.Graphic;
9import com.runehive.game.Projectile;
10import com.runehive.game.ProjectileTest;
11import com.runehive.game.event.bus.DataBus;
12import com.runehive.game.event.listener.WorldEventListener;
13import com.runehive.game.task.Task;
14import com.runehive.game.task.TaskDeadlockWatcher;
15import com.runehive.game.task.TaskManager;
16import com.runehive.game.task.impl.PlayerRemovalTask;
17import com.runehive.game.task.impl.SystemUpdateEvent;
18import com.runehive.game.world.entity.MobList;
19import com.runehive.game.world.entity.mob.Mob;
20import com.runehive.game.world.entity.mob.npc.Npc;
21import com.runehive.game.world.entity.mob.player.Player;
22import com.runehive.game.world.entity.mob.player.PlayerRight;
23import com.runehive.game.world.entity.mob.player.persist.PlayerSerializer;
24import com.runehive.game.world.object.GameObject;
25import com.runehive.game.world.position.Area;
26import com.runehive.game.world.position.Position;
27import com.runehive.game.world.region.Region;
28import com.runehive.game.world.region.RegionManager;
29import com.runehive.net.packet.out.*;
30import com.runehive.util.GameSaver;
31import com.runehive.util.Utility;
32import org.apache.logging.log4j.LogManager;
33import org.apache.logging.log4j.Logger;
36import java.util.concurrent.ConcurrentLinkedQueue;
37import java.util.concurrent.atomic.AtomicBoolean;
38import java.util.function.Predicate;
48 private static final Logger
logger = LogManager.getLogger(
World.class);
57 private final Queue<Player>
logins =
new ConcurrentLinkedQueue<>();
60 private final Queue<Player>
logouts =
new ConcurrentLinkedQueue<>();
72 public static final AtomicBoolean
update =
new AtomicBoolean(
false);
97 public static void save() {
98 System.out.println(
"Saving osroyale...");
100 logger.info(
"All players were successfully saved.");
104 logger.info(
"All clans were successfully saved.");
107 logger.info(
"All trading post history was successfully saved");
109 logger.info(
"All game data were successfully saved.");
117 get().
players.stream().forEach(it -> it.send(
new SendGameMessage(0, time / 100,
"System update" +
" in:")));
123 System.out.println(
"shutting down...");
139 if (player ==
null ||
get().
logouts.contains(player)) {
147 public static Optional<Player>
search(String name) {
149 if (player ==
null) {
153 if (player.getUsername().equalsIgnoreCase(name)) {
154 return Optional.of(player);
157 return Optional.empty();
162 if (player ==
null) {
166 if (player.getUsername().equalsIgnoreCase(name)) {
167 return Optional.of(player);
172 if (player ==
null) {
176 if (player.getUsername().equalsIgnoreCase(name)) {
177 return Optional.of(player);
180 return Optional.empty();
185 if (Objects.equals(player.usernameLong, usernameHash))
186 return Optional.of(player);
188 return Optional.empty();
193 if (Objects.equals(player.getUsername(), username))
194 return Optional.of(player);
196 return Optional.empty();
202 if (player.getIndex() == index)
203 return Optional.of(player);
205 return Optional.empty();
211 if (npc.getIndex() == index)
212 return Optional.of(npc);
214 return Optional.empty();
226 if (!condition.test(player))
236 }
catch (Exception ex) {
237 logger.error(
"Error sequencing task manager", ex);
241 }
catch (Exception ex) {
242 logger.error(
"Error sequencing pest control.", ex);
252 public static void schedule(
int delay, Runnable runnable) {
255 public void execute() {
270 if (player.instance != instance)
286 if (player.instance !=
object.getInstancedHeight())
297 if (player.instance != instance)
309 int sourceX = source.
getX() + source.
width() / 2, sourceY = source.
getY() + source.
length() / 2;
310 int targetX = target.
getX() + target.
width() / 2, targetY = target.
getY() + target.
length() / 2;
311 byte offsetX = (byte) (targetX - sourceX);
312 byte offsetY = (byte) (targetY - sourceY);
316 if (source.
instance != player.instance)
320 player.send(
new SendProjectile(projectile, center, lockon, offsetX, offsetY));
326 int sourceX = source.
getX(), sourceY = source.
getY();
327 int targetX = target.
getX() + target.
width() / 2, targetY = target.
getY() + target.
length() / 2;
328 byte offsetX = (byte) (targetX - sourceX);
329 byte offsetY = (byte) (targetY - sourceY);
335 player.send(
new SendProjectile(projectile, center, lockon, offsetX, offsetY));
341 int sourceX = source.
getX() + source.
width() / 2, sourceY = source.
getY() + source.
length() / 2;
343 byte offsetX = (byte) (target.
getX() - sourceX);
344 byte offsetY = (byte) (target.
getY() - sourceY);
346 if (source.
instance != player.instance)
350 player.send(
new SendProjectile(projectile, center, -1, offsetX, offsetY));
355 int sourceX = source.
getX(), sourceY = source.
getY();
357 byte offsetX = (byte) (target.
getX() - sourceX);
358 byte offsetY = (byte) (target.
getY() - sourceY);
362 player.send(
new SendProjectile(projectile, center, -1, offsetX, offsetY));
367 if (projectile ==
null) {
372 if (source ==
null || target ==
null) {
377 int distance =
entity.getPosition().getChevDistance(target);
379 if (distance <= 60) {
383 if (player ==
null) {
388 player.send(
new SendProjectileTestPacket(source, delta, projectile.
getSlope(), projectile.
getSpeed(), projectile.
getProjectileId(), projectile.
getStartHeight(), projectile.
getEndHeight(), projectile.
getLockon(), projectile.
getDelay(), creatorSize, projectile.
getStartDistanceOffset()));
397 if (com.runehive.Config.DISABLE_BROADCASTS)
return;
400 player.message(messages);
405 public static void sendMessage(String message, Predicate<Player> filter) {
406 if (com.runehive.Config.DISABLE_BROADCASTS)
return;
408 if (player !=
null && filter.test(player))
409 player.message(message);
415 player.message(messages);
420 public static void sendBroadcast(
int time, String message,
boolean countdown) {
421 get().
players.stream().forEach($it -> {
435 if (player ==
null) {
441 }
catch (Exception ex) {
442 logger.error(String.format(
"error registering %s", player), ex);
456 if (player ==
null) {
475 if (victim.
isNpc()) {
522 List<Player> staff =
new ArrayList<>();
540 public static void cancelTask(Object attachment,
boolean logout) {
The class that contains setting-related constants for the server.
static final int LOGIN_THESHOLD
The amount of players that can be logged in, in a single cycle.
static final int MAX_NPCS
The maximum amount of npcs that can be held within the game world.
static final int LOGOUT_THESHOLD
The amount of players that can logout in a single cycle.
static final int MAX_PLAYERS
The maximum amount of players that can be held within the game world.
A PestControlLobby manages all the PestControlGame that are active.
static void sequence()
Sequences all the active PestControlGame.
static final AtomicInteger BOT_COUNT
The repository containing all the clans and their corresponding data.
static void saveAllActiveClans()
static void saveRecentHistory()
static void saveAllItemHistory()
Represents a single graphic that can be used by entities.
int getStartDistanceOffset()
int getHitDelay(int distance)
static DataBus getInstance()
Gets the singleton object.
A game representing a cyclic unit of work.
The class that handles scheduling tasks, and processing them.
static final AtomicBoolean update
static Optional< Player > getPlayerBySlot(int index)
Gets a player by index.
static void sendProjectile(Position source, Mob target, Projectile projectile)
static final World WORLD
The world instance.
static void sendProjectile(Position source, Position target, Projectile projectile)
static void kickPlayer(Predicate< Player > condition)
final RegionManager regionManager
The region manager.
static void kickPlayer(Player other)
static void schedule(Task task)
Submits a new event.
static final Logger logger
final TaskManager taskManager
The task manager.
static void shutdown()
Shuts down the server.
final MobList< Player > players
The players registered in this world.
static int getPlayerCount()
Gets the amount of valid players online.
static void sendProjectile(Mob source, Mob target, Projectile projectile)
Sends a world projectile.
static void sendStaffMessage(String... messages)
static void sendGraphic(Graphic graphic, Position position, int instance)
Sends a graphic to the world.
final Queue< Player > logouts
The Players waiting to onLogout.
static void sendProjectile(Projectile projectile, Position position, int instance, int lock, byte offsetX, byte offsetY)
Sends a world projectile.
static int getStaffCount()
int getWildernessResourcePlayers()
static void schedule(Runnable runnable)
Executes a runnable on a 1 tick delay.
static void sendBroadcast(int time, String message, boolean countdown)
Sends a game message.
TaskManager getTaskManager()
static Optional< Player > search(String name)
Gets a player by name.
static void sendProjectile(Mob source, Position target, Projectile projectile)
Sends a world projectile.
void sequence()
Handles the world sequencing.
static void cancelTask(Object attachment)
Queue< Player > getLogouts()
static Optional< Player > getPlayerByHash(long usernameHash)
static void schedule(int delay, Runnable runnable)
Executes a runnable on a delay.
static MobList< Npc > getNpcs()
final MobList< Npc > npcs
The npcs registered in this world.
static int executeProjectile(Mob entity, ProjectileTest projectile)
static Optional< Npc > getNpcBySlot(int index)
Gets an npc by index.
static void queueLogin(Player player)
Handles queueing the player logins.
static void sendMessage(String... messages)
Sends a global message.
Queue< Player > getLogins()
static final DataBus dataBus
static void sendGraphic(Graphic graphic, Position position)
static DataBus getDataBus()
final Thread taskDeadlockWatcherThread
The thread to watch task deadlocks on.
static void update(int time)
Updates the server.
final TaskDeadlockWatcher taskDeadlockWatcher
The task deadlock watcher relies on the TaskManager.
static Optional< Player > searchAll(String name)
static void cancelTask(Object attachment, boolean logout)
static Optional< Player > getPlayerByName(String username)
static MobList< Player > getPlayers()
static void save()
Saves all the game data.
final Queue< Player > logins
The Players waiting to login.
static RegionManager getRegions()
static void sendKillFeed(final Mob killer, final Mob victim)
Sends a kill feed notification.
static void queueLogout(Player player)
Handles queueing the player logouts.
static void sendMessage(String message, Predicate< Player > filter)
Sends a global message with an exception.
static List< Player > getStaff()
Gets the staff players currently online.
static void sendObjectAnimation(int animation, GameObject object)
Sends a world object animation.
static int getWildernessCount()
An Iterable implementation acting as a repository that holds instances of Entitys.
final boolean isNpc()
Check if an entity is an npc.
Represents a non-player character in the in-game world.
final NpcAssistant npcAssistant
This class represents a character controlled by a player.
Optional< GameSession > getSession()
void send(OutgoingPacket encoder)
void register()
Registers an entity to the World.
static void save(Player player)
Handles checking if mobs are in a certain area.
static boolean inWilderness(Position position)
static boolean inWildernessResource(Interactable entity)
Represents a single tile on the game world.
Position getDelta(Position location, Position other)
int getY()
Gets the absolute y coordinate.
boolean isViewableFrom(Position other)
int getX()
Gets the absolute x coordinate.
Represents a single region.
static final int VIEW_DISTANCE
Manages the world regions.
The OutgoingPacket responsible for sending game messages.
The OutgoingPacket that sends a message to a Players chatbox in the client.
Created by Daniel on 2017-11-05.
Handles miscellaneous methods.
static String capitalizeSentence(final String string)
Capitalize each letter after .
static boolean withinDistance(Interactable source, Interactable target, int radius)
Holds all the player right data.
static boolean isModerator(Player player)
Checks if the player is a management member.
Represents a game object.