Merge pull request #65 from Proximyst/refactor-utils

Refactor utility classes
This commit is contained in:
Josh (Vicarious) authored and GitHub committed 2020-09-05 07:26:24 -07:00
commit ed3a41bcfa
8 files changed
+110 -106

No files matched your search

Vendored Regular → Executable
View File
File mode changed.
@@ -47,7 +47,7 @@ public class ChannelRegistry implements Registry<ChatChannel> {
@Override
@Nullable
public ChatChannel get(String key) {
public ChatChannel get(@NotNull String key) {
return registry.get(key);
}
@@ -5,26 +5,32 @@ import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CarbonPlaceholders extends PlaceholderExpansion {
@NotNull
private final CarbonChat carbonChat;
public CarbonPlaceholders(CarbonChat carbonChat) {
public CarbonPlaceholders(@NotNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
}
@Override
@NotNull
public String getIdentifier() {
return "carbonchat";
}
@Override
@NotNull
public String getAuthor() {
return "Draycia (Vicarious#0001)";
}
@Override
@NotNull
public String getVersion() {
return "1.0.0";
}
@@ -34,7 +40,9 @@ public class CarbonPlaceholders extends PlaceholderExpansion {
return true;
}
public String onPlaceholderRequest(final Player player, final String identifier) {
@Override
@Nullable
public String onPlaceholderRequest(@NotNull final Player player, @NotNull final String identifier) {
String key = identifier.toLowerCase();
if (key.startsWith("can_use_")) {
@@ -32,56 +32,81 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.*;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.api.chat.hover.content.Content;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public final class CarbonUtils {
public static Component createComponent(final Player player) {
try {
final ItemStack itemStack = player.getInventory().getItemInMainHand();
@NotNull
private static final String[] colors;
if (itemStack == null || itemStack.getType() == Material.AIR) {
return net.kyori.adventure.text.TextComponent.empty();
}
static {
List<String> colorList = new ArrayList<>();
Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
for (NamedTextColor color : NamedTextColor.values()) {
colorList.add(color.toString());
}
ComponentBuilder component = new ComponentBuilder();
component.event(event); // Let this be inherited by all coming components.
component.color(ChatColor.WHITE).append("[");
if (itemStack.getItemMeta().hasDisplayName()) {
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
} else {
String name = itemStack.getItemMeta().hasDisplayName()
? itemStack.getItemMeta().getDisplayName()
: "item.minecraft." + itemStack.getType().name().toLowerCase(); // As of 1.13, Material is 1:1 with MC's names
component.append(new TranslatableComponent(name));
}
component.color(ChatColor.WHITE).append("]");
return BungeeCordComponentSerializer.get().deserialize(component.create());
} catch (NoSuchMethodError ignored) {}
return net.kyori.adventure.text.TextComponent.empty();
colors = colorList.toArray(new String[0]);
}
public static TextColor parseColor(ChatUser user, String input) {
@NotNull
public static Component createComponent(@NotNull final Player player) {
if (!FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
return net.kyori.adventure.text.TextComponent.empty();
}
final ItemStack itemStack = player.getInventory().getItemInMainHand();
if (itemStack.getType().isAir()) {
return net.kyori.adventure.text.TextComponent.empty();
}
Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
ComponentBuilder component = new ComponentBuilder();
component.event(event); // Let this be inherited by all coming components.
component.color(ChatColor.WHITE).append("[");
if (itemStack.getItemMeta().hasDisplayName()) {
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
} else {
String name = itemStack.getItemMeta().hasDisplayName()
? itemStack.getItemMeta().getDisplayName()
: "item.minecraft." + itemStack.getType().name().toLowerCase(); // As of 1.13, Material is 1:1 with MC's names
component.append(new TranslatableComponent(name));
}
component.color(ChatColor.WHITE).append("]");
return BungeeCordComponentSerializer.get().deserialize(component.create());
}
@Nullable
public static TextColor parseColor(@Nullable String input) {
return parseColor(null, input);
}
@Nullable
public static TextColor parseColor(@Nullable ChatUser user, @Nullable String input) {
if (input == null) {
input = "white";
}
if (user.isOnline()) {
if (user != null && user.isOnline()) {
input = PlaceholderAPI.setPlaceholders(user.asPlayer(), input);
}
@@ -100,26 +125,7 @@ public final class CarbonUtils {
return TextColor.fromCSSHexString(input);
}
public static TextColor parseColor(String input) {
if (input == null) {
input = "white";
}
for (NamedTextColor namedColor : NamedTextColor.values()) {
if (namedColor.toString().equalsIgnoreCase(input)) {
return namedColor;
}
}
if (input.contains("&") || input.contains("§")) {
input = input.replace("&", "§");
return LegacyComponentSerializer.legacySection().deserialize(input).color();
}
return TextColor.fromCSSHexString(input);
}
@NotNull
public static Argument onlineChatUserArgument() {
return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
@@ -142,6 +148,7 @@ public final class CarbonUtils {
});
}
@NotNull
public static Argument chatUserArgument() {
return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
@@ -158,21 +165,10 @@ public final class CarbonUtils {
});
}
private static String[] colors;
static {
ArrayList<String> colorList = new ArrayList<>();
for (NamedTextColor color : NamedTextColor.values()) {
colorList.add(color.toString());
}
colors = colorList.toArray(new String[0]);
}
@NotNull
public static Argument textColorArgument() {
return new CustomArgument<>((input) -> {
TextColor color = parseColor(input);
TextColor color = parseColor(input);
if (color == null) {
throw new CustomArgument.CustomArgumentException("Invalid color for input (" + input + ")");
@@ -182,6 +178,7 @@ public final class CarbonUtils {
}).overrideSuggestions(colors);
}
@NotNull
public static Argument channelArgument() {
return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
@@ -0,0 +1,25 @@
package net.draycia.carbon.util;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("ResultOfMethodCallIgnored") // This is a common pattern in this class.
public final class FunctionalityConstants {
public static final boolean HAS_HOVER_EVENT_METHOD = noThrow(() -> ItemFactory.class.getMethod("hoverContentOf", ItemStack.class));
private static boolean noThrow(@NotNull ThrowingRunnable runnable) {
try {
runnable.run();
return true;
} catch (Exception ex) {
return false;
}
}
@FunctionalInterface
private interface ThrowingRunnable {
void run() throws Exception;
}
}
@@ -1,25 +1,27 @@
package net.draycia.carbon.util;
import io.lettuce.core.pubsub.RedisPubSubListener;
import org.jetbrains.annotations.NotNull;
@FunctionalInterface
public interface RedisListener extends RedisPubSubListener<String, String> {
void message(String channel, String message);
@Override
void message(@NotNull String channel, @NotNull String message);
@Override
default void message(String pattern, String channel, String message) { }
default void message(@NotNull String pattern, @NotNull String channel, @NotNull String message) { }
@Override
default void subscribed(String channel, long count) { }
default void subscribed(@NotNull String channel, long count) { }
@Override
default void psubscribed(String pattern, long count) { }
default void psubscribed(@NotNull String pattern, long count) { }
@Override
default void unsubscribed(String channel, long count) { }
default void unsubscribed(@NotNull String channel, long count) { }
@Override
default void punsubscribed(String pattern, long count) { }
default void punsubscribed(@NotNull String pattern, long count) { }
}
@@ -9,7 +9,8 @@ public interface Registry<T> {
/**
* Adds the specified key and value to the registry
* @param key The entry's key
*
* @param key The entry's key
* @param value The value to insert
* @return If successfully registered
*/
@@ -17,6 +18,7 @@ public interface Registry<T> {
/**
* Gets the list of registered values
*
* @return The list of registered values
*/
@NotNull
@@ -24,11 +26,12 @@ public interface Registry<T> {
/**
* Obtains an entry associated with the specified key
*
* @param key The key for the entry
* @return The entry associated with the key
*/
@Nullable
T get(String key);
T get(@NotNull String key);
/**
* Clears all registered entries from the registry
@@ -1,31 +0,0 @@
package net.draycia.carbon.util;
import org.jetbrains.annotations.Nullable;
public class ThisOrThat<A, B> {
@Nullable A objectA;
@Nullable B objectB;
public ThisOrThat(@Nullable A objectA, @Nullable B objectB) {
this.objectA = objectA;
this.objectB = objectB;
}
public boolean isThis() {
return objectA != null;
}
public boolean isThat() {
return objectB != null;
}
public @Nullable A getThis() {
return objectA;
}
public @Nullable B getThat() {
return objectB;
}
}