diff --git a/src/main/java/com/yyon/grapplinghook/client/ClientEventHandlers.java b/src/main/java/com/yyon/grapplinghook/client/ClientEventHandlers.java index 6c79567..2cd9440 100644 --- a/src/main/java/com/yyon/grapplinghook/client/ClientEventHandlers.java +++ b/src/main/java/com/yyon/grapplinghook/client/ClientEventHandlers.java @@ -14,9 +14,12 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.GameType; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; +import net.minecraftforge.client.event.ClientPlayerChangeGameTypeEvent; import net.minecraftforge.client.event.ClientPlayerNetworkEvent.LoggingOut; import net.minecraftforge.client.event.InputEvent.Key; import net.minecraftforge.client.event.MovementInputUpdateEvent; @@ -27,6 +30,8 @@ import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; +import java.util.UUID; + public class ClientEventHandlers { public static ClientEventHandlers instance = null; @@ -191,6 +196,27 @@ public void onCameraSetup(ComputeCameraAngles event) { } } + @SubscribeEvent + public void onGameModeChange(ClientPlayerChangeGameTypeEvent event) { + Level level = Minecraft.getInstance().level; + + if(level == null) return; + + if(event.getNewGameType() == GameType.SPECTATOR) { + UUID profile = event.getInfo().getProfile().getId(); + Player p = level.getPlayerByUUID(profile); + + if (p == null) return; + + int id = p.getId(); + + GrappleController controller = ClientControllerManager.controllers.get(id); + + if (controller != null) + controller.unattach(false); + } + } + public ItemStack getKeypressStack(Player player) { if (player != null) { ItemStack stack = player.getItemInHand(InteractionHand.MAIN_HAND); diff --git a/src/main/java/com/yyon/grapplinghook/controllers/GrappleController.java b/src/main/java/com/yyon/grapplinghook/controllers/GrappleController.java index 03d9285..284b2aa 100644 --- a/src/main/java/com/yyon/grapplinghook/controllers/GrappleController.java +++ b/src/main/java/com/yyon/grapplinghook/controllers/GrappleController.java @@ -11,11 +11,14 @@ import com.yyon.grapplinghook.utils.GrapplemodUtils; import com.yyon.grapplinghook.utils.Vec; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.PlayerInfo; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.BlockPos; import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.GameType; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.SoundType; @@ -93,16 +96,27 @@ public GrappleController(int grapplehookEntityId, int entityId, Level world, Vec ClientProxyInterface.proxy.updateRocketRegen(custom.rocket_active_time, custom.rocket_refuel_ratio); } } - + + public void unattach() { - if (ClientProxyInterface.proxy.unregisterController(this.entityId) != null) { - this.attached = false; - - if (this.controllerId != GrapplemodUtils.AIRID) { - CommonSetup.network.sendToServer(new GrappleEndMessage(this.entityId, this.grapplehookEntityIds)); - ClientProxyInterface.proxy.createControl(GrapplemodUtils.AIRID, -1, this.entityId, this.entity.level(), new Vec(0,0,0), null, this.custom); - } - } + // old behaviour was always true to trigger air friction - retain this unless + // there's a good reason to change it. + this.unattach(true); + } + + public void unattach(boolean allowFollowupControllers) { + if (ClientProxyInterface.proxy.unregisterController(this.entityId) == null) + return; + + this.attached = false; + + if (this.controllerId == GrapplemodUtils.AIRID) + return; + + CommonSetup.network.sendToServer(new GrappleEndMessage(this.entityId, this.grapplehookEntityIds)); + + if(allowFollowupControllers) + ClientProxyInterface.proxy.createControl(GrapplemodUtils.AIRID, -1, this.entityId, this.entity.level(), new Vec(0,0,0), null, this.custom); }