Skip to content

Commit

Permalink
Add gui language and config
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyboy-roks committed Aug 12, 2023
1 parent e0fdca8 commit 1c23261
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

if (args.length == 0) {
new NickColorGui().openGui(player);
return true;
if (HexNicks.config().NICKCOLOR_GUI) {
new NickColorGui().openGui(player);
return true;
} else {
return false;
}
}

String nickInput = String.join(" ", args);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ConfigValues {
public Boolean PREVENT_DUPLICATE_NICKS;
public Boolean PREVENT_DUPLICATE_NICKS_STRICT;
public List<String> BLOCKED_NICKNAMES;
public Boolean NICKCOLOR_GUI;
public Boolean DEBUG;

public ConfigValues() {
Expand All @@ -84,6 +85,7 @@ public void reload() {
PREVENT_DUPLICATE_NICKS = HexNicks.core().getConfig().getBoolean("prevent-duplicate-nicks", true);
PREVENT_DUPLICATE_NICKS_STRICT = HexNicks.core().getConfig().getBoolean("prevent-duplicate-nicks-strict", false);
BLOCKED_NICKNAMES = HexNicks.core().getConfig().getStringList("blocked-nicknames");
NICKCOLOR_GUI = HexNicks.core().getConfig().getBoolean("nickcolor-gui", true);
DEBUG = HexNicks.core().getConfig().getBoolean("debug", false);
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.majek.hexnicks.util.MiscUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -120,6 +121,27 @@ public interface Messages {
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(player.getName()).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%nick%").replacement(nickname).build());

Args0 GUI_BACK = () -> MiscUtils.configString("messages.guiBack", "<yellow><!italic>Back");

Args0 GUI_NICK_COLOR_TITLE = () -> MiscUtils.configString("messages.guiNickColorTitle", "Nick Color");

Args1<TextColor> GUI_NICK_COLOR_HEX_TITLE = (col) -> MiniMessage.miniMessage().deserialize(HexNicks.core().getConfig().getString(
"messages.guiNickColorHexTitle",
"Nick Color - <color:%color%>%color%"
).replace("%color%", col.toString()));

Args0 GUI_NICK_COLOR_RANDOM_HEX = () -> MiscUtils.configString("messages.guiNickColorRandomHex", "<!italic><rainbow>Random Hexadecimal Color");

Args1<Integer> GUI_NICK_COLOR_HEX_RED = (c) -> MiscUtils.configString("messages.guiNickColorHexRed", "<!italic><red>Red %step%")
.replaceText(TextReplacementConfig.builder().matchLiteral("%step%").replacement("%+d".formatted(c)).build());
Args1<Integer> GUI_NICK_COLOR_HEX_GREEN = (c) -> MiscUtils.configString("messages.guiNickColorHexGreen", "<!italic><green>Green %step%")
.replaceText(TextReplacementConfig.builder().matchLiteral("%step%").replacement("%+d".formatted(c)).build());
Args1<Integer> GUI_NICK_COLOR_HEX_BLUE = (c) -> MiscUtils.configString("messages.guiNickColorHexBlue", "<!italic><blue>Blue %step%")
.replaceText(TextReplacementConfig.builder().matchLiteral("%step%").replacement("%+d".formatted(c)).build());

Args0 GUI_NICK_COLOR_HEX_BUTTON = () -> MiscUtils.configString("messages.guiNickColorHexButton", "<!italic><yellow>Custom Hexadecimal Color");
Args0 GUI_NICK_COLOR_HEX_SAVE = () -> MiscUtils.configString("messages.guiNickColorHexSave", "<!italic><yellow>Click to save nickname");

/**
* A message that has no arguments that need to be replaced.
*/
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/dev/majek/hexnicks/gui/NickColorGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.majek.hexnicks.command.CommandHexNicks;
import dev.majek.hexnicks.command.CommandNickColor;
import dev.majek.hexnicks.config.Messages;
import dev.majek.hexnicks.util.CustomHead;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -79,18 +80,12 @@ private static ItemStack generateItem(@NotNull String name, @NotNull TextColor c

RAINBOW = CustomHead.RAINBOW.asItemStack();
ItemMeta meta = RAINBOW.getItemMeta();
// TODO: Language
meta.displayName(Component.text("Hexadecimal Color").decoration(TextDecoration.ITALIC, false));
meta.lore(List.of(
// TODO: Language
Component.text("Create a custom hexadecimal color", NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false)
));
meta.displayName(Messages.GUI_NICK_COLOR_HEX_BUTTON.build());
RAINBOW.setItemMeta(meta);
}

public NickColorGui() {
// TODO: Language
super(54, Component.text("Nick Color"));
super(54, Messages.GUI_NICK_COLOR_TITLE.build());
}

/**
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/dev/majek/hexnicks/gui/NickColorHexGui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.majek.hexnicks.gui;

import dev.majek.hexnicks.HexNicks;
import dev.majek.hexnicks.config.Messages;
import dev.majek.hexnicks.message.MiniMessageWrapper;
import dev.majek.hexnicks.util.CustomHead;
import dev.majek.hexnicks.util.MiscUtils;
Expand Down Expand Up @@ -43,18 +44,16 @@ public class NickColorHexGui extends Gui {
/**
* Generate the array of items that should be used for changing the r/g/b values
*
* @param name the name of the value that should be displayed (i.e. "Red")
* @param color The colour of the text to be displayed
* @param base The base {@link ItemStack} that should be used (this is cloned for each item in the array)
* @param msg The message that should be used for the name (passed the step)
* @param base The base {@link ItemStack} that should be used (this is cloned for each item in the array)
* @return An array with 6 elements that maps to the values in {@code STEPS}
*/
private static ItemStack[] generateItems(String name, NamedTextColor color, ItemStack base) {
private static ItemStack[] generateItems(Messages.Args1<Integer> msg, ItemStack base) {
ItemStack[] out = new ItemStack[6];
Component nameComp = Component.text(name, color).decoration(TextDecoration.ITALIC, false);
ItemMeta meta = base.getItemMeta();
for (int i = 0; i < 6; i++) {
int step = STEPS[i];
meta.displayName(nameComp.append(Component.space()).append(Component.text("%+d".formatted(step))));
meta.displayName(msg.build(step));
out[i] = base.clone();
out[i].setAmount(Math.abs(step));
out[i].setItemMeta(meta);
Expand All @@ -64,33 +63,29 @@ private static ItemStack[] generateItems(String name, NamedTextColor color, Item

static {
// @formatter:off
// TODO: Language
RED = generateItems("Red", NamedTextColor.RED, CustomHead.RED.asItemStack());
GREEN = generateItems("Green", NamedTextColor.GREEN, CustomHead.GREEN.asItemStack());
BLUE = generateItems("Blue", NamedTextColor.AQUA, CustomHead.AQUA.asItemStack());
RED = generateItems(Messages.GUI_NICK_COLOR_HEX_RED, CustomHead.RED.asItemStack());
GREEN = generateItems(Messages.GUI_NICK_COLOR_HEX_GREEN, CustomHead.GREEN.asItemStack());
BLUE = generateItems(Messages.GUI_NICK_COLOR_HEX_BLUE, CustomHead.AQUA.asItemStack());
// @formatter:on

// Generate the rainbow button
RAINBOW = CustomHead.RAINBOW.asItemStack();
ItemMeta meta = RAINBOW.getItemMeta();
// TODO: Language
meta.displayName(MiniMessageWrapper.standard().mmParse("<rainbow>Random Hexadecimal Color</rainbow>").decoration(TextDecoration.ITALIC, false));
meta.displayName(Messages.GUI_NICK_COLOR_RANDOM_HEX.build());
RAINBOW.setItemMeta(meta);

BACK_BUTTON = new ItemStack(Material.NETHER_STAR);
meta = BACK_BUTTON.getItemMeta();
// TODO: Language
meta.displayName(Component.text("Back", NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false));
meta.displayName(Messages.GUI_BACK.build());
BACK_BUTTON.setItemMeta(meta);
}

public NickColorHexGui() {
// TODO: Language
this(0x88, 0x88, 0x88);
}

public NickColorHexGui(int r, int g, int b) {
super(54, Component.text("Nick Color - ").append(Component.text(TextColor.color(r, g, b).toString(), TextColor.color(r, g, b))));
super(54, Messages.GUI_NICK_COLOR_HEX_TITLE.build(TextColor.color(r, g, b)));

this.red = r;
this.green = g;
Expand All @@ -105,7 +100,10 @@ private void updateName(boolean newGui) {
ItemStack s = new ItemStack(Material.NAME_TAG);
ItemMeta meta = s.getItemMeta();
meta.displayName(Component.text(this.nickname).color(col).decoration(TextDecoration.ITALIC, false));
meta.lore(List.of(Component.text(col.toString()).decoration(TextDecoration.ITALIC, false)));
meta.lore(List.of(
Component.text(col.toString()).decoration(TextDecoration.ITALIC, false),
Messages.GUI_NICK_COLOR_HEX_SAVE.build()
));
s.setItemMeta(meta);
addActionButton(20, s, () -> {
// Use the command since it already handles the logic
Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ blocked-nicknames:
- "replace these lines"
- "what to block"

# Whether a gui should open when running `/nickcolor` with no arguments -- `/nickcolor <color>` works either way.
nickcolor-gui: true

# Database settings
# Only enable this if you know what you're doing and have an existing database
database-enabled: false
Expand Down Expand Up @@ -105,4 +108,13 @@ messages:
latestLog: "<green>View the latest log <click:open_url:'%link%'><aqua><u>here</u></aqua></click>."
working: "<gray>Working..."
notAllowed: "<red>That nickname is not allowed!"
joinAnnouncement: "<yellow>%player% has the nickname</yellow> %nick%"
joinAnnouncement: "<yellow>%player% has the nickname</yellow> %nick%"
guiBack: "<yellow><!italic>Back"
guiNickColorTitle: "Nick Color"
guiNickColorHexTitle: "Nick Color - <color:%color%>%color%"
guiNickColorRandomHex: "<!italic><rainbow>Random Hexadecimal Color"
guiNickColorHexRed: "<!italic><red>Red %step%"
guiNickColorHexGreen: "<!italic><green>Green %step%"
guiNickColorHexBlue: "<!italic><blue>Blue %step%"
guiNickColorHexButton: "<!italic><yellow>Custom Hexadecimal Color"
guiNickColorHexSave: "<!italic><yellow>Click to save nickname"

0 comments on commit 1c23261

Please sign in to comment.