PlayerOpenStateManager.java 3.02 KB
Newer Older
akatatsu27's avatar
akatatsu27 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package emu.grasscutter.game.player;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.OpenState;
import emu.grasscutter.server.packet.send.PacketOpenStateChangeNotify;
import emu.grasscutter.server.packet.send.PacketOpenStateUpdateNotify;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static emu.grasscutter.game.props.OpenState.*;

@Entity
Melledy's avatar
Melledy committed
19
public class PlayerOpenStateManager extends BasePlayerDataManager {
20
21
    // Map of all open states that this player has. Do not put default values here.
    private Map<Integer, Integer> map;
github-actions's avatar
github-actions committed
22

akatatsu27's avatar
akatatsu27 committed
23
24
25
26
27
28
29
     /*
    //DO NOT MODIFY. Based on conversation of official server and client, game version 2.7
    private static Set<OpenState> newPlayerOpenStates = Set.of(OPEN_STATE_DERIVATIVE_MALL,OPEN_STATE_PHOTOGRAPH,OPEN_STATE_BATTLE_PASS,OPEN_STATE_SHOP_TYPE_GENESISCRYSTAL,OPEN_STATE_SHOP_TYPE_RECOMMANDED,
        OPEN_STATE_SHOP_TYPE_GIFTPACKAGE,OPEN_STATE_GUIDE_RELIC_PROM,OPEN_STATE_GUIDE_TALENT,OPEN_STATE_SHOP_TYPE_BLACKSMITH,OPEN_STATE_SHOP_TYPE_PAIMON,OPEN_STATE_WEAPON_AWAKEN,
        OPEN_STATE_WEAPON_PROMOTE,OPEN_STATE_AVATAR_PROMOTE,OPEN_STATE_AVATAR_TALENT,OPEN_STATE_WEAPON_UPGRADE,OPEN_STATE_RESIN,OPEN_STATE_RELIQUARY_UPGRADE,
        OPEN_STATE_SHOP_TYPE_VIRTUAL_SHOP,OPEN_STATE_RELIQUARY_PROMOTE);
    */
github-actions's avatar
github-actions committed
30

31
32
    // For development. Remove entry when properly implemented
    // TODO - Set as boolean in OpenState
33
34
35
    public static final Set<OpenState> DEV_OPEN_STATES = Stream.of(OpenState.values())
        .filter(s -> s != OpenState.OPEN_STATE_NONE && s.getUnlockLevel() <= 1)
        .collect(Collectors.toSet());
36
    
akatatsu27's avatar
akatatsu27 committed
37
    public PlayerOpenStateManager(Player player) {
Melledy's avatar
Melledy committed
38
        super(player);
akatatsu27's avatar
akatatsu27 committed
39
40
    }

github-actions's avatar
github-actions committed
41
42
43
44
    public Map<Integer, Integer> getOpenStateMap() {
        if (this.map == null) this.map = new HashMap<>();
        return this.map;
    }
45
46

    public int getOpenState(OpenState openState) {
47
        return getOpenStateMap().getOrDefault(openState.getValue(), 0);
akatatsu27's avatar
akatatsu27 committed
48
    }
github-actions's avatar
github-actions committed
49

akatatsu27's avatar
akatatsu27 committed
50
    public void setOpenState(OpenState openState, Integer value) {
51
        Integer previousValue = getOpenStateMap().getOrDefault(openState.getValue(),0);
github-actions's avatar
github-actions committed
52
        if (value != previousValue) {
53
            this.map.put(openState.getValue(), value);
akatatsu27's avatar
akatatsu27 committed
54
55
56
57
58
            player.getSession().send(new PacketOpenStateChangeNotify(openState.getValue(),value));
        }
    }

    public void setOpenStates(Map<OpenState,Integer> openStatesChanged) {
github-actions's avatar
github-actions committed
59
        for (Map.Entry<OpenState, Integer> entry : openStatesChanged.entrySet()) {
akatatsu27's avatar
akatatsu27 committed
60
61
62
63
64
            setOpenState(entry.getKey(), entry.getValue());
        }
    }

    public void onPlayerLogin() {
65
        player.getSession().send(new PacketOpenStateUpdateNotify(this));
akatatsu27's avatar
akatatsu27 committed
66
    }
67

68
    public void unlockLevelDependentStates() {
69
        Stream.of(OpenState.values())
70
            .filter(s -> s.getUnlockLevel() > 1 && s.getUnlockLevel() <= this.player.getLevel())
71
72
            .forEach(s -> this.setOpenState(s, 1));
    }
github-actions's avatar
github-actions committed
73
}