diff --git a/Common/src/main/java/tschipp/carryon/CarryOnCommon.java b/Common/src/main/java/tschipp/carryon/CarryOnCommon.java index 37fa933..307d7a3 100644 --- a/Common/src/main/java/tschipp/carryon/CarryOnCommon.java +++ b/Common/src/main/java/tschipp/carryon/CarryOnCommon.java @@ -40,6 +40,7 @@ import tschipp.carryon.common.carry.CarryOnDataManager; import tschipp.carryon.common.carry.PlacementHandler; import tschipp.carryon.common.command.CommandCarryOn; import tschipp.carryon.config.ConfigLoader; +import tschipp.carryon.networking.clientbound.ClientboundStartRidingOtherPlayerPacket; import tschipp.carryon.networking.clientbound.ClientboundStartRidingPacket; import tschipp.carryon.networking.clientbound.ClientboundSyncScriptsPacket; import tschipp.carryon.networking.serverbound.ServerboundCarryKeyPressedPacket; @@ -83,6 +84,14 @@ public class CarryOnCommon ClientboundSyncScriptsPacket::handle, args ); + + Services.PLATFORM.registerClientboundPacket( + ClientboundStartRidingOtherPlayerPacket.TYPE, + ClientboundStartRidingOtherPlayerPacket.class, + ClientboundStartRidingOtherPlayerPacket.CODEC, + ClientboundStartRidingOtherPlayerPacket::handle, + args + ); } public static void registerConfig() @@ -160,6 +169,16 @@ public class CarryOnCommon } } + public static void onRiderDisconnected(Player rider) + { + if(rider.getVehicle() instanceof ServerPlayer vehicle) { + CarryOnData data = CarryOnDataManager.getCarryData(vehicle); + if(data.isCarrying(CarryType.PLAYER)) { + PlacementHandler.placeCarried(vehicle); + } + } + } + public static int potionLevel(CarryOnData carry, Level level) { diff --git a/Common/src/main/java/tschipp/carryon/common/carry/PickupHandler.java b/Common/src/main/java/tschipp/carryon/common/carry/PickupHandler.java index 2fb734e..8650336 100644 --- a/Common/src/main/java/tschipp/carryon/common/carry/PickupHandler.java +++ b/Common/src/main/java/tschipp/carryon/common/carry/PickupHandler.java @@ -22,6 +22,7 @@ package tschipp.carryon.common.carry; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; @@ -47,6 +48,7 @@ import tschipp.carryon.common.pickupcondition.PickupCondition; import tschipp.carryon.common.pickupcondition.PickupConditionHandler; import tschipp.carryon.common.scripting.CarryOnScript; import tschipp.carryon.common.scripting.ScriptManager; +import tschipp.carryon.networking.clientbound.ClientboundStartRidingOtherPlayerPacket; import tschipp.carryon.networking.clientbound.ClientboundStartRidingPacket; import tschipp.carryon.platform.Services; @@ -230,7 +232,7 @@ public class PickupHandler { } otherPlayer.startRiding(player, true); - Services.PLATFORM.sendPacketToAllPlayers(Constants.PACKET_ID_START_RIDING_OTHER, new ClientboundStartRidingOtherPlayerPacket(player.getId(), otherPlayer.getId(), true), player.level()); + Services.PLATFORM.sendPacketToAllPlayers(Constants.PACKET_ID_START_RIDING_OTHER, new ClientboundStartRidingOtherPlayerPacket(player.getId(), otherPlayer.getId(), true), (ServerLevel) player.level()); carry.setCarryingPlayer(); player.swing(InteractionHand.MAIN_HAND, true); player.level().playSound(null, player.getOnPos(), SoundEvents.ARMOR_EQUIP_GENERIC.value(), SoundSource.AMBIENT, 1.0f, 0.5f); diff --git a/Common/src/main/java/tschipp/carryon/common/carry/PlacementHandler.java b/Common/src/main/java/tschipp/carryon/common/carry/PlacementHandler.java index 555fb92..c38b4c8 100644 --- a/Common/src/main/java/tschipp/carryon/common/carry/PlacementHandler.java +++ b/Common/src/main/java/tschipp/carryon/common/carry/PlacementHandler.java @@ -46,6 +46,8 @@ import tschipp.carryon.Constants; import tschipp.carryon.common.carry.CarryOnData.CarryType; import tschipp.carryon.common.config.ListHandler; import tschipp.carryon.common.scripting.CarryOnScript.ScriptEffects; +import tschipp.carryon.networking.clientbound.ClientboundStartRidingOtherPlayerPacket; +import tschipp.carryon.platform.Services; import javax.annotation.Nullable; import java.util.ArrayList; @@ -173,11 +175,10 @@ public class PlacementHandler if (carry.isCarrying(CarryType.PLAYER)) { Entity otherPlayer = player.getFirstPassenger(); player.ejectPassengers(); + Services.PLATFORM.sendPacketToAllPlayers(Constants.PACKET_ID_START_RIDING_OTHER, new ClientboundStartRidingOtherPlayerPacket(player.getId(), otherPlayer.getId(), false), player.serverLevel()); carry.clear(); CarryOnDataManager.setCarryData(player, carry); - if (otherPlayer == null) - return true; - otherPlayer.teleportTo(placementPos.x, placementPos.y, placementPos.z); + otherPlayer.teleportTo(placementPos.x, placementPos.y, placementPos.z); player.swing(InteractionHand.MAIN_HAND, true); return true; } @@ -222,6 +223,8 @@ public class PlacementHandler else entityHeld = player.getFirstPassenger(); + if(entityHeld == null) + return; double sizeHeldEntity = entityHeld.getBbHeight() * entityHeld.getBbWidth(); double distance = entityClicked.blockPosition().distSqr(player.blockPosition()); diff --git a/Common/src/main/java/tschipp/carryon/mixin/EntityMixin.java b/Common/src/main/java/tschipp/carryon/mixin/EntityMixin.java index c434768..8e4e662 100644 --- a/Common/src/main/java/tschipp/carryon/mixin/EntityMixin.java +++ b/Common/src/main/java/tschipp/carryon/mixin/EntityMixin.java @@ -20,6 +20,7 @@ package tschipp.carryon.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import net.minecraft.server.level.ServerPlayer; import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; @@ -49,6 +50,12 @@ public abstract class EntityMixin @Shadow public abstract void onPassengerTurned(Entity $$0); + @ModifyExpressionValue(method = "startRiding(Lnet/minecraft/world/entity/Entity;Z)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/EntityType;canSerialize()Z")) + private boolean onStartRidingCheck(boolean original, Entity entity, boolean force) { + if (force && entity instanceof Player) return true; + return original; + } + @Inject(method = "positionRider(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/entity/Entity$MoveFunction;)V", at = @At("HEAD"), cancellable = true) private void onPositionPassenger(Entity entity, MoveFunction move, CallbackInfo ci) { diff --git a/Common/src/main/java/tschipp/carryon/networking/clientbound/ClientboundStartRidingOtherPlayerPacket.java b/Common/src/main/java/tschipp/carryon/networking/clientbound/ClientboundStartRidingOtherPlayerPacket.java new file mode 100644 index 0000000..3dfa75f --- /dev/null +++ b/Common/src/main/java/tschipp/carryon/networking/clientbound/ClientboundStartRidingOtherPlayerPacket.java @@ -0,0 +1,59 @@ +/* + * GNU Lesser General Public License v3 + * Copyright (C) 2024 Tschipp + * mrtschipp@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package tschipp.carryon.networking.clientbound; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; +import tschipp.carryon.Constants; +import tschipp.carryon.networking.PacketBase; + +public record ClientboundStartRidingOtherPlayerPacket(int mount, int rider, boolean ride) implements PacketBase +{ + public static final StreamCodec CODEC = StreamCodec.composite( + ByteBufCodecs.INT, ClientboundStartRidingOtherPlayerPacket::mount, + ByteBufCodecs.INT, ClientboundStartRidingOtherPlayerPacket::rider, + ByteBufCodecs.BOOL, ClientboundStartRidingOtherPlayerPacket::ride, + ClientboundStartRidingOtherPlayerPacket::new + ); + + public static final Type TYPE = new Type<>(Constants.PACKET_ID_START_RIDING_OTHER); + + @Override + public void handle(Player player) + { + Entity mount = player.level().getEntity(this.mount); + Entity rider = player.level().getEntity(this.rider); + + if(mount != null && rider != null) + if(ride) + rider.startRiding(mount, true); + else + rider.stopRiding(); + } + + @Override + public Type type() { + return TYPE; + } +} diff --git a/Fabric/src/main/java/tschipp/carryon/events/CommonEvents.java b/Fabric/src/main/java/tschipp/carryon/events/CommonEvents.java index d1fc739..fc7f9ac 100644 --- a/Fabric/src/main/java/tschipp/carryon/events/CommonEvents.java +++ b/Fabric/src/main/java/tschipp/carryon/events/CommonEvents.java @@ -26,6 +26,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.CommonLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.player.*; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -155,6 +156,10 @@ public class CommonEvents { return InteractionResult.PASS; })); + ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { + CarryOnCommon.onRiderDisconnected(handler.getPlayer()); + }); + //TODO: drop carried when attacked } diff --git a/Forge/src/main/java/tschipp/carryon/events/CommonEvents.java b/Forge/src/main/java/tschipp/carryon/events/CommonEvents.java index 5ca7814..88a4387 100644 --- a/Forge/src/main/java/tschipp/carryon/events/CommonEvents.java +++ b/Forge/src/main/java/tschipp/carryon/events/CommonEvents.java @@ -33,11 +33,6 @@ import net.minecraft.world.level.ServerLevelAccessor; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.BlockSnapshot; import net.minecraftforge.event.*; -import net.minecraftforge.event.AddReloadListenerEvent; -import net.minecraftforge.event.OnDatapackSyncEvent; -import net.minecraftforge.event.RegisterCommandsEvent; -import net.minecraftforge.event.TagsUpdatedEvent; -import net.minecraftforge.event.TickEvent.Phase; import net.minecraftforge.event.TickEvent.ServerTickEvent; import net.minecraftforge.event.entity.EntityJoinLevelEvent; import net.minecraftforge.event.entity.living.LivingAttackEvent; @@ -55,7 +50,6 @@ import net.minecraftforge.eventbus.api.Event.Result; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; -import oshi.jna.platform.mac.SystemB; import tschipp.carryon.CarryOnCommon; import tschipp.carryon.Constants; import tschipp.carryon.carry.CarryOnDataCapabilityProvider; diff --git a/NeoForge/src/main/java/tschipp/carryon/events/CommonEvents.java b/NeoForge/src/main/java/tschipp/carryon/events/CommonEvents.java index 958b0ab..ab52a2c 100644 --- a/NeoForge/src/main/java/tschipp/carryon/events/CommonEvents.java +++ b/NeoForge/src/main/java/tschipp/carryon/events/CommonEvents.java @@ -212,4 +212,10 @@ public class CommonEvents CarryOnCommon.onPlayerAttacked(player); } + @SubscribeEvent + public static void onPlayerLoggedOut(PlayerEvent.PlayerLoggedOutEvent event) { + if(event.getEntity() instanceof ServerPlayer player) + CarryOnCommon.onRiderDisconnected(player); + } + }