Skip to content

Commit

Permalink
v2.0.5 - new perms and dependency bumps
Browse files Browse the repository at this point in the history
- bumped adventure dependencies
- new MiniMessageWrapper to deal with removing mm tokens via regex

- 3 new perms, all given by default
- new perm `hexnicks.nick.color`: Permission to use standard color codes in nicknames.
- new perm `hexnicks.nick.hex`: Permission to use hex color codes in nicknames.
- new perm `hexnicks.nick.gradient`: Permission to use gradients in nicknames.
  • Loading branch information
Majekdor committed Sep 13, 2021
1 parent 094253c commit a1351b2
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 8 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.majek.hexnicks</groupId>
<artifactId>HexNicks</artifactId>
<version>2.0.4</version>
<version>2.0.5</version>
<packaging>jar</packaging>

<name>HexNicks</name>
Expand Down Expand Up @@ -144,7 +144,7 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<scope>compile</scope>
</dependency>
<!-- Adventure Platform Bukkit -->
Expand All @@ -165,21 +165,21 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<scope>compile</scope>
</dependency>
<!-- Adventure Gson Serializer -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-gson</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<scope>compile</scope>
</dependency>
<!-- Adventure Plain Text Serializer -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
<version>4.8.1</version>
<version>4.9.1</version>
<scope>compile</scope>
</dependency>
<!-- PlaceholderAPI -->
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/majek/hexnicks/command/CommandNick.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import dev.majek.hexnicks.config.NicksMessages;
import java.util.Collections;
import java.util.List;

import dev.majek.hexnicks.util.MiniMessageWrapper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
Expand Down Expand Up @@ -64,7 +66,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
nickInput = Nicks.utils().legacyToMini(nickInput);
}

Component nickname = MiniMessage.get().parse(nickInput);
MiniMessageWrapper wrapper = new MiniMessageWrapper(nickInput);

// Check permissions for colors
if(!player.hasPermission("hexnicks.nick.gradient")) {
wrapper.removeGradients();
}
if(!player.hasPermission("hexnicks.nick.hex")) {
wrapper.removeHex();
}
if(!player.hasPermission("hexnicks.nick.color")) {
wrapper.removeAllTokens();
}

Component nickname = wrapper.mmParse();
String plainTextNick = PlainTextComponentSerializer.plainText().serialize(nickname);
int maxLength = Nicks.config().MAX_LENGTH;
int minLength = Nicks.config().MIN_LENGTH;
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/majek/hexnicks/command/CommandNickColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import dev.majek.hexnicks.config.NicksMessages;
import java.util.Collections;
import java.util.List;

import dev.majek.hexnicks.util.MiniMessageWrapper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
Expand Down Expand Up @@ -72,10 +74,23 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

MiniMessageWrapper wrapper = new MiniMessageWrapper(nickInput);

// Check permissions for colors
if(!player.hasPermission("hexnicks.nick.gradient")) {
wrapper.removeGradients();
}
if(!player.hasPermission("hexnicks.nick.hex")) {
wrapper.removeHex();
}
if(!player.hasPermission("hexnicks.nick.color")) {
wrapper.removeAllTokens();
}

// Get the players current nickname to apply color codes to
String plainTextNick = PlainTextComponentSerializer.plainText()
.serialize(Nicks.core().getDisplayName(player));
Component nickname = MiniMessage.get().parse(nickInput + plainTextNick);
Component nickname = MiniMessage.get().parse(wrapper.mmString() + plainTextNick);

// Call event
NickColorEvent colorEvent = new NickColorEvent(player, nickname,
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/dev/majek/hexnicks/util/MiniMessageWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* This file is part of HexNicks, licensed under the MIT License.
*
* Copyright (c) 2020-2021 Majekdor
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.majek.hexnicks.util;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.jetbrains.annotations.NotNull;

public class MiniMessageWrapper {

private String mmString;

/**
* Create a new wrapper with a string with {@link MiniMessage} tags to eventually be parsed.
*
* @param mmString String with {@link MiniMessage} tags.
*/
public MiniMessageWrapper(@NotNull String mmString) {
this.mmString = mmString;
}

/**
* Remove {@link MiniMessage}'s hex color code tokens.
*
* @return Wrapper.
*/
public MiniMessageWrapper removeHex() {
this.mmString = this.mmString.replaceAll("<#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("<c:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</c:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</c>", "");
this.mmString = this.mmString.replaceAll("<color:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</color:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</color>", "");
this.mmString = this.mmString.replaceAll("<colour:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</colour:#([0-9a-fA-F]{6})>", "");
this.mmString = this.mmString.replaceAll("</colour>", "");
return this;
}

/**
* Remove {@link MiniMessage}'s gradient tokens.
*
* @return Wrapper.
*/
public MiniMessageWrapper removeGradients() {
this.mmString = this.mmString.replaceAll("<gradient([:#0-9a-fA-F]{8})+>", "");
this.mmString = this.mmString.replaceAll("</gradient>", "");
return this;
}

/**
* Remove all of {@link MiniMessage}'s tokens.
*
* @return Wrapper.
*/
public MiniMessageWrapper removeAllTokens() {
this.mmString = MiniMessage.get().stripTokens(mmString);
return this;
}

/**
* Parse the string passed through in the constructor with {@link MiniMessage}.
*
* @return Parsed {@link Component}.
*/
public Component mmParse() {
return MiniMessage.get().parse(mmString);
}

/**
* Return the modified string passed through in the constructor.
*
* @return Modified string.
*/
public String mmString() {
return mmString;
}
}
11 changes: 10 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ permissions:
hexnicks.nick:
description: Permission to change nicknames.
default: true
hexnicks.nick.color:
description: Permission to use standard color codes in nicknames.
default: true
hexnicks.nick.hex:
description: Permission to use hex color codes in nicknames.
default: true
hexnicks.nick.gradient:
description: Permission to use gradients in nicknames.
default: true
hexnicks.nick.other:
description: Permission to change other player's nicknames.
default: op
Expand All @@ -41,7 +50,7 @@ permissions:
description: Permission to remove other player's nicknames.
default: op
hexnicks.nickcolor:
description: Permission to change color of nickname.
description: Permission to use the nickcolor command to change only the nickname's color.
default: true
hexnicks.reload:
description: Permission to reload the plugin.
Expand Down

0 comments on commit a1351b2

Please sign in to comment.