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
+87 -83

No files matched your search

Vendored Regular → Executable
View File
File mode changed.
@@ -47,7 +47,7 @@ public class ChannelRegistry implements Registry<ChatChannel> {
@Override @Override
@Nullable @Nullable
public ChatChannel get(String key) { public ChatChannel get(@NotNull String key) {
return registry.get(key); return registry.get(key);
} }
@@ -5,26 +5,32 @@ import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel; import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser; import net.draycia.carbon.storage.ChatUser;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CarbonPlaceholders extends PlaceholderExpansion { public class CarbonPlaceholders extends PlaceholderExpansion {
@NotNull
private final CarbonChat carbonChat; private final CarbonChat carbonChat;
public CarbonPlaceholders(CarbonChat carbonChat) { public CarbonPlaceholders(@NotNull CarbonChat carbonChat) {
this.carbonChat = carbonChat; this.carbonChat = carbonChat;
} }
@Override @Override
@NotNull
public String getIdentifier() { public String getIdentifier() {
return "carbonchat"; return "carbonchat";
} }
@Override @Override
@NotNull
public String getAuthor() { public String getAuthor() {
return "Draycia (Vicarious#0001)"; return "Draycia (Vicarious#0001)";
} }
@Override @Override
@NotNull
public String getVersion() { public String getVersion() {
return "1.0.0"; return "1.0.0";
} }
@@ -34,7 +40,9 @@ public class CarbonPlaceholders extends PlaceholderExpansion {
return true; 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(); String key = identifier.toLowerCase();
if (key.startsWith("can_use_")) { if (key.startsWith("can_use_")) {
@@ -32,23 +32,45 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer; import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor; 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 net.md_5.bungee.api.chat.hover.content.Content;
import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.NotImplementedException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public final class CarbonUtils { public final class CarbonUtils {
public static Component createComponent(final Player player) { @NotNull
try { private static final String[] colors;
static {
List<String> colorList = new ArrayList<>();
for (NamedTextColor color : NamedTextColor.values()) {
colorList.add(color.toString());
}
colors = colorList.toArray(new String[0]);
}
@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(); final ItemStack itemStack = player.getInventory().getItemInMainHand();
if (itemStack == null || itemStack.getType() == Material.AIR) { if (itemStack.getType().isAir()) {
return net.kyori.adventure.text.TextComponent.empty(); return net.kyori.adventure.text.TextComponent.empty();
} }
@@ -71,17 +93,20 @@ public final class CarbonUtils {
component.color(ChatColor.WHITE).append("]"); component.color(ChatColor.WHITE).append("]");
return BungeeCordComponentSerializer.get().deserialize(component.create()); return BungeeCordComponentSerializer.get().deserialize(component.create());
} catch (NoSuchMethodError ignored) {}
return net.kyori.adventure.text.TextComponent.empty();
} }
public static TextColor parseColor(ChatUser user, String input) { @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) { if (input == null) {
input = "white"; input = "white";
} }
if (user.isOnline()) { if (user != null && user.isOnline()) {
input = PlaceholderAPI.setPlaceholders(user.asPlayer(), input); input = PlaceholderAPI.setPlaceholders(user.asPlayer(), input);
} }
@@ -100,26 +125,7 @@ public final class CarbonUtils {
return TextColor.fromCSSHexString(input); return TextColor.fromCSSHexString(input);
} }
public static TextColor parseColor(String input) { @NotNull
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);
}
public static Argument onlineChatUserArgument() { public static Argument onlineChatUserArgument() {
return new CustomArgument<>((input) -> { return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat"); CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
@@ -142,6 +148,7 @@ public final class CarbonUtils {
}); });
} }
@NotNull
public static Argument chatUserArgument() { public static Argument chatUserArgument() {
return new CustomArgument<>((input) -> { return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat"); CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat");
@@ -158,18 +165,7 @@ public final class CarbonUtils {
}); });
} }
private static String[] colors; @NotNull
static {
ArrayList<String> colorList = new ArrayList<>();
for (NamedTextColor color : NamedTextColor.values()) {
colorList.add(color.toString());
}
colors = colorList.toArray(new String[0]);
}
public static Argument textColorArgument() { public static Argument textColorArgument() {
return new CustomArgument<>((input) -> { return new CustomArgument<>((input) -> {
TextColor color = parseColor(input); TextColor color = parseColor(input);
@@ -182,6 +178,7 @@ public final class CarbonUtils {
}).overrideSuggestions(colors); }).overrideSuggestions(colors);
} }
@NotNull
public static Argument channelArgument() { public static Argument channelArgument() {
return new CustomArgument<>((input) -> { return new CustomArgument<>((input) -> {
CarbonChat carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat"); 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; package net.draycia.carbon.util;
import io.lettuce.core.pubsub.RedisPubSubListener; import io.lettuce.core.pubsub.RedisPubSubListener;
import org.jetbrains.annotations.NotNull;
@FunctionalInterface @FunctionalInterface
public interface RedisListener extends RedisPubSubListener<String, String> { public interface RedisListener extends RedisPubSubListener<String, String> {
void message(String channel, String message); @Override
void message(@NotNull String channel, @NotNull String message);
@Override @Override
default void message(String pattern, String channel, String message) { } default void message(@NotNull String pattern, @NotNull String channel, @NotNull String message) { }
@Override @Override
default void subscribed(String channel, long count) { } default void subscribed(@NotNull String channel, long count) { }
@Override @Override
default void psubscribed(String pattern, long count) { } default void psubscribed(@NotNull String pattern, long count) { }
@Override @Override
default void unsubscribed(String channel, long count) { } default void unsubscribed(@NotNull String channel, long count) { }
@Override @Override
default void punsubscribed(String pattern, long count) { } default void punsubscribed(@NotNull String pattern, long count) { }
} }
@@ -9,6 +9,7 @@ public interface Registry<T> {
/** /**
* Adds the specified key and value to the registry * 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 * @param value The value to insert
* @return If successfully registered * @return If successfully registered
@@ -17,6 +18,7 @@ public interface Registry<T> {
/** /**
* Gets the list of registered values * Gets the list of registered values
*
* @return The list of registered values * @return The list of registered values
*/ */
@NotNull @NotNull
@@ -24,11 +26,12 @@ public interface Registry<T> {
/** /**
* Obtains an entry associated with the specified key * Obtains an entry associated with the specified key
*
* @param key The key for the entry * @param key The key for the entry
* @return The entry associated with the key * @return The entry associated with the key
*/ */
@Nullable @Nullable
T get(String key); T get(@NotNull String key);
/** /**
* Clears all registered entries from the registry * 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;
}
}