|
| 1 | +package com.oldschooljail.mixin; |
| 2 | + |
| 3 | +import com.oldschooljail.OldSchoolJailMod; |
| 4 | +import com.oldschooljail.data.JailData; |
| 5 | +import com.oldschooljail.data.JailedPlayersData; |
| 6 | +import com.oldschooljail.model.Jail; |
| 7 | +import com.oldschooljail.model.JailedPlayer; |
| 8 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 9 | +import net.minecraft.util.math.Vec3d; |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 13 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +@Mixin(ServerPlayerEntity.class) |
| 20 | +public class ServerPlayerEntityAfkMixin { |
| 21 | + |
| 22 | + // Store original AFK positions for jailed players |
| 23 | + private static final Map<UUID, Vec3d> originalAfkPositions = new HashMap<>(); |
| 24 | + // Track previous jail state to detect transitions |
| 25 | + private static final Map<UUID, Boolean> previousJailState = new HashMap<>(); |
| 26 | + |
| 27 | + @Inject(method = "tick", at = @At("HEAD")) |
| 28 | + private void onTick(CallbackInfo ci) { |
| 29 | + ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; |
| 30 | + UUID playerUuid = player.getUuid(); |
| 31 | + |
| 32 | + JailedPlayersData jailedData = OldSchoolJailMod.getJailedPlayersData(); |
| 33 | + boolean currentlyJailed = jailedData != null && jailedData.isJailed(playerUuid); |
| 34 | + boolean previouslyJailed = previousJailState.getOrDefault(playerUuid, false); |
| 35 | + |
| 36 | + // Update previous state |
| 37 | + previousJailState.put(playerUuid, currentlyJailed); |
| 38 | + |
| 39 | + // Only restore AFK position when transitioning from jailed to not-jailed |
| 40 | + if (!currentlyJailed && previouslyJailed && originalAfkPositions.containsKey(playerUuid)) { |
| 41 | + try { |
| 42 | + Object playerData = player.getClass().getMethod("ec$getPlayerData").invoke(player); |
| 43 | + if (playerData != null) { |
| 44 | + java.lang.reflect.Field lastTickPosField = playerData.getClass().getDeclaredField("lastTickPos"); |
| 45 | + lastTickPosField.setAccessible(true); |
| 46 | + lastTickPosField.set(playerData, originalAfkPositions.get(playerUuid)); |
| 47 | + |
| 48 | + // Remove from our storage since player is no longer jailed |
| 49 | + originalAfkPositions.remove(playerUuid); |
| 50 | + |
| 51 | + OldSchoolJailMod.LOGGER.debug("Restored original AFK position for released player {}", player.getName().getString()); |
| 52 | + } |
| 53 | + } catch (Exception e) { |
| 54 | + // Essentials not present or different version, ignore |
| 55 | + originalAfkPositions.remove(playerUuid); // Clean up anyway |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // If not jailed, don't do anything else |
| 61 | + if (!currentlyJailed) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + JailedPlayer jailedPlayer = jailedData.getJailedPlayer(player.getUuid()); |
| 66 | + JailData jailData = OldSchoolJailMod.getJailData(); |
| 67 | + Jail jail = jailData.getJail(jailedPlayer.getJailName()); |
| 68 | + |
| 69 | + if (jail == null) { |
| 70 | + return; // Jail doesn't exist |
| 71 | + } |
| 72 | + |
| 73 | + // Override Essentials Commands AFK position |
| 74 | + try { |
| 75 | + Object playerData = player.getClass().getMethod("ec$getPlayerData").invoke(player); |
| 76 | + if (playerData != null) { |
| 77 | + java.lang.reflect.Field lastTickPosField = playerData.getClass().getDeclaredField("lastTickPos"); |
| 78 | + lastTickPosField.setAccessible(true); |
| 79 | + |
| 80 | + // Store original AFK position if we haven't already |
| 81 | + if (!originalAfkPositions.containsKey(playerUuid)) { |
| 82 | + Vec3d currentAfkPos = (Vec3d) lastTickPosField.get(playerData); |
| 83 | + originalAfkPositions.put(playerUuid, currentAfkPos); |
| 84 | + OldSchoolJailMod.LOGGER.debug("Stored original AFK position for jailed player {}: {}", player.getName().getString(), currentAfkPos); |
| 85 | + } |
| 86 | + |
| 87 | + // Override with jail position |
| 88 | + lastTickPosField.set(playerData, new Vec3d(jail.getX(), jail.getY(), jail.getZ())); |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + // Essentials not present or different version, ignore |
| 92 | + OldSchoolJailMod.LOGGER.debug("Could not override Essentials AFK position: {}", e.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments