aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java
blob: 2db8acd4877eebc37e57c8e37efb9377b9ea698d (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package dev.isxander.yacl.impl;

import com.google.common.collect.ImmutableList;
import dev.isxander.yacl.api.ListOption;
import dev.isxander.yacl.api.Option;
import dev.isxander.yacl.api.OptionDescription;
import dev.isxander.yacl.api.OptionGroup;
import dev.isxander.yacl.impl.utils.YACLConstants;
import net.minecraft.network.chat.Component;
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.List;

@ApiStatus.Internal
public final class OptionGroupImpl implements OptionGroup {
    private final @NotNull Component name;
    private final @NotNull OptionDescription description;
    private final ImmutableList<? extends Option<?>> options;
    private final boolean collapsed;
    private final boolean isRoot;

    public OptionGroupImpl(@NotNull Component name, @NotNull OptionDescription description, ImmutableList<? extends Option<?>> options, boolean collapsed, boolean isRoot) {
        this.name = name;
        this.description = description;
        this.options = options;
        this.collapsed = collapsed;
        this.isRoot = isRoot;
    }

    @Override
    public @NotNull Component name() {
        return name;
    }

    @Override
    public OptionDescription description() {
        return description;
    }

    @Override
    public @NotNull Component tooltip() {
        return description.description();
    }

    @Override
    public @NotNull ImmutableList<? extends Option<?>> options() {
        return options;
    }

    @Override
    public boolean collapsed() {
        return collapsed;
    }

    @Override
    public boolean isRoot() {
        return isRoot;
    }

    @ApiStatus.Internal
    public static final class BuilderImpl implements Builder {
        private Component name = Component.empty();
        private OptionDescription description = null;
        private OptionDescription.Builder legacyBuilder = null;
        private final List<Option<?>> options = new ArrayList<>();
        private boolean collapsed = false;

        @Override
        public Builder name(@NotNull Component name) {
            Validate.notNull(name, "`name` must not be null");

            this.name = name;
            return this;
        }

        @Override
        public Builder description(@NotNull OptionDescription description) {
            Validate.isTrue(legacyBuilder == null, "Cannot set description when deprecated `tooltip` method is used");
            Validate.notNull(description, "`description` must not be null");

            this.description = description;
            return this;
        }

        @Override
        public Builder tooltip(@NotNull Component... tooltips) {
            Validate.isTrue(description == null, "Cannot use deprecated `tooltip` method when `description` in use.");
            Validate.notEmpty(tooltips, "`tooltips` cannot be empty");

            ensureLegacyDescriptionBuilder();

            legacyBuilder.description(tooltips);
            return this;
        }

        @Override
        public Builder option(@NotNull Option<?> option) {
            Validate.notNull(option, "`option` must not be null");

            if (option instanceof ListOption<?>)
                throw new UnsupportedOperationException("List options must not be added as an option but a group!");

            this.options.add(option);
            return this;
        }

        @Override
        public Builder options(@NotNull Collection<? extends Option<?>> options) {
            Validate.notEmpty(options, "`options` must not be empty");

            if (options.stream().anyMatch(ListOption.class::isInstance))
                throw new UnsupportedOperationException("List options must not be added as an option but a group!");

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

        @Override
        public Builder collapsed(boolean collapsible) {
            this.collapsed = collapsible;
            return this;
        }

        @Override
        public OptionGroup build() {
            Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`");

            if (description == null) {
                if (ensureLegacyDescriptionBuilder())
                    YACLConstants.LOGGER.warn("Using deprecated `tooltip` method in option group '{}'. Use `description` instead.", name != null ? name.getString() : "unnamed group");

                description = legacyBuilder.build();
            }

            return new OptionGroupImpl(name, description, ImmutableList.copyOf(options), collapsed, false);
        }

        private boolean ensureLegacyDescriptionBuilder() {
            if (legacyBuilder == null) {
                legacyBuilder = OptionDescription.createBuilder();
                return false;
            } else {
                return true;
            }
        }
    }
}