aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/impl/InstantStateManager.java
blob: 880661703326338762733c03ee193c42b138f600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package dev.isxander.yacl3.impl;

import dev.isxander.yacl3.api.Binding;
import dev.isxander.yacl3.api.StateManager;

public class InstantStateManager<T> implements StateManager<T>, ProvidesBindingForDeprecation<T> {
    private final Binding<T> binding;
    private StateListener<T> stateListener;

    public InstantStateManager(Binding<T> binding) {
        this.binding = binding;
        this.stateListener = StateListener.noop();
    }

    @Override
    public void set(T value) {
        boolean changed = !this.get().equals(value);

        this.binding.setValue(value);

        if (changed) stateListener.onStateChange(this.get(), value);
    }

    @Override
    public T get() {
        return this.binding.getValue();
    }

    @Override
    public void apply() {
        // no-op, state is always applied
    }

    @Override
    public void resetToDefault(ResetAction action) {
        this.set(binding.defaultValue());
    }

    @Override
    public void sync() {
        // no-op, state is always synced
    }

    @Override
    public boolean isSynced() {
        return true;
    }

    @Override
    public boolean isAlwaysSynced() {
        return true;
    }

    @Override
    public boolean isDefault() {
        return binding.defaultValue().equals(this.get());
    }

    @Override
    public void addListener(StateListener<T> stateListener) {
        this.stateListener = this.stateListener.andThen(stateListener);
    }

    @Override
    public Binding<T> getBinding() {
        return binding;
    }
}