PlayerHook.java 3.6 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.plugin.api;

3
import emu.grasscutter.game.avatar.Avatar;
KingRainbow44's avatar
KingRainbow44 committed
4
import emu.grasscutter.game.entity.EntityAvatar;
Melledy's avatar
Melledy committed
5
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
6
7
import emu.grasscutter.game.props.EnterReason;
import emu.grasscutter.game.props.FightProperty;
8
import emu.grasscutter.net.packet.BasePacket;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11
12
13
14
15
import emu.grasscutter.net.proto.EnterTypeOuterClass.EnterType;
import emu.grasscutter.server.packet.send.PacketAvatarFightPropUpdateNotify;
import emu.grasscutter.server.packet.send.PacketAvatarLifeStateChangeNotify;
import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify;
import emu.grasscutter.utils.Position;

/**
16
 * Hooks into the {@link Player} class, adding convenient ways to do certain things.
KingRainbow44's avatar
KingRainbow44 committed
17
18
 */
public final class PlayerHook {
19
    private final Player player;
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
23
24

    /**
     * Hooks into the player. 
     * @param player The player to hook into.
     */
25
    public PlayerHook(Player player) {
KingRainbow44's avatar
KingRainbow44 committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        this.player = player;
    }
    
    /**
     * Kicks a player from the server.
     */
    public void kick() {
        this.player.getSession().close();
    }

    /**
     * Sends a player to another scene.
     * @param sceneId The scene to send the player to.
     */
    public void changeScenes(int sceneId) {
        this.player.getWorld().transferPlayerToScene(this.player, sceneId, this.player.getPos());
    }

    /**
     * Broadcasts an avatar property notify to all world players.
     * @param property The property that was updated.
     */
    public void updateFightProperty(FightProperty property) {
        this.broadcastPacketToWorld(new PacketAvatarFightPropUpdateNotify(this.getCurrentAvatar(), property));
    }

    /**
     * Broadcasts the packet sent to all world players.
     * @param packet The packet to send.
     */
56
    public void broadcastPacketToWorld(BasePacket packet) {
KingRainbow44's avatar
KingRainbow44 committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        this.player.getWorld().broadcastPacket(packet);
    }

    /**
     * Set the currently equipped avatar's health.
     * @param health The health to set the avatar to.
     */
    public void setHealth(float health) {
        this.getCurrentAvatarEntity().setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, health);
        this.updateFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
    }

    /**
     * Revives the specified avatar.
     * @param avatar The avatar to revive.
     */
73
    public void reviveAvatar(Avatar avatar) {
KingRainbow44's avatar
KingRainbow44 committed
74
75
76
77
78
79
80
81
82
83
84
        this.broadcastPacketToWorld(new PacketAvatarLifeStateChangeNotify(avatar));
    }

    /**
     * Teleports a player to a position.
     * This will **not** transfer the player to another scene.
     * @param position The position to teleport the player to.
     */
    public void teleport(Position position) {
        this.player.getPos().set(position);
        this.player.sendPacket(new PacketPlayerEnterSceneNotify(this.player, 
85
                EnterType.ENTER_JUMP, EnterReason.TransPoint,
KingRainbow44's avatar
KingRainbow44 committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                this.player.getSceneId(), position
        ));
    }

    /**
     * Gets the currently selected avatar's max health.
     * @return The max health as a float.
     */
    public float getMaxHealth() {
        return this.getCurrentAvatarEntity().getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
    }

    /**
     * Gets the currently selected avatar in entity form.
     * @return The avatar as an {@link EntityAvatar}.
     */
    public EntityAvatar getCurrentAvatarEntity() {
        return this.player.getTeamManager().getCurrentAvatarEntity();
    }

    /**
     * Gets the currently selected avatar.
108
     * @return The avatar as an {@link Avatar}.
KingRainbow44's avatar
KingRainbow44 committed
109
     */
110
    public Avatar getCurrentAvatar() {
KingRainbow44's avatar
KingRainbow44 committed
111
112
113
        return this.getCurrentAvatarEntity().getAvatar();
    }
}