Update GsonUtil, more javadoc

This commit is contained in:
KennyTV 2021-04-16 23:05:31 +02:00
parent cfec0cc25f
commit d0882cf02c
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
14 changed files with 69 additions and 37 deletions

View File

@ -23,11 +23,11 @@
package us.myles.ViaVersion.api.type.types;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.util.GsonUtil;
public class ComponentType extends Type<JsonElement> {
private static final StringType STRING_TAG = new StringType(262144);
@ -40,7 +40,7 @@ public class ComponentType extends Type<JsonElement> {
public JsonElement read(ByteBuf buffer) throws Exception {
String s = STRING_TAG.read(buffer);
try {
return GsonUtil.getJsonParser().parse(s);
return JsonParser.parseString(s);
} catch (JsonSyntaxException e) {
Via.getPlatform().getLogger().severe("Error when trying to parse json: " + s);
throw e;

View File

@ -25,11 +25,11 @@ package us.myles.ViaVersion.packets;
public enum Direction {
/**
* Outgoing server packets sent to the client.
* Clientbound packets sent by the server.
*/
OUTGOING,
/**
* Incoming server packets send by the client to the server.
* Serverbound packets sent by the client.
*/
INCOMING
}

View File

@ -22,6 +22,7 @@
*/
package us.myles.ViaVersion.protocols.base;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
@ -42,6 +43,11 @@ public class ProtocolInfo extends StoredObject {
super(user);
}
/**
* Returns the protocol state the user is currently in.
*
* @return protocol state
*/
public State getState() {
return state;
}
@ -50,6 +56,12 @@ public class ProtocolInfo extends StoredObject {
this.state = state;
}
/**
* Returns the user's protocol version, or -1 if not set.
* This is set during the {@link State#HANDSHAKE} state.
*
* @return protocol version, or -1 if not set
*/
public int getProtocolVersion() {
return protocolVersion;
}
@ -60,6 +72,12 @@ public class ProtocolInfo extends StoredObject {
this.protocolVersion = protocol.getVersion();
}
/**
* Returns the server protocol version the user is connected to, or -1 if not set.
* This is set during the {@link State#HANDSHAKE} state.
*
* @return server protocol version, or -1 if not set
*/
public int getServerProtocolVersion() {
return serverProtocolVersion;
}
@ -69,7 +87,13 @@ public class ProtocolInfo extends StoredObject {
this.serverProtocolVersion = protocol.getVersion();
}
public String getUsername() {
/**
* Returns the username associated with this connection.
* This is set once the connection enters the {@link State#PLAY} state.
*
* @return username, set when entering the {@link State#PLAY} state
*/
public @MonotonicNonNull String getUsername() {
return username;
}
@ -77,6 +101,12 @@ public class ProtocolInfo extends StoredObject {
this.username = username;
}
/**
* Returns the uuid associated with this connection.
* This is set once the connection enters the {@link State#PLAY} state.
*
* @return uuid, set when entering the {@link State#PLAY} state
*/
public UUID getUuid() {
return uuid;
}
@ -85,6 +115,11 @@ public class ProtocolInfo extends StoredObject {
this.uuid = uuid;
}
/**
* Returns the user's pipeline.
*
* @return protocol pipeline
*/
public ProtocolPipeline getPipeline() {
return pipeline;
}

View File

@ -29,9 +29,18 @@ public interface VersionProvider extends Provider {
/**
* Returns the closest server protocol version to the user's protocol version.
* On non-proxy servers, this returns the actual server version.
*
* @param connection connection
* @return closest server protocol version to the user's protocol version
*/
int getServerProtocol(UserConnection connection) throws Exception;
int getClosestServerProtocol(UserConnection connection) throws Exception;
/**
* @deprecated misleading name, use {@link #getClosestServerProtocol(UserConnection)}
*/
@Deprecated
default int getServerProtocol(UserConnection connection) throws Exception {
return getClosestServerProtocol(connection);
}
}

View File

@ -27,28 +27,24 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
public final class GsonUtil {
private static final JsonParser JSON_PARSER = new JsonParser();
private static final Gson GSON = getGsonBuilder().create();
private static final Gson GSON = new GsonBuilder().create();
/**
* Get google's Gson magic
* Returns google's Gson magic.
*
* @return Gson instance
* @return gson instance
*/
public static Gson getGson() {
return GSON;
}
/**
* Get the GsonBuilder in case you want to add other stuff
*
* @return GsonBuilder instance
*/
@Deprecated
public static GsonBuilder getGsonBuilder() {
return new GsonBuilder();
}
@Deprecated
public static JsonParser getJsonParser() {
return JSON_PARSER;
return new JsonParser();
}
}

View File

@ -43,9 +43,9 @@ public class BungeeVersionProvider extends BaseVersionProvider {
}
@Override
public int getServerProtocol(UserConnection user) throws Exception {
public int getClosestServerProtocol(UserConnection user) throws Exception {
if (ref == null)
return super.getServerProtocol(user);
return super.getClosestServerProtocol(user);
// TODO Have one constant list forever until restart? (Might limit plugins if they change this)
List<Integer> list = ReflectionUtil.getStatic(ref, "SUPPORTED_VERSION_IDS", List.class);
List<Integer> sorted = new ArrayList<>(list);

View File

@ -1,11 +0,0 @@
machine:
java:
version: oraclejdk8
general:
artifacts:
- "target/*.jar"
test:
override:
- mvn clean install -B
post:
- cp ./target/*.jar $CIRCLE_ARTIFACTS

View File

@ -19,6 +19,7 @@ package us.myles.ViaVersion.api.rewriters;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import us.myles.ViaVersion.api.Via;
@ -118,7 +119,7 @@ public class ComponentRewriter {
public JsonElement processText(String value) {
try {
JsonElement root = GsonUtil.getJsonParser().parse(value);
JsonElement root = JsonParser.parseString(value);
processText(root);
return root;
} catch (JsonSyntaxException e) {

View File

@ -18,6 +18,7 @@
package us.myles.ViaVersion.commands.defaultsubs;
import com.google.common.io.CharStreams;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.command.ViaCommandSender;
@ -85,7 +86,7 @@ public class DumpSubCmd extends ViaSubCommand {
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write(GsonUtil.getGsonBuilder().setPrettyPrinting().create().toJson(template).getBytes(StandardCharsets.UTF_8));
out.write(new GsonBuilder().setPrettyPrinting().create().toJson(template).getBytes(StandardCharsets.UTF_8));
out.close();
if (con.getResponseCode() == 429) {

View File

@ -57,7 +57,7 @@ public class BaseProtocol extends SimpleProtocol {
}
// Choose the pipe
int serverProtocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user());
int serverProtocol = Via.getManager().getProviders().get(VersionProvider.class).getClosestServerProtocol(wrapper.user());
info.setServerProtocolVersion(serverProtocol);
List<ProtocolPathEntry> protocols = null;

View File

@ -93,7 +93,7 @@ public class BaseProtocol1_7 extends SimpleProtocol {
return;
}
int closestServerProtocol = versionProvider.getServerProtocol(wrapper.user());
int closestServerProtocol = versionProvider.getClosestServerProtocol(wrapper.user());
List<ProtocolPathEntry> protocols = null;
if (info.getProtocolVersion() >= closestServerProtocol || Via.getPlatform().isOldClientsAllowed()) {
protocols = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), closestServerProtocol);

View File

@ -22,7 +22,8 @@ import us.myles.ViaVersion.api.data.UserConnection;
public class BaseVersionProvider implements VersionProvider {
public int getServerProtocol(UserConnection connection) throws Exception {
@Override
public int getClosestServerProtocol(UserConnection connection) throws Exception {
return Via.getAPI().getServerVersion().lowestSupportedVersion();
}
}

View File

@ -21,10 +21,10 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import us.myles.ViaVersion.util.GsonUtil;
public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler {
@Override
@ -35,7 +35,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
}
Tag out = tag.get("LastOutput");
if (out instanceof StringTag) {
JsonElement value = GsonUtil.getJsonParser().parse(((StringTag) out).getValue());
JsonElement value = JsonParser.parseString(((StringTag) out).getValue());
ChatRewriter.processTranslate(value);
((StringTag) out).setValue(value.toString());
}

View File

@ -43,7 +43,7 @@ public class VelocityVersionProvider extends BaseVersionProvider {
}
@Override
public int getServerProtocol(UserConnection user) throws Exception {
public int getClosestServerProtocol(UserConnection user) throws Exception {
return user.isClientSide() ? getBackProtocol(user) : getFrontProtocol(user);
}