Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Rename EnumProtocol to ConnectionMode and move it out the API.
Browse files Browse the repository at this point in the history
API endpoints have been added to replace old functionality
  • Loading branch information
Earthcomputer committed Feb 10, 2020
1 parent 0cdbc1f commit 96e6f0a
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 116 deletions.
86 changes: 0 additions & 86 deletions src/api/java/net/earthcomputer/multiconnect/api/EnumProtocol.java

This file was deleted.

18 changes: 18 additions & 0 deletions src/api/java/net/earthcomputer/multiconnect/api/IProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.earthcomputer.multiconnect.api;

/**
* Contains information about a supported protocol version
*/
public interface IProtocol {

/**
* Returns the protocol version ID, to be compared with values in {@link Protocols}.
*/
int getValue();

/**
* Returns the name of the Minecraft version that this protocol represents.
*/
String getName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.minecraft.SharedConstants;

import java.util.Collections;
import java.util.List;

/**
* The MultiConnect API
*/
Expand All @@ -24,4 +27,32 @@ public int getProtocolVersion() {
return SharedConstants.getGameVersion().getProtocolVersion();
}

/**
* Gets a supported {@link IProtocol} object by its protocol version, or <tt>null</tt> if the protocol is not supported
*/
public IProtocol byProtocolVersion(int version) {
return version == SharedConstants.getGameVersion().getProtocolVersion() ? CurrentVersionProtocol.INSTANCE : null;
}

/**
* Returns a list of supported protocols, from newest to oldest
*/
public List<IProtocol> getSupportedProtocols() {
return Collections.singletonList(CurrentVersionProtocol.INSTANCE);
}

private static class CurrentVersionProtocol implements IProtocol {
public static CurrentVersionProtocol INSTANCE = new CurrentVersionProtocol();

@Override
public int getValue() {
return SharedConstants.getGameVersion().getProtocolVersion();
}

@Override
public String getName() {
return SharedConstants.getGameVersion().getName();
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/net/earthcomputer/multiconnect/api/Initializer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.earthcomputer.multiconnect.api;

import net.earthcomputer.multiconnect.impl.ConnectionInfo;
import net.earthcomputer.multiconnect.impl.ConnectionMode;
import net.fabricmc.api.ModInitializer;

import java.util.Arrays;
import java.util.List;

// In the api package so we can access MultiConnectAPI.INSTANCE
public class Initializer implements ModInitializer {
@Override
Expand All @@ -12,6 +16,17 @@ public void onInitialize() {
public int getProtocolVersion() {
return ConnectionInfo.protocolVersion;
}

@Override
public IProtocol byProtocolVersion(int version) {
ConnectionMode protocol = ConnectionMode.byValue(version);
return protocol == ConnectionMode.AUTO ? null : protocol;
}

@Override
public List<IProtocol> getSupportedProtocols() {
return Arrays.asList(ConnectionMode.protocolValues());
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package net.earthcomputer.multiconnect.impl;

import net.earthcomputer.multiconnect.api.IProtocol;
import net.earthcomputer.multiconnect.api.Protocols;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* Enum constants for each protocol, and the auto connection mode
*/
public enum ConnectionMode implements IProtocol {

// Protocols should go in reverse chronological order
AUTO("Auto", -1),
V1_15_2("1.15.2", Protocols.V1_15_2),
V1_15_1("1.15.1", Protocols.V1_15_1),
V1_15("1.15", Protocols.V1_15),
V1_14_4("1.14.4", Protocols.V1_14_4),
V1_14_3("1.14.3", Protocols.V1_14_3),
V1_14_2("1.14.2", Protocols.V1_14_2),
V1_14_1("1.14.1", Protocols.V1_14_1),
V1_14("1.14", Protocols.V1_14),
V1_13_2("1.13.2", Protocols.V1_13_2),
V1_13_1("1.13.1", Protocols.V1_13_1),
V1_13("1.13", Protocols.V1_13),
V1_12_2("1.12.2", Protocols.V1_12_2),
V1_12_1("1.12.1", Protocols.V1_12_1),
V1_12("1.12", Protocols.V1_12),
;

private final int value;
private final String name;
private final String assetId;

ConnectionMode(final String name, final int value) {
this(name, name, value);
}

ConnectionMode(final String name, final String assetId, final int value) {
this.value = value;
this.name = name;
this.assetId = assetId;
}

@Override
public int getValue() {
return value;
}

@Override
public String getName() {
return name;
}

public String getAssetId() {
return assetId;
}

private static final ConnectionMode[] VALUES = values();
private static final ConnectionMode[] PROTOCOLS = Arrays.stream(VALUES).filter(it -> it != AUTO).toArray(ConnectionMode[]::new);
private static final Map<Integer, ConnectionMode> BY_VALUE = new HashMap<>();

public static ConnectionMode byValue(int value) {
return BY_VALUE.getOrDefault(value, AUTO);
}

public static ConnectionMode[] protocolValues() {
return PROTOCOLS;
}

public static boolean isSupportedProtocol(int protocol) {
return byValue(protocol) != AUTO;
}

public ConnectionMode next() {
return VALUES[(ordinal() + 1) % VALUES.length];
}

static {
for (ConnectionMode value : VALUES) {
BY_VALUE.put(value.getValue(), value);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import net.earthcomputer.multiconnect.api.EnumProtocol;
import net.earthcomputer.multiconnect.protocols.ProtocolRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.SharedConstants;
Expand Down Expand Up @@ -51,8 +50,8 @@ public static void addExtraTranslations(String nativeLang, BiConsumer<String, St
if (ConnectionInfo.protocol == ProtocolRegistry.latest())
return;

String currentVersion = EnumProtocol.byValue(ConnectionInfo.protocolVersion).getAssetID();
String latestVersion = EnumProtocol.byValue(SharedConstants.getGameVersion().getProtocolVersion()).getAssetID();
String currentVersion = ConnectionMode.byValue(ConnectionInfo.protocolVersion).getAssetId();
String latestVersion = ConnectionMode.byValue(SharedConstants.getGameVersion().getProtocolVersion()).getAssetId();

Map<String, String> currentNative = getTranslations(currentVersion, nativeLang);
Map<String, String> currentFallback = getTranslations(currentVersion, "en_us");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.earthcomputer.multiconnect.api.EnumProtocol;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.ServerAddress;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -52,7 +51,7 @@ private ServersExt() {}

public int getForcedProtocol(String address) {
ServerExt server = servers.get(normalizeAddress(address));
return server == null ? EnumProtocol.AUTO.getValue() : server.forcedProtocol;
return server == null ? ConnectionMode.AUTO.getValue() : server.forcedProtocol;
}

public boolean hasServer(String address) {
Expand All @@ -74,7 +73,7 @@ private static String normalizeAddress(String address) {

public static class ServerExt {

public int forcedProtocol = EnumProtocol.AUTO.getValue();
public int forcedProtocol = ConnectionMode.AUTO.getValue();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.earthcomputer.multiconnect.mixin;

import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.earthcomputer.multiconnect.api.EnumProtocol;
import net.earthcomputer.multiconnect.impl.ConnectionMode;
import net.earthcomputer.multiconnect.impl.ServersExt;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.AddServerScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand All @@ -23,7 +22,7 @@ public abstract class MixinAddServerScreen extends Screen {

@Shadow @Final private ServerInfo server;

@Unique private EnumProtocol currentProtocol;
@Unique private ConnectionMode currentProtocol;
@Unique private ButtonWidget protocolSelector;

protected MixinAddServerScreen(Text title) {
Expand All @@ -32,7 +31,7 @@ protected MixinAddServerScreen(Text title) {

@Inject(method = "<init>", at = @At("RETURN"))
private void onConstructor(Screen parent, BooleanConsumer callback, ServerInfo server, CallbackInfo ci) {
currentProtocol = EnumProtocol.byValue(ServersExt.getInstance().getForcedProtocol(server.address));
currentProtocol = ConnectionMode.byValue(ServersExt.getInstance().getForcedProtocol(server.address));
}

@Inject(method = "init", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.earthcomputer.multiconnect.mixin;

import net.earthcomputer.multiconnect.api.EnumProtocol;
import net.earthcomputer.multiconnect.impl.ConnectionMode;
import net.earthcomputer.multiconnect.impl.*;
import net.earthcomputer.multiconnect.protocols.ProtocolRegistry;
import net.minecraft.SharedConstants;
Expand Down Expand Up @@ -41,9 +41,9 @@ public void beforeConnect(CallbackInfo ci) throws UnknownHostException {
address = ConnectionInfo.ip + ":" + ConnectionInfo.port;
}
int forcedVersion = ServersExt.getInstance().getForcedProtocol(address);
if (forcedVersion != EnumProtocol.AUTO.getValue()) {
if (forcedVersion != ConnectionMode.AUTO.getValue()) {
ConnectionInfo.protocolVersion = forcedVersion;
LogManager.getLogger("multiconnect").info("Protocol version forced to " + ConnectionInfo.protocolVersion + " (" + EnumProtocol.byValue(forcedVersion).getName() + ")");
LogManager.getLogger("multiconnect").info("Protocol version forced to " + ConnectionInfo.protocolVersion + " (" + ConnectionMode.byValue(forcedVersion).getName() + ")");
return;
}

Expand Down Expand Up @@ -83,8 +83,8 @@ public void beforeConnect(CallbackInfo ci) throws UnknownHostException {

connectScreen.multiconnect_setVersionRequestConnection(null);

if (ProtocolRegistry.isSupported(ConnectionInfo.protocolVersion)) {
LogManager.getLogger("multiconnect").info("Discovered server protocol: " + ConnectionInfo.protocolVersion + " (" + EnumProtocol.byValue(ConnectionInfo.protocolVersion).getName() + ")");
if (ConnectionMode.isSupportedProtocol(ConnectionInfo.protocolVersion)) {
LogManager.getLogger("multiconnect").info("Discovered server protocol: " + ConnectionInfo.protocolVersion + " (" + ConnectionMode.byValue(ConnectionInfo.protocolVersion).getName() + ")");
} else {
LogManager.getLogger("multiconnect").info("Discovered server protocol: " + ConnectionInfo.protocolVersion + " (unsupported), " +
"falling back to " + SharedConstants.getGameVersion().getProtocolVersion() + " (" + SharedConstants.getGameVersion().getName() + ")");
Expand All @@ -94,7 +94,7 @@ public void beforeConnect(CallbackInfo ci) throws UnknownHostException {

@Redirect(method = "run()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;send(Lnet/minecraft/network/Packet;)V", ordinal = 0))
public void sendHandshake(ClientConnection connect, Packet<?> packet) {
if (ProtocolRegistry.isSupported(ConnectionInfo.protocolVersion)) {
if (ConnectionMode.isSupportedProtocol(ConnectionInfo.protocolVersion)) {
((HandshakePacketAccessor) packet).setVersion(ConnectionInfo.protocolVersion);
ConnectionInfo.protocol = ProtocolRegistry.get(ConnectionInfo.protocolVersion);
ConnectionInfo.protocol.setup(false);
Expand Down
Loading

0 comments on commit 96e6f0a

Please sign in to comment.