mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-02-09 13:13:06 +08:00
Change the way existing hooks work
This commit is contained in:
parent
c21f95928b
commit
7b097e879e
@ -1,8 +1,5 @@
|
||||
package emu.grasscutter;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.SERVER;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
@ -14,7 +11,7 @@ import emu.grasscutter.config.ConfigContainer;
|
||||
import emu.grasscutter.data.ResourceLoader;
|
||||
import emu.grasscutter.database.DatabaseManager;
|
||||
import emu.grasscutter.plugin.PluginManager;
|
||||
import emu.grasscutter.plugin.api.ServerHook;
|
||||
import emu.grasscutter.plugin.api.ServerHelper;
|
||||
import emu.grasscutter.server.game.GameServer;
|
||||
import emu.grasscutter.server.http.HttpServer;
|
||||
import emu.grasscutter.server.http.dispatch.DispatchHandler;
|
||||
@ -27,12 +24,6 @@ import emu.grasscutter.server.http.handlers.GenericHandler;
|
||||
import emu.grasscutter.server.http.handlers.LogHandler;
|
||||
import emu.grasscutter.tools.Tools;
|
||||
import emu.grasscutter.utils.*;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import javax.annotation.Nullable;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jline.reader.EndOfFileException;
|
||||
@ -44,6 +35,16 @@ import org.jline.terminal.TerminalBuilder;
|
||||
import org.reflections.Reflections;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.SERVER;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
public final class Grasscutter {
|
||||
public static final File configFile = new File("./config.json");
|
||||
public static final Reflections reflector = new Reflections("emu.grasscutter");
|
||||
@ -121,7 +122,7 @@ public final class Grasscutter {
|
||||
httpServer = new HttpServer();
|
||||
gameServer = new GameServer();
|
||||
// Create a server hook instance with both servers.
|
||||
new ServerHook(gameServer, httpServer);
|
||||
new ServerHelper(gameServer, httpServer);
|
||||
|
||||
// Create plugin manager instance.
|
||||
pluginManager = new PluginManager();
|
||||
|
@ -15,7 +15,6 @@ import emu.grasscutter.game.activity.ActivityManager;
|
||||
import emu.grasscutter.game.avatar.Avatar;
|
||||
import emu.grasscutter.game.avatar.AvatarStorage;
|
||||
import emu.grasscutter.game.battlepass.BattlePassManager;
|
||||
import emu.grasscutter.game.entity.EntityAvatar;
|
||||
import emu.grasscutter.game.entity.GameEntity;
|
||||
import emu.grasscutter.game.expedition.ExpeditionInfo;
|
||||
import emu.grasscutter.game.friends.FriendsList;
|
||||
@ -51,17 +50,21 @@ import emu.grasscutter.game.tower.TowerManager;
|
||||
import emu.grasscutter.game.world.Scene;
|
||||
import emu.grasscutter.game.world.World;
|
||||
import emu.grasscutter.net.packet.BasePacket;
|
||||
import emu.grasscutter.net.proto.*;
|
||||
import emu.grasscutter.net.proto.AbilityInvokeEntryOuterClass.AbilityInvokeEntry;
|
||||
import emu.grasscutter.net.proto.AttackResultOuterClass.AttackResult;
|
||||
import emu.grasscutter.net.proto.CombatInvokeEntryOuterClass.CombatInvokeEntry;
|
||||
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
|
||||
import emu.grasscutter.net.proto.MpSettingTypeOuterClass.MpSettingType;
|
||||
import emu.grasscutter.net.proto.OnlinePlayerInfoOuterClass.OnlinePlayerInfo;
|
||||
import emu.grasscutter.net.proto.PlayerApplyEnterMpResultNotifyOuterClass;
|
||||
import emu.grasscutter.net.proto.PlayerLocationInfoOuterClass.PlayerLocationInfo;
|
||||
import emu.grasscutter.net.proto.PlayerWorldLocationInfoOuterClass;
|
||||
import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture;
|
||||
import emu.grasscutter.net.proto.PropChangeReasonOuterClass.PropChangeReason;
|
||||
import emu.grasscutter.net.proto.ShowAvatarInfoOuterClass;
|
||||
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
|
||||
import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass;
|
||||
import emu.grasscutter.plugin.api.PlayerHook;
|
||||
import emu.grasscutter.scripts.data.SceneRegion;
|
||||
import emu.grasscutter.server.event.player.PlayerJoinEvent;
|
||||
import emu.grasscutter.server.event.player.PlayerQuitEvent;
|
||||
@ -89,7 +92,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import static emu.grasscutter.config.Configuration.GAME_OPTIONS;
|
||||
|
||||
@Entity(value = "players", useDiscriminator = false)
|
||||
public class Player {
|
||||
public class Player implements PlayerHook {
|
||||
@Id private int id;
|
||||
@Indexed(options = @IndexOptions(unique = true)) private String accountId;
|
||||
@Setter private transient Account account;
|
||||
@ -322,6 +325,11 @@ public class Player {
|
||||
this.satiationManager = new SatiationManager(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayer() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the player's game time if it has changed.
|
||||
*
|
||||
|
@ -1,18 +1,19 @@
|
||||
package emu.grasscutter.plugin;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.plugin.api.ServerHook;
|
||||
import emu.grasscutter.plugin.api.ServerHelper;
|
||||
import emu.grasscutter.server.game.GameServer;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLClassLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/** The base class for all plugins to extend. */
|
||||
public abstract class Plugin {
|
||||
private final ServerHook server = ServerHook.getInstance();
|
||||
private final ServerHelper server = ServerHelper.getInstance();
|
||||
|
||||
private PluginIdentifier identifier;
|
||||
private URLClassLoader classLoader;
|
||||
@ -100,7 +101,7 @@ public abstract class Plugin {
|
||||
*
|
||||
* @return A server hook singleton.
|
||||
*/
|
||||
public final ServerHook getHandle() {
|
||||
public final ServerHelper getHandle() {
|
||||
return this.server;
|
||||
}
|
||||
|
||||
|
@ -1,5 +0,0 @@
|
||||
package emu.grasscutter.plugin.api;
|
||||
|
||||
public enum Item {
|
||||
/* TODO: Use handbook to generate an Item enum. */
|
||||
}
|
@ -13,21 +13,15 @@ import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify;
|
||||
import emu.grasscutter.utils.Position;
|
||||
|
||||
/** Hooks into the {@link Player} class, adding convenient ways to do certain things. */
|
||||
public final class PlayerHook {
|
||||
private final Player player;
|
||||
|
||||
public interface PlayerHook {
|
||||
/**
|
||||
* Hooks into the player.
|
||||
*
|
||||
* @param player The player to hook into.
|
||||
* @return The player that this hook is attached to.
|
||||
*/
|
||||
public PlayerHook(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
Player getPlayer();
|
||||
|
||||
/** Kicks a player from the server. TODO: Refactor to kick using a packet. */
|
||||
public void kick() {
|
||||
this.player.getSession().close();
|
||||
default void kick() {
|
||||
this.getPlayer().getSession().close();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -35,8 +29,8 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param sceneId The scene to send the player to.
|
||||
*/
|
||||
public void changeScenes(int sceneId) {
|
||||
this.player.getWorld().transferPlayerToScene(this.player, sceneId, this.player.getPosition());
|
||||
default void changeScenes(int sceneId) {
|
||||
this.getPlayer().getWorld().transferPlayerToScene(this.getPlayer(), sceneId, this.getPlayer().getPosition());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +38,7 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param property The property that was updated.
|
||||
*/
|
||||
public void updateFightProperty(FightProperty property) {
|
||||
default void updateFightProperty(FightProperty property) {
|
||||
this.broadcastPacketToWorld(
|
||||
new PacketAvatarFightPropUpdateNotify(this.getCurrentAvatar(), property));
|
||||
}
|
||||
@ -54,8 +48,8 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param packet The packet to send.
|
||||
*/
|
||||
public void broadcastPacketToWorld(BasePacket packet) {
|
||||
this.player.getWorld().broadcastPacket(packet);
|
||||
default void broadcastPacketToWorld(BasePacket packet) {
|
||||
this.getPlayer().getWorld().broadcastPacket(packet);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,7 +57,7 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param health The health to set the avatar to.
|
||||
*/
|
||||
public void setHealth(float health) {
|
||||
default void setHealth(float health) {
|
||||
this.getCurrentAvatarEntity().setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, health);
|
||||
this.updateFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
|
||||
}
|
||||
@ -73,7 +67,7 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param avatar The avatar to revive.
|
||||
*/
|
||||
public void reviveAvatar(Avatar avatar) {
|
||||
default void reviveAvatar(Avatar avatar) {
|
||||
this.broadcastPacketToWorld(new PacketAvatarLifeStateChangeNotify(avatar));
|
||||
}
|
||||
|
||||
@ -82,14 +76,14 @@ public final class PlayerHook {
|
||||
*
|
||||
* @param position The position to teleport the player to.
|
||||
*/
|
||||
public void teleport(Position position) {
|
||||
this.player.getPosition().set(position);
|
||||
this.player.sendPacket(
|
||||
default void teleport(Position position) {
|
||||
this.getPlayer().getPosition().set(position);
|
||||
this.getPlayer().sendPacket(
|
||||
new PacketPlayerEnterSceneNotify(
|
||||
this.player,
|
||||
this.getPlayer(),
|
||||
EnterType.ENTER_TYPE_JUMP,
|
||||
EnterReason.TransPoint,
|
||||
this.player.getSceneId(),
|
||||
this.getPlayer().getSceneId(),
|
||||
position));
|
||||
}
|
||||
|
||||
@ -98,7 +92,7 @@ public final class PlayerHook {
|
||||
*
|
||||
* @return The max health as a float.
|
||||
*/
|
||||
public float getMaxHealth() {
|
||||
default float getMaxHealth() {
|
||||
return this.getCurrentAvatarEntity().getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
|
||||
}
|
||||
|
||||
@ -107,8 +101,8 @@ public final class PlayerHook {
|
||||
*
|
||||
* @return The avatar as an {@link EntityAvatar}.
|
||||
*/
|
||||
public EntityAvatar getCurrentAvatarEntity() {
|
||||
return this.player.getTeamManager().getCurrentAvatarEntity();
|
||||
default EntityAvatar getCurrentAvatarEntity() {
|
||||
return this.getPlayer().getTeamManager().getCurrentAvatarEntity();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,7 +110,7 @@ public final class PlayerHook {
|
||||
*
|
||||
* @return The avatar as an {@link Avatar}.
|
||||
*/
|
||||
public Avatar getCurrentAvatar() {
|
||||
default Avatar getCurrentAvatar() {
|
||||
return this.getCurrentAvatarEntity().getAvatar();
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,8 @@ import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Hooks into the {@link GameServer} class, adding convenient ways to do certain things. */
|
||||
public final class ServerHook {
|
||||
private static ServerHook instance;
|
||||
public final class ServerHelper {
|
||||
private static ServerHelper instance;
|
||||
private final GameServer gameServer;
|
||||
private final HttpServer httpServer;
|
||||
|
||||
@ -26,7 +26,7 @@ public final class ServerHook {
|
||||
* @param gameServer The game server to hook into.
|
||||
* @param httpServer The HTTP server to hook into.
|
||||
*/
|
||||
public ServerHook(GameServer gameServer, HttpServer httpServer) {
|
||||
public ServerHelper(GameServer gameServer, HttpServer httpServer) {
|
||||
this.gameServer = gameServer;
|
||||
this.httpServer = httpServer;
|
||||
|
||||
@ -36,9 +36,9 @@ public final class ServerHook {
|
||||
/**
|
||||
* Gets the server hook instance.
|
||||
*
|
||||
* @return A {@link ServerHook} singleton.
|
||||
* @return A {@link ServerHelper} singleton.
|
||||
*/
|
||||
public static ServerHook getInstance() {
|
||||
public static ServerHelper getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user