Search in sources :

Example 1 with OrientedContraptionEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity in project Create by Creators-of-Create.

the class CartAssemblerTileEntity method disassemble.

protected void disassemble(Level world, BlockPos pos, AbstractMinecart cart) {
    if (cart.getPassengers().isEmpty())
        return;
    Entity entity = cart.getPassengers().get(0);
    if (!(entity instanceof OrientedContraptionEntity))
        return;
    OrientedContraptionEntity contraption = (OrientedContraptionEntity) entity;
    UUID couplingId = contraption.getCouplingId();
    if (couplingId == null) {
        contraption.yaw = CartAssemblerBlock.getHorizontalDirection(getBlockState()).toYRot();
        disassembleCart(cart);
        return;
    }
    Couple<MinecartController> coupledCarts = contraption.getCoupledCartsIfPresent();
    if (coupledCarts == null)
        return;
    // Make sure connected cart is present and being disassembled
    for (boolean current : Iterate.trueAndFalse) {
        MinecartController minecartController = coupledCarts.get(current);
        if (minecartController.cart() == cart)
            continue;
        BlockPos otherPos = minecartController.cart().blockPosition();
        BlockState blockState = world.getBlockState(otherPos);
        if (!AllBlocks.CART_ASSEMBLER.has(blockState))
            return;
        if (!CartAssemblerBlock.getActionForCart(blockState, minecartController.cart()).shouldDisassemble())
            return;
    }
    for (boolean current : Iterate.trueAndFalse) coupledCarts.get(current).removeConnection(current);
    disassembleCart(cart);
}
Also used : SmartTileEntity(com.simibubi.create.foundation.tileEntity.SmartTileEntity) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) Entity(net.minecraft.world.entity.Entity) BlockState(net.minecraft.world.level.block.state.BlockState) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) BlockPos(net.minecraft.core.BlockPos) UUID(java.util.UUID) MinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController) CapabilityMinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController)

Example 2 with OrientedContraptionEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity in project Create by Creators-of-Create.

the class MinecartContraptionItem method wrenchCanBeUsedToPickUpMinecartContraptions.

@SubscribeEvent
public static void wrenchCanBeUsedToPickUpMinecartContraptions(PlayerInteractEvent.EntityInteract event) {
    Entity entity = event.getTarget();
    Player player = event.getPlayer();
    if (player == null || entity == null)
        return;
    ItemStack wrench = player.getItemInHand(event.getHand());
    if (!AllItems.WRENCH.isIn(wrench))
        return;
    if (entity instanceof AbstractContraptionEntity)
        entity = entity.getVehicle();
    if (!(entity instanceof AbstractMinecart))
        return;
    if (!entity.isAlive())
        return;
    AbstractMinecart cart = (AbstractMinecart) entity;
    Type type = cart.getMinecartType();
    if (type != Type.RIDEABLE && type != Type.FURNACE && type != Type.CHEST)
        return;
    List<Entity> passengers = cart.getPassengers();
    if (passengers.isEmpty() || !(passengers.get(0) instanceof OrientedContraptionEntity))
        return;
    OrientedContraptionEntity contraption = (OrientedContraptionEntity) passengers.get(0);
    if (ContraptionMovementSetting.isNoPickup(contraption.getContraption().getBlocks().values())) {
        player.displayClientMessage(Lang.translate("contraption.minecart_contraption_illegal_pickup").withStyle(ChatFormatting.RED), true);
        return;
    }
    if (event.getWorld().isClientSide) {
        event.setCancellationResult(InteractionResult.SUCCESS);
        event.setCanceled(true);
        return;
    }
    ItemStack generatedStack = create(type, contraption).setHoverName(entity.getCustomName());
    try {
        ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
        NbtIo.write(generatedStack.serializeNBT(), dataOutput);
        int estimatedPacketSize = dataOutput.toByteArray().length;
        if (estimatedPacketSize > 2_000_000) {
            player.displayClientMessage(Lang.translate("contraption.minecart_contraption_too_big").withStyle(ChatFormatting.RED), true);
            return;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    player.getInventory().placeItemBackInInventory(generatedStack);
    contraption.discard();
    entity.discard();
    event.setCancellationResult(InteractionResult.SUCCESS);
    event.setCanceled(true);
}
Also used : AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) Entity(net.minecraft.world.entity.Entity) Player(net.minecraft.world.entity.player.Player) Type(net.minecraft.world.entity.vehicle.AbstractMinecart.Type) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) IOException(java.io.IOException) ItemStack(net.minecraft.world.item.ItemStack) AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 3 with OrientedContraptionEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity in project LittleContraptions by EDToaster.

the class BargeAssemblerBlockEntity method assemble.

protected void assemble(Level world, BlockPos pos, ContraptionBargeEntity barge) {
    if (!barge.getPassengers().isEmpty())
        return;
    CartAssemblerTileEntity.CartMovementMode mode = CartAssemblerTileEntity.CartMovementMode.ROTATE;
    MountedContraption contraption = new MountedContraption(mode);
    try {
        if (!contraption.assemble(world, pos)) {
            return;
        }
        lastException = null;
    } catch (AssemblyException e) {
        lastException = e;
        e.printStackTrace();
        return;
    }
    Direction initialOrientation = BargeAssemblerBlock.getHorizontalDirection(getBlockState());
    contraption.removeBlocksFromWorld(world, BlockPos.ZERO);
    contraption.startMoving(world);
    contraption.expandBoundsAroundAxis(Direction.Axis.Y);
    OrientedContraptionEntity entity = OrientedContraptionEntity.create(world, contraption, initialOrientation);
    entity.setPos(pos.getX(), pos.getY(), pos.getZ());
    world.addFreshEntity(entity);
    entity.startRiding(barge);
}
Also used : AssemblyException(com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException) MountedContraption(com.simibubi.create.content.contraptions.components.structureMovement.mounted.MountedContraption) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) CartAssemblerTileEntity(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity) Direction(net.minecraft.core.Direction)

Example 4 with OrientedContraptionEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity in project LittleContraptions by EDToaster.

the class BargeAssemblerBlockEntity method disassemble.

protected void disassemble(Level world, BlockPos pos, ContraptionBargeEntity barge) {
    if (barge.getPassengers().isEmpty())
        return;
    Entity entity = barge.getPassengers().get(0);
    if (!(entity instanceof OrientedContraptionEntity))
        return;
    OrientedContraptionEntity contraption = (OrientedContraptionEntity) entity;
    contraption.yaw = BargeAssemblerBlock.getHorizontalDirection(getBlockState()).toYRot();
    barge.ejectPassengers();
}
Also used : ContraptionBargeEntity(dev.murad.littlecontraptions.entity.ContraptionBargeEntity) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Entity(net.minecraft.world.entity.Entity) CartAssemblerTileEntity(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity)

Example 5 with OrientedContraptionEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity in project Create by Creators-of-Create.

the class CartAssemblerTileEntity method assemble.

protected void assemble(Level world, BlockPos pos, AbstractMinecart cart) {
    if (!cart.getPassengers().isEmpty())
        return;
    LazyOptional<MinecartController> optional = cart.getCapability(CapabilityMinecartController.MINECART_CONTROLLER_CAPABILITY);
    if (optional.isPresent() && optional.orElse(null).isCoupledThroughContraption())
        return;
    CartMovementMode mode = CartMovementMode.values()[movementMode.value];
    MountedContraption contraption = new MountedContraption(mode);
    try {
        if (!contraption.assemble(world, pos))
            return;
        lastException = null;
        sendData();
    } catch (AssemblyException e) {
        lastException = e;
        sendData();
        return;
    }
    boolean couplingFound = contraption.connectedCart != null;
    Direction initialOrientation = CartAssemblerBlock.getHorizontalDirection(getBlockState());
    if (couplingFound) {
        cart.setPos(pos.getX() + .5f, pos.getY(), pos.getZ() + .5f);
        if (!CouplingHandler.tryToCoupleCarts(null, world, cart.getId(), contraption.connectedCart.getId()))
            return;
    }
    contraption.removeBlocksFromWorld(world, BlockPos.ZERO);
    contraption.startMoving(world);
    contraption.expandBoundsAroundAxis(Axis.Y);
    if (couplingFound) {
        Vec3 diff = contraption.connectedCart.position().subtract(cart.position());
        initialOrientation = Direction.fromYRot(Mth.atan2(diff.z, diff.x) * 180 / Math.PI);
    }
    OrientedContraptionEntity entity = OrientedContraptionEntity.create(world, contraption, initialOrientation);
    if (couplingFound)
        entity.setCouplingId(cart.getUUID());
    entity.setPos(pos.getX(), pos.getY(), pos.getZ());
    world.addFreshEntity(entity);
    entity.startRiding(cart);
    if (cart instanceof MinecartFurnace) {
        CompoundTag nbt = cart.serializeNBT();
        nbt.putDouble("PushZ", 0);
        nbt.putDouble("PushX", 0);
        cart.deserializeNBT(nbt);
    }
}
Also used : MinecartFurnace(net.minecraft.world.entity.vehicle.MinecartFurnace) AssemblyException(com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) Vec3(net.minecraft.world.phys.Vec3) MinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController) CapabilityMinecartController(com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController) Direction(net.minecraft.core.Direction) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

OrientedContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity)7 Direction (net.minecraft.core.Direction)3 Entity (net.minecraft.world.entity.Entity)3 AbstractContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity)2 AssemblyException (com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException)2 CartAssemblerTileEntity (com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity)2 CapabilityMinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController)2 MinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 Contraption (com.simibubi.create.content.contraptions.components.structureMovement.Contraption)1 ControlledContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity)1 MountedContraption (com.simibubi.create.content.contraptions.components.structureMovement.mounted.MountedContraption)1 SmartTileEntity (com.simibubi.create.foundation.tileEntity.SmartTileEntity)1 ContraptionBargeEntity (dev.murad.littlecontraptions.entity.ContraptionBargeEntity)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 BlockPos (net.minecraft.core.BlockPos)1 Axis (net.minecraft.core.Direction.Axis)1 Player (net.minecraft.world.entity.player.Player)1