Skip to content

Commit

Permalink
Fix connection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Oct 27, 2024
1 parent ccad1c4 commit fdc682f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public abstract class PlayerManagerMixin {
@Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/server/network/ConnectedClientData;)V", at = @At("RETURN"))
private void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) {
AuthEventHandler.onPlayerJoin(player);
((PlayerAuth) player).easyAuth$setIpAddress(connection);
}

@ModifyVariable(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/server/network/ConnectedClientData;)V",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xyz.nikitacartes.easyauth.mixin;

import com.google.common.net.InetAddresses;
import net.minecraft.entity.Entity;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.registry.RegistryKey;
import net.minecraft.scoreboard.ScoreboardCriterion;
Expand All @@ -24,6 +26,8 @@
import xyz.nikitacartes.easyauth.storage.PlayerCacheV0;
import xyz.nikitacartes.easyauth.utils.*;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.EnumSet;
import java.util.Locale;

Expand All @@ -42,6 +46,9 @@ public abstract class ServerPlayerEntityMixin implements PlayerAuth {
@Unique
private long kickTimer = config.kickTimeout * 20;

@Unique
private String ipAddr = null;

@Override
public void easyAuth$saveLastLocation(boolean saveDimension) {
PlayerCacheV0 cache = playerCacheMap.get(this.easyAuth$getFakeUuid());
Expand All @@ -58,9 +65,9 @@ public abstract class ServerPlayerEntityMixin implements PlayerAuth {
cache.lastLocation.pitch = player.getPitch();
cache.ridingEntityUUID = player.getVehicle() != null ? player.getVehicle().getUuid() : null;
cache.wasDead = player.isDead();
LogDebug(String.format("Saving position of player %s as %s", player.getName().getContent(), cache.lastLocation));
LogDebug(String.format("Saving position of player %s as %s", player.getNameForScoreboard(), cache.lastLocation));
if (cache.ridingEntityUUID != null) {
LogDebug(String.format("Saving vehicle of player %s as %s", player.getName().getContent(), cache.ridingEntityUUID));
LogDebug(String.format("Saving vehicle of player %s as %s", player.getNameForScoreboard(), cache.ridingEntityUUID));
}
}

Expand Down Expand Up @@ -100,7 +107,7 @@ public abstract class ServerPlayerEntityMixin implements PlayerAuth {
cache.lastLocation.yaw,
cache.lastLocation.pitch,
true);
LogDebug(String.format("Teleported player %s to %s", player.getName().getContent(), cache.lastLocation));
LogDebug(String.format("Teleported player %s to %s", player.getNameForScoreboard(), cache.lastLocation));

if (cache.ridingEntityUUID != null) {
LogDebug(String.format("Mounting player to vehicle %s", cache.ridingEntityUUID));
Expand All @@ -111,7 +118,7 @@ public abstract class ServerPlayerEntityMixin implements PlayerAuth {
if (entity != null) {
player.startRiding(entity, true);
} else {
LogDebug("Could not find vehicle for player " + player.getName().getContent());
LogDebug("Could not find vehicle for player " + player.getNameForScoreboard());
}
}
}
Expand Down Expand Up @@ -261,4 +268,13 @@ private boolean onPlayerConnectStartRiding(ServerPlayerEntity instance) {
}
return instance.hasVehicle();
}

public String easyAuth$getIpAddress() {
return ipAddr;
}

public void easyAuth$setIpAddress(ClientConnection connection) {
SocketAddress socketAddress = connection.getAddress();
ipAddr = socketAddress instanceof InetSocketAddress inetSocketAddress ? InetAddresses.toAddrString(inetSocketAddress.getAddress()) : "<unknown>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public enum PasswordOptions {

public static boolean hasValidSession(ServerPlayerEntity player) {
String uuid = ((PlayerAuth) player).easyAuth$getFakeUuid();
String playerIp = player.getIp();
String playerIp = ((PlayerAuth) player).easyAuth$getIpAddress();
return ((PlayerAuth) player).easyAuth$canSkipAuth() ||
(playerCacheMap.containsKey(uuid) &&
playerCacheMap.get(uuid).isAuthenticated &&
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/xyz/nikitacartes/easyauth/utils/PlayerAuth.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nikitacartes.easyauth.utils;

import net.minecraft.network.ClientConnection;
import net.minecraft.registry.RegistryKey;
import net.minecraft.world.World;

Expand Down Expand Up @@ -63,4 +64,18 @@ public interface PlayerAuth {
* @return true if paid, false if cracked
*/
boolean easyAuth$isUsingMojangAccount();

/**
* Gets the player's IP address on connection step.
*
* @return player's IP address as string
*/
String easyAuth$getIpAddress();

/**
* Sets the player's IP address on connection step.
*
* @param ClientConnection connection
*/
void easyAuth$setIpAddress(ClientConnection connection);
}

0 comments on commit fdc682f

Please sign in to comment.