|
| 1 | +package org.valkyrienskies.clockwork.content.curiosities.tools.drill |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMultimap |
| 4 | +import com.google.common.collect.Multimap |
| 5 | +import com.simibubi.create.content.equipment.armor.BacktankUtil |
| 6 | +import com.simibubi.create.foundation.item.CustomArmPoseItem |
| 7 | +import net.minecraft.client.model.HumanoidModel |
| 8 | +import net.minecraft.client.player.AbstractClientPlayer |
| 9 | +import net.minecraft.core.BlockPos |
| 10 | +import net.minecraft.core.Direction |
| 11 | +import net.minecraft.stats.Stats |
| 12 | +import net.minecraft.tags.BlockTags |
| 13 | +import net.minecraft.tags.TagKey |
| 14 | +import net.minecraft.world.InteractionHand |
| 15 | +import net.minecraft.world.entity.EquipmentSlot |
| 16 | +import net.minecraft.world.entity.LivingEntity |
| 17 | +import net.minecraft.world.entity.ai.attributes.Attribute |
| 18 | +import net.minecraft.world.entity.ai.attributes.AttributeModifier |
| 19 | +import net.minecraft.world.entity.player.Player |
| 20 | +import net.minecraft.world.item.ItemStack |
| 21 | +import net.minecraft.world.item.PickaxeItem |
| 22 | +import net.minecraft.world.item.Tier |
| 23 | +import net.minecraft.world.item.UseAnim |
| 24 | +import net.minecraft.world.level.Level |
| 25 | +import net.minecraft.world.level.block.Block |
| 26 | +import net.minecraft.world.level.block.state.BlockState |
| 27 | +import org.lwjgl.system.NonnullDefault |
| 28 | +import org.valkyrienskies.clockwork.ClockworkItems |
| 29 | +import org.valkyrienskies.clockwork.platform.PlatformUtils.getReachAttribute |
| 30 | +import org.valkyrienskies.clockwork.util.BoundingBoxHelper |
| 31 | +import java.util.function.Consumer |
| 32 | +import kotlin.math.ceil |
| 33 | + |
| 34 | +@NonnullDefault |
| 35 | +class HandheldMechanicalDrill(tier: Tier?, attackBonus: Int, attackSpeedBonus: Float, properties: Properties?) : |
| 36 | + PickaxeItem( |
| 37 | + tier, |
| 38 | + attackBonus, |
| 39 | + attackSpeedBonus, |
| 40 | + properties |
| 41 | + ), CustomArmPoseItem { |
| 42 | + override fun mineBlock( |
| 43 | + tool: ItemStack, |
| 44 | + level: Level, |
| 45 | + block: BlockState, |
| 46 | + pos: BlockPos, |
| 47 | + player: LivingEntity |
| 48 | + ): Boolean { |
| 49 | + val success = super.mineBlock(tool, level, block, pos, player) |
| 50 | + |
| 51 | + if (success && isCorrectToolForDrops(block) && !level!!.isClientSide) { |
| 52 | + if (player!!.isCrouching()) { |
| 53 | + mineTunnel(level, pos, player, CROUCH_MINE_DIAMETER, CROUCH_MINE_DEPTH) |
| 54 | + } else { |
| 55 | + mineTunnel(level, pos, player, MINE_DIAMETER, MINE_DEPTH) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return success |
| 60 | + } |
| 61 | + |
| 62 | + override fun isCorrectToolForDrops(block: BlockState): Boolean { |
| 63 | + val ret = block.`is`(BlockTags.MINEABLE_WITH_PICKAXE) || block.`is`(BlockTags.MINEABLE_WITH_SHOVEL) || super.isCorrectToolForDrops(block) |
| 64 | + //println(ret) |
| 65 | + return ret |
| 66 | + } |
| 67 | + |
| 68 | + override fun getDestroySpeed(stack: ItemStack, state: BlockState): Float { |
| 69 | + return if (isCorrectToolForDrops(state)) { |
| 70 | + this.speed |
| 71 | + } else { |
| 72 | + 1.0f |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Mines a tunnel in the `direction` the player is facing at the given block.<br></br> |
| 78 | + * The tunnel is a rectangular prism of width and height `diameter`, and depth `depth`. |
| 79 | + * |
| 80 | + * @param level level to break the tunnel in |
| 81 | + * @param origin position to start the tunnel at |
| 82 | + * @param player player to simulate breaking the tunnel with |
| 83 | + * @param diameter diameter of the tunnel |
| 84 | + * @param depth depth of the tunnel |
| 85 | + */ |
| 86 | + private fun mineTunnel(level: Level, origin: BlockPos, player: LivingEntity, diameter: Int, depth: Int) { |
| 87 | + val direction = determineDirection(player!!) |
| 88 | + |
| 89 | + val offsetX = ceil((-diameter / 2f).toDouble()).toInt() |
| 90 | + val offsetY = ceil((-diameter / 2f).toDouble()).toInt() |
| 91 | + val offsetZ = 0 |
| 92 | + |
| 93 | + val boundWidth = diameter |
| 94 | + val boundHeight = diameter |
| 95 | + val boundDepth = depth |
| 96 | + |
| 97 | + val boundingBox = BoundingBoxHelper.orientBox( |
| 98 | + origin, |
| 99 | + offsetX, |
| 100 | + offsetY, |
| 101 | + offsetZ, |
| 102 | + boundWidth, |
| 103 | + boundHeight, |
| 104 | + boundDepth, |
| 105 | + direction |
| 106 | + ) |
| 107 | + |
| 108 | + BlockPos.betweenClosedStream(boundingBox).forEach { testPos: BlockPos? -> tryMine(level!!, testPos, player) } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the player direction, with added logic for Up and Down based on `MINING_ANGLE_LIMIT` |
| 113 | + * |
| 114 | + * @param player player direction to check |
| 115 | + * |
| 116 | + * @return player direction |
| 117 | + */ |
| 118 | + private fun determineDirection(player: LivingEntity): Direction { |
| 119 | + val xRot = player.getXRot() |
| 120 | + if (xRot <= -MINING_ANGLE_LIMIT) { |
| 121 | + return Direction.UP |
| 122 | + } else if (xRot >= MINING_ANGLE_LIMIT) { |
| 123 | + return Direction.DOWN |
| 124 | + } else { |
| 125 | + return player.getDirection() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Attempts to simulate mining a block. If a tool is specified, it must be breakable by that tool |
| 131 | + * |
| 132 | + * @param level level to break in |
| 133 | + * @param pos position to break at |
| 134 | + * @param livingEntity entity to simulate the break as (can be `null`) |
| 135 | + */ |
| 136 | + private fun tryMine( |
| 137 | + level: Level, pos: BlockPos?, |
| 138 | + livingEntity: LivingEntity? |
| 139 | + ) { |
| 140 | + val minedBlockState = level.getBlockState(pos) |
| 141 | + |
| 142 | + if (livingEntity != null) { |
| 143 | + val tool = livingEntity.getMainHandItem() |
| 144 | + |
| 145 | + if (tool.`is`(ClockworkItems.HANDHELD_DRILL.get())) { |
| 146 | + val toolItem = tool.item as HandheldMechanicalDrill |
| 147 | + if (!toolItem.isCorrectToolForDrops(minedBlockState)) { |
| 148 | + return |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (livingEntity is Player && livingEntity.hasCorrectToolForDrops(minedBlockState)) { |
| 153 | + livingEntity.awardStat(Stats.BLOCK_MINED.get(minedBlockState.getBlock())) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (level.removeBlock(pos, false)) { |
| 158 | + minedBlockState.getBlock().destroy(level, pos, minedBlockState) |
| 159 | + } else { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + if (livingEntity == null) return |
| 164 | + Block.dropResources(minedBlockState, level, pos, null, livingEntity, livingEntity.getMainHandItem()) |
| 165 | + } |
| 166 | + |
| 167 | + override fun getDefaultAttributeModifiers(slot: EquipmentSlot?): Multimap<Attribute?, AttributeModifier?>? { |
| 168 | + val attributes = ImmutableMultimap.Builder<Attribute?, AttributeModifier?>() |
| 169 | + val distanceAttribute = getReachAttribute() |
| 170 | + attributes.putAll(super.getDefaultAttributeModifiers(slot)) |
| 171 | + //attributes.put(distanceAttribute, AttributeModifier("Reach", -3.0, AttributeModifier.Operation.ADDITION)) |
| 172 | + return attributes.build() |
| 173 | + } |
| 174 | + |
| 175 | + override fun isBarVisible(stack: ItemStack?): Boolean { |
| 176 | + return BacktankUtil.isBarVisible(stack, MAX_BACKTANK_USES) |
| 177 | + } |
| 178 | + |
| 179 | + override fun getBarWidth(stack: ItemStack?): Int { |
| 180 | + return BacktankUtil.getBarWidth(stack, MAX_BACKTANK_USES) |
| 181 | + } |
| 182 | + |
| 183 | + override fun getBarColor(stack: ItemStack?): Int { |
| 184 | + return BacktankUtil.getBarColor(stack, MAX_BACKTANK_USES) |
| 185 | + } |
| 186 | + |
| 187 | + override fun getUseAnimation(stack: ItemStack?): UseAnim? { |
| 188 | + return UseAnim.NONE |
| 189 | + } |
| 190 | + |
| 191 | + fun <T : LivingEntity?> damageItem(stack: ItemStack?, amount: Int, entity: T?, onBroken: Consumer<T?>?): Int { |
| 192 | + if (BacktankUtil.canAbsorbDamage(entity, MAX_BACKTANK_USES)) { |
| 193 | + return 0 |
| 194 | + } |
| 195 | + return amount |
| 196 | + } |
| 197 | + |
| 198 | + override fun getArmPose( |
| 199 | + stack: ItemStack?, |
| 200 | + player: AbstractClientPlayer, |
| 201 | + hand: InteractionHand? |
| 202 | + ): HumanoidModel.ArmPose? { |
| 203 | + return HumanoidModel.ArmPose.CROSSBOW_HOLD |
| 204 | + } |
| 205 | + |
| 206 | + companion object { |
| 207 | + // Constants |
| 208 | + protected const val MAX_DAMAGE: Int = 2048 |
| 209 | + protected const val MAX_BACKTANK_USES: Int = 1000 |
| 210 | + |
| 211 | + // Mining Constants |
| 212 | + private const val MINING_ANGLE_LIMIT = 55 |
| 213 | + |
| 214 | + private const val MINE_DIAMETER = 3 |
| 215 | + private const val MINE_DEPTH = 1 |
| 216 | + |
| 217 | + private const val CROUCH_MINE_DIAMETER = 1 |
| 218 | + private const val CROUCH_MINE_DEPTH = 3 |
| 219 | + } |
| 220 | +} |
0 commit comments