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)
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

View File

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

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

View File

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