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);
}
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);
}
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);
}
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();
}
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);
}
}
Aggregations