Skip to content

Commit

Permalink
feat: allow pick without source
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Nov 20, 2024
1 parent 73b5b8b commit 76b9536
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 0 additions & 2 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ data class C2SConvertItemPacket(val slot: Int, val target: ItemStack, val mode:

val selected = player.inventory.getItem(player.inventory.selected)

C2SConvertTargetPacket.insertResult(itemToInsert, selected, removeMaterials, player)
C2SConvertTargetPacket.insertResult(packet.target, itemToInsert, selected, removeMaterials, player)
}.onFailure {
ItemConverter.LOGGER.error("Error handling C2SConvertItemPacket", it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ object C2SConvertTargetPacket {
ItemConverter.serverCoroutineScope!!.launch(CoroutineExceptionHandler { context, throwable ->
ItemConverter.LOGGER.error("Error while converting", throwable)
}) {

val selected = player.inventory.getItem(player.inventory.selected)
val paths = player.inventory.items.asSequence()
.mapIndexed { slot, it -> slot to it }
.filter { (_, it) -> !ItemStack.isSameItemSameTags(it, target) }
Expand All @@ -67,8 +69,6 @@ object C2SConvertTargetPacket {
.filter { (_, path, ratio) -> path.startVertex.predicate.count >= ratio.denominator }
.sortedBy { (slot, _, _) -> if (slot <= 8) slot + 36 else slot }

val selected = player.inventory.getItem(player.inventory.selected)

val shift = player.isShiftKeyDown

val (itemToInsert, removeMaterials) = if (shift) {
Expand Down Expand Up @@ -106,36 +106,42 @@ object C2SConvertTargetPacket {
Unit
}
} else {
val (slot, path, ratio) = paths.firstOrNull() ?: return@launch
val itemToInsert = to.predicate.copy().also {
it.count = ratio.numerator
}
itemToInsert to {
player.inventory.removeItem(slot, ratio.denominator)
val context = paths.firstOrNull()
if (context == null) {
ItemStack.EMPTY to {}
} else {
val (slot, path, ratio) = context
val itemToInsert = to.predicate.copy().also {
it.count = ratio.numerator
}
itemToInsert to {
player.inventory.removeItem(slot, ratio.denominator)

val lastEdge = path.edgeList.last()
player.playNotifySound(
lastEdge.sound,
SoundSource.BLOCKS,
(player.random.nextFloat() * 0.7F + 1.0F) * 2.0f * lastEdge.volume,
lastEdge.pitch
)
val lastEdge = path.edgeList.last()
player.playNotifySound(
lastEdge.sound,
SoundSource.BLOCKS,
(player.random.nextFloat() * 0.7F + 1.0F) * 2.0f * lastEdge.volume,
lastEdge.pitch
)
}
}
}

insertResult(itemToInsert, selected, removeMaterials, player)
insertResult(target, itemToInsert, selected, removeMaterials, player)
}
}.onFailure {
ItemConverter.LOGGER.error("Error handling C2SConvertTargetPacket", it)
}

fun insertResult(
target: ItemStack,
itemToInsert: ItemStack,
selected: ItemStack,
removeMaterials: () -> Unit,
player: ServerPlayer
) {
val isInHand = ItemStack.isSameItemSameTags(itemToInsert, selected)
val isInHand = ItemStack.isSameItemSameTags(target, selected)

ItemConverter.serverCoroutineScope!!.launch {
runCatching {
Expand All @@ -145,7 +151,7 @@ object C2SConvertTargetPacket {
player.drop(itemToInsert, true)
}
} else {
val existIndex = player.inventory.findSlotMatchingItem(itemToInsert)
val existIndex = player.inventory.findSlotMatchingItem(target)
if (existIndex in 0..8) {
player.inventory.selected = existIndex
player.connection.send(ClientboundSetCarriedItemPacket(player.inventory.selected));
Expand Down

0 comments on commit 76b9536

Please sign in to comment.