PLayer portal rescue with fake blocks replacing portal

This commit is contained in:
samo_lego 2020-05-11 21:25:20 +02:00
parent 5d68fe9f57
commit c335e2233f
5 changed files with 44 additions and 67 deletions

View File

@ -157,6 +157,9 @@ public class SimpleAuth implements DedicatedServerModInitializer {
if(config.main.spawnOnJoin) if(config.main.spawnOnJoin)
teleportPlayer(player, false); teleportPlayer(player, false);
// Updating chunk if needed (if portal rescue action happened)
if(deauthenticatedUsers.get(convertUuid(player)).wasInPortal)
deauthenticatedUsers.remove(convertUuid(player)); deauthenticatedUsers.remove(convertUuid(player));
// Player no longer needs to be invisible and invulnerable // Player no longer needs to be invisible and invulnerable

View File

@ -6,6 +6,7 @@ import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket; 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.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
@ -13,15 +14,13 @@ import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult; import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos; 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 org.samo_lego.simpleauth.storage.PlayerCache;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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.SimpleAuth.*;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
@ -92,6 +91,10 @@ public class AuthEventHandler {
} }
// Invalidating session // Invalidating session
playerCache.wasAuthenticated = false; playerCache.wasAuthenticated = false;
if(playerCache.isRegistered)
player.sendMessage(new LiteralText(config.lang.loginRequired), false);
else
player.sendMessage(new LiteralText(config.lang.notRegistered), false);
} }
else else
deauthenticatePlayer(player); deauthenticatePlayer(player);
@ -99,73 +102,26 @@ public class AuthEventHandler {
if(config.main.spawnOnJoin) if(config.main.spawnOnJoin)
teleportPlayer(player, true); teleportPlayer(player, true);
// Tries to rescue player from nether portal // Tries to rescue player from nether portal
if(config.main.tryPortalRescue && player.getBlockState().getBlock().equals(Blocks.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(); // Faking portal blocks to be air
World world = player.getEntityWorld(); BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket();
((BlockUpdateS2CPacketAccess) feetPacket).setState(new BlockState(Blocks.AIR, null));
((BlockUpdateS2CPacketAccess) feetPacket).setBlockPos(pos);
player.networkHandler.sendPacket(feetPacket);
double x = player.getX(); BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket();
double y = player.getY(); ((BlockUpdateS2CPacketAccess) headPacket).setState(new BlockState(Blocks.AIR, null));
double z = player.getZ(); ((BlockUpdateS2CPacketAccess) headPacket).setBlockPos(pos.up());
player.networkHandler.sendPacket(headPacket);
if(portalState.get(AXIS) == Z) { // Teleporting player to the middle of the block
// Player should be put to eastern or western block player.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
if( // Checking towards east newPlayerCache.wasInPortal = true;
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);
}
} }
} }

View File

@ -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);
}

View File

@ -23,6 +23,7 @@ public class PlayerCache {
public double lastZ; public double lastZ;
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
public boolean wasInPortal;
public PlayerCache(String uuid, ServerPlayerEntity player) { public PlayerCache(String uuid, ServerPlayerEntity player) {
@ -90,5 +91,6 @@ public class PlayerCache {
} }
this.wasAuthenticated = false; this.wasAuthenticated = false;
this.loginTries = 0; this.loginTries = 0;
this.wasInPortal = false;
} }
} }

View File

@ -2,9 +2,9 @@
"required": true, "required": true,
"package": "org.samo_lego.simpleauth.mixin", "package": "org.samo_lego.simpleauth.mixin",
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"mixins": [ "mixins": [],
],
"server": [ "server": [
"BlockUpdateS2CPacketAccess",
"MixinPlayerEntity", "MixinPlayerEntity",
"MixinPlayerManager", "MixinPlayerManager",
"MixinServerPlayNetworkHandler", "MixinServerPlayNetworkHandler",