mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-06-26 18:14:50 +08:00
Compare commits
No commits in common. "af570654cba5521e7c0c345d623ae7c1da6eceb0" and "c2a260858c371c5c5a5ebd5a567e1840c7477df7" have entirely different histories.
af570654cb
...
c2a260858c
@ -117,6 +117,8 @@ public final class Grasscutter {
|
|||||||
Grasscutter.updateDayOfWeek();
|
Grasscutter.updateDayOfWeek();
|
||||||
ResourceLoader.loadAll();
|
ResourceLoader.loadAll();
|
||||||
ScriptLoader.init();
|
ScriptLoader.init();
|
||||||
|
EnergyManager.initialize();
|
||||||
|
DungeonChallenge.initialize();
|
||||||
|
|
||||||
// Initialize database.
|
// Initialize database.
|
||||||
DatabaseManager.initialize();
|
DatabaseManager.initialize();
|
||||||
@ -144,6 +146,9 @@ public final class Grasscutter {
|
|||||||
httpServer.addRouter(GachaHandler.class);
|
httpServer.addRouter(GachaHandler.class);
|
||||||
httpServer.addRouter(DocumentationServerHandler.class);
|
httpServer.addRouter(DocumentationServerHandler.class);
|
||||||
|
|
||||||
|
// TODO: find a better place?
|
||||||
|
StaminaManager.initialize();
|
||||||
|
|
||||||
// Start servers.
|
// Start servers.
|
||||||
var runMode = SERVER.runMode;
|
var runMode = SERVER.runMode;
|
||||||
if (runMode == ServerRunMode.HYBRID) {
|
if (runMode == ServerRunMode.HYBRID) {
|
||||||
|
@ -92,7 +92,7 @@ public class DungeonChallenge extends WorldChallenge {
|
|||||||
settle();
|
settle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void settle() {
|
private void settle() {
|
||||||
if(!stage){
|
if(!stage){
|
||||||
getScene().getDungeonSettleObservers().forEach(o -> o.onDungeonSettle(getScene()));
|
getScene().getDungeonSettleObservers().forEach(o -> o.onDungeonSettle(getScene()));
|
||||||
@ -100,7 +100,7 @@ public class DungeonChallenge extends WorldChallenge {
|
|||||||
new ScriptArgs(this.isSuccess() ? 1 : 0));
|
new ScriptArgs(this.isSuccess() ? 1 : 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<GameItem> rollRewards(boolean useCondensed) {
|
private List<GameItem> rollRewards(boolean useCondensed) {
|
||||||
List<GameItem> rewards = new ArrayList<>();
|
List<GameItem> rewards = new ArrayList<>();
|
||||||
int dungeonId = this.getScene().getDungeonData().getId();
|
int dungeonId = this.getScene().getDungeonData().getId();
|
||||||
@ -150,7 +150,7 @@ public class DungeonChallenge extends WorldChallenge {
|
|||||||
for (ItemParamData param : getScene().getDungeonData().getRewardPreview().getPreviewItems()) {
|
for (ItemParamData param : getScene().getDungeonData().getRewardPreview().getPreviewItems()) {
|
||||||
rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
|
rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rewards;
|
return rewards;
|
||||||
}
|
}
|
||||||
@ -162,12 +162,12 @@ public class DungeonChallenge extends WorldChallenge {
|
|||||||
if (!isSuccess() || dungeonData == null || dungeonData.getRewardPreview() == null || dungeonData.getRewardPreview().getPreviewItems().length == 0) {
|
if (!isSuccess() || dungeonData == null || dungeonData.getRewardPreview() == null || dungeonData.getRewardPreview().getPreviewItems().length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Already rewarded
|
// Already rewarded
|
||||||
if (getRewardedPlayers().contains(player.getUid())) {
|
if (getRewardedPlayers().contains(player.getUid())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get rewards.
|
// Get rewards.
|
||||||
List<GameItem> rewards = new ArrayList<>();
|
List<GameItem> rewards = new ArrayList<>();
|
||||||
|
|
||||||
@ -202,11 +202,11 @@ public class DungeonChallenge extends WorldChallenge {
|
|||||||
// Roll rewards.
|
// Roll rewards.
|
||||||
rewards.addAll(this.rollRewards(false));
|
rewards.addAll(this.rollRewards(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add rewards to player and send notification.
|
// Add rewards to player and send notification.
|
||||||
player.getInventory().addItems(rewards, ActionReason.DungeonStatueDrop);
|
player.getInventory().addItems(rewards, ActionReason.DungeonStatueDrop);
|
||||||
player.sendPacket(new PacketGadgetAutoPickDropInfoNotify(rewards));
|
player.sendPacket(new PacketGadgetAutoPickDropInfoNotify(rewards));
|
||||||
|
|
||||||
getRewardedPlayers().add(player.getUid());
|
getRewardedPlayers().add(player.getUid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,36 +14,36 @@ public class GachaBanner {
|
|||||||
private String prefabPath;
|
private String prefabPath;
|
||||||
private String previewPrefabPath;
|
private String previewPrefabPath;
|
||||||
private String titlePath;
|
private String titlePath;
|
||||||
private int costItemId = 0;
|
private final int costItemId = 0;
|
||||||
private int costItemAmount = 1;
|
private final int costItemAmount = 1;
|
||||||
private int costItemId10 = 0;
|
private final int costItemId10 = 0;
|
||||||
private int costItemAmount10 = 10;
|
private final int costItemAmount10 = 10;
|
||||||
private int beginTime;
|
private int beginTime;
|
||||||
private int endTime;
|
private int endTime;
|
||||||
private int sortId;
|
private int sortId;
|
||||||
private int[] rateUpItems4 = {};
|
private final int[] rateUpItems4 = {};
|
||||||
private int[] rateUpItems5 = {};
|
private final int[] rateUpItems5 = {};
|
||||||
private int[] fallbackItems3 = {11301, 11302, 11306, 12301, 12302, 12305, 13303, 14301, 14302, 14304, 15301, 15302, 15304};
|
private final int[] fallbackItems3 = {11301, 11302, 11306, 12301, 12302, 12305, 13303, 14301, 14302, 14304, 15301, 15302, 15304};
|
||||||
private int[] fallbackItems4Pool1 = {1014, 1020, 1023, 1024, 1025, 1027, 1031, 1032, 1034, 1036, 1039, 1043, 1044, 1045, 1048, 1053, 1055, 1056, 1064};
|
private final int[] fallbackItems4Pool1 = {1014, 1020, 1023, 1024, 1025, 1027, 1031, 1032, 1034, 1036, 1039, 1043, 1044, 1045, 1048, 1053, 1055, 1056, 1064};
|
||||||
private int[] fallbackItems4Pool2 = {11401, 11402, 11403, 11405, 12401, 12402, 12403, 12405, 13401, 13407, 14401, 14402, 14403, 14409, 15401, 15402, 15403, 15405};
|
private final int[] fallbackItems4Pool2 = {11401, 11402, 11403, 11405, 12401, 12402, 12403, 12405, 13401, 13407, 14401, 14402, 14403, 14409, 15401, 15402, 15403, 15405};
|
||||||
private int[] fallbackItems5Pool1 = {1003, 1016, 1042, 1035, 1041};
|
private final int[] fallbackItems5Pool1 = {1003, 1016, 1042, 1035, 1041};
|
||||||
private int[] fallbackItems5Pool2 = {11501, 11502, 12501, 12502, 13502, 13505, 14501, 14502, 15501, 15502};
|
private final int[] fallbackItems5Pool2 = {11501, 11502, 12501, 12502, 13502, 13505, 14501, 14502, 15501, 15502};
|
||||||
private boolean removeC6FromPool = false;
|
private final boolean removeC6FromPool = false;
|
||||||
private boolean autoStripRateUpFromFallback = true;
|
private final boolean autoStripRateUpFromFallback = true;
|
||||||
private int[][] weights4 = {{1, 510}, {8, 510}, {10, 10000}};
|
private final int[][] weights4 = {{1, 510}, {8, 510}, {10, 10000}};
|
||||||
private int[][] weights5 = {{1, 75}, {73, 150}, {90, 10000}};
|
private final int[][] weights5 = {{1, 75}, {73, 150}, {90, 10000}};
|
||||||
private int[][] poolBalanceWeights4 = {{1, 255}, {17, 255}, {21, 10455}};
|
private final int[][] poolBalanceWeights4 = {{1, 255}, {17, 255}, {21, 10455}};
|
||||||
private int[][] poolBalanceWeights5 = {{1, 30}, {147, 150}, {181, 10230}};
|
private final int[][] poolBalanceWeights5 = {{1, 30}, {147, 150}, {181, 10230}};
|
||||||
private int eventChance4 = 50; // Chance to win a featured event item
|
private final int eventChance4 = 50; // Chance to win a featured event item
|
||||||
private int eventChance5 = 50; // Chance to win a featured event item
|
private final int eventChance5 = 50; // Chance to win a featured event item
|
||||||
private BannerType bannerType = BannerType.STANDARD;
|
private final BannerType bannerType = BannerType.STANDARD;
|
||||||
|
|
||||||
// Kinda wanna deprecate these but they're in people's configs
|
// Kinda wanna deprecate these but they're in people's configs
|
||||||
private int[] rateUpItems1 = {};
|
private final int[] rateUpItems1 = {};
|
||||||
private int[] rateUpItems2 = {};
|
private final int[] rateUpItems2 = {};
|
||||||
private int eventChance = -1;
|
private final int eventChance = -1;
|
||||||
private int costItem = 0;
|
private final int costItem = 0;
|
||||||
private int wishMaxProgress = 2;
|
private final int wishMaxProgress = 2;
|
||||||
|
|
||||||
public int getGachaType() {
|
public int getGachaType() {
|
||||||
return this.gachaType;
|
return this.gachaType;
|
||||||
@ -71,7 +71,8 @@ public class GachaBanner {
|
|||||||
|
|
||||||
public ItemParamData getCost(int numRolls) {
|
public ItemParamData getCost(int numRolls) {
|
||||||
return switch (numRolls) {
|
return switch (numRolls) {
|
||||||
case 10 -> new ItemParamData((this.costItemId10 > 0) ? this.costItemId10 : this.getCostItem(), this.costItemAmount10);
|
case 10 ->
|
||||||
|
new ItemParamData((this.costItemId10 > 0) ? this.costItemId10 : this.getCostItem(), this.costItemAmount10);
|
||||||
default -> new ItemParamData(this.getCostItem(), this.costItemAmount * numRolls);
|
default -> new ItemParamData(this.getCostItem(), this.costItemAmount * numRolls);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -2,23 +2,8 @@ package emu.grasscutter.server.event.game;
|
|||||||
|
|
||||||
import emu.grasscutter.server.event.types.ServerEvent;
|
import emu.grasscutter.server.event.types.ServerEvent;
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
public final class ServerTickEvent extends ServerEvent {
|
public final class ServerTickEvent extends ServerEvent {
|
||||||
private final Instant start, end;
|
public ServerTickEvent() {
|
||||||
|
|
||||||
public ServerTickEvent(Instant start, Instant end) {
|
|
||||||
super(Type.GAME);
|
super(Type.GAME);
|
||||||
|
|
||||||
this.start = start;
|
|
||||||
this.end = end;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getTickStart() {
|
|
||||||
return this.start;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Instant getTickEnd() {
|
|
||||||
return this.end;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,12 @@ import emu.grasscutter.game.Account;
|
|||||||
import emu.grasscutter.game.combine.CombineManger;
|
import emu.grasscutter.game.combine.CombineManger;
|
||||||
import emu.grasscutter.game.drop.DropManager;
|
import emu.grasscutter.game.drop.DropManager;
|
||||||
import emu.grasscutter.game.dungeons.DungeonManager;
|
import emu.grasscutter.game.dungeons.DungeonManager;
|
||||||
import emu.grasscutter.game.dungeons.challenge.DungeonChallenge;
|
|
||||||
import emu.grasscutter.game.expedition.ExpeditionManager;
|
import emu.grasscutter.game.expedition.ExpeditionManager;
|
||||||
import emu.grasscutter.game.gacha.GachaManager;
|
import emu.grasscutter.game.gacha.GachaManager;
|
||||||
import emu.grasscutter.game.managers.InventoryManager;
|
import emu.grasscutter.game.managers.InventoryManager;
|
||||||
import emu.grasscutter.game.managers.MultiplayerManager;
|
import emu.grasscutter.game.managers.MultiplayerManager;
|
||||||
import emu.grasscutter.game.managers.chat.ChatManager;
|
import emu.grasscutter.game.managers.chat.ChatManager;
|
||||||
import emu.grasscutter.game.managers.chat.ChatManagerHandler;
|
import emu.grasscutter.game.managers.chat.ChatManagerHandler;
|
||||||
import emu.grasscutter.game.managers.energy.EnergyManager;
|
|
||||||
import emu.grasscutter.game.managers.stamina.StaminaManager;
|
|
||||||
import emu.grasscutter.game.player.Player;
|
import emu.grasscutter.game.player.Player;
|
||||||
import emu.grasscutter.game.quest.ServerQuestHandler;
|
import emu.grasscutter.game.quest.ServerQuestHandler;
|
||||||
import emu.grasscutter.game.shop.ShopManager;
|
import emu.grasscutter.game.shop.ShopManager;
|
||||||
@ -29,7 +26,6 @@ import emu.grasscutter.server.event.game.ServerTickEvent;
|
|||||||
import emu.grasscutter.server.event.internal.ServerStartEvent;
|
import emu.grasscutter.server.event.internal.ServerStartEvent;
|
||||||
import emu.grasscutter.server.event.internal.ServerStopEvent;
|
import emu.grasscutter.server.event.internal.ServerStopEvent;
|
||||||
import emu.grasscutter.server.event.types.ServerEvent;
|
import emu.grasscutter.server.event.types.ServerEvent;
|
||||||
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
|
|
||||||
import emu.grasscutter.task.TaskMap;
|
import emu.grasscutter.task.TaskMap;
|
||||||
import kcp.highway.ChannelConfig;
|
import kcp.highway.ChannelConfig;
|
||||||
import kcp.highway.KcpServer;
|
import kcp.highway.KcpServer;
|
||||||
@ -37,7 +33,6 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.time.Instant;
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -51,8 +46,6 @@ public final class GameServer extends KcpServer {
|
|||||||
private final GameServerPacketHandler packetHandler;
|
private final GameServerPacketHandler packetHandler;
|
||||||
@Getter
|
@Getter
|
||||||
private final ServerQuestHandler questHandler;
|
private final ServerQuestHandler questHandler;
|
||||||
@Getter
|
|
||||||
private final ServerTaskScheduler scheduler;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final Map<Integer, Player> players;
|
private final Map<Integer, Player> players;
|
||||||
@ -108,7 +101,6 @@ public final class GameServer extends KcpServer {
|
|||||||
this.address = address;
|
this.address = address;
|
||||||
this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
|
this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
|
||||||
this.questHandler = new ServerQuestHandler();
|
this.questHandler = new ServerQuestHandler();
|
||||||
this.scheduler = new ServerTaskScheduler();
|
|
||||||
this.players = new ConcurrentHashMap<>();
|
this.players = new ConcurrentHashMap<>();
|
||||||
this.worlds = Collections.synchronizedSet(new HashSet<>());
|
this.worlds = Collections.synchronizedSet(new HashSet<>());
|
||||||
|
|
||||||
@ -125,11 +117,6 @@ public final class GameServer extends KcpServer {
|
|||||||
this.combineManger = new CombineManger(this);
|
this.combineManger = new CombineManger(this);
|
||||||
this.towerScheduleManager = new TowerScheduleManager(this);
|
this.towerScheduleManager = new TowerScheduleManager(this);
|
||||||
this.worldDataManager = new WorldDataManager(this);
|
this.worldDataManager = new WorldDataManager(this);
|
||||||
|
|
||||||
StaminaManager.initialize();
|
|
||||||
EnergyManager.initialize();
|
|
||||||
DungeonChallenge.initialize();
|
|
||||||
|
|
||||||
// Hook into shutdown event.
|
// Hook into shutdown event.
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
|
||||||
}
|
}
|
||||||
@ -201,9 +188,6 @@ public final class GameServer extends KcpServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void onTick() {
|
public synchronized void onTick() {
|
||||||
var tickStart = Instant.now();
|
|
||||||
|
|
||||||
// Tick worlds.
|
|
||||||
Iterator<World> it = this.getWorlds().iterator();
|
Iterator<World> it = this.getWorlds().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
World world = it.next();
|
World world = it.next();
|
||||||
@ -215,16 +199,11 @@ public final class GameServer extends KcpServer {
|
|||||||
world.onTick();
|
world.onTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick players.
|
|
||||||
for (Player player : this.getPlayers().values()) {
|
for (Player player : this.getPlayers().values()) {
|
||||||
player.onTick();
|
player.onTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick scheduler.
|
ServerTickEvent event = new ServerTickEvent();
|
||||||
this.getScheduler().runTasks();
|
|
||||||
|
|
||||||
// Call server tick event.
|
|
||||||
ServerTickEvent event = new ServerTickEvent(tickStart, Instant.now());
|
|
||||||
event.call();
|
event.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,98 +0,0 @@
|
|||||||
package emu.grasscutter.server.scheduler;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A server task that should be run asynchronously.
|
|
||||||
*/
|
|
||||||
public final class AsyncServerTask implements Runnable {
|
|
||||||
/* The runnable to run. */
|
|
||||||
private final Runnable task;
|
|
||||||
/* This ID is assigned by the scheduler. */
|
|
||||||
@Getter private final int taskId;
|
|
||||||
/* The result callback to run. */
|
|
||||||
@Nullable private final Runnable callback;
|
|
||||||
|
|
||||||
/* Has the task already been started? */
|
|
||||||
private boolean started = false;
|
|
||||||
/* Has the task finished execution? */
|
|
||||||
private boolean finished = false;
|
|
||||||
/* The result produced in the async task. */
|
|
||||||
@Nullable private Object result = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For tasks without a callback.
|
|
||||||
* @param task The task to run.
|
|
||||||
*/
|
|
||||||
public AsyncServerTask(Runnable task, int taskId) {
|
|
||||||
this(task, null, taskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For tasks with a callback.
|
|
||||||
* @param task The task to run.
|
|
||||||
* @param callback The task to run after the task is complete.
|
|
||||||
*/
|
|
||||||
public AsyncServerTask(Runnable task, @Nullable Runnable callback, int taskId) {
|
|
||||||
this.task = task;
|
|
||||||
this.callback = callback;
|
|
||||||
this.taskId = taskId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the state of the task.
|
|
||||||
* @return True if the task has been started, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean hasStarted() {
|
|
||||||
return this.started;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the state of the task.
|
|
||||||
* @return True if the task has finished execution, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean isFinished() {
|
|
||||||
return this.finished;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the task.
|
|
||||||
*/
|
|
||||||
@Override public void run() {
|
|
||||||
// Declare the task as started.
|
|
||||||
this.started = true;
|
|
||||||
|
|
||||||
// Run the runnable.
|
|
||||||
this.task.run();
|
|
||||||
|
|
||||||
// Declare the task as finished.
|
|
||||||
this.finished = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the callback.
|
|
||||||
*/
|
|
||||||
public void complete() {
|
|
||||||
// Run the callback.
|
|
||||||
if(this.callback != null)
|
|
||||||
this.callback.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the result of the async task.
|
|
||||||
* @param result The result of the async task.
|
|
||||||
*/
|
|
||||||
public void setResult(@Nullable Object result) {
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the set result of the async task.
|
|
||||||
* @return The result, or null if it has not been set.
|
|
||||||
*/
|
|
||||||
@Nullable public Object getResult() {
|
|
||||||
return this.result;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
package emu.grasscutter.server.scheduler;
|
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class works the same as a runnable, except with more information.
|
|
||||||
*/
|
|
||||||
public final class ServerTask implements Runnable {
|
|
||||||
/* The runnable to run. */
|
|
||||||
private final Runnable runnable;
|
|
||||||
/* This ID is assigned by the scheduler. */
|
|
||||||
@Getter private final int taskId;
|
|
||||||
/* The period at which the task should be run. */
|
|
||||||
/* The delay between the first execute. */
|
|
||||||
private final int period, delay;
|
|
||||||
|
|
||||||
public ServerTask(Runnable runnable, int taskId, int period, int delay) {
|
|
||||||
this.runnable = runnable;
|
|
||||||
this.taskId = taskId;
|
|
||||||
this.period = period;
|
|
||||||
this.delay = delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The amount of times the task has been run. */
|
|
||||||
@Getter private int ticks = 0;
|
|
||||||
/* Should the check consider delay? */
|
|
||||||
private boolean considerDelay = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cancels the task from running the next time.
|
|
||||||
*/
|
|
||||||
public void cancel() {
|
|
||||||
Grasscutter.getGameServer().getScheduler().cancelTask(this.taskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the task should run at the current tick.
|
|
||||||
* @return True if the task should run, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean shouldRun() {
|
|
||||||
if(this.delay != -1 && this.considerDelay) {
|
|
||||||
this.considerDelay = false;
|
|
||||||
return this.ticks == this.delay;
|
|
||||||
} else if(this.period != -1)
|
|
||||||
return this.ticks % this.period == 0;
|
|
||||||
else return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the task should be canceled.
|
|
||||||
* @return True if the task should be canceled, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean shouldCancel() {
|
|
||||||
return this.period == -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the task.
|
|
||||||
*/
|
|
||||||
@Override public void run() {
|
|
||||||
// Run the runnable.
|
|
||||||
this.runnable.run();
|
|
||||||
// Increase tick count.
|
|
||||||
this.ticks++;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,148 +0,0 @@
|
|||||||
package emu.grasscutter.server.scheduler;
|
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class to manage all time-based tasks scheduled on the server.
|
|
||||||
* This handles both synchronous and asynchronous tasks.
|
|
||||||
*
|
|
||||||
* Developers note: A server tick is ONE REAL-TIME SECOND.
|
|
||||||
*/
|
|
||||||
public final class ServerTaskScheduler {
|
|
||||||
/* A map to contain all running tasks. */
|
|
||||||
private final ConcurrentHashMap<Integer, ServerTask> tasks
|
|
||||||
= new ConcurrentHashMap<>();
|
|
||||||
/* A map to contain all async tasks. */
|
|
||||||
private final ConcurrentHashMap<Integer, AsyncServerTask> asyncTasks
|
|
||||||
= new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/* The ID assigned to the next runnable. */
|
|
||||||
private int nextTaskId = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ran every server tick.
|
|
||||||
* Attempts to run all scheduled tasks.
|
|
||||||
* This method is synchronous and will block until all tasks are complete.
|
|
||||||
*/
|
|
||||||
public void runTasks() {
|
|
||||||
// Skip if there are no tasks.
|
|
||||||
if(this.tasks.size() == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Run all tasks.
|
|
||||||
for(ServerTask task : this.tasks.values()) {
|
|
||||||
// Check if the task should run.
|
|
||||||
if (task.shouldRun()) {
|
|
||||||
// Run the task.
|
|
||||||
task.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the task should be canceled.
|
|
||||||
if (task.shouldCancel()) {
|
|
||||||
// Cancel the task.
|
|
||||||
this.cancelTask(task.getTaskId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run all async tasks.
|
|
||||||
for(AsyncServerTask task : this.asyncTasks.values()) {
|
|
||||||
if(!task.hasStarted()) {
|
|
||||||
// Create a thread for the task.
|
|
||||||
Thread thread = new Thread(task);
|
|
||||||
// Start the thread.
|
|
||||||
thread.start();
|
|
||||||
} else if(task.isFinished()) {
|
|
||||||
// Cancel the task.
|
|
||||||
this.asyncTasks.remove(task.getTaskId());
|
|
||||||
// Run the task's callback.
|
|
||||||
task.complete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a task from the scheduler.
|
|
||||||
* @param taskId The ID of the task to get.
|
|
||||||
* @return The task, or null if it does not exist.
|
|
||||||
*/
|
|
||||||
public ServerTask getTask(int taskId) {
|
|
||||||
return this.tasks.get(taskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an async task from the scheduler.
|
|
||||||
* @param taskId The ID of the task to get.
|
|
||||||
* @return The task, or null if it does not exist.
|
|
||||||
*/
|
|
||||||
public AsyncServerTask getAsyncTask(int taskId) {
|
|
||||||
return this.asyncTasks.get(taskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes a task from the scheduler.
|
|
||||||
* @param taskId The ID of the task to remove.
|
|
||||||
*/
|
|
||||||
public void cancelTask(int taskId) {
|
|
||||||
this.tasks.remove(taskId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedules a task to be run on a separate thread.
|
|
||||||
* The task runs on the next server tick.
|
|
||||||
* @param runnable The runnable to run.
|
|
||||||
* @return The ID of the task.
|
|
||||||
*/
|
|
||||||
public int scheduleAsyncTask(Runnable runnable) {
|
|
||||||
// Get the next task ID.
|
|
||||||
var taskId = this.nextTaskId++;
|
|
||||||
// Create a new task.
|
|
||||||
this.asyncTasks.put(taskId, new AsyncServerTask(runnable, taskId));
|
|
||||||
// Return the task ID.
|
|
||||||
return taskId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedules a task to be run on the next server tick.
|
|
||||||
* @param runnable The runnable to run.
|
|
||||||
* @return The ID of the task.
|
|
||||||
*/
|
|
||||||
public int scheduleTask(Runnable runnable) {
|
|
||||||
return this.scheduleDelayedRepeatingTask(runnable, -1, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedules a task to be run after the amount of ticks has passed.
|
|
||||||
* @param runnable The runnable to run.
|
|
||||||
* @param delay The amount of ticks to wait before running.
|
|
||||||
* @return The ID of the task.
|
|
||||||
*/
|
|
||||||
public int scheduleDelayedTask(Runnable runnable, int delay) {
|
|
||||||
return this.scheduleDelayedRepeatingTask(runnable, -1, delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedules a task to be run every amount of ticks.
|
|
||||||
* @param runnable The runnable to run.
|
|
||||||
* @param period The amount of ticks to wait before running again.
|
|
||||||
* @return The ID of the task.
|
|
||||||
*/
|
|
||||||
public int scheduleRepeatingTask(Runnable runnable, int period) {
|
|
||||||
return this.scheduleDelayedRepeatingTask(runnable, period, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedules a task to be run after the amount of ticks has passed.
|
|
||||||
* @param runnable The runnable to run.
|
|
||||||
* @param period The amount of ticks to wait before running again.
|
|
||||||
* @param delay The amount of ticks to wait before running the first time.
|
|
||||||
* @return The ID of the task.
|
|
||||||
*/
|
|
||||||
public int scheduleDelayedRepeatingTask(Runnable runnable, int period, int delay) {
|
|
||||||
// Get the next task ID.
|
|
||||||
var taskId = this.nextTaskId++;
|
|
||||||
// Create a new task.
|
|
||||||
this.tasks.put(taskId, new ServerTask(runnable, taskId, period, delay));
|
|
||||||
// Return the task ID.
|
|
||||||
return taskId;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user