Search in sources :

Example 1 with SPacketMoveVehicle

use of net.minecraft.network.play.server.SPacketMoveVehicle in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method processVehicleMoveEvent.

/**
 * @author gabizou - June 22nd, 2016
 * @author blood - May 6th, 2017
 * @reason Redirects the {@link Entity#getLowestRidingEntity()} call to throw our
 * {@link MoveEntityEvent}. The peculiarity of this redirect is that the entity
 * returned is perfectly valid to be {@link this#player} since, if the player
 * is NOT riding anything, the lowest riding entity is themselves. This way, if
 * the event is cancelled, the player can be returned instead of the actual riding
 * entity.
 *
 * @param playerMP The player
 * @param packetIn The packet movement
 * @return The lowest riding entity
 */
@Redirect(method = "processVehicleMove", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayerMP;getLowestRidingEntity()Lnet/minecraft/entity/Entity;"))
private Entity processVehicleMoveEvent(EntityPlayerMP playerMP, CPacketVehicleMove packetIn) {
    final Entity ridingEntity = this.player.getLowestRidingEntity();
    if (ridingEntity == this.player || ridingEntity.getControllingPassenger() != this.player || ridingEntity != this.lowestRiddenEnt) {
        return ridingEntity;
    }
    // Sponge Start - Movement event
    org.spongepowered.api.entity.Entity spongeEntity = (org.spongepowered.api.entity.Entity) ridingEntity;
    Vector3d fromrot = spongeEntity.getRotation();
    Location<World> from = spongeEntity.getLocation();
    Vector3d torot = new Vector3d(packetIn.getPitch(), packetIn.getYaw(), 0);
    Location<World> to = new Location<>(spongeEntity.getWorld(), packetIn.getX(), packetIn.getY(), packetIn.getZ());
    Transform<World> fromTransform = spongeEntity.getTransform().setLocation(from).setRotation(fromrot);
    Transform<World> toTransform = spongeEntity.getTransform().setLocation(to).setRotation(torot);
    MoveEntityEvent event = SpongeEventFactory.createMoveEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), fromTransform, toTransform, this.getPlayer());
    SpongeImpl.postEvent(event);
    if (event.isCancelled()) {
        // There is no need to change the current riding entity position as it hasn't changed yet.
        // Send packet to client in order to update rider position.
        this.netManager.sendPacket(new SPacketMoveVehicle(ridingEntity));
        return this.player;
    }
    return ridingEntity;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CPacketUseEntity(net.minecraft.network.play.client.CPacketUseEntity) Entity(net.minecraft.entity.Entity) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) SPacketMoveVehicle(net.minecraft.network.play.server.SPacketMoveVehicle) World(org.spongepowered.api.world.World) Vector3d(com.flowpowered.math.vector.Vector3d) Location(org.spongepowered.api.world.Location) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Vector3d (com.flowpowered.math.vector.Vector3d)1 Entity (net.minecraft.entity.Entity)1 CPacketUseEntity (net.minecraft.network.play.client.CPacketUseEntity)1 SPacketMoveVehicle (net.minecraft.network.play.server.SPacketMoveVehicle)1 TileEntity (net.minecraft.tileentity.TileEntity)1 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)1 Location (org.spongepowered.api.world.Location)1 World (org.spongepowered.api.world.World)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1