Skip to content

Commit

Permalink
feat(perm): Support different length for different permissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 26, 2024
1 parent b93bc30 commit 6427e92
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<project.package>${project.groupId}.plugin.${project.groupId}</project.package>
<deps.core.version>1.0.2</deps.core.version>
<project.package>${project.groupId}.plugin.${project.artifactId}</project.package>
<deps.core.version>1.1.0</deps.core.version>
</properties>
<groupId>com.artformgames</groupId>
<artifactId>usersuffix</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>

<name>UserSuffix</name>
<description>
Expand Down Expand Up @@ -106,16 +106,16 @@
<dependencies>

<dependency>
<groupId>com.artformgames</groupId>
<artifactId>artcore-api</artifactId>
<version>${deps.core.version}</version>
<scope>provided</scope>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.4</version>
<groupId>com.artformgames</groupId>
<artifactId>artcore-api</artifactId>
<version>${deps.core.version}</version>
<scope>provided</scope>
</dependency>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/artformgames/plugin/usersuffix/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ protected boolean initialize() {
log("Version checker is disabled, skipped.");
}

log("Register placeholders...");
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new SuffixPlaceholder(this, getName());
log("Register placeholders...");
new SuffixPlaceholder(this, getName()).register();
} else {
log("PlaceholderAPI is not enabled, skipped.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.stream.Collectors;

@Command(name = "usersuffix", aliases = "suffix")
Expand All @@ -31,8 +32,12 @@ void clearSuffix(@Context Player player) {

@Execute(name = "content")
void setContent(@Context Player player, @Join String content) {
if (content.contains("&")) {
PluginMessages.CONTAIN_COLOR_CODE.send(player);
int maxLength = PluginConfig.MAX_LENGTH.entrySet().stream()
.filter(e -> player.hasPermission(e.getValue()))
.mapToInt(Map.Entry::getKey).max().orElse(-1);

if (maxLength <= 0) {
PluginMessages.NO_PERMISSION.send(player);
return;
}

Expand All @@ -41,7 +46,8 @@ void setContent(@Context Player player, @Join String content) {
return;
}

if (content.length() > PluginConfig.MAX_LENGTH.getNotNull()) {

if (ColorParser.clear(content).length() > maxLength) {
PluginMessages.TOO_LONG.send(player, PluginConfig.MAX_LENGTH.getNotNull());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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.impl.ConfigValueMap;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import org.bukkit.ChatColor;

Expand Down Expand Up @@ -49,7 +51,12 @@ public interface PluginConfig extends Configuration {
ChatColor.YELLOW, ChatColor.WHITE
).build();

@HeaderComment("Maximum length of the suffix (excluding color symbols)")
ConfigValue<Integer> MAX_LENGTH = ConfiguredValue.of(8);
@HeaderComment("Maximum length of the suffix of each permissions (excluding color symbols)")
ConfiguredMap<Integer, String> MAX_LENGTH = ConfigValueMap.builderOf(Integer.class, String.class)
.asLinkedMap().fromString().parseKey(Integer::parseInt).parseValue(Object::toString)
.defaults(m -> {
m.put(8, "usersuffix.use");
m.put(12, "group.vip");
}).build();

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

import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.core.conf.MessagesRoot;
import com.artformgames.core.conf.Messages;
import net.md_5.bungee.api.chat.BaseComponent;

public class PluginMessages extends MessagesRoot {
public interface PluginMessages extends Messages {

public static final ConfiguredMessageList<BaseComponent[]> CLEARED = list()
ConfiguredMessageList<BaseComponent[]> CLEARED = Messages.list()
.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()
ConfiguredMessageList<BaseComponent[]> NO_PERMISSION = Messages.list()
.defaults("&c&lSorry! &fBut you dont have enough permission to set a suffix!")
.build();

ConfiguredMessageList<BaseComponent[]> TOO_LONG = Messages.list()
.defaults("&fThe suffix you want to set is too long, no longer than &e% (length) &f characters (excluding colors).")
.params("length")
.build();

public static final ConfiguredMessageList<BaseComponent[]> TOO_SHORT = list()
ConfiguredMessageList<BaseComponent[]> TOO_SHORT = Messages.list()
.defaults("&fYour suffix contains at least one character (excluding colors).")
.build();

public static final ConfiguredMessageList<BaseComponent[]> SUCCESS = list()
ConfiguredMessageList<BaseComponent[]> SUCCESS = Messages.list()
.defaults("&fSuccessfully modified your suffix to &r%(suffix) &f.")
.params("suffix")
.build();

public static final ConfiguredMessageList<BaseComponent[]> FAILED = list()
ConfiguredMessageList<BaseComponent[]> FAILED = Messages.list()
.defaults("&fYou can't use this suffix because it may contain a violating word, please try again after changing something else.")
.build();

public static final ConfiguredMessageList<BaseComponent[]> NO_SPACE = list()
ConfiguredMessageList<BaseComponent[]> NO_SPACE = Messages.list()
.defaults("&fSuffix should not contain spaces.")
.build();

public static final ConfiguredMessageList<BaseComponent[]> CONTAIN_COLOR_CODE = list()
ConfiguredMessageList<BaseComponent[]> CONTAIN_COLOR_CODE = Messages.list()
.defaults("&fYou can't include color codes directly in your suffix!")
.build();

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



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SuffixLoader extends UserHandlerLoader<SuffixAccount> {

protected SQLTable table = SQLTable.of("user_suffix", builder -> {
builder.addColumn("user", "INT UNSIGNED NOT NULL PRIMARY KEY");
builder.addColumn("suffix", "VARCHAR(16)");
builder.addColumn("content", "VARCHAR(255)");
builder.addColumn("color", "CHAR(1)");
});

Expand All @@ -43,9 +43,9 @@ public SuffixLoader(@NotNull Plugin plugin) {
.setLimit(1).build().execute()) {
ResultSet rs = query.getResultSet();
if (rs.next()) {
String suffix = rs.getString("suffix");
String content = rs.getString("content");
String color = rs.getString("color");
return new SuffixAccount(user, suffix, color == null ? null : ChatColor.getByChar(color.charAt(0)));
return new SuffixAccount(user, content, color == null ? null : ChatColor.getByChar(color.charAt(0)));
}
}
return null;
Expand All @@ -57,7 +57,7 @@ public void saveData(UserKey user, SuffixAccount handler) throws Exception {
table.createDelete().addCondition("user", user.id()).build().execute();
} else {
table.createInsert()
.setColumnNames("user", "suffix", "color")
.setColumnNames("user", "content", "color")
.setParams(
user.id(), handler.getContent(),
handler.getColor() == null ? null : handler.getColor().getChar()
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/PLUGIN_INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
&e _ _ &f ___ __ __ _
&e| | | |___ ___ _ _&f/ __|_ _ / _|/ _(_)_ __
&e| |_| (_-</ -_) '_&f\__ \ || | _| _| \ \ /
&e \___//__/\___|_| &f|___/\_,_|_| |_| |_/_\_\
&f UserSuffix &7v${project.version}
1 change: 0 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ api-version: 1.16

depend:
- ArtCore
- LuckPerms

softdepend:
- PlaceholderAPI
Expand Down

0 comments on commit 6427e92

Please sign in to comment.