From 5cf96aabdd63eacad8df2deff7c29033974a6a52 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 29 Dec 2018 14:09:30 -0500 Subject: [PATCH] Ensure that getting a builder from the ping will produce the same ping. --- .../api/proxy/server/ServerPing.java | 77 ++++++++++++++++++- .../com/velocitypowered/api/util/ModInfo.java | 35 +++++++++ .../api/proxy/server/ServerPingTest.java | 22 ++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java index cd64862f2..66483ae33 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/ServerPing.java @@ -8,6 +8,7 @@ import com.velocitypowered.api.util.ModInfo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.UUID; @@ -73,10 +74,32 @@ public final class ServerPing { + "version=" + version + ", players=" + players + ", description=" + description - + ", favicon='" + favicon + '\'' + + ", favicon=" + favicon + + ", modinfo=" + modinfo + '}'; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ServerPing ping = (ServerPing) o; + return Objects.equals(version, ping.version) + && Objects.equals(players, ping.players) + && Objects.equals(description, ping.description) + && Objects.equals(favicon, ping.favicon) + && Objects.equals(modinfo, ping.modinfo); + } + + @Override + public int hashCode() { + return Objects.hash(version, players, description, favicon, modinfo); + } + /** * Returns a copy of this {@link ServerPing} instance as a builder so that it can be modified. * @return a copy of this instance as a {@link Builder} @@ -281,6 +304,23 @@ public final class ServerPing { + ", name='" + name + '\'' + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Version version = (Version) o; + return protocol == version.protocol && Objects.equals(name, version.name); + } + + @Override + public int hashCode() { + return Objects.hash(protocol, name); + } } public static final class Players { @@ -321,6 +361,24 @@ public final class ServerPing { + ", sample=" + sample + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Players players = (Players) o; + return online == players.online && max == players.max + && Objects.equals(sample, players.sample); + } + + @Override + public int hashCode() { + return Objects.hash(online, max, sample); + } } public static final class SamplePlayer { @@ -348,5 +406,22 @@ public final class ServerPing { + ", id=" + id + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SamplePlayer that = (SamplePlayer) o; + return Objects.equals(name, that.name) && Objects.equals(id, that.id); + } + + @Override + public int hashCode() { + return Objects.hash(name, id); + } } } diff --git a/api/src/main/java/com/velocitypowered/api/util/ModInfo.java b/api/src/main/java/com/velocitypowered/api/util/ModInfo.java index 623a39cb4..ac1bd0080 100644 --- a/api/src/main/java/com/velocitypowered/api/util/ModInfo.java +++ b/api/src/main/java/com/velocitypowered/api/util/ModInfo.java @@ -4,6 +4,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.gson.annotations.SerializedName; import java.util.List; +import java.util.Objects; public final class ModInfo { @@ -33,6 +34,23 @@ public final class ModInfo { + '}'; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ModInfo modInfo = (ModInfo) o; + return type.equals(modInfo.type) && modList.equals(modInfo.modList); + } + + @Override + public int hashCode() { + return Objects.hash(type, modList); + } + public static final class Mod { @SerializedName("modid") @@ -59,5 +77,22 @@ public final class ModInfo { + ", version='" + version + '\'' + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Mod mod = (Mod) o; + return id.equals(mod.id) && version.equals(mod.version); + } + + @Override + public int hashCode() { + return Objects.hash(id, version); + } } } \ No newline at end of file diff --git a/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java b/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java new file mode 100644 index 000000000..1c1ac49d8 --- /dev/null +++ b/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java @@ -0,0 +1,22 @@ +package com.velocitypowered.api.proxy.server; + +import static org.junit.jupiter.api.Assertions.*; + +import com.google.common.collect.ImmutableList; +import com.velocitypowered.api.proxy.server.ServerPing.Players; +import com.velocitypowered.api.proxy.server.ServerPing.SamplePlayer; +import com.velocitypowered.api.proxy.server.ServerPing.Version; +import java.util.UUID; +import net.kyori.text.TextComponent; +import org.junit.jupiter.api.Test; + +class ServerPingTest { + + @Test + void asBuilderConsistency() { + ServerPing ping = new ServerPing(new Version(404, "1.13.2"), + new Players(1, 1, ImmutableList.of(new SamplePlayer("tuxed", UUID.randomUUID()))), + TextComponent.of("test"), null); + assertEquals(ping, ping.asBuilder().build()); + } +} \ No newline at end of file