Merge branch 'workingfluidcapsules' of https://git.dbotthepony.ru/DBot/overdrive_that_matters into master
This commit is contained in:
commit
b81971babd
@ -8,23 +8,29 @@ import net.minecraft.server.level.ServerLevel
|
|||||||
import net.minecraft.sounds.SoundEvent
|
import net.minecraft.sounds.SoundEvent
|
||||||
import net.minecraft.sounds.SoundEvents
|
import net.minecraft.sounds.SoundEvents
|
||||||
import net.minecraft.sounds.SoundSource
|
import net.minecraft.sounds.SoundSource
|
||||||
|
import net.minecraft.world.InteractionHand
|
||||||
import net.minecraft.world.InteractionResult
|
import net.minecraft.world.InteractionResult
|
||||||
|
import net.minecraft.world.InteractionResultHolder
|
||||||
import net.minecraft.world.entity.player.Player
|
import net.minecraft.world.entity.player.Player
|
||||||
import net.minecraft.world.item.Item
|
import net.minecraft.world.item.Item
|
||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import net.minecraft.world.item.TooltipFlag
|
import net.minecraft.world.item.TooltipFlag
|
||||||
import net.minecraft.world.item.context.UseOnContext
|
import net.minecraft.world.item.context.UseOnContext
|
||||||
|
import net.minecraft.world.level.ClipContext
|
||||||
import net.minecraft.world.level.Level
|
import net.minecraft.world.level.Level
|
||||||
import net.minecraft.world.level.block.Block
|
import net.minecraft.world.level.block.Block
|
||||||
import net.minecraft.world.level.block.Blocks
|
import net.minecraft.world.level.block.Blocks
|
||||||
import net.minecraft.world.level.block.LayeredCauldronBlock
|
import net.minecraft.world.level.block.LayeredCauldronBlock
|
||||||
|
import net.minecraft.world.level.block.LiquidBlockContainer
|
||||||
import net.minecraft.world.level.block.state.BlockState
|
import net.minecraft.world.level.block.state.BlockState
|
||||||
import net.minecraft.world.level.material.Fluid
|
import net.minecraft.world.level.material.Fluid
|
||||||
import net.minecraft.world.level.material.Fluids
|
import net.minecraft.world.level.material.Fluids
|
||||||
|
import net.minecraft.world.phys.HitResult
|
||||||
import net.minecraftforge.common.SoundActions
|
import net.minecraftforge.common.SoundActions
|
||||||
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
||||||
import net.minecraftforge.fluids.FluidStack
|
import net.minecraftforge.fluids.FluidStack
|
||||||
|
import net.minecraftforge.fluids.FluidUtil
|
||||||
import net.minecraftforge.fluids.capability.IFluidHandler
|
import net.minecraftforge.fluids.capability.IFluidHandler
|
||||||
import ru.dbotthepony.mc.otm.capability.fluid.ItemMatteryFluidHandler
|
import ru.dbotthepony.mc.otm.capability.fluid.ItemMatteryFluidHandler
|
||||||
import ru.dbotthepony.mc.otm.capability.fluidLevel
|
import ru.dbotthepony.mc.otm.capability.fluidLevel
|
||||||
@ -35,6 +41,7 @@ import ru.dbotthepony.mc.otm.core.TranslatableComponent
|
|||||||
import ru.dbotthepony.mc.otm.core.ifPresentK
|
import ru.dbotthepony.mc.otm.core.ifPresentK
|
||||||
import ru.dbotthepony.mc.otm.core.immutableList
|
import ru.dbotthepony.mc.otm.core.immutableList
|
||||||
import ru.dbotthepony.mc.otm.core.immutableMap
|
import ru.dbotthepony.mc.otm.core.immutableMap
|
||||||
|
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||||
import ru.dbotthepony.mc.otm.core.orNull
|
import ru.dbotthepony.mc.otm.core.orNull
|
||||||
import java.util.function.IntSupplier
|
import java.util.function.IntSupplier
|
||||||
|
|
||||||
@ -76,6 +83,51 @@ class FluidCapsuleItem(val capacity: IntSupplier) : Item(Properties().stacksTo(6
|
|||||||
return ItemMatteryFluidHandler(stack, capacity)
|
return ItemMatteryFluidHandler(stack, capacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun use(level: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
|
||||||
|
val item = player.getItemInHand(hand)
|
||||||
|
var targetItem = item.copyWithCount(1)
|
||||||
|
val cap = targetItem.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).orNull() ?: return InteractionResultHolder.fail(item)
|
||||||
|
val fluid = cap.getFluidInTank(0)
|
||||||
|
if (fluid.isNotEmpty && fluid.amount != 1000) return InteractionResultHolder.pass(item)
|
||||||
|
|
||||||
|
val hitResult = getPlayerPOVHitResult(level, player, if (fluid.isEmpty) ClipContext.Fluid.SOURCE_ONLY else ClipContext.Fluid.NONE)
|
||||||
|
if (hitResult.type != HitResult.Type.BLOCK) return InteractionResultHolder.pass(item)
|
||||||
|
|
||||||
|
val hitPos = hitResult.blockPos
|
||||||
|
val hitDir = hitResult.direction
|
||||||
|
val nextPos = hitPos.relative(hitDir)
|
||||||
|
|
||||||
|
if (level.mayInteract(player, hitPos) && player.mayUseItemAt(nextPos, hitDir, item)) {
|
||||||
|
targetItem = if (fluid.isEmpty) {
|
||||||
|
val actionResult = FluidUtil.tryPickUpFluid(item, player, level, hitPos, hitDir)
|
||||||
|
if (!actionResult.isSuccess) return InteractionResultHolder.pass(item)
|
||||||
|
|
||||||
|
actionResult.result
|
||||||
|
} else {
|
||||||
|
val state = level.getBlockState(hitPos)
|
||||||
|
val placePos = if (state.block is LiquidBlockContainer && (state.block as LiquidBlockContainer).canPlaceLiquid(level, hitPos, state, fluid.fluid)) hitPos else nextPos
|
||||||
|
|
||||||
|
val actionResult = FluidUtil.tryPlaceFluid(player, level, hand, placePos, targetItem, fluid)
|
||||||
|
if (!actionResult.isSuccess) return InteractionResultHolder.pass(item)
|
||||||
|
|
||||||
|
actionResult.result
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!player.abilities.instabuild && !player.level().isClientSide) {
|
||||||
|
item.shrink(1)
|
||||||
|
|
||||||
|
if (item.isEmpty) return InteractionResultHolder.sidedSuccess(targetItem, player.level().isClientSide)
|
||||||
|
if (!player.inventory.add(targetItem)) {
|
||||||
|
player.spawnAtLocation(targetItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return InteractionResultHolder.sidedSuccess(item, player.level().isClientSide)
|
||||||
|
}
|
||||||
|
|
||||||
|
return InteractionResultHolder.pass(item)
|
||||||
|
}
|
||||||
|
|
||||||
interface Interaction {
|
interface Interaction {
|
||||||
fun canInteract(item: ItemStack, player: Player, context: Context): Boolean
|
fun canInteract(item: ItemStack, player: Player, context: Context): Boolean
|
||||||
fun interact(item: ItemStack, player: Player, context: Context): InteractionResult
|
fun interact(item: ItemStack, player: Player, context: Context): InteractionResult
|
||||||
|
Loading…
Reference in New Issue
Block a user