diff options
author | Luck <git@lucko.me> | 2019-12-03 14:35:07 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-12-03 14:35:07 +0000 |
commit | 03b6f817f89275e4d5c3d5b1868c3dcc861bf146 (patch) | |
tree | 669fa3086044d5fe9662d161e67866a50734d430 /spark-common/src/main/java/me/lucko/spark/common/command | |
parent | ba1e2a04ddb56f183ac39323ca44733748cb43fd (diff) | |
download | spark-03b6f817f89275e4d5c3d5b1868c3dcc861bf146.tar.gz spark-03b6f817f89275e4d5c3d5b1868c3dcc861bf146.tar.bz2 spark-03b6f817f89275e4d5c3d5b1868c3dcc861bf146.zip |
Some cleanup & refactoring
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/command')
8 files changed, 149 insertions, 6 deletions
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) <luck@lucko.me> + * 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 <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.command.sender; + +public abstract class AbstractCommandSender<S> 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) <luck@lucko.me> + * 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 <http://www.gnu.org/licenses/>. + */ + +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); + } + } + +} |