Skip to content

Commit

Permalink
feat: Finished placeholders and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Dec 28, 2023
1 parent 5c30858 commit 1bf117d
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 37 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ see [Dependencies](https://github.com/ArtformGames/UserSuffix/network/dependenc

## Commands

The main command is `/UserSuffix` or `/title`.
The main command is `/UserSuffix` or `/suffix`.

```text
# set <Content> [Color]
# content <Content>
@ User command
- Set the title of yourself.
- Set the content of your suffix.
# color <brackets-Color>
@User command
- Set the brackets color of your suffix.
```

## Configurations
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<project.package>${project.groupId}.plugin.${project.groupId}</project.package>
<deps.core.version>1.0.1</deps.core.version>
<deps.core.version>1.0.2</deps.core.version>
</properties>
<groupId>com.artformgames</groupId>
<artifactId>usersuffix</artifactId>
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/artformgames/plugin/usersuffix/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import com.artformgames.plugin.usersuffix.command.UserSuffixCommands;
import com.artformgames.plugin.usersuffix.conf.PluginConfig;
import com.artformgames.plugin.usersuffix.conf.PluginMessages;
import com.artformgames.plugin.usersuffix.user.SuffixAccount;
import com.artformgames.plugin.usersuffix.hooker.SuffixPlaceholder;
import com.artformgames.plugin.usersuffix.user.SuffixLoader;
import dev.rollczi.litecommands.LiteCommands;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

public class Main extends EasyPlugin {
Expand All @@ -29,8 +30,8 @@ protected boolean initialize() {

log("Loading plugin configurations...");
configuration = new MineConfiguration(this);
configuration.initializeConfig(PluginConfig.class);
configuration.initializeMessage(PluginMessages.class);
configuration.getConfigProvider().initialize(PluginConfig.class);
configuration.getMessageProvider().initialize(PluginMessages.class);

log("Register handlers...");
ArtCore.getUserManager().registerHandler(new SuffixLoader(this));
Expand All @@ -46,6 +47,14 @@ protected boolean initialize() {
log("Version checker is disabled, skipped.");
}


log("Register placeholders...");
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new SuffixPlaceholder(this, getName());
} else {
log("PlaceholderAPI is not enabled, skipped.");
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,76 @@
package com.artformgames.plugin.usersuffix.command;

import cc.carm.lib.easyplugin.utils.ColorParser;
import com.artformgames.core.ArtCore;
import com.artformgames.plugin.usersuffix.conf.PluginConfig;
import com.artformgames.plugin.usersuffix.conf.PluginMessages;
import com.artformgames.plugin.usersuffix.user.SuffixAccount;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.join.Join;
import dev.rollczi.litecommands.annotations.permission.Permission;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.stream.Collectors;

@Command(name = "usersuffix", aliases = "suffix")
@Permission("usersuffix.use")
public class UserSuffixCommands {

@Execute
void set(@Context Player player, @Arg String suffix, @OptionalArg String blanketColor) {

@Execute(name = "clear")
void clearSuffix(@Context Player player) {
SuffixAccount account = ArtCore.getHandler(player, SuffixAccount.class);
account.setContent(null);
account.setColor(null);
PluginMessages.CLEARED.send(player);
}

public static boolean setSuffix(SuffixAccount data, Player player, String color, String suffixText) {
String suffix = color + suffixText + color;
if (!SuffixAccount.validColor(color)) {
PluginMessages.INVALID_COLOR_CODE.send(player);
} else if (suffixText.contains("&")) {

@Execute(name = "content")
void setContent(@Context Player player, @Join String content) {
if (content.contains("&")) {
PluginMessages.CONTAIN_COLOR_CODE.send(player);
} else if (suffixText.length() > PluginConfig.MAX_LENGTH.getNotNull()) {
return;
}

if (content.contains(" ")) {
PluginMessages.NO_SPACE.send(player);
return;
}

if (content.length() > PluginConfig.MAX_LENGTH.getNotNull()) {
PluginMessages.TOO_LONG.send(player, PluginConfig.MAX_LENGTH.getNotNull());
} else {
data.setSuffix(suffixText, ChatColor.getByChar(color));
PluginMessages.SUCCESS.send(player, PlaceholderAPI.setPlaceholders(player, "%artessentials_suffix%"));
return;
}

if (content.isBlank()) {
PluginMessages.TOO_SHORT.send(player);
return;
}

SuffixAccount account = ArtCore.getHandler(player, SuffixAccount.class);
account.setContent(content.isBlank() ? null : content);
PluginMessages.SUCCESS.send(player, account.getSuffix());
}


@Execute(name = "color")
void setBracketsColor(@Context Player player, @Arg ChatColor color) {
if (!color.isColor() || !PluginConfig.ALLOWED_COLORS.contains(color)) {
PluginMessages.INVALID_COLOR_CODE.send(player);
String colors = PluginConfig.ALLOWED_COLORS.copy().stream()
.map(c -> "&" + c.getChar() + c.name())
.collect(Collectors.joining(", "));
player.sendMessage(ColorParser.parse(colors));
return;
}

return true;
SuffixAccount account = ArtCore.getHandler(player, SuffixAccount.class);
account.setColor(color);
PluginMessages.SUCCESS.send(player, account.getSuffix());
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
package com.artformgames.plugin.usersuffix.conf;

import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.Configuration;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.bukkit.ChatColor;

public class PluginConfig extends ConfigurationRoot {
public interface PluginConfig extends Configuration {

public static final ConfiguredValue<Boolean> DEBUG = ConfiguredValue.of(Boolean.class, false);
ConfiguredValue<Boolean> DEBUG = ConfiguredValue.of(false);

@HeaderComment({
"Check update settings",
"This option is used by the plug-in to determine whether to check for updates.",
"If you do not want the plug-in to check for updates and prompt you, you can choose to close.",
"Checking for updates is an asynchronous operation that will never affect performance and user experience."
})
public static final ConfiguredValue<Boolean> CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true);

@HeaderComment("Suffix weight in LuckPerms")
public static final ConfigValue<Integer> WEIGHT = ConfiguredValue.of(Integer.class, 1);
ConfiguredValue<Boolean> CHECK_UPDATE = ConfiguredValue.of(true);

@HeaderComment({"Suffix format. The `%(suffix)` will be replaced with the suffix content."})
public static final ConfigValue<String> FORMAT = ConfiguredValue.of(String.class, " %(color)[%(suffix)%(color)]");
ConfigValue<String> FORMAT = ConfiguredValue.of(" %(color)[%(suffix)%(color)]");

@HeaderComment("The default color for the suffix format section.")
public static final ConfigValue<String> DEFAULT_COLOR = ConfiguredValue.of(String.class, "&7");
ConfigValue<ChatColor> DEFAULT_COLOR = ConfiguredValue.builderOf(ChatColor.class).fromString()
.parseValue(c -> ChatColor.valueOf(c.toUpperCase())).serializeValue(ChatColor::name)
.defaults(ChatColor.GRAY).build();

@HeaderComment("Allowed colors for the suffix format section.")
ConfiguredList<ChatColor> ALLOWED_COLORS = ConfiguredList.builderOf(ChatColor.class).fromString()
.parseValue(c -> ChatColor.valueOf(c.toUpperCase())).serializeValue(ChatColor::name)
.defaults(
ChatColor.BLACK, ChatColor.DARK_BLUE,
ChatColor.DARK_GREEN, ChatColor.DARK_AQUA,
ChatColor.DARK_RED, ChatColor.DARK_PURPLE,
ChatColor.GOLD, ChatColor.GRAY,
ChatColor.DARK_GRAY, ChatColor.BLUE,
ChatColor.GREEN, ChatColor.AQUA,
ChatColor.RED, ChatColor.LIGHT_PURPLE,
ChatColor.YELLOW, ChatColor.WHITE
).build();

@HeaderComment("Maximum length of the suffix (excluding color symbols)")
public static final ConfigValue<Integer> MAX_LENGTH = ConfiguredValue.of(Integer.class, 8);
ConfigValue<Integer> MAX_LENGTH = ConfiguredValue.of(8);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class PluginMessages extends MessagesRoot {

public static final ConfiguredMessageList<BaseComponent[]> CLEARED = list()
.defaults("&f The existing suffix has been cleared for you. To set up, enter &e/suffix <content> &f.")
.defaults("&fThe existing suffix has been cleared for you. To set up again, enter &e/suffix content <content> &f.")
.build();

public static final ConfiguredMessageList<BaseComponent[]> TOO_LONG = list()
Expand Down Expand Up @@ -37,7 +37,11 @@ public class PluginMessages extends MessagesRoot {
.build();

public static final ConfiguredMessageList<BaseComponent[]> INVALID_COLOR_CODE = list()
.defaults("&fYour color code is invalid!")
.build();
.defaults(
"&fYour color is invalid!",
"&fYou can choose from the following colors:"
).build();



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.artformgames.plugin.usersuffix.hooker;

import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
import com.artformgames.core.ArtCore;
import com.artformgames.plugin.usersuffix.user.SuffixAccount;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import panda.std.function.TriFunction;

import java.util.Optional;

public class SuffixPlaceholder extends EasyPlaceholder {

public SuffixPlaceholder(@NotNull JavaPlugin plugin, @NotNull String rootIdentifier) {
super(plugin, rootIdentifier);
handleSuffix("suffix", (player, account, args) -> account.getSuffix());
handleSuffix("content", ((player, account, args) -> Optional.ofNullable(account.getContent()).orElse("")));
handleSuffix("color", ((player, account, args) -> account.getColorCode()));
}

protected void handleSuffix(String id, TriFunction<Player, SuffixAccount, String[], String> handler) {
handle(id, (player, args) -> {
if (!(player instanceof Player onlinePlayer)) return "Offline";
SuffixAccount account = ArtCore.getHandler(onlinePlayer, SuffixAccount.class);
return handler.apply(onlinePlayer, account, args);
});
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void setContent(@Nullable String content) {
.replace("%(suffix)", this.content));
}

protected @NotNull String getColorCode() {
return this.color != null ? "&" + this.color.getChar() : PluginConfig.DEFAULT_COLOR.getNotNull();
public @NotNull String getColorCode() {
return this.color != null ? "&" + this.color.getChar() : "&" + PluginConfig.DEFAULT_COLOR.getNotNull().getChar();
}

public void setSuffix(@Nullable String content, @Nullable ChatColor color) {
Expand Down

0 comments on commit 1bf117d

Please sign in to comment.