Merge branch 'portalRescue-overhaul'

This commit is contained in:
samo_lego 2020-05-18 13:52:03 +02:00
commit 3e0f9777d6
5 changed files with 56 additions and 68 deletions

View File

@ -11,6 +11,8 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.dimension.DimensionType;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -156,6 +158,17 @@ public class SimpleAuth implements DedicatedServerModInitializer {
if(config.main.spawnOnJoin) if(config.main.spawnOnJoin)
teleportPlayer(player, false); teleportPlayer(player, false);
// 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)); 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.BlockUpdateS2CPacketAccessor;
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,80 +91,38 @@ 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);
playerCache = deauthenticatedUsers.get(uuid);
}
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; BlockPos pos = player.getBlockPos();
BlockState portalState = player.getBlockState(); // Faking portal blocks to be air
World world = player.getEntityWorld(); BlockUpdateS2CPacket feetPacket = new BlockUpdateS2CPacket();
((BlockUpdateS2CPacketAccessor) feetPacket).setState(new BlockState(Blocks.AIR, null));
((BlockUpdateS2CPacketAccessor) feetPacket).setBlockPos(pos);
player.networkHandler.sendPacket(feetPacket);
double x = player.getX(); BlockUpdateS2CPacket headPacket = new BlockUpdateS2CPacket();
double y = player.getY(); ((BlockUpdateS2CPacketAccessor) headPacket).setState(new BlockState(Blocks.AIR, null));
double z = player.getZ(); ((BlockUpdateS2CPacketAccessor) 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 playerCache.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 BlockUpdateS2CPacketAccessor {
@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": [
"BlockUpdateS2CPacketAccessor",
"MixinPlayerEntity", "MixinPlayerEntity",
"MixinPlayerManager", "MixinPlayerManager",
"MixinServerPlayNetworkHandler", "MixinServerPlayNetworkHandler",