PlayerBuffManager.java 6.82 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.player;

AnimeGitB's avatar
AnimeGitB committed
3
import java.util.ArrayList;
Melledy's avatar
Melledy committed
4
import java.util.List;
AnimeGitB's avatar
AnimeGitB committed
5
import java.util.Optional;
Melledy's avatar
Melledy committed
6
7
8

import emu.grasscutter.data.GameData;
import emu.grasscutter.data.excels.BuffData;
AnimeGitB's avatar
AnimeGitB committed
9
10
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.props.FightProperty;
Melledy's avatar
Melledy committed
11
12
13
14
15
16
17
18
19
20
21
import emu.grasscutter.net.proto.ServerBuffChangeNotifyOuterClass.ServerBuffChangeNotify.ServerBuffChangeType;
import emu.grasscutter.net.proto.ServerBuffOuterClass.ServerBuff;
import emu.grasscutter.server.packet.send.PacketServerBuffChangeNotify;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

import lombok.Getter;

public class PlayerBuffManager extends BasePlayerManager {
    private int nextBuffUid;
github-actions's avatar
github-actions committed
22

Melledy's avatar
Melledy committed
23
24
    private final List<PlayerBuff> pendingBuffs;
    private final Int2ObjectMap<PlayerBuff> buffs; // Server buffs
github-actions's avatar
github-actions committed
25

Melledy's avatar
Melledy committed
26
27
28
    public PlayerBuffManager(Player player) {
        super(player);
        this.buffs = new Int2ObjectOpenHashMap<>();
AnimeGitB's avatar
AnimeGitB committed
29
        this.pendingBuffs = new ArrayList<>();
Melledy's avatar
Melledy committed
30
    }
github-actions's avatar
github-actions committed
31

Melledy's avatar
Melledy committed
32
33
34
35
36
37
38
    /**
     * Gets a new uid for a server buff
     * @return New integer buff uid
     */
    private int getNextBuffUid() {
        return ++nextBuffUid;
    }
github-actions's avatar
github-actions committed
39

Melledy's avatar
Melledy committed
40
41
42
43
44
45
    /**
     * Returns true if the player has a buff with this group id
     * @param groupId Buff group id
     * @return True if a buff with this group id exists
     */
    public synchronized boolean hasBuff(int groupId) {
AnimeGitB's avatar
AnimeGitB committed
46
        return this.buffs.containsKey(groupId);
Melledy's avatar
Melledy committed
47
    }
github-actions's avatar
github-actions committed
48

Melledy's avatar
Melledy committed
49
50
51
52
53
54
    /**
     * Clears all player buffs
     */
    public synchronized void clearBuffs() {
        // Remove from player
        getPlayer().sendPacket(
AnimeGitB's avatar
AnimeGitB committed
55
            new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.buffs.values())
Melledy's avatar
Melledy committed
56
        );
github-actions's avatar
github-actions committed
57

Melledy's avatar
Melledy committed
58
        // Clear
AnimeGitB's avatar
AnimeGitB committed
59
        this.buffs.clear();
Melledy's avatar
Melledy committed
60
    }
github-actions's avatar
github-actions committed
61

Melledy's avatar
Melledy committed
62
63
64
65
66
67
68
69
    /**
     * Adds a server buff to the player.
     * @param buffId Server buff id
     * @return True if a buff was added
     */
    public boolean addBuff(int buffId) {
        return addBuff(buffId, -1f);
    }
github-actions's avatar
github-actions committed
70

Melledy's avatar
Melledy committed
71
72
73
74
75
76
77
    /**
     * Adds a server buff to the player.
     * @param buffId Server buff id
     * @param duration Duration of the buff in seconds. Set to 0 for an infinite buff.
     * @return True if a buff was added
     */
    public synchronized boolean addBuff(int buffId, float duration) {
AnimeGitB's avatar
AnimeGitB committed
78
79
80
81
82
83
84
85
86
87
88
        return addBuff(buffId, duration, null);
    }

    /**
     * Adds a server buff to the player.
     * @param buffId Server buff id
     * @param duration Duration of the buff in seconds. Set to 0 for an infinite buff.
     * @param target Target avatar
     * @return True if a buff was added
     */
    public synchronized boolean addBuff(int buffId, float duration, Avatar target) {
Melledy's avatar
Melledy committed
89
90
91
        // Get buff excel data
        BuffData buffData = GameData.getBuffDataMap().get(buffId);
        if (buffData == null) return false;
github-actions's avatar
github-actions committed
92

AnimeGitB's avatar
AnimeGitB committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        boolean success = false;

        // Perform onAdded actions
        success |= Optional.ofNullable(GameData.getAbilityData(buffData.getAbilityName()))
            .map(data -> data.modifiers.get(buffData.getModifierName()))
            .map(modifier -> modifier.onAdded)
            .map(onAdded -> {
                System.out.println("onAdded exists");
                boolean s = false;
                for (var a: onAdded) {
                    switch (a.type) {
                        case HealHP:
                            System.out.println("Attempting heal");
                            if (target == null) break;
                            float maxHp = target.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
                            float amount = a.amount.get() + a.amountByTargetMaxHPRatio.get() * maxHp;
                            target.getAsEntity().heal(amount);
                            s = true;
                            System.out.printf("Healed {}", amount);
                            break;
                        default:
                            break;
                    }
                }
                return s;
            })
            .orElse(false);
        System.out.println("Oh no");

Melledy's avatar
Melledy committed
122
123
124
125
        // Set duration
        if (duration < 0f) {
            duration = buffData.getTime();
        }
github-actions's avatar
github-actions committed
126

Melledy's avatar
Melledy committed
127
128
        // Dont add buff if duration is equal or less than 0
        if (duration <= 0) {
AnimeGitB's avatar
AnimeGitB committed
129
            return success;
Melledy's avatar
Melledy committed
130
        }
github-actions's avatar
github-actions committed
131

Melledy's avatar
Melledy committed
132
        // Clear previous buff if it exists
133
        this.removeBuff(buffData.getGroupId());
github-actions's avatar
github-actions committed
134

Melledy's avatar
Melledy committed
135
136
        // Create and store buff
        PlayerBuff buff = new PlayerBuff(getNextBuffUid(), buffData, duration);
AnimeGitB's avatar
AnimeGitB committed
137
        this.buffs.put(buff.getGroupId(), buff);
github-actions's avatar
github-actions committed
138

Melledy's avatar
Melledy committed
139
140
        // Packet
        getPlayer().sendPacket(new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_ADD_SERVER_BUFF, buff));
github-actions's avatar
github-actions committed
141

Melledy's avatar
Melledy committed
142
143
        return true;
    }
github-actions's avatar
github-actions committed
144

Melledy's avatar
Melledy committed
145
146
147
148
149
150
    /**
     * Removes a buff by its group id
     * @param buffGroupId Server buff group id
     * @return True if a buff was remove
     */
    public synchronized boolean removeBuff(int buffGroupId) {
151
        PlayerBuff buff = this.buffs.remove(buffGroupId);
github-actions's avatar
github-actions committed
152

Melledy's avatar
Melledy committed
153
154
155
156
157
158
        if (buff != null) {
            getPlayer().sendPacket(
                new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, buff)
            );
            return true;
        }
github-actions's avatar
github-actions committed
159

Melledy's avatar
Melledy committed
160
161
        return false;
    }
github-actions's avatar
github-actions committed
162

Melledy's avatar
Melledy committed
163
164
    public synchronized void onTick() {
        // Skip if no buffs
AnimeGitB's avatar
AnimeGitB committed
165
        if (this.buffs.isEmpty()) return;
github-actions's avatar
github-actions committed
166

Melledy's avatar
Melledy committed
167
        long currentTime = System.currentTimeMillis();
github-actions's avatar
github-actions committed
168

Melledy's avatar
Melledy committed
169
        // Add to pending buffs to remove if buff has expired
AnimeGitB's avatar
AnimeGitB committed
170
171
172
173
174
175
        this.buffs.values().removeIf(buff -> {
            if (currentTime <= buff.getEndTime())
                return false;
            this.pendingBuffs.add(buff);
            return true;
        });
github-actions's avatar
github-actions committed
176

AnimeGitB's avatar
AnimeGitB committed
177
        if (this.pendingBuffs.size() > 0) {
Melledy's avatar
Melledy committed
178
179
180
181
            // Send packet
            getPlayer().sendPacket(
                new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.pendingBuffs)
            );
AnimeGitB's avatar
AnimeGitB committed
182
            this.pendingBuffs.clear();
Melledy's avatar
Melledy committed
183
184
        }
    }
github-actions's avatar
github-actions committed
185

Melledy's avatar
Melledy committed
186
187
188
189
190
    @Getter
    public static class PlayerBuff {
        private final int uid;
        private final BuffData buffData;
        private final long endTime;
github-actions's avatar
github-actions committed
191

Melledy's avatar
Melledy committed
192
193
194
195
196
        public PlayerBuff(int uid, BuffData buffData, float duration) {
            this.uid = uid;
            this.buffData = buffData;
            this.endTime = System.currentTimeMillis() + ((long) duration * 1000);
        }
github-actions's avatar
github-actions committed
197

Melledy's avatar
Melledy committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        public int getGroupId() {
            return getBuffData().getGroupId();
        }

        public ServerBuff toProto() {
            return ServerBuff.newBuilder()
                .setServerBuffUid(this.getUid())
                .setServerBuffId(this.getBuffData().getId())
                .setServerBuffType(this.getBuffData().getServerBuffType().getValue())
                .setInstancedModifierId(1)
                .build();
        }
    }
}