aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2019-05-13 10:57:06 +0100
committerLuck <git@lucko.me>2019-05-13 10:57:06 +0100
commit32f6355c8c7fc1611140bfcce6afe8b25cea5697 (patch)
treed2f87081d98f312ff46ba734c104ff730c848d4f
parentcf3ce4d9d7c443e0051eb7cff7dc71cf2a0eb851 (diff)
downloadspark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.tar.gz
spark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.tar.bz2
spark-32f6355c8c7fc1611140bfcce6afe8b25cea5697.zip
Activity log improvements
-rw-r--r--spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitCommandSender.java11
-rw-r--r--spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordCommandSender.java11
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java62
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/CommandSender.java4
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java18
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java10
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java4
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java11
-rw-r--r--spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java11
-rw-r--r--spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java10
10 files changed, 128 insertions, 24 deletions
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 dacea76..d353845 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
@@ -23,6 +23,9 @@ package me.lucko.spark.bukkit;
import me.lucko.spark.common.CommandSender;
import net.kyori.text.Component;
import net.kyori.text.adapter.bukkit.TextAdapter;
+import org.bukkit.entity.Player;
+
+import java.util.UUID;
public class BukkitCommandSender implements CommandSender {
private final org.bukkit.command.CommandSender sender;
@@ -37,6 +40,14 @@ public class BukkitCommandSender implements CommandSender {
}
@Override
+ public UUID getUniqueId() {
+ if (this.sender instanceof Player) {
+ return ((Player) this.sender).getUniqueId();
+ }
+ return null;
+ }
+
+ @Override
public void sendMessage(Component message) {
TextAdapter.sendComponent(this.sender, message);
}
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 d3a831c..193b38a 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
@@ -23,6 +23,9 @@ package me.lucko.spark.bungeecord;
import me.lucko.spark.common.CommandSender;
import net.kyori.text.Component;
import net.kyori.text.adapter.bungeecord.TextAdapter;
+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;
@@ -37,6 +40,14 @@ public class BungeeCordCommandSender implements CommandSender {
}
@Override
+ public UUID getUniqueId() {
+ if (this.sender instanceof ProxiedPlayer) {
+ return ((ProxiedPlayer) this.sender).getUniqueId();
+ }
+ return null;
+ }
+
+ @Override
public void sendMessage(Component message) {
TextAdapter.sendComponent(this.sender, message);
}
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
index 4b5e942..9228938 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/ActivityLog.java
@@ -36,6 +36,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
+import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class ActivityLog {
@@ -45,7 +46,7 @@ public class ActivityLog {
private final Path file;
- private final List<Activity> log = new LinkedList<>();
+ private final LinkedList<Activity> log = new LinkedList<>();
private final Object[] mutex = new Object[0];
public ActivityLog(Path file) {
@@ -54,7 +55,7 @@ public class ActivityLog {
public void addToLog(Activity activity) {
synchronized (this.mutex) {
- this.log.add(activity);
+ this.log.addFirst(activity);
}
save();
}
@@ -132,15 +133,28 @@ public class ActivityLog {
public static final class Activity {
private final String user;
+ private final UUID uuid;
private final long time;
private final String type;
- private final String url;
- public Activity(String user, long time, String type, String url) {
+ private final String dataType;
+ private final String dataValue;
+
+ public static Activity urlActivity(CommandSender user, long time, String type, String url) {
+ return new Activity(user.getName(), user.getUniqueId(), time, type, "url", url);
+ }
+
+ public static Activity fileActivity(CommandSender user, long time, String type, String filePath) {
+ return new Activity(user.getName(), user.getUniqueId(), time, type, "file", filePath);
+ }
+
+ private Activity(String user, UUID uuid, long time, String type, String dataType, String dataValue) {
this.user = user;
+ this.uuid = uuid;
this.time = time;
this.type = type;
- this.url = url;
+ this.dataType = dataType;
+ this.dataValue = dataValue;
}
public String getUser() {
@@ -155,26 +169,39 @@ public class ActivityLog {
return this.type;
}
- public String getUrl() {
- return this.url;
+ public String getDataType() {
+ return this.dataType;
+ }
+
+ public String getDataValue() {
+ return this.dataValue;
}
public boolean shouldExpire() {
- return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7);
+ if (dataType.equals("url")) {
+ return (System.currentTimeMillis() - this.time) > TimeUnit.DAYS.toMillis(7);
+ } else {
+ return false;
+ }
}
public JsonObject serialize() {
JsonObject object = new JsonObject();
JsonObject user = new JsonObject();
+ user.add("type", new JsonPrimitive(this.uuid != null ? "player" : "other"));
user.add("name", new JsonPrimitive(this.user));
+ if (this.uuid != null) {
+ user.add("uuid", new JsonPrimitive(this.uuid.toString()));
+ }
object.add("user", user);
object.add("time", new JsonPrimitive(this.time));
object.add("type", new JsonPrimitive(this.type));
JsonObject data = new JsonObject();
- data.add("url", new JsonPrimitive(this.url));
+ data.add("type", new JsonPrimitive(this.dataType));
+ data.add("value", new JsonPrimitive(this.dataValue));
object.add("data", data);
return object;
@@ -183,12 +210,23 @@ public class ActivityLog {
public static Activity deserialize(JsonElement element) {
JsonObject object = element.getAsJsonObject();
- String user = object.get("user").getAsJsonObject().get("name").getAsJsonPrimitive().getAsString();
+ JsonObject userObject = object.get("user").getAsJsonObject();
+ String user = userObject.get("name").getAsJsonPrimitive().getAsString();
+ UUID uuid;
+ if (userObject.has("uuid")) {
+ uuid = UUID.fromString(userObject.get("uuid").getAsJsonPrimitive().getAsString());
+ } else {
+ uuid = null;
+ }
+
long time = object.get("time").getAsJsonPrimitive().getAsLong();
String type = object.get("type").getAsJsonPrimitive().getAsString();
- String url = object.get("data").getAsJsonObject().get("url").getAsJsonPrimitive().getAsString();
- return new Activity(user, time, type, url);
+ JsonObject dataObject = object.get("data").getAsJsonObject();
+ String dataType = dataObject.get("type").getAsJsonPrimitive().getAsString();
+ String dataValue = dataObject.get("value").getAsJsonPrimitive().getAsString();
+
+ return new Activity(user, uuid, 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
index 9d2ecc0..4c684ba 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/CommandSender.java
@@ -22,10 +22,14 @@ package me.lucko.spark.common;
import net.kyori.text.Component;
+import java.util.UUID;
+
public interface CommandSender {
String getName();
+ UUID getUniqueId();
+
void sendMessage(Component message);
boolean hasPermission(String permission);
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 36ba47c..e2bd80d 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
@@ -67,14 +67,18 @@ public class ActivityLogModule implements CommandModule {
.append(TextComponent.of(activity.getUser(), TextColor.WHITE))
.build()
);
+
+ TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue())
+ .color(TextColor.WHITE)
+ .decoration(TextDecoration.UNDERLINED, true);
+
+ if (activity.getDataType().equals("url")) {
+ valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue()));
+ }
+
resp.replyPrefixed(TextComponent.builder(" ")
- .append(TextComponent.of("Link: ", TextColor.GRAY))
- .append(TextComponent.builder(activity.getUrl())
- .color(TextColor.WHITE)
- .decoration(TextDecoration.UNDERLINED, true)
- .clickEvent(ClickEvent.openUrl(activity.getUrl()))
- .build()
- )
+ .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY))
+ .append(valueComponent)
.build()
);
resp.reply(TextComponent.space());
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 e3f1cdb..42c03d0 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,7 +20,7 @@
package me.lucko.spark.common.command.modules;
-import me.lucko.spark.common.ActivityLog;
+import me.lucko.spark.common.ActivityLog.Activity;
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.command.Command;
import me.lucko.spark.common.command.CommandModule;
@@ -77,7 +77,7 @@ public class MemoryModule implements CommandModule {
.build()
);
- platform.getActivityLog().addToLog(new ActivityLog.Activity(sender.getName(), System.currentTimeMillis(), "Heap dump summary", url));
+ platform.getActivityLog().addToLog(Activity.urlActivity(sender, System.currentTimeMillis(), "Heap dump summary", url));
} catch (IOException e) {
resp.broadcastPrefixed(TextComponent.of("An error occurred whilst uploading the data.", TextColor.RED));
e.printStackTrace();
@@ -119,7 +119,11 @@ public class MemoryModule implements CommandModule {
return;
}
- resp.broadcastPrefixed(TextComponent.of("Heap dump written to: " + file.toString(), TextColor.GOLD));
+ resp.broadcastPrefixed(TextComponent.builder("Heap dump written to: ", TextColor.GOLD)
+ .append(TextComponent.of(file.toString(), TextColor.DARK_GRAY))
+ .build()
+ );
+ platform.getActivityLog().addToLog(Activity.fileActivity(sender, System.currentTimeMillis(), "Heap dump", file.toString()));
});
})
.tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--run-gc-before", "--include-non-live"))
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 11d23da..4ea6144 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,7 +20,7 @@
package me.lucko.spark.common.command.modules;
-import me.lucko.spark.common.ActivityLog;
+import me.lucko.spark.common.ActivityLog.Activity;
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.common.command.Command;
import me.lucko.spark.common.command.CommandModule;
@@ -255,7 +255,7 @@ public class SamplerModule implements CommandModule {
.build()
);
- platform.getActivityLog().addToLog(new ActivityLog.Activity(resp.sender().getName(), System.currentTimeMillis(), "Sampler", url));
+ platform.getActivityLog().addToLog(Activity.urlActivity(resp.sender(), System.currentTimeMillis(), "Sampler", url));
} catch (IOException e) {
resp.broadcastPrefixed(TextComponent.of("An error occurred whilst uploading the results.", TextColor.RED));
e.printStackTrace();
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 4b71d86..f7b2122 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
@@ -24,8 +24,11 @@ import me.lucko.spark.common.CommandSender;
import net.kyori.text.Component;
import net.kyori.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.command.ICommandSender;
+import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.ITextComponent;
+import java.util.UUID;
+
public class ForgeCommandSender implements CommandSender {
private final ICommandSender sender;
private final ForgeSparkPlugin plugin;
@@ -41,6 +44,14 @@ public class ForgeCommandSender implements CommandSender {
}
@Override
+ public UUID getUniqueId() {
+ if (this.sender instanceof EntityPlayer) {
+ return ((EntityPlayer) this.sender).getUniqueID();
+ }
+ return null;
+ }
+
+ @Override
public void sendMessage(Component message) {
ITextComponent component = ITextComponent.Serializer.jsonToComponent(GsonComponentSerializer.INSTANCE.serialize(message));
this.sender.sendMessage(component);
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 5b434fc..6f2c3a2 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
@@ -24,6 +24,9 @@ import me.lucko.spark.common.CommandSender;
import net.kyori.text.Component;
import net.kyori.text.adapter.spongeapi.TextAdapter;
import org.spongepowered.api.command.CommandSource;
+import org.spongepowered.api.entity.living.player.Player;
+
+import java.util.UUID;
public class SpongeCommandSender implements CommandSender {
private final CommandSource source;
@@ -38,6 +41,14 @@ public class SpongeCommandSender implements CommandSender {
}
@Override
+ public UUID getUniqueId() {
+ if (this.source instanceof Player) {
+ return ((Player) this.source).getUniqueId();
+ }
+ return null;
+ }
+
+ @Override
public void sendMessage(Component message) {
TextAdapter.sendComponent(this.source, message);
}
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 1cad493..e9b4474 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
@@ -26,6 +26,8 @@ import com.velocitypowered.api.proxy.Player;
import me.lucko.spark.common.CommandSender;
import net.kyori.text.Component;
+import java.util.UUID;
+
public class VelocityCommandSender implements CommandSender {
private final CommandSource source;
@@ -45,6 +47,14 @@ public class VelocityCommandSender implements CommandSender {
}
@Override
+ public UUID getUniqueId() {
+ if (this.source instanceof Player) {
+ return ((Player) this.source).getUniqueId();
+ }
+ return null;
+ }
+
+ @Override
public void sendMessage(Component message) {
this.source.sendMessage(message);
}