upgrade minecarts as well
This commit is contained in:
parent
cf45fe69c5
commit
15023b6599
@ -48,6 +48,7 @@ import ru.dbotthepony.mc.otm.config.MachinesConfig;
|
||||
import ru.dbotthepony.mc.otm.config.ServerCompatConfig;
|
||||
import ru.dbotthepony.mc.otm.config.ServerConfig;
|
||||
import ru.dbotthepony.mc.otm.config.ToolsConfig;
|
||||
import ru.dbotthepony.mc.otm.item.ChestUpgraderItem;
|
||||
import ru.dbotthepony.mc.otm.item.tool.ExplosiveHammerItem;
|
||||
import ru.dbotthepony.mc.otm.item.armor.TritaniumArmorItem;
|
||||
import ru.dbotthepony.mc.otm.item.QuantumBatteryItem;
|
||||
@ -198,6 +199,8 @@ public final class OverdriveThatMatters {
|
||||
|
||||
EVENT_BUS.addListener(EventPriority.NORMAL, ExplosiveHammerItem.Companion::onLeftClickBlock);
|
||||
|
||||
EVENT_BUS.addListener(EventPriority.NORMAL, ChestUpgraderItem.Companion::onEntityInteract);
|
||||
|
||||
EVENT_BUS.addListener(EventPriority.NORMAL, DevChestBlockEntity.Companion::mappingsChanged);
|
||||
|
||||
MatteryPlayerNetworkChannel.INSTANCE.register();
|
||||
|
@ -7,10 +7,9 @@ import net.minecraft.server.level.ServerLevel
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.InteractionHand
|
||||
import net.minecraft.world.InteractionResult
|
||||
import net.minecraft.world.item.BlockItem
|
||||
import net.minecraft.world.item.Item
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.item.TooltipFlag
|
||||
import net.minecraft.world.entity.vehicle.AbstractMinecart
|
||||
import net.minecraft.world.entity.vehicle.MinecartChest
|
||||
import net.minecraft.world.item.*
|
||||
import net.minecraft.world.item.context.BlockPlaceContext
|
||||
import net.minecraft.world.item.context.UseOnContext
|
||||
import net.minecraft.world.level.Level
|
||||
@ -20,11 +19,15 @@ import net.minecraft.world.level.block.entity.ChestBlockEntity
|
||||
import net.minecraft.world.level.gameevent.GameEvent
|
||||
import net.minecraft.world.level.storage.loot.LootParams
|
||||
import net.minecraft.world.level.storage.loot.parameters.LootContextParams
|
||||
import net.minecraftforge.event.entity.player.PlayerInteractEvent
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters.MOD_ID
|
||||
import ru.dbotthepony.mc.otm.block.decorative.CargoCrateBlock
|
||||
import ru.dbotthepony.mc.otm.block.entity.decorative.CargoCrateBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.set
|
||||
import ru.dbotthepony.mc.otm.core.TranslatableComponent
|
||||
import ru.dbotthepony.mc.otm.core.math.Vector
|
||||
import ru.dbotthepony.mc.otm.entity.MinecartCargoCrate
|
||||
import ru.dbotthepony.mc.otm.registry.MEntityTypes
|
||||
|
||||
class ChestUpgraderItem : Item(Properties().stacksTo(1)) {
|
||||
override fun onItemUseFirst(stack: ItemStack, context: UseOnContext): InteractionResult {
|
||||
@ -46,7 +49,7 @@ class ChestUpgraderItem : Item(Properties().stacksTo(1)) {
|
||||
if (hitBlockEntity is ChestBlockEntity || hitBlockEntity is BarrelBlockEntity) {
|
||||
val container = hitBlockEntity as Container
|
||||
|
||||
if (container.containerSize >= 54) return super.onItemUseFirst(stack, context)
|
||||
if (container.containerSize > 54) return super.onItemUseFirst(stack, context)
|
||||
|
||||
val blockState = context.level.getBlockState(pos)
|
||||
val newState = block.getStateForPlacement(BlockPlaceContext(context)) ?: return InteractionResult.FAIL
|
||||
@ -109,5 +112,59 @@ class ChestUpgraderItem : Item(Properties().stacksTo(1)) {
|
||||
companion object {
|
||||
private val DESCRIPTION = TranslatableComponent("item.${MOD_ID}.chest_upgrader.desc").withStyle(ChatFormatting.DARK_GRAY)
|
||||
private val DESCRIPTION2= TranslatableComponent("item.${MOD_ID}.chest_upgrader.desc2").withStyle(ChatFormatting.DARK_GRAY)
|
||||
|
||||
fun onEntityInteract(event: PlayerInteractEvent.EntityInteract) {
|
||||
if (event.target !is MinecartChest) return
|
||||
|
||||
val offhand = if (event.entity.getItemInHand(InteractionHand.MAIN_HAND).item is ChestUpgraderItem) {
|
||||
InteractionHand.OFF_HAND
|
||||
} else if (event.entity.getItemInHand(InteractionHand.OFF_HAND).item is ChestUpgraderItem) {
|
||||
InteractionHand.MAIN_HAND
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
val stack = event.entity.getItemInHand(offhand)
|
||||
if (stack.isEmpty || stack.item !is MinecartCargoCrateItem) return
|
||||
val color = (stack.item as MinecartCargoCrateItem).color
|
||||
|
||||
val cart = event.target as MinecartChest
|
||||
if (cart.containerSize > 54) return
|
||||
|
||||
event.level.levelEvent(event.entity, 2001, event.pos, Block.getId(cart.blockStateOn))
|
||||
|
||||
if (event.level is ServerLevel) {
|
||||
val level = event.level as ServerLevel
|
||||
|
||||
val pos = cart.position()
|
||||
val delta = cart.deltaMovement
|
||||
|
||||
val type = MEntityTypes.CARGO_CRATE_MINECARTS[color] ?: return
|
||||
val newCart = MinecartCargoCrate(type, color, level, pos.x, pos.y, pos.z)
|
||||
newCart.xRot = cart.xRot
|
||||
newCart.yRot = cart.yRot
|
||||
newCart.deltaMovement = delta
|
||||
newCart.customName = if (stack.hasCustomHoverName()) stack.hoverName else cart.customName
|
||||
|
||||
val contents = Int2ObjectArrayMap<ItemStack>(cart.containerSize)
|
||||
for (i in 0 until cart.containerSize) {
|
||||
contents.put(i, cart.getItem(i))
|
||||
}
|
||||
cart.clearContent()
|
||||
|
||||
(cart as AbstractMinecart).destroy(cart.damageSources().genericKill())
|
||||
level.addFreshEntity(newCart)
|
||||
level.gameEvent(GameEvent.ENTITY_PLACE, event.pos, GameEvent.Context.of(event.entity, level.getBlockState(event.pos.below())))
|
||||
|
||||
if (!event.entity.isCreative)
|
||||
stack.shrink(1)
|
||||
|
||||
for (i in 0 until contents.size) {
|
||||
newCart[i] = contents.get(i)
|
||||
}
|
||||
|
||||
event.cancellationResult = InteractionResult.SUCCESS
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user