From c335e2233f38f2823e54bb9e69563bab2bfb9493 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Mon, 11 May 2020 21:25:20 +0200 Subject: [PATCH 1/2] PLayer portal rescue with fake blocks replacing portal --- .../org/samo_lego/simpleauth/SimpleAuth.java | 3 + .../simpleauth/event/AuthEventHandler.java | 86 +++++-------------- .../mixin/BlockUpdateS2CPacketAccess.java | 16 ++++ .../simpleauth/storage/PlayerCache.java | 2 + src/main/resources/mixins.simpleauth.json | 4 +- 5 files changed, 44 insertions(+), 67 deletions(-) create mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 1ecf692..9cd1b74 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -157,6 +157,9 @@ public class SimpleAuth implements DedicatedServerModInitializer { if(config.main.spawnOnJoin) teleportPlayer(player, false); + // Updating chunk if needed (if portal rescue action happened) + if(deauthenticatedUsers.get(convertUuid(player)).wasInPortal) + deauthenticatedUsers.remove(convertUuid(player)); // Player no longer needs to be invisible and invulnerable diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 6dbfc8e..ea36b95 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -6,6 +6,7 @@ import net.minecraft.block.Blocks; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; +import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; @@ -13,15 +14,13 @@ import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; +import org.samo_lego.simpleauth.mixin.BlockUpdateS2CPacketAccess; import org.samo_lego.simpleauth.storage.PlayerCache; import java.net.SocketAddress; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static net.minecraft.block.NetherPortalBlock.AXIS; -import static net.minecraft.util.math.Direction.Axis.Z; import static org.samo_lego.simpleauth.SimpleAuth.*; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; @@ -92,6 +91,10 @@ public class AuthEventHandler { } // Invalidating session playerCache.wasAuthenticated = false; + if(playerCache.isRegistered) + player.sendMessage(new LiteralText(config.lang.loginRequired), false); + else + player.sendMessage(new LiteralText(config.lang.notRegistered), false); } else deauthenticatePlayer(player); @@ -99,73 +102,26 @@ public class AuthEventHandler { if(config.main.spawnOnJoin) teleportPlayer(player, true); + // Tries to rescue player from nether portal if(config.main.tryPortalRescue && player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL)) { - boolean wasSuccessful = false; + PlayerCache newPlayerCache = deauthenticatedUsers.get(uuid); + BlockPos pos = player.getBlockPos(); - BlockState portalState = player.getBlockState(); - World world = player.getEntityWorld(); + // Faking portal blocks to be air + BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket(); + ((BlockUpdateS2CPacketAccess) feetPacket).setState(new BlockState(Blocks.AIR, null)); + ((BlockUpdateS2CPacketAccess) feetPacket).setBlockPos(pos); + player.networkHandler.sendPacket(feetPacket); - double x = player.getX(); - double y = player.getY(); - double z = player.getZ(); + BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket(); + ((BlockUpdateS2CPacketAccess) headPacket).setState(new BlockState(Blocks.AIR, null)); + ((BlockUpdateS2CPacketAccess) headPacket).setBlockPos(pos.up()); + player.networkHandler.sendPacket(headPacket); - if(portalState.get(AXIS) == Z) { - // Player should be put to eastern or western block - if( // Checking towards east - world.getBlockState(new BlockPos(x + 1, y, z)).isAir() && - world.getBlockState(new BlockPos(x + 1, y + 1, z)).isAir() && - ( - world.getBlockState(new BlockPos(x + 1, y - 1, z)).isOpaque() || - world.getBlockState(new BlockPos(x + 1, y - 1, z)).hasSolidTopSurface(world, new BlockPos(x + 1, y - 1, z), player) - ) - ) { - x++; // Towards east - wasSuccessful = true; - } - - else if( // Checking towards south - world.getBlockState(new BlockPos(x - 1, y, z)).isAir() && - world.getBlockState(new BlockPos(x - 1, y + 1, z)).isAir() && - ( - world.getBlockState(new BlockPos(x - 1, y - 1, z)).isOpaque() || - world.getBlockState(new BlockPos(x - 1, y - 1, z)).hasSolidTopSurface(world, new BlockPos(x - 1, y - 1, z), player) - ) - ) { - x--; // Towards south - wasSuccessful = true; - } - } - else { - // Player should be put to northern or southern block - if( // Checking towards south - world.getBlockState(new BlockPos(x, y, z + 1)).isAir() && - world.getBlockState(new BlockPos(x, y + 1, z + 1)).isAir() && - ( - world.getBlockState(new BlockPos(x, y - 1, z + 1)).isOpaque() || - world.getBlockState(new BlockPos(x, y - 1, z + 1)).hasSolidTopSurface(world, new BlockPos(x, y - 1, z + 1), player) - ) - ) { - z++; // Towards south - wasSuccessful = true; - } - - else if( // Checking towards north - world.getBlockState(new BlockPos(x, y, z - 1)).isAir() && - world.getBlockState(new BlockPos(x, y + 1, z - 1)).isAir() && - ( - world.getBlockState(new BlockPos(x, y - 1, z - 1)).isOpaque() || - world.getBlockState(new BlockPos(x, y - 1, z - 1)).hasSolidTopSurface(world, new BlockPos(x, y - 1, z - 1), player) - ) - ) { - z--; // Towards north - wasSuccessful = true; - } - } - if(wasSuccessful) { - player.teleport(x, y, z); - player.sendMessage(successfulPortalRescue, false); - } + // Teleporting player to the middle of the block + player.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); + newPlayerCache.wasInPortal = true; } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java b/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java new file mode 100644 index 0000000..b3778ce --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java @@ -0,0 +1,16 @@ +package org.samo_lego.simpleauth.mixin; + +import net.minecraft.block.BlockState; +import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket; +import net.minecraft.util.math.BlockPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(BlockUpdateS2CPacket.class) +public interface BlockUpdateS2CPacketAccess { + @Accessor("state") + void setState(BlockState state); + + @Accessor("pos") + void setBlockPos(BlockPos pos); +} diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index b15bf5f..f7d43bf 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -23,6 +23,7 @@ public class PlayerCache { public double lastZ; private static final Gson gson = new Gson(); + public boolean wasInPortal; public PlayerCache(String uuid, ServerPlayerEntity player) { @@ -90,5 +91,6 @@ public class PlayerCache { } this.wasAuthenticated = false; this.loginTries = 0; + this.wasInPortal = false; } } diff --git a/src/main/resources/mixins.simpleauth.json b/src/main/resources/mixins.simpleauth.json index f0db7e1..5b109a5 100644 --- a/src/main/resources/mixins.simpleauth.json +++ b/src/main/resources/mixins.simpleauth.json @@ -2,9 +2,9 @@ "required": true, "package": "org.samo_lego.simpleauth.mixin", "compatibilityLevel": "JAVA_8", - "mixins": [ - ], + "mixins": [], "server": [ + "BlockUpdateS2CPacketAccess", "MixinPlayerEntity", "MixinPlayerManager", "MixinServerPlayNetworkHandler", From b250a524263568501615fbaf748216ddb949eebe Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Mon, 18 May 2020 13:22:43 +0200 Subject: [PATCH 2/2] Finishing portal rescue overhaul --- .../org/samo_lego/simpleauth/SimpleAuth.java | 14 ++++++++++++-- .../simpleauth/event/AuthEventHandler.java | 17 +++++++++-------- ...s.java => BlockUpdateS2CPacketAccessor.java} | 2 +- src/main/resources/mixins.simpleauth.json | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) rename src/main/java/org/samo_lego/simpleauth/mixin/{BlockUpdateS2CPacketAccess.java => BlockUpdateS2CPacketAccessor.java} (90%) diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 9cd1b74..251c895 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -11,6 +11,8 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; import net.minecraft.world.dimension.DimensionType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -157,8 +159,16 @@ public class SimpleAuth implements DedicatedServerModInitializer { if(config.main.spawnOnJoin) teleportPlayer(player, false); - // Updating chunk if needed (if portal rescue action happened) - if(deauthenticatedUsers.get(convertUuid(player)).wasInPortal) + // Updating blocks if needed (if portal rescue action happened) + if(deauthenticatedUsers.get(convertUuid(player)).wasInPortal) { + World world = player.getEntityWorld(); + BlockPos pos = player.getBlockPos(); + + // Sending updates to portal blocks + // This is technically not needed, but it cleans the "messed portal" on the client + world.updateListeners(pos, world.getBlockState(pos), world.getBlockState(pos), 3); + world.updateListeners(pos.up(), world.getBlockState(pos.up()), world.getBlockState(pos.up()), 3); + } deauthenticatedUsers.remove(convertUuid(player)); diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index ea36b95..231f52f 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -14,7 +14,7 @@ import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; -import org.samo_lego.simpleauth.mixin.BlockUpdateS2CPacketAccess; +import org.samo_lego.simpleauth.mixin.BlockUpdateS2CPacketAccessor; import org.samo_lego.simpleauth.storage.PlayerCache; import java.net.SocketAddress; @@ -96,8 +96,10 @@ public class AuthEventHandler { else player.sendMessage(new LiteralText(config.lang.notRegistered), false); } - else + else { deauthenticatePlayer(player); + playerCache = deauthenticatedUsers.get(uuid); + } if(config.main.spawnOnJoin) teleportPlayer(player, true); @@ -105,23 +107,22 @@ public class AuthEventHandler { // Tries to rescue player from nether portal if(config.main.tryPortalRescue && player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL)) { - PlayerCache newPlayerCache = deauthenticatedUsers.get(uuid); BlockPos pos = player.getBlockPos(); // Faking portal blocks to be air BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket(); - ((BlockUpdateS2CPacketAccess) feetPacket).setState(new BlockState(Blocks.AIR, null)); - ((BlockUpdateS2CPacketAccess) feetPacket).setBlockPos(pos); + ((BlockUpdateS2CPacketAccessor) feetPacket).setState(new BlockState(Blocks.AIR, null)); + ((BlockUpdateS2CPacketAccessor) feetPacket).setBlockPos(pos); player.networkHandler.sendPacket(feetPacket); BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket(); - ((BlockUpdateS2CPacketAccess) headPacket).setState(new BlockState(Blocks.AIR, null)); - ((BlockUpdateS2CPacketAccess) headPacket).setBlockPos(pos.up()); + ((BlockUpdateS2CPacketAccessor) headPacket).setState(new BlockState(Blocks.AIR, null)); + ((BlockUpdateS2CPacketAccessor) headPacket).setBlockPos(pos.up()); player.networkHandler.sendPacket(headPacket); // Teleporting player to the middle of the block player.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5); - newPlayerCache.wasInPortal = true; + playerCache.wasInPortal = true; } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java b/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccessor.java similarity index 90% rename from src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java rename to src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccessor.java index b3778ce..3e63319 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccess.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/BlockUpdateS2CPacketAccessor.java @@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; @Mixin(BlockUpdateS2CPacket.class) -public interface BlockUpdateS2CPacketAccess { +public interface BlockUpdateS2CPacketAccessor { @Accessor("state") void setState(BlockState state); diff --git a/src/main/resources/mixins.simpleauth.json b/src/main/resources/mixins.simpleauth.json index 5b109a5..ddab9d2 100644 --- a/src/main/resources/mixins.simpleauth.json +++ b/src/main/resources/mixins.simpleauth.json @@ -4,7 +4,7 @@ "compatibilityLevel": "JAVA_8", "mixins": [], "server": [ - "BlockUpdateS2CPacketAccess", + "BlockUpdateS2CPacketAccessor", "MixinPlayerEntity", "MixinPlayerManager", "MixinServerPlayNetworkHandler",