diff --git a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/CarbonChatBukkit.java b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/CarbonChatBukkit.java index 6b4f4309..8d518103 100644 --- a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/CarbonChatBukkit.java +++ b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/CarbonChatBukkit.java @@ -139,13 +139,13 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat { if (storageType == CarbonSettings.StorageType.MYSQL) { this.logger().info("Enabling MySQL storage!"); final SQLCredentials credentials = this.carbonSettings().sqlCredentials(); - this.userService = new MySQLUserService<>(BukkitChatUser.class, this, credentials, supplier); + this.userService = new MySQLUserService<>(this, credentials, supplier, BukkitChatUser::new); } else if (storageType == CarbonSettings.StorageType.JSON) { this.logger().info("Enabling JSON storage!"); - this.userService = new JSONUserService<>(BukkitChatUser.class, this, supplier); + this.userService = new JSONUserService<>(BukkitChatUser.class, this, supplier, BukkitChatUser::new); } else { this.logger().error("Invalid storage type selected! Falling back to JSON."); - this.userService = new JSONUserService<>(BukkitChatUser.class, this, supplier); + this.userService = new JSONUserService<>(BukkitChatUser.class, this, supplier, BukkitChatUser::new); } // Setup listeners diff --git a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/listeners/contexts/mcMMOContext.java b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/listeners/contexts/mcMMOContext.java index 73c3bc82..f88f8538 100644 --- a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/listeners/contexts/mcMMOContext.java +++ b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/listeners/contexts/mcMMOContext.java @@ -12,6 +12,7 @@ import net.draycia.carbon.api.users.ChatUser; import net.draycia.carbon.api.Context; import net.kyori.event.PostOrders; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; @@ -66,23 +67,16 @@ public final class mcMMOContext implements Listener { return; } - final ChatUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId()); - final ChatChannel channel = user.selectedChannel(); - - if (channel == null) { - return; - } - - final Context context = channel.context(KEY); - - if (context != null && context.isBoolean() && context.asBoolean() && !this.isInParty(user)) { - user.clearSelectedChannel(); - } + this.checkContext(event.getPlayer()); } @EventHandler public void onPlayerJoin(final PlayerJoinEvent event) { - final ChatUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId()); + this.checkContext(event.getPlayer()); + } + + private void checkContext(final Player player) { + final ChatUser user = this.carbonChat.userService().wrap(player.getUniqueId()); final ChatChannel channel = user.selectedChannel(); if (channel == null) { diff --git a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/CarbonUtils.java b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/CarbonUtils.java index aa025a83..1fd2feb6 100644 --- a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/CarbonUtils.java +++ b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/CarbonUtils.java @@ -20,11 +20,7 @@ */ package net.draycia.carbon.util; -import dev.jorel.commandapi.arguments.Argument; -import dev.jorel.commandapi.arguments.CustomArgument; import me.clip.placeholderapi.PlaceholderAPI; -import net.draycia.carbon.CarbonChatBukkit; -import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.ChatUser; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -37,7 +33,6 @@ 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.entity.Player; import org.bukkit.inventory.ItemStack; @@ -99,38 +94,4 @@ public final class CarbonUtils { return BungeeCordComponentSerializer.get().deserialize(component.create()); } - @Nullable - public static TextColor parseColor(@Nullable final String input) { - return parseColor(null, input); - } - - @Nullable - public static TextColor parseColor(@Nullable final ChatUser user, @Nullable String input) { - if (input == null) { - input = "white"; - } - - if (user != null) { - final Player player = Bukkit.getPlayer(user.uuid()); - - if (player != null) { - input = PlaceholderAPI.setPlaceholders(player, input); - } - } - - for (final 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); - } - } diff --git a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/Metrics.java b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/Metrics.java index 0488a5e9..1eb212a8 100644 --- a/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/Metrics.java +++ b/CarbonChat-Bukkit/src/main/java/net/draycia/carbon/util/Metrics.java @@ -193,7 +193,7 @@ public class Metrics { } // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) - Bukkit.getScheduler().runTask(Metrics.this.plugin, () -> Metrics.this.submitData()); + Bukkit.getScheduler().runTask(Metrics.this.plugin, Metrics.this::submitData); } }, 1000 * 60 * 5, 1000 * 60 * 30); // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start @@ -207,7 +207,7 @@ public class Metrics { * * @return The plugin specific data. */ - public JsonObject getPluginData() { + public JsonObject pluginData() { final JsonObject data = new JsonObject(); final String pluginName = this.plugin.getDescription().getName(); @@ -219,7 +219,7 @@ public class Metrics { final JsonArray customCharts = new JsonArray(); for (final CustomChart customChart : this.charts) { // Add the data of the custom charts - final JsonObject chart = customChart.getRequestJsonObject(); + final JsonObject chart = customChart.requestJsonObject(); if (chart == null) { // If the chart is null, we skip it continue; } @@ -235,7 +235,7 @@ public class Metrics { * * @return The server specific data. */ - private JsonObject getServerData() { + private JsonObject serverData() { // Minecraft specific data int playerAmount; try { @@ -281,7 +281,7 @@ public class Metrics { * Collects the data and sends it afterwards. */ private void submitData() { - final JsonObject data = this.getServerData(); + final JsonObject data = this.serverData(); final JsonArray pluginData = new JsonArray(); // Search for all other bStats Metrics classes to get their plugin data @@ -422,11 +422,11 @@ public class Metrics { this.chartId = chartId; } - private JsonObject getRequestJsonObject() { + private JsonObject requestJsonObject() { final JsonObject chart = new JsonObject(); chart.addProperty("chartId", this.chartId); try { - final JsonObject data = this.getChartData(); + final JsonObject data = this.chartData(); if (data == null) { // If the data is null we don't send the chart. return null; @@ -441,7 +441,7 @@ public class Metrics { return chart; } - protected abstract JsonObject getChartData() throws Exception; + protected abstract JsonObject chartData() throws Exception; } @@ -464,7 +464,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final String value = this.callable.call(); if (value == null || value.isEmpty()) { @@ -495,7 +495,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final JsonObject values = new JsonObject(); final Map map = this.callable.call(); @@ -539,7 +539,7 @@ public class Metrics { } @Override - public JsonObject getChartData() throws Exception { + public JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final JsonObject values = new JsonObject(); final Map> map = this.callable.call(); @@ -588,7 +588,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final int value = this.callable.call(); if (value == 0) { @@ -620,7 +620,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final JsonObject values = new JsonObject(); final Map map = this.callable.call(); @@ -665,7 +665,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final JsonObject values = new JsonObject(); final Map map = this.callable.call(); @@ -703,7 +703,7 @@ public class Metrics { } @Override - protected JsonObject getChartData() throws Exception { + protected JsonObject chartData() throws Exception { final JsonObject data = new JsonObject(); final JsonObject values = new JsonObject(); final Map map = this.callable.call(); diff --git a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/commands/misc/CommandRegistrar.java b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/commands/misc/CommandRegistrar.java index 2282d084..ba91b9c8 100644 --- a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/commands/misc/CommandRegistrar.java +++ b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/commands/misc/CommandRegistrar.java @@ -1,8 +1,6 @@ package net.draycia.carbon.common.commands.misc; import com.intellectualsites.commands.CommandManager; -import com.intellectualsites.commands.meta.CommandMeta; -import com.intellectualsites.commands.meta.SimpleCommandMeta; import net.draycia.carbon.api.users.ChatUser; import net.draycia.carbon.common.commands.ChannelCommand; import net.draycia.carbon.common.commands.ChannelListCommand; diff --git a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/JSONUserService.java b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/JSONUserService.java index 41b09274..4924adf8 100644 --- a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/JSONUserService.java +++ b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/JSONUserService.java @@ -24,6 +24,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.function.Function; import java.util.function.Supplier; public class JSONUserService implements UserService { @@ -32,6 +33,7 @@ public class JSONUserService implements UserService { private final @NonNull Gson gson = new GsonBuilder().setPrettyPrinting().create(); private final @NonNull Type userType; private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier; + private final @NonNull Function userFactory; private final @NonNull LoadingCache<@NonNull UUID, @NonNull T> userCache = CacheBuilder.newBuilder() .removalListener(this::saveUser) @@ -39,10 +41,12 @@ public class JSONUserService implements UserService { public JSONUserService(final @NonNull Class userType, final @NonNull CarbonChat carbonChat, - final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier) { + final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier, + final @NonNull Function userFactory) { this.userType = userType; this.carbonChat = carbonChat; this.supplier = supplier; + this.userFactory = userFactory; final TimerTask timerTask = new TimerTask() { @Override @@ -115,13 +119,7 @@ public class JSONUserService implements UserService { exception.printStackTrace(); } - try { - return (T) this.userType.getClass().getDeclaredConstructor(UUID.class).newInstance(uuid); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - } - - return null; + return this.userFactory.apply(uuid); } private void saveUser(final @NonNull RemovalNotification<@NonNull UUID, @NonNull T> notification) { diff --git a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/MySQLUserService.java b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/MySQLUserService.java index fb2357b4..170d414f 100644 --- a/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/MySQLUserService.java +++ b/CarbonChat-Common/src/main/java/net/draycia/carbon/common/users/MySQLUserService.java @@ -20,8 +20,6 @@ import net.kyori.adventure.text.format.TextColor; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Type; import java.sql.SQLException; import java.sql.SQLSyntaxErrorException; import java.util.List; @@ -30,25 +28,26 @@ import java.util.Timer; import java.util.TimerTask; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.function.Function; import java.util.function.Supplier; public class MySQLUserService implements UserService { private final @NonNull CarbonChat carbonChat; private final @NonNull Database database; - private final @NonNull Type userType; private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier; + private final @NonNull Function userFactory; private final @NonNull LoadingCache<@NonNull UUID, @NonNull T> userCache = CacheBuilder.newBuilder() .removalListener(this::saveUser) .build(CacheLoader.from(this::loadUser)); - public MySQLUserService(final @NonNull Class userType, - final @NonNull CarbonChat carbonChat, final @NonNull SQLCredentials credentials, - final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier) { + public MySQLUserService(final @NonNull CarbonChat carbonChat, final @NonNull SQLCredentials credentials, + final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier, + final @NonNull Function userFactory) { this.carbonChat = carbonChat; - this.userType = userType; this.supplier = supplier; + this.userFactory = userFactory; final String username = credentials.username(); final String password = credentials.password(); @@ -140,14 +139,7 @@ public class MySQLUserService implements UserService { } private @Nullable T loadUser(final @NonNull UUID uuid) { - final T user; - - try { - user = (T) this.userType.getClass().getDeclaredConstructor(UUID.class).newInstance(uuid); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - return null; - } + final T user = this.userFactory.apply(uuid); try (final DbStatement statement = this.database.query("SELECT * from sc_users WHERE uuid = ?;")) { statement.execute(uuid.toString());