Update CopyTileNbtFunction
This commit is contained in:
parent
cb85689ea9
commit
2323854705
@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.block
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectFunction
|
||||
import it.unimi.dsi.fastutil.objects.ObjectIterators
|
||||
import net.minecraft.ChatFormatting
|
||||
import net.minecraft.Util
|
||||
import net.minecraft.core.BlockPos
|
||||
@ -13,7 +12,6 @@ import net.minecraft.core.particles.DustParticleOptions
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.util.RandomSource
|
||||
import net.minecraft.world.Containers
|
||||
import net.minecraft.world.InteractionHand
|
||||
import net.minecraft.world.InteractionResult
|
||||
import net.minecraft.world.MenuProvider
|
||||
import net.minecraft.world.entity.LivingEntity
|
||||
@ -21,7 +19,6 @@ import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.item.Item
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.item.TooltipFlag
|
||||
import net.minecraft.world.level.BlockGetter
|
||||
import net.minecraft.world.level.Level
|
||||
import net.minecraft.world.level.block.Block
|
||||
import net.minecraft.world.level.block.EntityBlock
|
||||
@ -45,7 +42,6 @@ import ru.dbotthepony.mc.otm.core.math.component1
|
||||
import ru.dbotthepony.mc.otm.core.math.component2
|
||||
import ru.dbotthepony.mc.otm.core.math.component3
|
||||
import ru.dbotthepony.mc.otm.once
|
||||
import ru.dbotthepony.mc.otm.registry.MDataComponentTypes
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.function.Function
|
||||
import java.util.function.Supplier
|
||||
@ -186,7 +182,6 @@ open class MatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : Block(pro
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_DEPRECATION")
|
||||
override fun getMenuProvider(blockState: BlockState, level: Level, blockPos: BlockPos): MenuProvider? {
|
||||
if (this is EntityBlock) {
|
||||
val tile = level.getBlockEntity(blockPos)
|
||||
@ -207,7 +202,6 @@ open class MatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : Block(pro
|
||||
neighbourPos: BlockPos,
|
||||
movedByPiston: Boolean
|
||||
) {
|
||||
@Suppress("DEPRECATION")
|
||||
super.neighborChanged(state, level, pos, neighbour, neighbourPos, movedByPiston)
|
||||
|
||||
if (this is EntityBlock && !level.isClientSide) {
|
||||
@ -225,7 +219,6 @@ open class MatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : Block(pro
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_DEPRECATION")
|
||||
override fun onRemove(
|
||||
oldBlockState: BlockState,
|
||||
level: Level,
|
||||
@ -246,7 +239,6 @@ open class MatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : Block(pro
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
super.onRemove(oldBlockState, level, blockPos, newBlockState, movedByPiston)
|
||||
}
|
||||
|
||||
@ -264,12 +256,12 @@ open class MatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : Block(pro
|
||||
|
||||
override fun appendHoverText(
|
||||
itemStack: ItemStack,
|
||||
p_339606_: Item.TooltipContext,
|
||||
context: Item.TooltipContext,
|
||||
components: MutableList<Component>,
|
||||
tooltipType: TooltipFlag
|
||||
) {
|
||||
super.appendHoverText(itemStack, p_339606_, components, tooltipType)
|
||||
tooltips.assemble(itemStack, p_339606_, components)
|
||||
super.appendHoverText(itemStack, context, components, tooltipType)
|
||||
tooltips.assemble(itemStack, context, components)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -1,33 +1,32 @@
|
||||
package ru.dbotthepony.mc.otm.block.entity
|
||||
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.google.gson.JsonParser
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType
|
||||
import net.minecraft.core.BlockPos
|
||||
import net.minecraft.core.Direction
|
||||
import net.minecraft.core.HolderLookup
|
||||
import net.minecraft.world.level.block.state.BlockState
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.NbtOps
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.network.chat.ComponentSerialization
|
||||
import net.minecraft.world.MenuProvider
|
||||
import net.minecraft.world.entity.player.Inventory
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.level.Level
|
||||
import net.minecraft.world.level.block.entity.BlockEntity
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType
|
||||
import net.minecraft.world.level.block.state.BlockState
|
||||
import net.neoforged.neoforge.capabilities.Capabilities
|
||||
import net.neoforged.neoforge.capabilities.ICapabilityProvider
|
||||
import net.neoforged.neoforge.fluids.FluidStack
|
||||
import net.neoforged.neoforge.fluids.capability.IFluidHandler
|
||||
import net.neoforged.neoforge.items.IItemHandler
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import ru.dbotthepony.kommons.util.getValue
|
||||
import ru.dbotthepony.kommons.util.setValue
|
||||
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
||||
import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.item.CombinedItemHandler
|
||||
import ru.dbotthepony.mc.otm.capability.item.EmptyItemHandler
|
||||
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
||||
import ru.dbotthepony.mc.otm.capability.item.UnmodifiableItemHandler
|
||||
import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.moveBetweenSlots
|
||||
import ru.dbotthepony.mc.otm.capability.moveEnergy
|
||||
import ru.dbotthepony.mc.otm.capability.moveFluid
|
||||
@ -36,10 +35,10 @@ import ru.dbotthepony.mc.otm.core.getValue
|
||||
import ru.dbotthepony.mc.otm.core.immutableMap
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
import ru.dbotthepony.mc.otm.core.math.RelativeSide
|
||||
import ru.dbotthepony.mc.otm.core.nbt.getJson
|
||||
import ru.dbotthepony.mc.otm.core.nbt.putJson
|
||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||
import ru.dbotthepony.mc.otm.core.util.ITickable
|
||||
import ru.dbotthepony.mc.otm.once
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
|
||||
/**
|
||||
* Device block entity base, implementing [MenuProvider] and [IRedstoneControlled], and also tracks custom display name
|
||||
@ -75,12 +74,23 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
|
||||
super.saveShared(nbt, registry)
|
||||
|
||||
if (customDisplayName != null)
|
||||
nbt.putJson("Name", JsonParser.parseString(Component.Serializer.toJson(customDisplayName!!, registry)))
|
||||
ComponentSerialization
|
||||
.CODEC
|
||||
.encodeStart(registry.createSerializationContext(NbtOps.INSTANCE), customDisplayName)
|
||||
.resultOrPartial { LOGGER.error("Unable to serialize custom name: {}", it) }
|
||||
.ifPresent { nbt["Name"] = it }
|
||||
}
|
||||
|
||||
override fun loadAdditional(nbt: CompoundTag, registry: HolderLookup.Provider) {
|
||||
super.loadAdditional(nbt, registry)
|
||||
customDisplayName = nbt.getJson("Name")?.let { Component.Serializer.fromJson(it, registry) }
|
||||
|
||||
if ("Name" in nbt) {
|
||||
customDisplayName = ComponentSerialization
|
||||
.CODEC
|
||||
.decode(registry.createSerializationContext(NbtOps.INSTANCE), nbt["Name"]!!)
|
||||
.resultOrPartial { LOGGER.error("Unable to deserialize custom name: {}", it) }
|
||||
.getOrNull()?.first
|
||||
}
|
||||
}
|
||||
|
||||
override fun setLevel(level: Level) {
|
||||
@ -699,6 +709,8 @@ abstract class MatteryDeviceBlockEntity(blockEntityType: BlockEntityType<*>, blo
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val LOGGER = LogManager.getLogger()
|
||||
|
||||
private fun determineDefaultMode(input: IItemHandler?, output: IItemHandler?, inputOutput: IItemHandler?, battery: IItemHandler?, side: RelativeSide): ItemHandlerMode {
|
||||
if (side == RelativeSide.BACK && battery != null) {
|
||||
return ItemHandlerMode.BATTERY
|
||||
|
@ -4,20 +4,21 @@ import com.google.common.collect.ImmutableList
|
||||
import com.mojang.serialization.Codec
|
||||
import com.mojang.serialization.MapCodec
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder
|
||||
import net.minecraft.core.component.DataComponents
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.world.item.BlockItem
|
||||
import net.minecraft.network.chat.ComponentSerialization
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.item.component.CustomData
|
||||
import net.minecraft.world.level.storage.loot.LootContext
|
||||
import net.minecraft.world.level.storage.loot.functions.LootItemFunction
|
||||
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType
|
||||
import net.minecraft.world.level.storage.loot.parameters.LootContextParams
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
|
||||
import ru.dbotthepony.mc.otm.core.nbt.getJson
|
||||
import ru.dbotthepony.mc.otm.core.fromNbt
|
||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||
import ru.dbotthepony.mc.otm.core.stream
|
||||
import ru.dbotthepony.mc.otm.registry.MItemFunctionTypes
|
||||
import java.util.Optional
|
||||
import java.util.*
|
||||
import java.util.stream.Stream
|
||||
|
||||
class CopyTileNbtFunction(filter: Stream<out String> = Stream.empty()) : LootItemFunction, LootItemFunction.Builder {
|
||||
@ -28,43 +29,49 @@ class CopyTileNbtFunction(filter: Stream<out String> = Stream.empty()) : LootIte
|
||||
filter.forEach(this)
|
||||
}*/ filter.collect(ImmutableList.toImmutableList())
|
||||
|
||||
override fun apply(t: ItemStack, u: LootContext): ItemStack {
|
||||
val blockEntity = u.getParamOrNull(LootContextParams.BLOCK_ENTITY) ?: return t
|
||||
val result = t.tagNotNull[BlockItem.BLOCK_ENTITY_TAG] as? CompoundTag
|
||||
|
||||
override fun apply(itemStack: ItemStack, context: LootContext): ItemStack {
|
||||
val blockEntity = context.getParamOrNull(LootContextParams.BLOCK_ENTITY) ?: return itemStack
|
||||
val result = itemStack[DataComponents.BLOCK_ENTITY_DATA] ?: CustomData.EMPTY
|
||||
val data: CompoundTag
|
||||
|
||||
if (blockEntity is MatteryBlockEntity) {
|
||||
data = CompoundTag()
|
||||
blockEntity.saveShared(data)
|
||||
// TODO: 1.21 added components, which are present even on blockentities; these are lost
|
||||
blockEntity.saveShared(data, context.level.registryAccess())
|
||||
} else {
|
||||
data = blockEntity.saveWithoutMetadata()
|
||||
data = blockEntity.saveWithoutMetadata(context.level.registryAccess())
|
||||
}
|
||||
|
||||
for (k in filter) {
|
||||
data.remove(k)
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
t.tagNotNull[BlockItem.BLOCK_ENTITY_TAG] = data
|
||||
if (result.isEmpty) {
|
||||
itemStack[DataComponents.BLOCK_ENTITY_DATA] = CustomData.of(data)
|
||||
} else {
|
||||
val copy = result.copyTag()
|
||||
|
||||
for (k in data.allKeys) {
|
||||
result[k] = data[k]!!
|
||||
copy[k] = data[k]!!
|
||||
}
|
||||
|
||||
itemStack[DataComponents.BLOCK_ENTITY_DATA] = CustomData.of(copy)
|
||||
}
|
||||
|
||||
if ("Name" in data && "display" !in t.tagNotNull) {
|
||||
t.tagNotNull["display"] = CompoundTag().also {
|
||||
it["Name"] = Component.Serializer.toJson(Component.Serializer.fromJson(data.getJson("Name")!!)!!)
|
||||
if ("Name" in data && !itemStack.has(DataComponents.CUSTOM_NAME)) {
|
||||
val deserialize = ComponentSerialization.CODEC.fromNbt(data["Name"]!!)
|
||||
|
||||
if (deserialize != null) {
|
||||
itemStack[DataComponents.CUSTOM_NAME] = deserialize
|
||||
}
|
||||
|
||||
data.remove("Name")
|
||||
}
|
||||
|
||||
return t
|
||||
return itemStack
|
||||
}
|
||||
|
||||
override fun getType(): LootItemFunctionType {
|
||||
override fun getType(): LootItemFunctionType<CopyTileNbtFunction> {
|
||||
return MItemFunctionTypes.COPY_TILE_NBT
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user