Skip to content

Commit 1770ca4

Browse files
committed
air place option
1 parent 5b78774 commit 1770ca4

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

common/src/main/kotlin/com/lambda/config/groups/PlaceSettings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PlaceSettings(
2727
vis: () -> Boolean = { true }
2828
) : PlaceConfig(priority) {
2929
override val rotateForPlace by c.setting("Rotate For Place", true, "Rotate towards block while placing") { vis() }
30+
override val airPlace by c.setting("Air Place", AirPlaceMode.None, "Allows for placing blocks without adjacent faces") { vis() }
3031
override val placeConfirmationMode by c.setting("Place Confirmation", PlaceConfirmationMode.PlaceThenAwait, "Wait for block placement confirmation") { vis() }
3132
override val maxPendingPlacements by c.setting("Max Pending Placements", 2, 0..5, 1, "The maximum amount of pending placements") { vis() }
3233
override val placementsPerTick by c.setting("Instant Places Per Tick", 1, 1..30, 1, "Maximum instant block places per tick") { vis() }

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import net.minecraft.entity.ItemEntity
6060
import net.minecraft.entity.player.PlayerEntity
6161
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
6262
import net.minecraft.sound.SoundCategory
63+
import net.minecraft.util.Hand
6364
import net.minecraft.util.math.BlockPos
6465

6566
object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
@@ -338,7 +339,7 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
338339
info.nullify()
339340
return false
340341
}
341-
if (info.breakConfig.swing != BreakConfig.SwingMode.End) swingHand(info.breakConfig.swingType)
342+
if (info.breakConfig.swing != BreakConfig.SwingMode.End) swingHand(info.breakConfig.swingType, Hand.MAIN_HAND)
342343
return true
343344
}
344345

@@ -389,10 +390,10 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
389390
onBlockBreak(info)
390391
PlayerActionC2SPacket(PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, ctx.expectedPos, hitResult.side, sequence)
391392
}
392-
if (info.breakConfig.swing != BreakConfig.SwingMode.Start) swingHand(info.breakConfig.swingType)
393+
if (info.breakConfig.swing != BreakConfig.SwingMode.Start) swingHand(info.breakConfig.swingType, Hand.MAIN_HAND)
393394
setBreakCooldown(info.breakConfig.breakDelay)
394395
} else {
395-
if (info.breakConfig.swing == BreakConfig.SwingMode.Constant) swingHand(info.breakConfig.swingType)
396+
if (info.breakConfig.swing == BreakConfig.SwingMode.Constant) swingHand(info.breakConfig.swingType, Hand.MAIN_HAND)
396397
}
397398

398399
return true

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceConfig.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class PlaceConfig(
2525
priority: Priority
2626
) : RequestConfig<PlaceRequest>(priority) {
2727
abstract val rotateForPlace: Boolean
28+
abstract val airPlace: AirPlaceMode
2829
abstract val placeConfirmationMode: PlaceConfirmationMode
2930
abstract val maxPendingPlacements: Int
3031
abstract val placementsPerTick: Int
@@ -36,6 +37,12 @@ abstract class PlaceConfig(
3637
PlaceManager.registerRequest(this, request)
3738
}
3839

40+
enum class AirPlaceMode {
41+
None,
42+
Standard,
43+
Grim
44+
}
45+
3946
enum class PlaceConfirmationMode {
4047
None,
4148
PlaceThenAwait,

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceManager.kt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ import net.minecraft.item.BlockItem
5050
import net.minecraft.item.ItemPlacementContext
5151
import net.minecraft.item.ItemStack
5252
import net.minecraft.item.ItemUsageContext
53+
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
5354
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket
5455
import net.minecraft.registry.RegistryKeys
5556
import net.minecraft.sound.SoundCategory
5657
import net.minecraft.util.ActionResult
5758
import net.minecraft.util.Hand
5859
import net.minecraft.util.hit.BlockHitResult
5960
import net.minecraft.util.math.BlockPos
61+
import net.minecraft.util.math.Direction
6062
import net.minecraft.world.GameMode
6163

6264
object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
@@ -197,12 +199,10 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
197199
return ActionResult.FAIL
198200
}
199201

200-
// checks if the player should be able to interact with the block for if its something
201-
// like a furnace or chest where an action would happen
202-
// val actionResult = blockState.onUse(world, player, hand, hitResult)
203-
// if (actionResult.isAccepted) {
204-
// return actionResult
205-
// }
202+
val actionResult = blockState.onUse(world, player, hand, hitResult)
203+
if (actionResult.isAccepted) {
204+
return actionResult
205+
}
206206
}
207207

208208
val itemStack = player.getStackInHand(hand)
@@ -261,12 +261,16 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
261261
else
262262
pendingPlacements.add(request)
263263

264-
interaction.sendSequencedPacket(world) { sequence: Int ->
265-
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
264+
if (request.buildConfig.placeSettings.airPlace == PlaceConfig.AirPlaceMode.Grim) {
265+
airPlaceOffhandSwap()
266+
sendPlacePacket(hand, hitResult)
267+
airPlaceOffhandSwap()
268+
} else {
269+
sendPlacePacket(hand, hitResult)
266270
}
267271

268272
if (request.buildConfig.placeSettings.swing) {
269-
swingHand(request.buildConfig.placeSettings.swingType)
273+
swingHand(request.buildConfig.placeSettings.swingType, hand)
270274
}
271275

272276
if (!stackInHand.isEmpty && (stackInHand.count != stackCountPre || interaction.hasCreativeInventory())) {
@@ -296,6 +300,11 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
296300
return ActionResult.success(world.isClient)
297301
}
298302

303+
private fun SafeContext.sendPlacePacket(hand: Hand, hitResult: BlockHitResult) =
304+
interaction.sendSequencedPacket(world) { sequence: Int ->
305+
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
306+
}
307+
299308
private fun SafeContext.placeSound(item: BlockItem, state: BlockState, pos: BlockPos) {
300309
val blockSoundGroup = state.soundGroup
301310
world.playSound(
@@ -308,6 +317,16 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
308317
)
309318
}
310319

320+
private fun SafeContext.airPlaceOffhandSwap() {
321+
connection.sendPacket(
322+
PlayerActionC2SPacket(
323+
PlayerActionC2SPacket.Action.SWAP_ITEM_WITH_OFFHAND,
324+
BlockPos.ORIGIN,
325+
Direction.DOWN
326+
)
327+
)
328+
}
329+
311330
override fun preEvent() = UpdateManagerEvent.Place.Pre().post()
312331
override fun postEvent() = UpdateManagerEvent.Place.Post().post()
313332
}

common/src/main/kotlin/com/lambda/util/player/PlayerUtils.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ fun SafeContext.spawnFakePlayer(
4747
return entity
4848
}
4949

50-
fun SafeContext.swingHand(swingType: BuildConfig.SwingType) =
50+
fun SafeContext.swingHand(swingType: BuildConfig.SwingType, hand: Hand) =
5151
when (swingType) {
52-
BuildConfig.SwingType.Vanilla -> player.swingHand(player.activeHand)
53-
BuildConfig.SwingType.Server -> connection.sendPacket(HandSwingC2SPacket(player.activeHand))
54-
BuildConfig.SwingType.Client -> swingHandClient(player.activeHand)
52+
BuildConfig.SwingType.Vanilla -> player.swingHand(hand)
53+
BuildConfig.SwingType.Server -> connection.sendPacket(HandSwingC2SPacket(hand))
54+
BuildConfig.SwingType.Client -> swingHandClient(hand)
5555
}
5656

5757
fun SafeContext.swingHandClient(hand: Hand) {

0 commit comments

Comments
 (0)