use of com.simibubi.create.content.contraptions.relays.belt.transport.BeltInventory in project Create by Creators-of-Create.
the class BeltSlicer method useWrench.
public static InteractionResult useWrench(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit, Feedback feedBack) {
BeltTileEntity controllerTE = BeltHelper.getControllerTE(world, pos);
if (controllerTE == null)
return InteractionResult.PASS;
if (state.getValue(BeltBlock.CASING) && hit.getDirection() != Direction.UP)
return InteractionResult.PASS;
if (state.getValue(BeltBlock.PART) == BeltPart.PULLEY && hit.getDirection().getAxis() != Axis.Y)
return InteractionResult.PASS;
int beltLength = controllerTE.beltLength;
if (beltLength == 2)
return InteractionResult.FAIL;
BlockPos beltVector = new BlockPos(BeltHelper.getBeltVector(state));
BeltPart part = state.getValue(BeltBlock.PART);
List<BlockPos> beltChain = BeltBlock.getBeltChain(world, controllerTE.getBlockPos());
boolean creative = player.isCreative();
// Shorten from End
if (hoveringEnd(state, hit)) {
if (world.isClientSide)
return InteractionResult.SUCCESS;
for (BlockPos blockPos : beltChain) {
BeltTileEntity belt = BeltHelper.getSegmentTE(world, blockPos);
if (belt == null)
continue;
belt.detachKinetics();
belt.invalidateItemHandler();
belt.beltLength = 0;
}
BeltInventory inventory = controllerTE.inventory;
BlockPos next = part == BeltPart.END ? pos.subtract(beltVector) : pos.offset(beltVector);
BlockState replacedState = world.getBlockState(next);
BeltTileEntity segmentTE = BeltHelper.getSegmentTE(world, next);
KineticTileEntity.switchToBlockState(world, next, state.setValue(BeltBlock.CASING, segmentTE != null && segmentTE.casing != CasingType.NONE));
world.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL | Block.UPDATE_MOVE_BY_PISTON);
world.removeBlockEntity(pos);
world.levelEvent(2001, pos, Block.getId(state));
if (!creative && AllBlocks.BELT.has(replacedState) && replacedState.getValue(BeltBlock.PART) == BeltPart.PULLEY)
player.getInventory().placeItemBackInInventory(AllBlocks.SHAFT.asStack());
// Eject overshooting items
if (part == BeltPart.END && inventory != null) {
List<TransportedItemStack> toEject = new ArrayList<>();
for (TransportedItemStack transportedItemStack : inventory.getTransportedItems()) if (transportedItemStack.beltPosition > beltLength - 1)
toEject.add(transportedItemStack);
toEject.forEach(inventory::eject);
toEject.forEach(inventory.getTransportedItems()::remove);
}
// Transfer items to new controller
if (part == BeltPart.START && segmentTE != null && inventory != null) {
controllerTE.inventory = null;
segmentTE.inventory = null;
segmentTE.setController(next);
for (TransportedItemStack transportedItemStack : inventory.getTransportedItems()) {
transportedItemStack.beltPosition -= 1;
if (transportedItemStack.beltPosition <= 0) {
ItemEntity entity = new ItemEntity(world, pos.getX() + .5f, pos.getY() + 11 / 16f, pos.getZ() + .5f, transportedItemStack.stack);
entity.setDeltaMovement(Vec3.ZERO);
entity.setDefaultPickUpDelay();
entity.hurtMarked = true;
world.addFreshEntity(entity);
} else
segmentTE.getInventory().addItem(transportedItemStack);
}
}
return InteractionResult.SUCCESS;
}
BeltTileEntity segmentTE = BeltHelper.getSegmentTE(world, pos);
if (segmentTE == null)
return InteractionResult.PASS;
// Split in half
int hitSegment = segmentTE.index;
Vec3 centerOf = VecHelper.getCenterOf(hit.getBlockPos());
Vec3 subtract = hit.getLocation().subtract(centerOf);
boolean towardPositive = subtract.dot(Vec3.atLowerCornerOf(beltVector)) > 0;
BlockPos next = !towardPositive ? pos.subtract(beltVector) : pos.offset(beltVector);
if (hitSegment == 0 || hitSegment == 1 && !towardPositive)
return InteractionResult.FAIL;
if (hitSegment == controllerTE.beltLength - 1 || hitSegment == controllerTE.beltLength - 2 && towardPositive)
return InteractionResult.FAIL;
// Look for shafts
if (!creative) {
int requiredShafts = 0;
if (!segmentTE.hasPulley())
requiredShafts++;
BlockState other = world.getBlockState(next);
if (AllBlocks.BELT.has(other) && other.getValue(BeltBlock.PART) == BeltPart.MIDDLE)
requiredShafts++;
int amountRetrieved = 0;
boolean beltFound = false;
Search: while (true) {
for (int i = 0; i < player.getInventory().getContainerSize(); ++i) {
if (amountRetrieved == requiredShafts && beltFound)
break Search;
ItemStack itemstack = player.getInventory().getItem(i);
if (itemstack.isEmpty())
continue;
int count = itemstack.getCount();
if (AllItems.BELT_CONNECTOR.isIn(itemstack)) {
if (!world.isClientSide)
itemstack.shrink(1);
beltFound = true;
continue;
}
if (AllBlocks.SHAFT.isIn(itemstack)) {
int taken = Math.min(count, requiredShafts - amountRetrieved);
if (!world.isClientSide)
if (taken == count)
player.getInventory().setItem(i, ItemStack.EMPTY);
else
itemstack.shrink(taken);
amountRetrieved += taken;
}
}
if (!world.isClientSide)
player.getInventory().placeItemBackInInventory(AllBlocks.SHAFT.asStack(amountRetrieved));
return InteractionResult.FAIL;
}
}
if (!world.isClientSide) {
for (BlockPos blockPos : beltChain) {
BeltTileEntity belt = BeltHelper.getSegmentTE(world, blockPos);
if (belt == null)
continue;
belt.detachKinetics();
belt.invalidateItemHandler();
belt.beltLength = 0;
}
BeltInventory inventory = controllerTE.inventory;
KineticTileEntity.switchToBlockState(world, pos, state.setValue(BeltBlock.PART, towardPositive ? BeltPart.END : BeltPart.START));
KineticTileEntity.switchToBlockState(world, next, world.getBlockState(next).setValue(BeltBlock.PART, towardPositive ? BeltPart.START : BeltPart.END));
world.playSound(null, pos, SoundEvents.WOOL_HIT, player == null ? SoundSource.BLOCKS : SoundSource.PLAYERS, 0.5F, 2.3F);
// Transfer items to new controller
BeltTileEntity newController = towardPositive ? BeltHelper.getSegmentTE(world, next) : segmentTE;
if (newController != null && inventory != null) {
newController.inventory = null;
newController.setController(newController.getBlockPos());
for (Iterator<TransportedItemStack> iterator = inventory.getTransportedItems().iterator(); iterator.hasNext(); ) {
TransportedItemStack transportedItemStack = iterator.next();
float newPosition = transportedItemStack.beltPosition - hitSegment - (towardPositive ? 1 : 0);
if (newPosition <= 0)
continue;
transportedItemStack.beltPosition = newPosition;
iterator.remove();
newController.getInventory().addItem(transportedItemStack);
}
}
}
return InteractionResult.SUCCESS;
}
use of com.simibubi.create.content.contraptions.relays.belt.transport.BeltInventory in project Create by Creators-of-Create.
the class BeltSlicer method useConnector.
public static InteractionResult useConnector(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit, Feedback feedBack) {
BeltTileEntity controllerTE = BeltHelper.getControllerTE(world, pos);
if (controllerTE == null)
return InteractionResult.PASS;
int beltLength = controllerTE.beltLength;
if (beltLength == BeltConnectorItem.maxLength())
return InteractionResult.FAIL;
BlockPos beltVector = new BlockPos(BeltHelper.getBeltVector(state));
BeltPart part = state.getValue(BeltBlock.PART);
Direction facing = state.getValue(BeltBlock.HORIZONTAL_FACING);
List<BlockPos> beltChain = BeltBlock.getBeltChain(world, controllerTE.getBlockPos());
boolean creative = player.isCreative();
if (!hoveringEnd(state, hit))
return InteractionResult.PASS;
BlockPos next = part == BeltPart.START ? pos.subtract(beltVector) : pos.offset(beltVector);
BeltTileEntity mergedController = null;
int mergedBeltLength = 0;
// Merge Belts / Extend at End
BlockState nextState = world.getBlockState(next);
if (!nextState.getMaterial().isReplaceable()) {
if (!AllBlocks.BELT.has(nextState))
return InteractionResult.FAIL;
if (!beltStatesCompatible(state, nextState))
return InteractionResult.FAIL;
mergedController = BeltHelper.getControllerTE(world, next);
if (mergedController == null)
return InteractionResult.FAIL;
if (mergedController.beltLength + beltLength > BeltConnectorItem.maxLength())
return InteractionResult.FAIL;
mergedBeltLength = mergedController.beltLength;
if (!world.isClientSide) {
boolean flipBelt = facing != nextState.getValue(BeltBlock.HORIZONTAL_FACING);
Optional<DyeColor> color = controllerTE.color;
for (BlockPos blockPos : BeltBlock.getBeltChain(world, mergedController.getBlockPos())) {
BeltTileEntity belt = BeltHelper.getSegmentTE(world, blockPos);
if (belt == null)
continue;
belt.detachKinetics();
belt.invalidateItemHandler();
belt.beltLength = 0;
belt.color = color;
if (flipBelt)
world.setBlock(blockPos, flipBelt(world.getBlockState(blockPos)), Block.UPDATE_ALL | Block.UPDATE_MOVE_BY_PISTON);
}
// Reverse items
if (flipBelt && mergedController.inventory != null) {
List<TransportedItemStack> transportedItems = mergedController.inventory.getTransportedItems();
for (TransportedItemStack transportedItemStack : transportedItems) {
transportedItemStack.beltPosition = mergedBeltLength - transportedItemStack.beltPosition;
transportedItemStack.prevBeltPosition = mergedBeltLength - transportedItemStack.prevBeltPosition;
}
}
}
}
if (!world.isClientSide) {
for (BlockPos blockPos : beltChain) {
BeltTileEntity belt = BeltHelper.getSegmentTE(world, blockPos);
if (belt == null)
continue;
belt.detachKinetics();
belt.invalidateItemHandler();
belt.beltLength = 0;
}
BeltInventory inventory = controllerTE.inventory;
KineticTileEntity.switchToBlockState(world, pos, state.setValue(BeltBlock.PART, BeltPart.MIDDLE));
if (mergedController == null) {
// Attach at end
world.setBlock(next, state.setValue(BeltBlock.CASING, false), Block.UPDATE_ALL | Block.UPDATE_MOVE_BY_PISTON);
BeltTileEntity segmentTE = BeltHelper.getSegmentTE(world, next);
if (segmentTE != null)
segmentTE.color = controllerTE.color;
world.playSound(null, pos, SoundEvents.WOOL_PLACE, player == null ? SoundSource.BLOCKS : SoundSource.PLAYERS, 0.5F, 1F);
// Transfer items to new controller
if (part == BeltPart.START && segmentTE != null && inventory != null) {
segmentTE.setController(next);
for (TransportedItemStack transportedItemStack : inventory.getTransportedItems()) {
transportedItemStack.beltPosition += 1;
segmentTE.getInventory().addItem(transportedItemStack);
}
}
} else {
// Merge with other
BeltInventory mergedInventory = mergedController.inventory;
world.playSound(null, pos, SoundEvents.WOOL_HIT, player == null ? SoundSource.BLOCKS : SoundSource.PLAYERS, 0.5F, 1.3F);
BeltTileEntity segmentTE = BeltHelper.getSegmentTE(world, next);
KineticTileEntity.switchToBlockState(world, next, state.setValue(BeltBlock.CASING, segmentTE != null && segmentTE.casing != CasingType.NONE).setValue(BeltBlock.PART, BeltPart.MIDDLE));
if (!creative) {
player.getInventory().placeItemBackInInventory(AllBlocks.SHAFT.asStack(2));
player.getInventory().placeItemBackInInventory(AllItems.BELT_CONNECTOR.asStack());
}
// Transfer items to other controller
BlockPos search = controllerTE.getBlockPos();
for (int i = 0; i < 10000; i++) {
BlockState blockState = world.getBlockState(search);
if (!AllBlocks.BELT.has(blockState))
break;
if (blockState.getValue(BeltBlock.PART) != BeltPart.START) {
search = search.subtract(beltVector);
continue;
}
BeltTileEntity newController = BeltHelper.getSegmentTE(world, search);
if (newController != controllerTE && inventory != null) {
newController.setController(search);
controllerTE.inventory = null;
for (TransportedItemStack transportedItemStack : inventory.getTransportedItems()) {
transportedItemStack.beltPosition += mergedBeltLength;
newController.getInventory().addItem(transportedItemStack);
}
}
if (newController != mergedController && mergedInventory != null) {
newController.setController(search);
mergedController.inventory = null;
for (TransportedItemStack transportedItemStack : mergedInventory.getTransportedItems()) {
if (newController == controllerTE)
transportedItemStack.beltPosition += beltLength;
newController.getInventory().addItem(transportedItemStack);
}
}
break;
}
}
}
return InteractionResult.SUCCESS;
}
use of com.simibubi.create.content.contraptions.relays.belt.transport.BeltInventory in project Create by Creators-of-Create.
the class BeltTileEntity method applyToAllItems.
private void applyToAllItems(float maxDistanceFromCenter, Function<TransportedItemStack, TransportedResult> processFunction) {
BeltTileEntity controller = getControllerTE();
if (controller == null)
return;
BeltInventory inventory = controller.getInventory();
if (inventory != null)
inventory.applyToEachWithin(index + .5f, maxDistanceFromCenter, processFunction);
}
use of com.simibubi.create.content.contraptions.relays.belt.transport.BeltInventory in project Create by Creators-of-Create.
the class BeltTileEntity method initializeItemHandler.
protected void initializeItemHandler() {
if (level.isClientSide || itemHandler.isPresent())
return;
if (!level.isLoaded(controller))
return;
BlockEntity te = level.getBlockEntity(controller);
if (te == null || !(te instanceof BeltTileEntity))
return;
BeltInventory inventory = ((BeltTileEntity) te).getInventory();
if (inventory == null)
return;
IItemHandler handler = new ItemHandlerBeltSegment(inventory, index);
itemHandler = LazyOptional.of(() -> handler);
}
use of com.simibubi.create.content.contraptions.relays.belt.transport.BeltInventory in project Create by Creators-of-Create.
the class BeltTileEntity method tryInsertingFromSide.
private ItemStack tryInsertingFromSide(TransportedItemStack transportedStack, Direction side, boolean simulate) {
BeltTileEntity nextBeltController = getControllerTE();
ItemStack inserted = transportedStack.stack;
ItemStack empty = ItemStack.EMPTY;
if (nextBeltController == null)
return inserted;
BeltInventory nextInventory = nextBeltController.getInventory();
if (nextInventory == null)
return inserted;
BlockEntity teAbove = level.getBlockEntity(worldPosition.above());
if (teAbove instanceof BrassTunnelTileEntity) {
BrassTunnelTileEntity tunnelTE = (BrassTunnelTileEntity) teAbove;
if (tunnelTE.hasDistributionBehaviour()) {
if (!tunnelTE.getStackToDistribute().isEmpty())
return inserted;
if (!tunnelTE.testFlapFilter(side.getOpposite(), inserted))
return inserted;
if (!simulate) {
BeltTunnelInteractionHandler.flapTunnel(nextInventory, index, side.getOpposite(), true);
tunnelTE.setStackToDistribute(inserted);
}
return empty;
}
}
if (getSpeed() == 0)
return inserted;
if (getMovementFacing() == side.getOpposite())
return inserted;
if (!nextInventory.canInsertAtFromSide(index, side))
return inserted;
if (simulate)
return empty;
transportedStack = transportedStack.copy();
transportedStack.beltPosition = index + .5f - Math.signum(getDirectionAwareBeltMovementSpeed()) / 16f;
Direction movementFacing = getMovementFacing();
if (!side.getAxis().isVertical()) {
if (movementFacing != side) {
transportedStack.sideOffset = side.getAxisDirection().getStep() * .35f;
if (side.getAxis() == Axis.X)
transportedStack.sideOffset *= -1;
} else
transportedStack.beltPosition = getDirectionAwareBeltMovementSpeed() > 0 ? index : index + 1;
}
transportedStack.prevSideOffset = transportedStack.sideOffset;
transportedStack.insertedAt = index;
transportedStack.insertedFrom = side;
transportedStack.prevBeltPosition = transportedStack.beltPosition;
BeltTunnelInteractionHandler.flapTunnel(nextInventory, index, side.getOpposite(), true);
nextInventory.addItem(transportedStack);
nextBeltController.setChanged();
nextBeltController.sendData();
return empty;
}
Aggregations