aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/impl/LabelOptionImpl.java
blob: 3ad0cafb447d86b20939b966b3d4abb3edeacd73 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package dev.isxander.yacl3.impl;

import com.google.common.collect.ImmutableSet;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.gui.controllers.LabelController;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class LabelOptionImpl extends OptionImpl<Component> implements LabelOption {
    public LabelOptionImpl(
            @NotNull StateManager<Component> stateManager,
            @NotNull Collection<OptionEventListener<Component>> optionEventListeners
    ) {
        super(
                Component.literal("Label Option"),
                OptionDescription::of,
                LabelController::new,
                stateManager,
                true,
                ImmutableSet.of(),
                optionEventListeners
        );
    }

    public LabelOptionImpl(Component label) {
        this(
                StateManager.createImmutable(label),
                ImmutableSet.of()
        );
    }

    @Override
    public @NotNull Component label() {
        return stateManager().get();
    }

    @Override
    public void setAvailable(boolean available) {
        throw new UnsupportedOperationException("Cannot change availability of label option");
    }

    @ApiStatus.Internal
    public static final class BuilderImpl implements LabelOption.Builder {
        private StateManager<Component> stateManager;
        private final List<Component> lines = new ArrayList<>();

        @Override
        public LabelOption.Builder state(@NotNull StateManager<Component> stateManager) {
            Validate.notNull(stateManager, "`stateManager` must not be null");
            Validate.isTrue(this.lines.isEmpty(), "Cannot set state manager if lines have already been defined");

            this.stateManager = stateManager;
            return this;
        }

        @Override
        public LabelOption.Builder line(@NotNull Component line) {
            Validate.isTrue(stateManager == null, ".line() is a helper to create a state manager for you at build. If you have defined a custom state manager, do not use .line()");
            Validate.notNull(line, "`line` must not be null");

            this.lines.add(line);
            return this;
        }

        @Override
        public LabelOption.Builder lines(@NotNull Collection<? extends Component> lines) {
            Validate.isTrue(stateManager == null, ".lines() is a helper to create a state manager for you at build. If you have defined a custom state manager, do not use .lines()");

            this.lines.addAll(lines);
            return this;
        }

        @Override
        public LabelOption build() {
            Validate.isTrue(stateManager != null || !lines.isEmpty(), "Cannot build label option without a state manager or lines");

            if (!lines.isEmpty()) {
                MutableComponent text = Component.empty();
                Iterator<Component> iterator = lines.iterator();
                while (iterator.hasNext()) {
                    text.append(iterator.next());

                    if (iterator.hasNext())
                        text.append("\n");
                }
                this.stateManager = StateManager.createSimple(new SelfContainedBinding<>(text));
            }

            return new LabelOptionImpl(this.stateManager, ImmutableSet.of());
        }
    }
}