Change the way existing hooks work

This commit is contained in:
KingRainbow44
2023-05-13 00:57:17 -04:00
Unverified
parent c21f95928b
commit 7b097e879e
6 changed files with 56 additions and 57 deletions
@@ -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;
}