mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 09:25:06 +01:00
Implement handbook teleporting
also a few formatting changes and sort data by logical sense
This commit is contained in:
@@ -9,11 +9,10 @@ import emu.grasscutter.Grasscutter.ServerRunMode;
|
||||
import emu.grasscutter.utils.JsonUtils;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Set;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import static emu.grasscutter.Grasscutter.config;
|
||||
|
||||
@@ -204,7 +203,7 @@ public class ConfigContainer {
|
||||
public Level serverLoggerLevel = Level.DEBUG;
|
||||
|
||||
/* Log level of the third-party services (works only with -debug arg):
|
||||
javalin, quartz, reflections, jetty, mongodb.driver*/
|
||||
javalin, quartz, reflections, jetty, mongodb.driver */
|
||||
public Level servicesLoggersLevel = Level.INFO;
|
||||
|
||||
/* Controls whether packets should be logged in console or not */
|
||||
|
||||
@@ -5,7 +5,8 @@ import emu.grasscutter.data.GameData;
|
||||
import emu.grasscutter.data.GameDepot;
|
||||
import emu.grasscutter.data.binout.SceneNpcBornEntry;
|
||||
import emu.grasscutter.data.binout.routes.Route;
|
||||
import emu.grasscutter.data.excels.*;
|
||||
import emu.grasscutter.data.excels.ItemData;
|
||||
import emu.grasscutter.data.excels.SceneData;
|
||||
import emu.grasscutter.data.excels.codex.CodexAnimalData;
|
||||
import emu.grasscutter.data.excels.monster.MonsterData;
|
||||
import emu.grasscutter.data.excels.world.WorldLevelData;
|
||||
@@ -40,14 +41,15 @@ import emu.grasscutter.server.packet.send.*;
|
||||
import emu.grasscutter.utils.KahnsSort;
|
||||
import emu.grasscutter.utils.Position;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.val;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nullable;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.val;
|
||||
|
||||
public final class Scene {
|
||||
@Getter private final World world;
|
||||
@@ -555,7 +557,7 @@ public final class Scene {
|
||||
/**
|
||||
* @return The script's default rotation, or the player's rotation.
|
||||
*/
|
||||
private Position getDefaultRot(Player player) {
|
||||
public Position getDefaultRotation(Player player) {
|
||||
var defaultRotation = this.getScriptManager().getConfig().born_rot;
|
||||
return defaultRotation != null ? defaultRotation : player.getRotation();
|
||||
}
|
||||
@@ -581,7 +583,7 @@ public final class Scene {
|
||||
private Position getRespawnRotation(Player player) {
|
||||
var lastCheckpointRot =
|
||||
this.dungeonManager != null ? this.dungeonManager.getRespawnRotation() : null;
|
||||
return lastCheckpointRot != null ? lastCheckpointRot : this.getDefaultRot(player);
|
||||
return lastCheckpointRot != null ? lastCheckpointRot : this.getDefaultRotation(player);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package emu.grasscutter.server.http.documentation;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.Grasscutter.ServerRunMode;
|
||||
import emu.grasscutter.data.GameData;
|
||||
@@ -14,6 +12,10 @@ import emu.grasscutter.utils.objects.HandbookBody;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
/** Handles requests for the new GM Handbook. */
|
||||
public final class HandbookHandler implements Router {
|
||||
private final byte[] handbook;
|
||||
@@ -38,6 +40,7 @@ public final class HandbookHandler implements Router {
|
||||
// Handbook control routes.
|
||||
javalin.post("/handbook/avatar", this::grantAvatar);
|
||||
javalin.post("/handbook/item", this::giveItem);
|
||||
javalin.post("/handbook/teleport", this::teleportTo);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,8 +103,8 @@ public final class HandbookHandler implements Router {
|
||||
var avatar = new Avatar(avatarData);
|
||||
avatar.setLevel(request.getLevel());
|
||||
avatar.setPromoteLevel(Avatar.getMinPromoteLevel(avatar.getLevel()));
|
||||
avatar
|
||||
.getSkillDepot()
|
||||
Objects.requireNonNull(avatar
|
||||
.getSkillDepot())
|
||||
.getSkillsAndEnergySkill()
|
||||
.forEach(id -> avatar.setSkillLevel(id, request.getTalentLevels()));
|
||||
avatar.forceConstellationLevel(request.getConstellations());
|
||||
@@ -166,4 +169,62 @@ public final class HandbookHandler implements Router {
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Teleports the user to a location.
|
||||
*
|
||||
* @route POST /handbook/teleport
|
||||
* @param ctx The Javalin request context.
|
||||
*/
|
||||
private void teleportTo(Context ctx) {
|
||||
if (!this.controlSupported()) {
|
||||
ctx.status(500).result("Handbook control not supported.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the request body into a class.
|
||||
var request = ctx.bodyAsClass(HandbookBody.TeleportTo.class);
|
||||
// Validate the request.
|
||||
if (request.getPlayer() == null || request.getScene() == null) {
|
||||
ctx.status(400).result("Invalid request.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the requested player.
|
||||
var playerId = Integer.parseInt(request.getPlayer());
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
|
||||
// Parse the requested scene.
|
||||
var sceneId = Integer.parseInt(request.getScene());
|
||||
|
||||
// Validate the request.
|
||||
if (player == null) {
|
||||
ctx.status(400).result("Invalid player UID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the scene in the player's world.
|
||||
var scene = player.getWorld().getSceneById(sceneId);
|
||||
if (scene == null) {
|
||||
ctx.status(400).result("Invalid scene ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve the correct teleport position.
|
||||
var position = scene.getDefaultLocation(player);
|
||||
var rotation = scene.getDefaultRotation(player);
|
||||
// Teleport the player.
|
||||
scene.getWorld().transferPlayerToScene(
|
||||
player, scene.getId(), position);
|
||||
player.getRotation().set(rotation);
|
||||
|
||||
ctx.json(HandbookBody.Response.builder().status(200).message("Player teleported.").build());
|
||||
} catch (NumberFormatException ignored) {
|
||||
ctx.status(400).result("Invalid scene ID.");
|
||||
} catch (Exception exception) {
|
||||
ctx.status(500).result("An error occurred while teleporting to the scene.");
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +29,10 @@ public interface HandbookBody {
|
||||
|
||||
private int amount = 1; // Range between 1 - Long.MAX_VALUE.
|
||||
}
|
||||
|
||||
@Getter
|
||||
class TeleportTo {
|
||||
private String player; // Parse into online player ID.
|
||||
private String scene; // Parse into a scene ID.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user