mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-06-26 10:04:58 +08:00
Compare commits
3 Commits
e9b3516854
...
5b76ef7dfa
Author | SHA1 | Date | |
---|---|---|---|
|
5b76ef7dfa | ||
|
897a38b45b | ||
|
f42334411b |
@ -8,6 +8,7 @@ import emu.grasscutter.auth.DefaultAuthentication;
|
||||
import emu.grasscutter.command.CommandMap;
|
||||
import emu.grasscutter.command.DefaultPermissionHandler;
|
||||
import emu.grasscutter.command.PermissionHandler;
|
||||
import emu.grasscutter.game.dungeons.challenge.DungeonChallenge;
|
||||
import emu.grasscutter.game.managers.energy.EnergyManager;
|
||||
import emu.grasscutter.game.managers.stamina.StaminaManager;
|
||||
import emu.grasscutter.plugin.PluginManager;
|
||||
@ -113,6 +114,7 @@ public final class Grasscutter {
|
||||
ResourceLoader.loadAll();
|
||||
ScriptLoader.init();
|
||||
EnergyManager.initialize();
|
||||
DungeonChallenge.initialize();
|
||||
|
||||
// Initialize database.
|
||||
DatabaseManager.initialize();
|
||||
|
22
src/main/java/emu/grasscutter/game/dungeons/DungeonDrop.java
Normal file
22
src/main/java/emu/grasscutter/game/dungeons/DungeonDrop.java
Normal file
@ -0,0 +1,22 @@
|
||||
package emu.grasscutter.game.dungeons;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DungeonDrop {
|
||||
private int dungeonId;
|
||||
private List<DungeonDropEntry> drops;
|
||||
|
||||
public int getDungeonId() {
|
||||
return dungeonId;
|
||||
}
|
||||
public void setDungeonId(int dungeonId) {
|
||||
this.dungeonId = dungeonId;
|
||||
}
|
||||
|
||||
public List<DungeonDropEntry> getDrops() {
|
||||
return drops;
|
||||
}
|
||||
public void setDrops(List<DungeonDropEntry> drops) {
|
||||
this.drops = drops;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package emu.grasscutter.game.dungeons;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DungeonDropEntry {
|
||||
private List<Integer> counts;
|
||||
private List<Integer> items;
|
||||
private List<Integer> probabilities;
|
||||
private List<Integer> itemProbabilities;
|
||||
private boolean mpDouble;
|
||||
|
||||
public List<Integer> getCounts() {
|
||||
return counts;
|
||||
}
|
||||
public void setCounts(List<Integer> counts) {
|
||||
this.counts = counts;
|
||||
}
|
||||
|
||||
public List<Integer> getItems() {
|
||||
return items;
|
||||
}
|
||||
public void setItems(List<Integer> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public List<Integer> getProbabilities() {
|
||||
return probabilities;
|
||||
}
|
||||
public void setProbabilities(List<Integer> probabilities) {
|
||||
this.probabilities = probabilities;
|
||||
}
|
||||
|
||||
public List<Integer> getItemProbabilities() {
|
||||
return itemProbabilities;
|
||||
}
|
||||
public void setItemProbabilities(List<Integer> itemProbabilities) {
|
||||
this.itemProbabilities = itemProbabilities;
|
||||
}
|
||||
|
||||
public boolean isMpDouble() {
|
||||
return mpDouble;
|
||||
}
|
||||
public void setMpDouble(boolean mpDouble) {
|
||||
this.mpDouble = mpDouble;
|
||||
}
|
||||
}
|
@ -1,21 +1,37 @@
|
||||
package emu.grasscutter.game.dungeons.challenge;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.DataLoader;
|
||||
import emu.grasscutter.data.common.ItemParamData;
|
||||
import emu.grasscutter.data.excels.DungeonData;
|
||||
import emu.grasscutter.game.dungeons.DungeonDrop;
|
||||
import emu.grasscutter.game.dungeons.DungeonDropEntry;
|
||||
import emu.grasscutter.game.dungeons.challenge.trigger.ChallengeTrigger;
|
||||
import emu.grasscutter.game.inventory.GameItem;
|
||||
import emu.grasscutter.game.inventory.ItemType;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.game.props.ActionReason;
|
||||
import emu.grasscutter.game.world.Scene;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.scripts.constants.EventType;
|
||||
import emu.grasscutter.scripts.data.SceneGroup;
|
||||
import emu.grasscutter.scripts.data.ScriptArgs;
|
||||
import emu.grasscutter.server.packet.send.PacketGadgetAutoPickDropInfoNotify;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntSet;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
public class DungeonChallenge extends WorldChallenge {
|
||||
|
||||
@ -25,6 +41,24 @@ public class DungeonChallenge extends WorldChallenge {
|
||||
private boolean stage;
|
||||
private IntSet rewardedPlayers;
|
||||
|
||||
private final static Int2ObjectMap<List<DungeonDropEntry>> dungeonDropData = new Int2ObjectOpenHashMap<>();
|
||||
|
||||
public static void initialize() {
|
||||
// Read the data we need for dungeon rewards drops.
|
||||
try (Reader fileReader = new InputStreamReader(DataLoader.load("DungeonDrop.json"))) {
|
||||
List<DungeonDrop> dungeonDropList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, DungeonDrop.class).getType());
|
||||
|
||||
for (DungeonDrop entry : dungeonDropList) {
|
||||
dungeonDropData.put(entry.getDungeonId(), entry.getDrops());
|
||||
}
|
||||
|
||||
Grasscutter.getLogger().info("Loaded {} dungeon drop data entries.", dungeonDropData.size());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Grasscutter.getLogger().error("Unable to load dungeon drop data.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public DungeonChallenge(Scene scene, SceneGroup group,
|
||||
int challengeId, int challengeIndex,
|
||||
List<Integer> paramList,
|
||||
@ -67,8 +101,64 @@ public class DungeonChallenge extends WorldChallenge {
|
||||
}
|
||||
}
|
||||
|
||||
public void getStatueDrops(Player player) {
|
||||
private List<GameItem> rollRewards(boolean useCondensed) {
|
||||
List<GameItem> rewards = new ArrayList<>();
|
||||
int dungeonId = this.getScene().getDungeonData().getId();
|
||||
// If we have specific drop data for this dungeon, we use it.
|
||||
if (dungeonDropData.containsKey(dungeonId)) {
|
||||
List<DungeonDropEntry> dropEntries = dungeonDropData.get(dungeonId);
|
||||
|
||||
// Roll for each drop group.
|
||||
for (var entry : dropEntries) {
|
||||
// Determine the number of drops we get for this entry.
|
||||
int start = entry.getCounts().get(0);
|
||||
int end = entry.getCounts().get(entry.getCounts().size() - 1);
|
||||
var candidateAmounts = IntStream.range(start, end + 1).boxed().collect(Collectors.toList());
|
||||
|
||||
int amount = Utils.drawRandomListElement(candidateAmounts, entry.getProbabilities());
|
||||
|
||||
if (useCondensed) {
|
||||
amount += Utils.drawRandomListElement(candidateAmounts, entry.getProbabilities());
|
||||
}
|
||||
|
||||
// Double rewards in multiplay mode, if specified.
|
||||
if (entry.isMpDouble() && this.getScene().getPlayerCount() > 1) {
|
||||
amount *= 2;
|
||||
}
|
||||
|
||||
// Roll items for this group.
|
||||
// Here, we have to handle stacking, or the client will not display results correctly.
|
||||
// For now, we use the following logic: If the possible drop item are a list of multiple items,
|
||||
// we roll them separately. If not, we stack them. This should work out in practice, at least
|
||||
// for the currently existing set of dungeons.
|
||||
if (entry.getItems().size() == 1) {
|
||||
rewards.add(new GameItem(entry.getItems().get(0), amount));
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < amount; i++) {
|
||||
// int itemIndex = ThreadLocalRandom.current().nextInt(0, entry.getItems().size());
|
||||
// int itemId = entry.getItems().get(itemIndex);
|
||||
int itemId = Utils.drawRandomListElement(entry.getItems(), entry.getItemProbabilities());
|
||||
rewards.add(new GameItem(itemId, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Otherwise, we fall back to the preview data.
|
||||
else {
|
||||
Grasscutter.getLogger().info("No drop data found or dungeon {}, falling back to preview data ...", dungeonId);
|
||||
for (ItemParamData param : getScene().getDungeonData().getRewardPreview().getPreviewItems()) {
|
||||
rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
|
||||
}
|
||||
}
|
||||
|
||||
return rewards;
|
||||
}
|
||||
|
||||
public void getStatueDrops(Player player, GadgetInteractReq request) {
|
||||
DungeonData dungeonData = getScene().getDungeonData();
|
||||
int resinCost = dungeonData.getStatueCostCount() != 0 ? dungeonData.getStatueCostCount() : 20;
|
||||
|
||||
if (!isSuccess() || dungeonData == null || dungeonData.getRewardPreview() == null || dungeonData.getRewardPreview().getPreviewItems().length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -78,11 +168,42 @@ public class DungeonChallenge extends WorldChallenge {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get rewards.
|
||||
List<GameItem> rewards = new ArrayList<>();
|
||||
for (ItemParamData param : getScene().getDungeonData().getRewardPreview().getPreviewItems()) {
|
||||
rewards.add(new GameItem(param.getId(), Math.max(param.getCount(), 1)));
|
||||
|
||||
if (request.getIsUseCondenseResin()) {
|
||||
// Check if condensed resin is usable here.
|
||||
// For this, we use the following logic for now:
|
||||
// The normal resin cost of the dungeon has to be 20.
|
||||
if (resinCost != 20) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure the player has condensed resin.
|
||||
GameItem condensedResin = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(220007);
|
||||
if (condensedResin == null || condensedResin.getCount() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Deduct.
|
||||
player.getInventory().removeItem(condensedResin, 1);
|
||||
|
||||
// Roll rewards.
|
||||
rewards.addAll(this.rollRewards(true));
|
||||
}
|
||||
else {
|
||||
// If the player used regular resin, try to deduct.
|
||||
// Stop if insufficient resin.
|
||||
boolean success = player.getResinManager().useResin(resinCost);
|
||||
if (!success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Roll rewards.
|
||||
rewards.addAll(this.rollRewards(false));
|
||||
}
|
||||
|
||||
// Add rewards to player and send notification.
|
||||
player.getInventory().addItems(rewards, ActionReason.DungeonStatueDrop);
|
||||
player.sendPacket(new PacketGadgetAutoPickDropInfoNotify(rewards));
|
||||
|
||||
|
@ -4,7 +4,7 @@ import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.game.entity.EntityGadget;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.net.proto.BossChestInfoOuterClass.BossChestInfo;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass.InterOpType;
|
||||
import emu.grasscutter.net.proto.InteractTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
|
||||
@ -18,7 +18,7 @@ public class GadgetChest extends GadgetContent {
|
||||
super(gadget);
|
||||
}
|
||||
|
||||
public boolean onInteract(Player player, InterOpType opType) {
|
||||
public boolean onInteract(Player player, GadgetInteractReq req) {
|
||||
var chestInteractHandlerMap = getGadget().getScene().getWorld().getServer().getWorldDataManager().getChestInteractHandlerMap();
|
||||
var handler = chestInteractHandlerMap.get(getGadget().getGadgetData().getJsonName());
|
||||
if(handler == null){
|
||||
@ -26,7 +26,7 @@ public class GadgetChest extends GadgetContent {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(opType == InterOpType.INTER_OP_TYPE_START && handler.isTwoStep()){
|
||||
if(req.getOpType() == InterOpType.INTER_OP_TYPE_START && handler.isTwoStep()){
|
||||
player.sendPacket(new PacketGadgetInteractRsp(getGadget(), InteractType.INTERACT_TYPE_OPEN_CHEST, InterOpType.INTER_OP_TYPE_START));
|
||||
return false;
|
||||
}else{
|
||||
|
@ -3,6 +3,7 @@ package emu.grasscutter.game.entity.gadget;
|
||||
import emu.grasscutter.game.entity.EntityGadget;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
|
||||
|
||||
public abstract class GadgetContent {
|
||||
@ -16,7 +17,7 @@ public abstract class GadgetContent {
|
||||
return gadget;
|
||||
}
|
||||
|
||||
public abstract boolean onInteract(Player player, InterOpTypeOuterClass.InterOpType opType);
|
||||
public abstract boolean onInteract(Player player, GadgetInteractReq req);
|
||||
|
||||
public abstract void onBuildProto(SceneGadgetInfo.Builder gadgetInfo);
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import emu.grasscutter.game.inventory.GameItem;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.game.props.ActionReason;
|
||||
import emu.grasscutter.net.proto.GatherGadgetInfoOuterClass.GatherGadgetInfo;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
|
||||
|
||||
public class GadgetGatherPoint extends GadgetContent {
|
||||
@ -26,7 +26,7 @@ public class GadgetGatherPoint extends GadgetContent {
|
||||
return getGatherData().getItemId();
|
||||
}
|
||||
|
||||
public boolean onInteract(Player player, InterOpTypeOuterClass.InterOpType opType) {
|
||||
public boolean onInteract(Player player, GadgetInteractReq req) {
|
||||
GameItem item = new GameItem(gatherData.getItemId(), 1);
|
||||
|
||||
player.getInventory().addItem(item, ActionReason.Gather);
|
||||
|
@ -3,7 +3,7 @@ package emu.grasscutter.game.entity.gadget;
|
||||
import emu.grasscutter.game.dungeons.challenge.DungeonChallenge;
|
||||
import emu.grasscutter.game.entity.EntityGadget;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
|
||||
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
|
||||
import emu.grasscutter.server.packet.send.PacketGadgetInteractRsp;
|
||||
@ -14,9 +14,9 @@ public class GadgetRewardStatue extends GadgetContent {
|
||||
super(gadget);
|
||||
}
|
||||
|
||||
public boolean onInteract(Player player, InterOpTypeOuterClass.InterOpType opType) {
|
||||
public boolean onInteract(Player player, GadgetInteractReq req) {
|
||||
if (player.getScene().getChallenge() != null && player.getScene().getChallenge() instanceof DungeonChallenge dungeonChallenge) {
|
||||
dungeonChallenge.getStatueDrops(player);
|
||||
dungeonChallenge.getStatueDrops(player, req);
|
||||
}
|
||||
|
||||
player.sendPacket(new PacketGadgetInteractRsp(getGadget(), InteractType.INTERACT_TYPE_OPEN_STATUE));
|
||||
|
@ -4,7 +4,7 @@ import java.util.Arrays;
|
||||
|
||||
import emu.grasscutter.game.entity.EntityGadget;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.net.proto.InterOpTypeOuterClass;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
|
||||
import emu.grasscutter.net.proto.WorktopInfoOuterClass.WorktopInfo;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
@ -35,7 +35,7 @@ public class GadgetWorktop extends GadgetContent {
|
||||
this.worktopOptions.remove(option);
|
||||
}
|
||||
|
||||
public boolean onInteract(Player player, InterOpTypeOuterClass.InterOpType opType) {
|
||||
public boolean onInteract(Player player, GadgetInteractReq req) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -990,7 +990,7 @@ public class Player {
|
||||
}
|
||||
|
||||
|
||||
public void interactWith(int gadgetEntityId, InterOpTypeOuterClass.InterOpType opType) {
|
||||
public void interactWith(int gadgetEntityId, GadgetInteractReq req) {
|
||||
GameEntity entity = getScene().getEntityById(gadgetEntityId);
|
||||
if (entity == null) {
|
||||
return;
|
||||
@ -1023,7 +1023,7 @@ public class Player {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean shouldDelete = gadget.getContent().onInteract(this, opType);
|
||||
boolean shouldDelete = gadget.getContent().onInteract(this, req);
|
||||
|
||||
if (shouldDelete) {
|
||||
entity.getScene().removeEntity(entity);
|
||||
|
@ -1,9 +1,23 @@
|
||||
package emu.grasscutter.server.event.game;
|
||||
|
||||
import emu.grasscutter.server.event.types.ServerEvent;
|
||||
import java.time.Instant;
|
||||
|
||||
public final class ServerTickEvent extends ServerEvent {
|
||||
public ServerTickEvent() {
|
||||
private final Instant start, end;
|
||||
|
||||
public ServerTickEvent(Instant start, Instant end) {
|
||||
super(Type.GAME);
|
||||
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public Instant getTickStart() {
|
||||
return this.start;
|
||||
}
|
||||
|
||||
public Instant getTickEnd() {
|
||||
return this.end;
|
||||
}
|
||||
}
|
@ -26,11 +26,14 @@ import emu.grasscutter.server.event.types.ServerEvent;
|
||||
import emu.grasscutter.server.event.game.ServerTickEvent;
|
||||
import emu.grasscutter.server.event.internal.ServerStartEvent;
|
||||
import emu.grasscutter.server.event.internal.ServerStopEvent;
|
||||
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
|
||||
import emu.grasscutter.task.TaskMap;
|
||||
import kcp.highway.ChannelConfig;
|
||||
import kcp.highway.KcpServer;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -42,6 +45,7 @@ public final class GameServer extends KcpServer {
|
||||
private final InetSocketAddress address;
|
||||
private final GameServerPacketHandler packetHandler;
|
||||
private final ServerQuestHandler questHandler;
|
||||
@Getter private final ServerTaskScheduler scheduler;
|
||||
|
||||
private final Map<Integer, Player> players;
|
||||
private final Set<World> worlds;
|
||||
@ -80,6 +84,7 @@ public final class GameServer extends KcpServer {
|
||||
this.address = address;
|
||||
this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
|
||||
this.questHandler = new ServerQuestHandler();
|
||||
this.scheduler = new ServerTaskScheduler();
|
||||
this.players = new ConcurrentHashMap<>();
|
||||
this.worlds = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
@ -238,7 +243,10 @@ public final class GameServer extends KcpServer {
|
||||
return DatabaseHelper.getAccountByName(username);
|
||||
}
|
||||
|
||||
public synchronized void onTick(){
|
||||
public synchronized void onTick() {
|
||||
var tickStart = Instant.now();
|
||||
|
||||
// Tick worlds.
|
||||
Iterator<World> it = this.getWorlds().iterator();
|
||||
while (it.hasNext()) {
|
||||
World world = it.next();
|
||||
@ -250,11 +258,17 @@ public final class GameServer extends KcpServer {
|
||||
world.onTick();
|
||||
}
|
||||
|
||||
// Tick players.
|
||||
for (Player player : this.getPlayers().values()) {
|
||||
player.onTick();
|
||||
}
|
||||
|
||||
ServerTickEvent event = new ServerTickEvent(); event.call();
|
||||
// Tick scheduler.
|
||||
this.getScheduler().runTasks();
|
||||
|
||||
// Call server tick event.
|
||||
ServerTickEvent event = new ServerTickEvent(tickStart, Instant.now());
|
||||
event.call();
|
||||
}
|
||||
|
||||
public void registerWorld(World world) {
|
||||
|
@ -13,7 +13,7 @@ public class HandlerGadgetInteractReq extends PacketHandler {
|
||||
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
|
||||
GadgetInteractReq req = GadgetInteractReq.parseFrom(payload);
|
||||
|
||||
session.getPlayer().interactWith(req.getGadgetEntityId(), req.getOpType());
|
||||
session.getPlayer().interactWith(req.getGadgetEntityId(), req);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,98 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
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++;
|
||||
}
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import java.nio.file.StandardCopyOption;
|
||||
import java.time.*;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.DataLoader;
|
||||
@ -377,4 +378,35 @@ public final class Utils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Draws a random element from the given list, following the given probability distribution, if given.
|
||||
* @param list The list from which to draw the element.
|
||||
* @param probabilities The probability distribution. This is given as a list of probabilities of the same length it `list`.
|
||||
* @return A randomly drawn element from the given list.
|
||||
*/
|
||||
public static <T> T drawRandomListElement(List<T> list, List<Integer> probabilities) {
|
||||
// If we don't have a probability distribution, or the size of the distribution does not match
|
||||
// the size of the list, we assume uniform distribution.
|
||||
if (probabilities == null || probabilities.size() <= 1 || probabilities.size() != list.size()) {
|
||||
int index = ThreadLocalRandom.current().nextInt(0, list.size());
|
||||
return list.get(index);
|
||||
}
|
||||
|
||||
// Otherwise, we roll with the given distribution.
|
||||
int totalProbabilityMass = probabilities.stream().reduce(Integer::sum).get();
|
||||
int roll = ThreadLocalRandom.current().nextInt(1, totalProbabilityMass + 1);
|
||||
|
||||
int currentTotalChance = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
currentTotalChance += probabilities.get(i);
|
||||
|
||||
if (roll <= currentTotalChance) {
|
||||
return list.get(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Should never happen.
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
|
4142
src/main/resources/defaults/data/DungeonDrop.json
Normal file
4142
src/main/resources/defaults/data/DungeonDrop.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user