-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add separate permissions for player message decorations
- Loading branch information
Showing
2 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
81 changes: 78 additions & 3 deletions
81
...ava/ru/brikster/chatty/chat/message/transform/decorations/PlayerDecorationsFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,94 @@ | ||
package ru.brikster.chatty.chat.message.transform.decorations; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.serializer.legacy.CharacterAndFormat; | ||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; | ||
import org.bukkit.command.CommandSender; | ||
import org.jetbrains.annotations.NotNull; | ||
import ru.brikster.chatty.util.CollectionUtil; | ||
|
||
import javax.inject.Singleton; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Singleton | ||
public final class PlayerDecorationsFormatter { | ||
|
||
private static final Map<String, CharacterAndFormat> COLORS = new HashMap<>() {{ | ||
put("black", CharacterAndFormat.BLACK); | ||
put("dark_blue", CharacterAndFormat.DARK_BLUE); | ||
put("dark_green", CharacterAndFormat.DARK_GREEN); | ||
put("dark_aqua", CharacterAndFormat.DARK_AQUA); | ||
put("dark_red", CharacterAndFormat.DARK_RED); | ||
put("dark_purple", CharacterAndFormat.DARK_PURPLE); | ||
put("gold", CharacterAndFormat.GOLD); | ||
put("gray", CharacterAndFormat.GRAY); | ||
put("dark_gray", CharacterAndFormat.DARK_GRAY); | ||
put("blue", CharacterAndFormat.BLUE); | ||
put("green", CharacterAndFormat.GREEN); | ||
put("aqua", CharacterAndFormat.AQUA); | ||
put("red", CharacterAndFormat.RED); | ||
put("light_purple", CharacterAndFormat.LIGHT_PURPLE); | ||
put("yellow", CharacterAndFormat.YELLOW); | ||
put("white", CharacterAndFormat.WHITE); | ||
}}; | ||
|
||
private static final Map<String, CharacterAndFormat> DECORATIONS = new HashMap<>() {{ | ||
put("obfuscated", CharacterAndFormat.OBFUSCATED); | ||
put("bold", CharacterAndFormat.BOLD); | ||
put("strikethrough", CharacterAndFormat.STRIKETHROUGH); | ||
put("underlined", CharacterAndFormat.UNDERLINED); | ||
put("italic", CharacterAndFormat.ITALIC); | ||
put("reset", CharacterAndFormat.RESET); | ||
}}; | ||
|
||
private static final LegacyComponentSerializer FULL_SERIALIZER = LegacyComponentSerializer | ||
.builder() | ||
.character('&') | ||
.formats(CollectionUtil.listOf(COLORS.values(), DECORATIONS.values())) | ||
.hexColors() | ||
.build(); | ||
|
||
public @NotNull Component formatMessageWithDecorations(@NotNull CommandSender sender, @NotNull String message) { | ||
return sender.hasPermission("chatty.decorations") | ||
? LegacyComponentSerializer.legacyAmpersand().deserialize(message) | ||
: Component.text(message); | ||
if (sender.hasPermission("chatty.decoration")) { | ||
return FULL_SERIALIZER.deserialize(message); | ||
} | ||
|
||
var componentSerializerBuilder = LegacyComponentSerializer | ||
.builder() | ||
.character('&'); | ||
|
||
List<CharacterAndFormat> formatList = new LinkedList<>(); | ||
|
||
if (sender.hasPermission("chatty.decoration.color")) { | ||
formatList.addAll(COLORS.values()); | ||
} else { | ||
for (var format : COLORS.entrySet()) { | ||
if (sender.hasPermission("chatty.decoration.color." + format.getKey())) { | ||
formatList.add(format.getValue()); | ||
} | ||
} | ||
} | ||
|
||
for (var format : DECORATIONS.entrySet()) { | ||
if (sender.hasPermission("chatty.decoration." + format.getKey())) { | ||
formatList.add(format.getValue()); | ||
} | ||
} | ||
|
||
if (!sender.hasPermission("chatty.decoration.hex")) { | ||
componentSerializerBuilder.hexCharacter((char) 0); | ||
} else { | ||
componentSerializerBuilder.hexColors(); | ||
} | ||
|
||
componentSerializerBuilder.formats(formatList); | ||
|
||
return componentSerializerBuilder | ||
.build() | ||
.deserialize(message); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
spigot/src/main/java/ru/brikster/chatty/util/CollectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.brikster.chatty.util; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@UtilityClass | ||
public class CollectionUtil { | ||
|
||
@SafeVarargs | ||
public <T> List<T> listOf(Collection<T>... collections) { | ||
List<T> list = new ArrayList<>(); | ||
for (Collection<T> collection : collections) { | ||
list.addAll(collection); | ||
} | ||
return list; | ||
} | ||
|
||
} |