Skip to content

Commit fbe4662

Browse files
committed
player utils
1 parent 1ec300d commit fbe4662

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import com.lambda.util.item.ItemUtils.findBestToolsForBreaking
5656
import com.lambda.util.math.distSq
5757
import com.lambda.util.player.SlotUtils.hotbar
5858
import com.lambda.util.player.copyPlayer
59+
import com.lambda.util.player.gamemode
5960
import com.lambda.util.world.raycast.RayCastUtils.blockResult
6061
import net.minecraft.block.OperatorBlock
6162
import net.minecraft.block.pattern.CachedBlockPosition
@@ -121,7 +122,7 @@ object BuildSimulator {
121122
}
122123

123124
/* the player is in the wrong game mode to alter the block state */
124-
if (player.isBlockBreakingRestricted(world, pos, interaction.currentGameMode)) {
125+
if (player.isBlockBreakingRestricted(world, pos, gamemode)) {
125126
return BuildResult.Restricted(pos)
126127
}
127128

@@ -136,7 +137,7 @@ object BuildSimulator {
136137
}
137138

138139
/* block is unbreakable, so it cant be broken or replaced */
139-
if (state.getHardness(world, pos) < 0 && !player.isCreative) {
140+
if (state.getHardness(world, pos) < 0 && !gamemode.isCreative) {
140141
return BuildResult.Unbreakable(pos, state)
141142
}
142143

@@ -461,7 +462,7 @@ object BuildSimulator {
461462
eye, blockHit, request, state, targetState, player.inventory.selectedSlot, instant
462463
)
463464

464-
if (player.isCreative) {
465+
if (gamemode.isCreative) {
465466
acc.add(BreakResult.Break(pos, breakContext))
466467
return acc
467468
}

common/src/main/kotlin/com/lambda/interaction/material/container/containers/CreativeContainer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import com.lambda.context.SafeContext
2222
import com.lambda.interaction.material.StackSelection
2323
import com.lambda.interaction.material.container.MaterialContainer
2424
import com.lambda.interaction.material.transfer.TransactionExecutor
25-
import com.lambda.interaction.material.transfer.TransactionExecutor.Companion
2625
import com.lambda.task.Task
2726
import com.lambda.util.item.ItemStackUtils.equal
27+
import com.lambda.util.player.gamemode
2828
import com.lambda.util.text.buildText
2929
import com.lambda.util.text.literal
3030
import net.minecraft.item.ItemStack
@@ -44,7 +44,7 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
4444
override val name: String get() = "Removing $selection from creative inventory"
4545

4646
override fun SafeContext.onStart() {
47-
if (!player.isCreative) {
47+
if (!gamemode.isCreative) {
4848
// ToDo: Maybe switch gamemode?
4949
throw NotInCreativeModeException()
5050
}
@@ -70,7 +70,7 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
7070
selection.optimalStack?.let { optimalStack ->
7171
if (player.mainHandStack.equal(optimalStack)) return
7272

73-
if (!player.isCreative) {
73+
if (!gamemode.isCreative) {
7474
// ToDo: Maybe switch gamemode?
7575
throw NotInCreativeModeException()
7676
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import com.lambda.util.Communication.info
4444
import com.lambda.util.Communication.warn
4545
import com.lambda.util.collections.LimitedDecayQueue
4646
import com.lambda.util.item.ItemUtils.block
47+
import com.lambda.util.player.gamemode
4748
import com.lambda.util.player.swingHand
4849
import net.minecraft.block.BlockState
4950
import net.minecraft.block.OperatorBlock
@@ -281,7 +282,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
281282
val ctx = info.context
282283
val hitResult = ctx.result
283284

284-
if (interaction.currentGameMode.isCreative && world.worldBorder.contains(ctx.expectedPos)) {
285+
if (gamemode.isCreative && world.worldBorder.contains(ctx.expectedPos)) {
285286
setBreakCooldown(info.breakConfig.breakDelay)
286287
interaction.sendSequencedPacket(world) { sequence ->
287288
onBlockBreak(info)
@@ -358,10 +359,10 @@ object BreakManager : RequestHandler<BreakRequest>() {
358359
private fun SafeContext.attackBlock(info: BreakInfo): Boolean {
359360
val ctx = info.context
360361

361-
if (player.isBlockBreakingRestricted(world, ctx.expectedPos, interaction.currentGameMode)) return false
362+
if (player.isBlockBreakingRestricted(world, ctx.expectedPos, gamemode)) return false
362363
if (!world.worldBorder.contains(ctx.expectedPos)) return false
363364

364-
if (interaction.currentGameMode.isCreative) {
365+
if (gamemode.isCreative) {
365366
interaction.sendSequencedPacket(world) { sequence: Int ->
366367
onBlockBreak(info)
367368
PlayerActionC2SPacket(PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, ctx.expectedPos, ctx.result.side, sequence)
@@ -433,7 +434,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
433434
private fun SafeContext.destroyBlock(info: BreakInfo): Boolean {
434435
val ctx = info.context
435436

436-
if (player.isBlockBreakingRestricted(world, ctx.expectedPos, interaction.currentGameMode)) return false
437+
if (player.isBlockBreakingRestricted(world, ctx.expectedPos, gamemode)) return false
437438

438439
if (!player.mainHandStack.item.canMine(ctx.checkedState, world, ctx.expectedPos, player))
439440
return false

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import com.lambda.module.modules.client.TaskFlowModule
3737
import com.lambda.util.Communication.info
3838
import com.lambda.util.Communication.warn
3939
import com.lambda.util.collections.LimitedDecayQueue
40+
import com.lambda.util.player.gamemode
41+
import com.lambda.util.player.isItemOnCooldown
4042
import com.lambda.util.player.swingHand
4143
import net.minecraft.block.BlockState
4244
import net.minecraft.block.pattern.CachedBlockPosition
@@ -192,17 +194,17 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
192194
hitResult: BlockHitResult
193195
): ActionResult {
194196
val itemStack = player.getStackInHand(hand)
195-
if (interaction.currentGameMode == GameMode.SPECTATOR) return ActionResult.SUCCESS
197+
if (gamemode == GameMode.SPECTATOR) return ActionResult.SUCCESS
196198

197199
// checks if the player should be able to interact with the block for if its something
198200
// like a furnace or chest where an action would happen
199201
// val handNotEmpty = player.getStackInHand(hand).isEmpty.not()
200202
// val cantInteract = player.shouldCancelInteraction() && handNotEmpty
201203
// if (!cantInteract) return ActionResult.PASS
202204

203-
if (!itemStack.isEmpty && !player.itemCooldownManager.isCoolingDown(itemStack.item)) {
205+
if (!itemStack.isEmpty && !isItemOnCooldown(itemStack.item)) {
204206
val itemUsageContext = ItemUsageContext(player, hand, hitResult)
205-
return if (interaction.currentGameMode.isCreative) {
207+
return if (gamemode.isCreative) {
206208
val i = itemStack.count
207209
useOnBlock(placeConfig, itemStack, itemUsageContext)
208210
.also {
@@ -219,7 +221,7 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
219221
itemStack: ItemStack,
220222
context: ItemUsageContext
221223
): ActionResult {
222-
val cachedBlockPosition = CachedBlockPosition(context.world, context.blockPos, false)
224+
val cachedBlockPosition = CachedBlockPosition(world, context.blockPos, false)
223225

224226
val cantModifyWorld = !player.abilities.allowModifyWorld
225227
val cantPlaceOn = !itemStack.canPlaceOn(context.world.registryManager.get(RegistryKeys.BLOCK), cachedBlockPosition)

common/src/main/kotlin/com/lambda/util/BlockUtils.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.util
1919

2020
import com.lambda.context.SafeContext
2121
import com.lambda.util.math.MathUtils.floorToInt
22+
import com.lambda.util.player.gamemode
2223
import net.minecraft.block.AbstractCauldronBlock
2324
import net.minecraft.block.AbstractFurnaceBlock
2425
import net.minecraft.block.AbstractSignBlock
@@ -237,12 +238,12 @@ object BlockUtils {
237238

238239
fun SafeContext.instantBreakable(blockState: BlockState, blockPos: BlockPos, breakThreshold: Float): Boolean {
239240
val ticksNeeded = 1 / (blockState.calcBlockBreakingDelta(player, world, blockPos) / breakThreshold)
240-
return (ticksNeeded <= 1 && ticksNeeded != 0f) || player.isCreative
241+
return (ticksNeeded <= 1 && ticksNeeded != 0f) || gamemode.isCreative
241242
}
242243

243244
fun SafeContext.instantBreakable(blockState: BlockState, blockPos: BlockPos, item: ItemStack, breakThreshold: Float): Boolean {
244245
val ticksNeeded = 1 / (blockState.calcItemBlockBreakingDelta(player, world, blockPos, item) / breakThreshold)
245-
return (ticksNeeded <= 1 && ticksNeeded != 0f) || player.isCreative
246+
return (ticksNeeded <= 1 && ticksNeeded != 0f) || gamemode.isCreative
246247
}
247248

248249
fun BlockState.calcItemBlockBreakingDelta(

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import net.minecraft.client.network.ClientPlayerEntity
77
import net.minecraft.client.network.OtherClientPlayerEntity
88
import net.minecraft.client.network.PlayerListEntry
99
import net.minecraft.entity.player.PlayerEntity
10+
import net.minecraft.item.Item
1011
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket
1112
import net.minecraft.util.Hand
1213

14+
val SafeContext.gamemode
15+
get() = interaction.currentGameMode
16+
1317
fun SafeContext.copyPlayer(entity: ClientPlayerEntity) =
1418
ClientPlayerEntity(mc, world, mc.networkHandler, null, null, entity.isSneaking, entity.isSprinting).apply {
1519
setPos(entity.x, entity.y, entity.z)
@@ -56,4 +60,6 @@ fun SafeContext.swingHandClient(hand: Hand) {
5660
player.handSwinging = true
5761
player.preferredHand = hand
5862
}
59-
}
63+
}
64+
65+
fun SafeContext.isItemOnCooldown(item: Item) = player.itemCooldownManager.isCoolingDown(item)

0 commit comments

Comments
 (0)