mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
Reduce redundancy, remove some reflection
This commit is contained in:
1 parent
18c6fd7e2b
commit
3a69d0d74b
7 files changed
+38
-95
No files matched your search
@@ -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
|
||||
|
||||
+7
-13
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, Integer> 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<String, Map<String, Integer>> 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<String, Integer> 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<String, Integer> 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<String, int[]> map = this.callable.call();
|
||||
|
||||
Reference in new issue
Block a user