From 03b6f817f89275e4d5c3d5b1868c3dcc861bf146 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 3 Dec 2019 14:35:07 +0000 Subject: Some cleanup & refactoring --- .../me/lucko/spark/bukkit/BukkitCommandSender.java | 35 ++-- .../bukkit/placeholder/SparkPlaceholderApi.java | 6 +- .../spark/bungeecord/BungeeCordCommandSender.java | 35 ++-- .../java/me/lucko/spark/common/ActivityLog.java | 215 -------------------- .../java/me/lucko/spark/common/CommandSender.java | 101 ---------- .../java/me/lucko/spark/common/SparkPlatform.java | 2 + .../java/me/lucko/spark/common/SparkPlugin.java | 1 + .../spark/common/activitylog/ActivityLog.java | 216 +++++++++++++++++++++ .../me/lucko/spark/common/command/Command.java | 2 +- .../common/command/CommandResponseHandler.java | 2 +- .../common/command/modules/ActivityLogModule.java | 2 +- .../spark/common/command/modules/MemoryModule.java | 2 +- .../common/command/modules/SamplerModule.java | 2 +- .../command/modules/TickMonitoringModule.java | 2 +- .../command/sender/AbstractCommandSender.java | 42 ++++ .../spark/common/command/sender/CommandSender.java | 101 ++++++++++ .../spark/common/heapdump/HeapDumpSummary.java | 9 +- .../lucko/spark/common/monitor/cpu/CpuMonitor.java | 8 +- .../me/lucko/spark/common/sampler/Sampler.java | 2 +- .../me/lucko/spark/fabric/FabricCommandSender.java | 42 ++-- .../me/lucko/spark/forge/ForgeCommandSender.java | 38 ++-- .../me/lucko/spark/sponge/SpongeCommandSender.java | 31 +-- .../spark/velocity/VelocityCommandSender.java | 37 ++-- 23 files changed, 446 insertions(+), 487 deletions(-) delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/CommandSender.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/command/sender/AbstractCommandSender.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/command/sender/CommandSender.java diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitCommandSender.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitCommandSender.java index d353845..8bfc015 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitCommandSender.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitCommandSender.java @@ -20,53 +20,40 @@ package me.lucko.spark.bukkit; -import me.lucko.spark.common.CommandSender; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; import net.kyori.text.Component; import net.kyori.text.adapter.bukkit.TextAdapter; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.UUID; -public class BukkitCommandSender implements CommandSender { - private final org.bukkit.command.CommandSender sender; - - public BukkitCommandSender(org.bukkit.command.CommandSender sender) { - this.sender = sender; +public class BukkitCommandSender extends AbstractCommandSender { + public BukkitCommandSender(CommandSender sender) { + super(sender); } @Override public String getName() { - return this.sender.getName(); + return this.delegate.getName(); } @Override public UUID getUniqueId() { - if (this.sender instanceof Player) { - return ((Player) this.sender).getUniqueId(); + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUniqueId(); } return null; } @Override public void sendMessage(Component message) { - TextAdapter.sendComponent(this.sender, message); + TextAdapter.sendComponent(super.delegate, message); } @Override public boolean hasPermission(String permission) { - return this.sender.hasPermission(permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BukkitCommandSender that = (BukkitCommandSender) o; - return this.sender.equals(that.sender); - } - - @Override - public int hashCode() { - return this.sender.hashCode(); + return super.delegate.hasPermission(permission); } } diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java index e5ada99..1eb2732 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java @@ -26,8 +26,6 @@ import me.lucko.spark.common.SparkPlatform; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -import java.util.stream.Collectors; - public class SparkPlaceholderApi extends PlaceholderExpansion { private final BukkitSparkPlugin plugin; private final SparkPlatform platform; @@ -55,12 +53,12 @@ public class SparkPlaceholderApi extends PlaceholderExpansion { @Override public String getAuthor() { - return plugin.getDescription().getAuthors().stream().collect(Collectors.joining(", ")); + return String.join(", ", this.plugin.getDescription().getAuthors()); } @Override public String getVersion() { - return plugin.getVersion(); + return this.plugin.getVersion(); } @Override diff --git a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordCommandSender.java b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordCommandSender.java index 193b38a..dcafcaa 100644 --- a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordCommandSender.java +++ b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordCommandSender.java @@ -20,53 +20,40 @@ package me.lucko.spark.bungeecord; -import me.lucko.spark.common.CommandSender; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; import net.kyori.text.Component; import net.kyori.text.adapter.bungeecord.TextAdapter; +import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.UUID; -public class BungeeCordCommandSender implements CommandSender { - private final net.md_5.bungee.api.CommandSender sender; - - public BungeeCordCommandSender(net.md_5.bungee.api.CommandSender sender) { - this.sender = sender; +public class BungeeCordCommandSender extends AbstractCommandSender { + public BungeeCordCommandSender(CommandSender sender) { + super(sender); } @Override public String getName() { - return this.sender.getName(); + return super.delegate.getName(); } @Override public UUID getUniqueId() { - if (this.sender instanceof ProxiedPlayer) { - return ((ProxiedPlayer) this.sender).getUniqueId(); + if (super.delegate instanceof ProxiedPlayer) { + return ((ProxiedPlayer) super.delegate).getUniqueId(); } return null; } @Override public void sendMessage(Component message) { - TextAdapter.sendComponent(this.sender, message); + TextAdapter.sendComponent(super.delegate, message); } @Override public boolean hasPermission(String permission) { - return this.sender.hasPermission(permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BungeeCordCommandSender that = (BungeeCordCommandSender) o; - return this.sender.equals(that.sender); - } - - @Override - public int hashCode() { - return this.sender.hashCode(); + return super.delegate.hasPermission(permission); } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java b/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java deleted file mode 100644 index 7327e2b..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.common; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -public class ActivityLog { - - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - private static final JsonParser PARSER = new JsonParser(); - - private final Path file; - - private final LinkedList log = new LinkedList<>(); - private final Object[] mutex = new Object[0]; - - public ActivityLog(Path file) { - this.file = file; - } - - public void addToLog(Activity activity) { - synchronized (this.mutex) { - this.log.addFirst(activity); - } - save(); - } - - public List getLog() { - synchronized (this.mutex) { - return new LinkedList<>(this.log); - } - } - - public void save() { - JsonArray array = new JsonArray(); - synchronized (this.mutex) { - for (Activity activity : this.log) { - if (!activity.shouldExpire()) { - array.add(activity.serialize()); - } - } - } - - try { - Files.createDirectories(this.file.getParent()); - } catch (IOException e) { - // ignore - } - - try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { - GSON.toJson(array, writer); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void load() { - if (!Files.exists(this.file)) { - synchronized (this.mutex) { - this.log.clear(); - return; - } - } - - JsonArray array; - try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { - array = PARSER.parse(reader).getAsJsonArray(); - } catch (IOException e) { - e.printStackTrace(); - return; - } - - boolean save = false; - - synchronized (this.mutex) { - this.log.clear(); - for (JsonElement element : array) { - try { - Activity activity = Activity.deserialize(element); - if (activity.shouldExpire()) { - save = true; - } - this.log.add(activity); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - if (save) { - try { - save(); - } catch (Exception e) { - // ignore - } - } - } - - public static final class Activity { - private final CommandSender.Data user; - private final long time; - private final String type; - - private final String dataType; - private final String dataValue; - - public static Activity urlActivity(CommandSender user, long time, String type, String url) { - return new Activity(user.toData(), time, type, "url", url); - } - - public static Activity fileActivity(CommandSender user, long time, String type, String filePath) { - return new Activity(user.toData(), time, type, "file", filePath); - } - - private Activity(CommandSender.Data user, long time, String type, String dataType, String dataValue) { - this.user = user; - this.time = time; - this.type = type; - this.dataType = dataType; - this.dataValue = dataValue; - } - - public CommandSender.Data getUser() { - return this.user; - } - - public long getTime() { - return this.time; - } - - public String getType() { - return this.type; - } - - public String getDataType() { - return this.dataType; - } - - public String getDataValue() { - return this.dataValue; - } - - public boolean shouldExpire() { - if (dataType.equals("url")) { - return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7); - } else { - return false; - } - } - - public JsonObject serialize() { - JsonObject object = new JsonObject(); - - object.add("user", this.user.serialize()); - object.add("time", new JsonPrimitive(this.time)); - object.add("type", new JsonPrimitive(this.type)); - - JsonObject data = new JsonObject(); - data.add("type", new JsonPrimitive(this.dataType)); - data.add("value", new JsonPrimitive(this.dataValue)); - object.add("data", data); - - return object; - } - - public static Activity deserialize(JsonElement element) { - JsonObject object = element.getAsJsonObject(); - - CommandSender.Data user = CommandSender.Data.deserialize(object.get("user")); - long time = object.get("time").getAsJsonPrimitive().getAsLong(); - String type = object.get("type").getAsJsonPrimitive().getAsString(); - - JsonObject dataObject = object.get("data").getAsJsonObject(); - String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString(); - String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString(); - - return new Activity(user, time, type, dataType, dataValue); - } - } - -} diff --git a/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java b/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java deleted file mode 100644 index 198bd79..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.common; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import me.lucko.spark.proto.SparkProtos.CommandSenderData; -import net.kyori.text.Component; - -import java.util.UUID; - -public interface CommandSender { - - String getName(); - - UUID getUniqueId(); - - void sendMessage(Component message); - - boolean hasPermission(String permission); - - default Data toData() { - return new Data(getName(), getUniqueId()); - } - - final class Data { - private final String name; - private final UUID uniqueId; - - public Data(String name, UUID uniqueId) { - this.name = name; - this.uniqueId = uniqueId; - } - - public String getName() { - return this.name; - } - - public UUID getUniqueId() { - return this.uniqueId; - } - - public boolean isPlayer() { - return this.uniqueId != null; - } - - public JsonObject serialize() { - JsonObject user = new JsonObject(); - user.add("type", new JsonPrimitive(isPlayer() ? "player" : "other")); - user.add("name", new JsonPrimitive(this.name)); - if (this.uniqueId != null) { - user.add("uniqueId", new JsonPrimitive(this.uniqueId.toString())); - } - return user; - } - - public CommandSenderData toProto() { - CommandSenderData.Builder proto = CommandSenderData.newBuilder() - .setType(isPlayer() ? CommandSenderData.Type.PLAYER : CommandSenderData.Type.OTHER) - .setName(this.name); - - if (this.uniqueId != null) { - proto.setUniqueId(this.uniqueId.toString()); - } - - return proto.build(); - } - - public static CommandSender.Data deserialize(JsonElement element) { - JsonObject userObject = element.getAsJsonObject(); - String user = userObject.get("name").getAsJsonPrimitive().getAsString(); - UUID uuid; - if (userObject.has("uniqueId")) { - uuid = UUID.fromString(userObject.get("uniqueId").getAsJsonPrimitive().getAsString()); - } else { - uuid = null; - } - return new CommandSender.Data(user, uuid); - } - } - -} diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index db5cb84..dc88306 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -21,6 +21,7 @@ package me.lucko.spark.common; import com.google.common.collect.ImmutableList; +import me.lucko.spark.common.activitylog.ActivityLog; import me.lucko.spark.common.command.Arguments; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; @@ -30,6 +31,7 @@ import me.lucko.spark.common.command.modules.HealthModule; import me.lucko.spark.common.command.modules.MemoryModule; import me.lucko.spark.common.command.modules.SamplerModule; import me.lucko.spark.common.command.modules.TickMonitoringModule; +import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.command.tabcomplete.CompletionSupplier; import me.lucko.spark.common.command.tabcomplete.TabCompleter; import me.lucko.spark.common.monitor.cpu.CpuMonitor; diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java index 7ccffab..1171b33 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -20,6 +20,7 @@ package me.lucko.spark.common; +import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.sampler.TickCounter; diff --git a/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java b/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java new file mode 100644 index 0000000..894593c --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/activitylog/ActivityLog.java @@ -0,0 +1,216 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.activitylog; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import me.lucko.spark.common.command.sender.CommandSender; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class ActivityLog { + + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static final JsonParser PARSER = new JsonParser(); + + private final Path file; + + private final LinkedList log = new LinkedList<>(); + private final Object[] mutex = new Object[0]; + + public ActivityLog(Path file) { + this.file = file; + } + + public void addToLog(Activity activity) { + synchronized (this.mutex) { + this.log.addFirst(activity); + } + save(); + } + + public List getLog() { + synchronized (this.mutex) { + return new LinkedList<>(this.log); + } + } + + public void save() { + JsonArray array = new JsonArray(); + synchronized (this.mutex) { + for (Activity activity : this.log) { + if (!activity.shouldExpire()) { + array.add(activity.serialize()); + } + } + } + + try { + Files.createDirectories(this.file.getParent()); + } catch (IOException e) { + // ignore + } + + try (BufferedWriter writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) { + GSON.toJson(array, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void load() { + if (!Files.exists(this.file)) { + synchronized (this.mutex) { + this.log.clear(); + return; + } + } + + JsonArray array; + try (BufferedReader reader = Files.newBufferedReader(this.file, StandardCharsets.UTF_8)) { + array = PARSER.parse(reader).getAsJsonArray(); + } catch (IOException e) { + e.printStackTrace(); + return; + } + + boolean save = false; + + synchronized (this.mutex) { + this.log.clear(); + for (JsonElement element : array) { + try { + Activity activity = Activity.deserialize(element); + if (activity.shouldExpire()) { + save = true; + } + this.log.add(activity); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + if (save) { + try { + save(); + } catch (Exception e) { + // ignore + } + } + } + + public static final class Activity { + private final CommandSender.Data user; + private final long time; + private final String type; + + private final String dataType; + private final String dataValue; + + public static Activity urlActivity(CommandSender user, long time, String type, String url) { + return new Activity(user.toData(), time, type, "url", url); + } + + public static Activity fileActivity(CommandSender user, long time, String type, String filePath) { + return new Activity(user.toData(), time, type, "file", filePath); + } + + private Activity(CommandSender.Data user, long time, String type, String dataType, String dataValue) { + this.user = user; + this.time = time; + this.type = type; + this.dataType = dataType; + this.dataValue = dataValue; + } + + public CommandSender.Data getUser() { + return this.user; + } + + public long getTime() { + return this.time; + } + + public String getType() { + return this.type; + } + + public String getDataType() { + return this.dataType; + } + + public String getDataValue() { + return this.dataValue; + } + + public boolean shouldExpire() { + if (this.dataType.equals("url")) { + return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7); + } else { + return false; + } + } + + public JsonObject serialize() { + JsonObject object = new JsonObject(); + + object.add("user", this.user.serialize()); + object.add("time", new JsonPrimitive(this.time)); + object.add("type", new JsonPrimitive(this.type)); + + JsonObject data = new JsonObject(); + data.add("type", new JsonPrimitive(this.dataType)); + data.add("value", new JsonPrimitive(this.dataValue)); + object.add("data", data); + + return object; + } + + public static Activity deserialize(JsonElement element) { + JsonObject object = element.getAsJsonObject(); + + CommandSender.Data user = CommandSender.Data.deserialize(object.get("user")); + long time = object.get("time").getAsJsonPrimitive().getAsLong(); + String type = object.get("type").getAsJsonPrimitive().getAsString(); + + JsonObject dataObject = object.get("data").getAsJsonObject(); + String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString(); + String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString(); + + return new Activity(user, time, type, dataType, dataValue); + } + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java index ab95cb6..db23454 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java @@ -21,8 +21,8 @@ package me.lucko.spark.common.command; import com.google.common.collect.ImmutableList; -import me.lucko.spark.common.CommandSender; import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.command.sender.CommandSender; import java.util.Collections; import java.util.List; diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java index 6f02180..635e785 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java @@ -20,8 +20,8 @@ package me.lucko.spark.common.command; -import me.lucko.spark.common.CommandSender; import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.command.sender.CommandSender; import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.format.TextColor; diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java index e09dc9d..c78e567 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java @@ -20,7 +20,7 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog.Activity; +import me.lucko.spark.common.activitylog.ActivityLog.Activity; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; import me.lucko.spark.common.command.tabcomplete.TabCompleter; diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java index dcfb0c4..0516f5b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java @@ -20,8 +20,8 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog.Activity; import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.activitylog.ActivityLog.Activity; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; import me.lucko.spark.common.command.tabcomplete.CompletionSupplier; diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index 9f14e6e..f63d7ba 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -20,8 +20,8 @@ package me.lucko.spark.common.command.modules; -import me.lucko.spark.common.ActivityLog.Activity; import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.activitylog.ActivityLog.Activity; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; import me.lucko.spark.common.command.CommandResponseHandler; diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/TickMonitoringModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/TickMonitoringModule.java index da26dea..0ebb252 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/TickMonitoringModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/TickMonitoringModule.java @@ -81,7 +81,7 @@ public class TickMonitoringModule implements CommandModule { ); } - private class ReportingTickMonitor extends TickMonitor { + private static class ReportingTickMonitor extends TickMonitor { private final CommandResponseHandler resp; ReportingTickMonitor(SparkPlatform platform, CommandResponseHandler resp, TickCounter tickCounter, int percentageChangeThreshold, boolean monitorGc) { diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/sender/AbstractCommandSender.java b/spark-common/src/main/java/me/lucko/spark/common/command/sender/AbstractCommandSender.java new file mode 100644 index 0000000..ce48889 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/command/sender/AbstractCommandSender.java @@ -0,0 +1,42 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.command.sender; + +public abstract class AbstractCommandSender implements CommandSender { + protected final S delegate; + + public AbstractCommandSender(S delegate) { + this.delegate = delegate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + AbstractCommandSender that = (AbstractCommandSender) o; + return this.delegate.equals(that.delegate); + } + + @Override + public int hashCode() { + return this.delegate.hashCode(); + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/sender/CommandSender.java b/spark-common/src/main/java/me/lucko/spark/common/command/sender/CommandSender.java new file mode 100644 index 0000000..ef56185 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/command/sender/CommandSender.java @@ -0,0 +1,101 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.command.sender; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import me.lucko.spark.proto.SparkProtos.CommandSenderData; +import net.kyori.text.Component; + +import java.util.UUID; + +public interface CommandSender { + + String getName(); + + UUID getUniqueId(); + + void sendMessage(Component message); + + boolean hasPermission(String permission); + + default Data toData() { + return new Data(getName(), getUniqueId()); + } + + final class Data { + private final String name; + private final UUID uniqueId; + + public Data(String name, UUID uniqueId) { + this.name = name; + this.uniqueId = uniqueId; + } + + public String getName() { + return this.name; + } + + public UUID getUniqueId() { + return this.uniqueId; + } + + public boolean isPlayer() { + return this.uniqueId != null; + } + + public JsonObject serialize() { + JsonObject user = new JsonObject(); + user.add("type", new JsonPrimitive(isPlayer() ? "player" : "other")); + user.add("name", new JsonPrimitive(this.name)); + if (this.uniqueId != null) { + user.add("uniqueId", new JsonPrimitive(this.uniqueId.toString())); + } + return user; + } + + public CommandSenderData toProto() { + CommandSenderData.Builder proto = CommandSenderData.newBuilder() + .setType(isPlayer() ? CommandSenderData.Type.PLAYER : CommandSenderData.Type.OTHER) + .setName(this.name); + + if (this.uniqueId != null) { + proto.setUniqueId(this.uniqueId.toString()); + } + + return proto.build(); + } + + public static CommandSender.Data deserialize(JsonElement element) { + JsonObject userObject = element.getAsJsonObject(); + String user = userObject.get("name").getAsJsonPrimitive().getAsString(); + UUID uuid; + if (userObject.has("uniqueId")) { + uuid = UUID.fromString(userObject.get("uniqueId").getAsJsonPrimitive().getAsString()); + } else { + uuid = null; + } + return new CommandSender.Data(user, uuid); + } + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java index 53de4e8..137a4fe 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java +++ b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java @@ -20,15 +20,12 @@ package me.lucko.spark.common.heapdump; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.util.TypeDescriptors; import me.lucko.spark.proto.SparkProtos; import me.lucko.spark.proto.SparkProtos.HeapData; import me.lucko.spark.proto.SparkProtos.HeapEntry; -import javax.management.JMX; -import javax.management.MBeanServer; -import javax.management.ObjectName; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -41,6 +38,10 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectName; + /** * Represents a "heap dump summary" from the VM. * diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuMonitor.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuMonitor.java index aa653b7..47e3fac 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuMonitor.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuMonitor.java @@ -22,15 +22,16 @@ package me.lucko.spark.common.monitor.cpu; import me.lucko.spark.common.util.RollingAverage; -import javax.management.JMX; -import javax.management.MBeanServer; -import javax.management.ObjectName; import java.lang.management.ManagementFactory; import java.math.BigDecimal; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectName; + /** * Exposes and monitors the system/process CPU usage. */ @@ -73,6 +74,7 @@ public enum CpuMonitor { /** * Ensures that the static initializer has been called. */ + @SuppressWarnings("EmptyMethod") public static void ensureMonitoring() { // intentionally empty } diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java index 7418776..033a2d2 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java @@ -22,7 +22,7 @@ package me.lucko.spark.common.sampler; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.sampler.aggregator.DataAggregator; import me.lucko.spark.common.sampler.aggregator.SimpleDataAggregator; import me.lucko.spark.common.sampler.aggregator.TickedDataAggregator; diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java index fea877c..d0bac92 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java @@ -20,7 +20,7 @@ package me.lucko.spark.fabric; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.AbstractCommandSender; import me.lucko.spark.fabric.plugin.FabricSparkPlugin; import net.kyori.text.Component; import net.kyori.text.serializer.gson.GsonComponentSerializer; @@ -31,30 +31,29 @@ import net.minecraft.text.Text; import java.util.UUID; -public class FabricCommandSender implements CommandSender { - private final CommandOutput sender; +public class FabricCommandSender extends AbstractCommandSender { private final FabricSparkPlugin plugin; - public FabricCommandSender(CommandOutput sender, FabricSparkPlugin plugin) { - this.sender = sender; + public FabricCommandSender(CommandOutput commandOutput, FabricSparkPlugin plugin) { + super(commandOutput); this.plugin = plugin; } @Override public String getName() { - if (this.sender instanceof PlayerEntity) { - return ((PlayerEntity) this.sender).getGameProfile().getName(); - } else if (this.sender instanceof DedicatedServer) { + if (super.delegate instanceof PlayerEntity) { + return ((PlayerEntity) super.delegate).getGameProfile().getName(); + } else if (super.delegate instanceof DedicatedServer) { return "Console"; } else { - return "unknown:" + this.sender.getClass().getSimpleName(); + return "unknown:" + super.delegate.getClass().getSimpleName(); } } @Override public UUID getUniqueId() { - if (this.sender instanceof PlayerEntity) { - return ((PlayerEntity) this.sender).getUuid(); + if (super.delegate instanceof PlayerEntity) { + return ((PlayerEntity) super.delegate).getUuid(); } return null; } @@ -62,28 +61,11 @@ public class FabricCommandSender implements CommandSender { @Override public void sendMessage(Component message) { Text component = Text.Serializer.fromJson(GsonComponentSerializer.INSTANCE.serialize(message)); - this.sender.sendMessage(component); + super.delegate.sendMessage(component); } @Override public boolean hasPermission(String permission) { - return this.plugin.hasPermission(this.sender, permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - FabricCommandSender that = (FabricCommandSender) o; - return this.sender.equals(that.sender); - } - - @Override - public int hashCode() { - return this.sender.hashCode(); + return this.plugin.hasPermission(super.delegate, permission); } } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java index ff988f0..4ef5087 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java @@ -20,7 +20,7 @@ package me.lucko.spark.forge; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.AbstractCommandSender; import me.lucko.spark.forge.plugin.ForgeSparkPlugin; import net.kyori.text.Component; import net.kyori.text.serializer.gson.GsonComponentSerializer; @@ -31,30 +31,29 @@ import net.minecraft.util.text.ITextComponent; import java.util.UUID; -public class ForgeCommandSender implements CommandSender { - private final ICommandSource sender; +public class ForgeCommandSender extends AbstractCommandSender { private final ForgeSparkPlugin plugin; - public ForgeCommandSender(ICommandSource sender, ForgeSparkPlugin plugin) { - this.sender = sender; + public ForgeCommandSender(ICommandSource source, ForgeSparkPlugin plugin) { + super(source); this.plugin = plugin; } @Override public String getName() { - if (this.sender instanceof PlayerEntity) { - return ((PlayerEntity) this.sender).getGameProfile().getName(); - } else if (this.sender instanceof IServer) { + if (super.delegate instanceof PlayerEntity) { + return ((PlayerEntity) super.delegate).getGameProfile().getName(); + } else if (super.delegate instanceof IServer) { return "Console"; } else { - return "unknown:" + this.sender.getClass().getSimpleName(); + return "unknown:" + super.delegate.getClass().getSimpleName(); } } @Override public UUID getUniqueId() { - if (this.sender instanceof PlayerEntity) { - return ((PlayerEntity) this.sender).getUniqueID(); + if (super.delegate instanceof PlayerEntity) { + return ((PlayerEntity) super.delegate).getUniqueID(); } return null; } @@ -62,24 +61,11 @@ public class ForgeCommandSender implements CommandSender { @Override public void sendMessage(Component message) { ITextComponent component = ITextComponent.Serializer.fromJson(GsonComponentSerializer.INSTANCE.serialize(message)); - this.sender.sendMessage(component); + super.delegate.sendMessage(component); } @Override public boolean hasPermission(String permission) { - return this.plugin.hasPermission(this.sender, permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - ForgeCommandSender that = (ForgeCommandSender) o; - return this.sender.equals(that.sender); - } - - @Override - public int hashCode() { - return this.sender.hashCode(); + return this.plugin.hasPermission(super.delegate, permission); } } diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java index 6f2c3a2..ca0d5d2 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java @@ -20,7 +20,7 @@ package me.lucko.spark.sponge; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.AbstractCommandSender; import net.kyori.text.Component; import net.kyori.text.adapter.spongeapi.TextAdapter; import org.spongepowered.api.command.CommandSource; @@ -28,46 +28,31 @@ import org.spongepowered.api.entity.living.player.Player; import java.util.UUID; -public class SpongeCommandSender implements CommandSender { - private final CommandSource source; - +public class SpongeCommandSender extends AbstractCommandSender { public SpongeCommandSender(CommandSource source) { - this.source = source; + super(source); } @Override public String getName() { - return this.source.getName(); + return super.delegate.getName(); } @Override public UUID getUniqueId() { - if (this.source instanceof Player) { - return ((Player) this.source).getUniqueId(); + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUniqueId(); } return null; } @Override public void sendMessage(Component message) { - TextAdapter.sendComponent(this.source, message); + TextAdapter.sendComponent(super.delegate, message); } @Override public boolean hasPermission(String permission) { - return this.source.hasPermission(permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - SpongeCommandSender that = (SpongeCommandSender) o; - return this.source.equals(that.source); - } - - @Override - public int hashCode() { - return this.source.hashCode(); + return super.delegate.hasPermission(permission); } } diff --git a/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java index e9b4474..e49e802 100644 --- a/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java +++ b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java @@ -23,57 +23,42 @@ package me.lucko.spark.velocity; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.api.proxy.Player; -import me.lucko.spark.common.CommandSender; +import me.lucko.spark.common.command.sender.AbstractCommandSender; import net.kyori.text.Component; import java.util.UUID; -public class VelocityCommandSender implements CommandSender { - private final CommandSource source; - +public class VelocityCommandSender extends AbstractCommandSender { public VelocityCommandSender(CommandSource source) { - this.source = source; + super(source); } @Override public String getName() { - if (this.source instanceof Player) { - return ((Player) this.source).getUsername(); - } else if (this.source instanceof ConsoleCommandSource) { + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUsername(); + } else if (super.delegate instanceof ConsoleCommandSource) { return "Console"; } else { - return "unknown:" + this.source.getClass().getSimpleName(); + return "unknown:" + super.delegate.getClass().getSimpleName(); } } @Override public UUID getUniqueId() { - if (this.source instanceof Player) { - return ((Player) this.source).getUniqueId(); + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUniqueId(); } return null; } @Override public void sendMessage(Component message) { - this.source.sendMessage(message); + super.delegate.sendMessage(message); } @Override public boolean hasPermission(String permission) { - return this.source.hasPermission(permission); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - VelocityCommandSender that = (VelocityCommandSender) o; - return this.source.equals(that.source); - } - - @Override - public int hashCode() { - return this.source.hashCode(); + return super.delegate.hasPermission(permission); } } -- cgit