Skip to content

Commit b25bc7c

Browse files
committed
add options system
1 parent 8ac9a12 commit b25bc7c

File tree

13 files changed

+822
-14
lines changed

13 files changed

+822
-14
lines changed

src/main/java/net/oceanias/opal/Opal.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.oceanias.opal.database.ODatabase;
66
import net.oceanias.opal.component.impl.OModule;
77
import net.oceanias.opal.module.admin.OAdminModule;
8+
import net.oceanias.opal.option.impl.StringOption;
89
import net.oceanias.opal.plugin.OPlugin;
910
import net.oceanias.opal.utility.helper.OTeleportHelper;
1011
import java.util.List;
@@ -61,6 +62,7 @@ protected void setInstance() {
6162

6263
@Override
6364
protected void enablePlugin() {
65+
new StringOption.Listener(this).registerInternally();
6466
new OTeleportHelper(this).registerInternally();
6567
}
6668
}

src/main/java/net/oceanias/opal/command/OCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.oceanias.opal.plugin.OPlugin;
55
import net.oceanias.opal.utility.extension.OCommandSenderExtension;
66
import net.oceanias.opal.utility.extension.OStringExtension;
7+
import net.oceanias.opal.utility.helper.OTextHelper;
78
import java.lang.reflect.Field;
89
import java.util.*;
910
import java.util.concurrent.ConcurrentHashMap;
@@ -124,7 +125,7 @@ private void attachHelp(final Object node) throws Exception {
124125

125126
private void sendHelp(final CommandSender sender, final CommandArguments args) {
126127
final List<Component> lines = new ArrayList<>(List.of(
127-
OStringExtension.CHAT_DIVIDER_LONG.deserialize(),
128+
OTextHelper.CHAT_DIVIDER_LONG.deserialize(),
128129
(getPlugin().getColour() + WordUtils.capitalize(getLabel()) + " Commands:").deserialize()
129130
));
130131

@@ -162,7 +163,7 @@ private void sendHelp(final CommandSender sender, final CommandArguments args) {
162163
lines.add(component);
163164
}
164165

165-
lines.add(OStringExtension.CHAT_DIVIDER_LONG.deserialize());
166+
lines.add(OTextHelper.CHAT_DIVIDER_LONG.deserialize());
166167

167168
sender.sendMessage(Component.join(JoinConfiguration.separator(Component.newline()), lines));
168169
sender.soundDSR(Sound.BLOCK_NOTE_BLOCK_CHIME);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.oceanias.opal.option;
2+
3+
import java.util.List;
4+
import org.bukkit.Material;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import lombok.experimental.Accessors;
8+
9+
@SuppressWarnings("unused")
10+
@Getter
11+
@Accessors(fluent = true, chain = false)
12+
public abstract class Option<T> {
13+
protected final String pretty;
14+
protected final T initial;
15+
16+
@Setter
17+
protected List<String> description;
18+
19+
@Setter
20+
protected Material material;
21+
22+
@Setter
23+
protected T value;
24+
25+
protected Option(final String pretty, final T initial) {
26+
this.pretty = pretty;
27+
this.initial = initial;
28+
29+
value = initial;
30+
}
31+
32+
public void reset() {
33+
this.value = initial;
34+
}
35+
36+
public abstract Type type();
37+
38+
public enum Type {
39+
BOOLEAN,
40+
CHOICE,
41+
DOUBLE,
42+
INTEGER,
43+
STRING,
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package net.oceanias.opal.option.impl;
2+
3+
import net.oceanias.opal.option.Option;
4+
import net.oceanias.opal.utility.builder.OItemBuilder;
5+
import net.oceanias.opal.utility.extension.OCommandSenderExtension;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.bukkit.Material;
9+
import org.bukkit.Sound;
10+
import org.bukkit.entity.Player;
11+
import org.bukkit.event.inventory.ClickType;
12+
import org.bukkit.event.inventory.InventoryClickEvent;
13+
import xyz.xenondevs.invui.item.ItemProvider;
14+
import xyz.xenondevs.invui.item.impl.AbstractItem;
15+
import lombok.RequiredArgsConstructor;
16+
import lombok.experimental.ExtensionMethod;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
@SuppressWarnings("unused")
20+
@ExtensionMethod(OCommandSenderExtension.class)
21+
public final class BooleanOption extends Option<Boolean> {
22+
public BooleanOption(final String pretty, final boolean initial) {
23+
super(pretty, initial);
24+
}
25+
26+
@Override
27+
public Type type() {
28+
return Type.BOOLEAN;
29+
}
30+
31+
@RequiredArgsConstructor
32+
public static final class Item extends AbstractItem {
33+
private final BooleanOption option;
34+
35+
@Override
36+
public ItemProvider getItemProvider(final Player viewer) {
37+
final Boolean value = option.value;
38+
final Material material = option.material;
39+
final String state = value ? "&aEnabled" : "&cDisabled";
40+
41+
final List<String> description = option.description;
42+
final List<String> lore = new ArrayList<>();
43+
44+
final Material type = material != null
45+
? material
46+
: (value ? Material.LIME_STAINED_GLASS_PANE : Material.RED_STAINED_GLASS_PANE);
47+
48+
if (description != null && !description.isEmpty()) {
49+
lore.addAll(description.stream().map(line -> "&7" + line).toList());
50+
lore.add("");
51+
}
52+
53+
lore.add("&fState: " + state);
54+
55+
lore.add("");
56+
lore.add("&eClick &7to toggle!");
57+
58+
return new OItemBuilder(type)
59+
.setName("&e" + option.pretty)
60+
.addLore(lore);
61+
}
62+
63+
@Override
64+
public void handleClick(
65+
@NotNull final ClickType click, @NotNull final Player player, @NotNull final InventoryClickEvent event
66+
) {
67+
option.value(!option.value);
68+
69+
player.soundDSR(Sound.BLOCK_NOTE_BLOCK_PLING);
70+
71+
notifyWindows();
72+
}
73+
}
74+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package net.oceanias.opal.option.impl;
2+
3+
import net.oceanias.opal.option.Option;
4+
import net.oceanias.opal.utility.builder.OItemBuilder;
5+
import net.oceanias.opal.utility.extension.OCommandSenderExtension;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.bukkit.Material;
9+
import org.bukkit.Sound;
10+
import org.bukkit.entity.Player;
11+
import org.bukkit.event.inventory.ClickType;
12+
import org.bukkit.event.inventory.InventoryClickEvent;
13+
import xyz.xenondevs.invui.item.ItemProvider;
14+
import xyz.xenondevs.invui.item.impl.AbstractItem;
15+
import lombok.Getter;
16+
import lombok.RequiredArgsConstructor;
17+
import lombok.experimental.ExtensionMethod;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
@ExtensionMethod(OCommandSenderExtension.class)
21+
@SuppressWarnings("unused")
22+
@Getter
23+
public final class ChoiceOption<T> extends Option<T> {
24+
private final List<T> choices;
25+
26+
public ChoiceOption(final String pretty, final T initial, final @NotNull List<T> choices) {
27+
super(pretty, initial);
28+
29+
this.choices = choices;
30+
31+
if (choices.contains(initial)) {
32+
return;
33+
}
34+
35+
throw new IllegalArgumentException("Error creating option; initial value must be in choices.");
36+
}
37+
38+
@Override
39+
public void value(final T value) {
40+
if (!choices.contains(value)) {
41+
return;
42+
}
43+
44+
this.value = value;
45+
}
46+
47+
@Override
48+
public Type type() {
49+
return Type.CHOICE;
50+
}
51+
52+
@RequiredArgsConstructor
53+
public static final class Item<T> extends AbstractItem {
54+
private final ChoiceOption<T> option;
55+
56+
@Override
57+
public ItemProvider getItemProvider(final Player viewer) {
58+
final Material material = option.material;
59+
60+
final List<String> description = option.description;
61+
final List<String> lore = new ArrayList<>();
62+
63+
final Material type = material != null
64+
? material
65+
: Material.COMPASS;
66+
67+
if (description != null && !description.isEmpty()) {
68+
lore.addAll(description.stream().map(line -> "&7" + line).toList());
69+
lore.add("");
70+
}
71+
72+
lore.add("&fCurrent: &6" + option.value);
73+
lore.add("");
74+
lore.add("&fChoices:");
75+
76+
for (final T choice : option.choices) {
77+
final String colour = choice.equals(option.value) ? "&a" : "&7";
78+
79+
lore.add(colour + "• " + choice);
80+
}
81+
82+
lore.add("");
83+
lore.add("&eLeft-click &7to cycle forwards.");
84+
lore.add("&eRight-click &7to cycle backwards.");
85+
86+
return new OItemBuilder(type)
87+
.setName("&e" + option.pretty)
88+
.addLore(lore);
89+
}
90+
91+
@Override
92+
public void handleClick(
93+
@NotNull final ClickType click, @NotNull final Player player, @NotNull final InventoryClickEvent event
94+
) {
95+
final List<T> choices = option.choices;
96+
97+
final int current = choices.indexOf(option.value);
98+
final int size = choices.size();
99+
100+
if (click.isLeftClick()) {
101+
option.value(choices.get((current + 1) % size));
102+
} else if (click.isRightClick()) {
103+
option.value(choices.get((current - 1 + size) % size));
104+
}
105+
106+
player.soundDSR(Sound.BLOCK_NOTE_BLOCK_BIT);
107+
108+
notifyWindows();
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)