copy dispensing behavior for minecarts
This commit is contained in:
parent
c39e139b01
commit
144c84dabd
@ -1,19 +1,31 @@
|
||||
package ru.dbotthepony.mc.otm.item
|
||||
|
||||
import net.minecraft.core.BlockSource
|
||||
import net.minecraft.core.Direction
|
||||
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior
|
||||
import net.minecraft.core.dispenser.DispenseItemBehavior
|
||||
import net.minecraft.tags.BlockTags
|
||||
import net.minecraft.world.InteractionResult
|
||||
import net.minecraft.world.entity.vehicle.AbstractMinecart
|
||||
import net.minecraft.world.item.DyeColor
|
||||
import net.minecraft.world.item.Item
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.item.MinecartItem
|
||||
import net.minecraft.world.item.context.UseOnContext
|
||||
import net.minecraft.world.level.Level
|
||||
import net.minecraft.world.level.block.BaseRailBlock
|
||||
import net.minecraft.world.level.block.DispenserBlock
|
||||
import net.minecraft.world.level.block.state.properties.RailShape
|
||||
import net.minecraft.world.level.gameevent.GameEvent
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters
|
||||
import ru.dbotthepony.mc.otm.entity.MinecartCargoCrate
|
||||
import ru.dbotthepony.mc.otm.registry.MEntityTypes
|
||||
import kotlin.math.floor
|
||||
|
||||
class MinecartCargoCrateItem(val color: DyeColor?) : Item(Properties().stacksTo(16).tab(OverdriveThatMatters.INSTANCE.CREATIVE_TAB)) {
|
||||
init {
|
||||
DispenserBlock.registerBehavior(this, Companion)
|
||||
}
|
||||
|
||||
override fun useOn(useOnContext: UseOnContext): InteractionResult {
|
||||
val level = useOnContext.level
|
||||
val blockpos = useOnContext.clickedPos
|
||||
@ -50,4 +62,57 @@ class MinecartCargoCrateItem(val color: DyeColor?) : Item(Properties().stacksTo(
|
||||
|
||||
return InteractionResult.sidedSuccess(level.isClientSide)
|
||||
}
|
||||
|
||||
companion object : DispenseItemBehavior {
|
||||
private val default = DefaultDispenseItemBehavior()
|
||||
|
||||
override fun dispense(blockSource: BlockSource, itemStack: ItemStack): ItemStack {
|
||||
val direction = blockSource.blockState.getValue(DispenserBlock.FACING)
|
||||
val level: Level = blockSource.level
|
||||
val x = blockSource.x() + direction.stepX.toDouble() * 1.125
|
||||
val y = floor(blockSource.y()) + direction.stepY.toDouble()
|
||||
val z = blockSource.z() + direction.stepZ.toDouble() * 1.125
|
||||
val blockpos = blockSource.pos.relative(direction)
|
||||
val blockstate = level.getBlockState(blockpos)
|
||||
val railshape =
|
||||
if (blockstate.block is BaseRailBlock)
|
||||
(blockstate.block as BaseRailBlock).getRailDirection(blockstate, level, blockpos, null)
|
||||
else
|
||||
RailShape.NORTH_SOUTH
|
||||
|
||||
val d3 = if (blockstate.`is`(BlockTags.RAILS)) {
|
||||
if (railshape.isAscending) {
|
||||
0.6
|
||||
} else {
|
||||
0.1
|
||||
}
|
||||
} else {
|
||||
if (!blockstate.isAir || !level.getBlockState(blockpos.below()).`is`(BlockTags.RAILS)) {
|
||||
return default.dispense(blockSource, itemStack)
|
||||
}
|
||||
|
||||
val blockstate1 = level.getBlockState(blockpos.below())
|
||||
val railshape1 = if (blockstate1.block is BaseRailBlock) blockstate1.getValue((blockstate1.block as BaseRailBlock).shapeProperty) else RailShape.NORTH_SOUTH
|
||||
|
||||
if (direction != Direction.DOWN && railshape1.isAscending) {
|
||||
-0.4
|
||||
} else {
|
||||
-0.9
|
||||
}
|
||||
}
|
||||
|
||||
val color = (itemStack.item as MinecartCargoCrateItem).color
|
||||
val type = MEntityTypes.CARGO_CRATE_MINECARTS[color] ?: throw NullPointerException("Unable to find minecart entity type for color $color!")
|
||||
|
||||
val entity = MinecartCargoCrate(type, color, level, x, y + d3, z)
|
||||
|
||||
if (itemStack.hasCustomHoverName()) {
|
||||
entity.customName = itemStack.hoverName
|
||||
}
|
||||
|
||||
level.addFreshEntity(entity)
|
||||
itemStack.shrink(1)
|
||||
return itemStack
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user