repo for my hex addons :3
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

try handling storages

+40 -18
+40 -18
src/main/scala/org/eu/net/pool/hexic/Hexic.scala
··· 49 49 import net.fabricmc.fabric.api.event.{Event, EventFactory} 50 50 import net.fabricmc.fabric.api.item.v1.FabricItemSettings 51 51 import net.fabricmc.fabric.api.transfer.v1.fluid.{FluidConstants, FluidVariant} 52 - import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant 53 - import net.fabricmc.fabric.api.transfer.v1.storage.TransferVariant 52 + import net.fabricmc.fabric.api.transfer.v1.item.{ItemStorage, ItemVariant} 53 + import net.fabricmc.fabric.api.transfer.v1.storage.{Storage, TransferVariant} 54 54 import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant 55 55 import net.fabricmc.fabric.api.transfer.v1.transaction.{Transaction, TransactionContext} 56 56 import net.fabricmc.loader.api.FabricLoader ··· 72 72 import net.minecraft.server.world.ServerWorld 73 73 import net.minecraft.text.{HoverEvent, LiteralTextContent, MutableText, Style, Text, TextColor, TextContent, Texts} 74 74 import net.minecraft.util.dynamic.Codecs 75 - import net.minecraft.util.math.{BlockPos, ChunkPos, Direction, Vec3d} 75 + import net.minecraft.util.math.{BlockPos, ChunkPos, Direction, Vec3d, Box as MCBox} 76 76 import net.minecraft.util.{ActionResult, Arm, ClickType, DyeColor, Formatting, Hand, Identifier, Rarity, TypedActionResult, Util, Uuids, WorldSavePath} 77 77 import net.minecraft.world.biome.Biome 78 78 import net.minecraft.world.{BlockView, TeleportTarget, World} ··· 94 94 import java.{lang, util} 95 95 import scala.annotation.unchecked.uncheckedVariance 96 96 import scala.annotation.{elidable, experimental, showAsInfix, tailrec, targetName, unused} 97 - import scala.collection.{IterableOps, IterableOnceOps} 97 + import scala.collection.{IterableOnceOps, IterableOps} 98 98 import scala.ref.WeakReference 99 - import scala.util.{Failure, Success, Try} 99 + import scala.util.{Failure, Success, Try, Using} 100 100 export scala.collection.convert.ImplicitConversions.* 101 101 import scala.collection.mutable 102 102 import scala.compiletime.summonFrom ··· 2169 2169 else if x > max then max 2170 2170 else x 2171 2171 2172 - trait InventoryView(val viewType: InventoryView.Type[?]): 2173 - def apply(idx: Int)(using CastingEnvironment): Option[SlotReference] = None 2172 + trait InventoryView(val viewType: InventoryView.Type[?]) extends InventoryView.Handler: 2174 2173 def isTruthy = true 2175 - @throws[Mishap] 2176 - def tryWithdraw(variant: TransferVariant[?], amount: Long)(using TransactionContext, CastingEnvironment): Long = 0 2177 - def entities(using TransactionContext): Set[Entity] = Set() 2178 - @throws[Mishap] 2179 - def teleportEntity(ent: Entity)(using TransactionContext, CastingEnvironment): Boolean = false 2180 2174 def serialize: NbtCompound = NbtCompound().tap(_.putString("id", InventoryView.registry.getId(viewType).toString)) 2181 - 2182 2175 object InventoryView extends Registrar[InventoryView.Type[?]]("inventory"): 2183 2176 trait Type[+T <: InventoryView]: 2184 2177 def deserialize(data: NbtCompound)(using ServerWorld): Option[T] 2178 + trait Handler: 2179 + def apply(idx: Int)(using CastingEnvironment): Option[SlotReference] = None 2180 + def tryExtract(variant: TransferVariant[?], amount: Long)(using TransactionContext, CastingEnvironment): Long = 0 2181 + def tryWithdraw(variant: TransferVariant[?], amount: Long)(using TransactionContext, CastingEnvironment): Long = 0 2182 + def capacity(variant: TransferVariant[?])(using TransactionContext, CastingEnvironment): Long = 2183 + Using(summon[TransactionContext].openNested()): tx => 2184 + given TransactionContext = tx 2185 + tryExtract(variant, Long.MaxValue) 2186 + match 2187 + case Success(n) => n 2188 + case Failure(ex) => 2189 + given_Logger.error("capacity", ex) 2190 + 0L 2191 + def entities(using TransactionContext): Set[Entity] = Set() 2192 + @throws[Mishap] 2193 + def teleportEntity(ent: Entity)(using TransactionContext, CastingEnvironment): Boolean = false 2185 2194 object Events: 2186 - val forEntity: Event[Entity => ServerWorld ?=> Seq[InventoryView]] = EventFactory.createArrayBacked[Entity => ServerWorld ?=> Seq[InventoryView]](classOf, _ => Seq(), fns => e => fns.flatMap(_(e))) 2187 - val forBlock: Event[(BlockPos, BlockState) => ServerWorld ?=> Seq[InventoryView]] = EventFactory.createArrayBacked[(BlockPos, BlockState) => ServerWorld ?=> Seq[InventoryView]](classOf, (_, _) => Seq(), fns => (pos, state) => fns.flatMap(_(pos, state))) 2188 - abstract class OfMerged(viewType: InventoryView.Type[?], views: => Seq[InventoryView]) extends InventoryView(viewType): 2195 + val forEntity: Event[Entity => ServerWorld ?=> Seq[Handler]] = EventFactory.createArrayBacked(classOf, _ => Seq(), fns => e => fns.flatMap(_(e))) 2196 + val forBlock: Event[(BlockPos, BlockState) => ServerWorld ?=> Seq[Handler]] = EventFactory.createArrayBacked(classOf, (_, _) => Seq(), fns => (pos, state) => fns.flatMap(_(pos, state))) 2197 + abstract class OfMerged(viewType: InventoryView.Type[?], views: => Seq[Handler]) extends InventoryView(viewType): 2189 2198 def getViews = views 2190 - override def isTruthy = views ∃(_.isTruthy) 2191 2199 override def apply(idx: Int)(using CastingEnvironment): Option[SlotReference] = views.collectFirst(hexicVisibilityHack.unlifted(_(idx))) 2192 2200 override def tryWithdraw(variant: TransferVariant[?], amount: Long)(using TransactionContext, CastingEnvironment): Long = LazyList.from(views).scanLeft(0L)((n, view) => view.tryWithdraw(variant, amount - n) + n).findFirstOrLast(_ >= amount).getOrElse(0) 2193 2201 override def entities(using TransactionContext) = views.flatMap(_.entities).toSet 2194 2202 override def teleportEntity(ent: Entity)(using TransactionContext, CastingEnvironment): Boolean = views.iterator∃(_.teleportEntity(ent)) 2195 2203 class OfSum private(private[InventoryView] val views: Seq[InventoryView]) extends OfMerged(typeOfSum, views): 2204 + override def isTruthy = views ∃(_.isTruthy) 2196 2205 override def serialize = 2197 2206 val c = NbtCompound() 2198 2207 val list = NbtList() ··· 2244 2253 private given typeOfEntity: InventoryView.Type[OfEntity]: 2245 2254 override def deserialize(data: NbtCompound)(using ServerWorld): Option[OfEntity] = ??? 2246 2255 private given typeOfBlock: InventoryView.Type[OfBlock]: 2247 - override def deserialize(data: NbtCompound)(using ServerWorld): Option[OfBlock] = ??? 2256 + override def deserialize(data: NbtCompound)(using ServerWorld): Option[OfBlock] = Some(data.get("p")).map(_.downcast[NbtLong].longValue).map(BlockPos.fromLong).map(OfBlock(_)) 2248 2257 private given typeOfExactEntity: InventoryView.Type[OfExactEntity]: 2249 2258 override def deserialize(data: NbtCompound)(using ServerWorld): Option[OfExactEntity] = ??? 2250 2259 registry("sum") = typeOfSum 2251 2260 registry("entity") = typeOfEntity 2252 2261 registry("block") = typeOfBlock 2253 2262 registry("exact") = typeOfExactEntity 2263 + Events.forBlock.register: (pos, state) => 2264 + val storage = ItemStorage.SIDED.find(summon, pos, null): Storage[ItemVariant] 2265 + val allowTP = state.isTransparent(summon, pos) 2266 + if storage == null then Seq() 2267 + else Seq( 2268 + new Handler: 2269 + override def tryWithdraw(variant: TransferVariant[?], amount: Long)(using TransactionContext, CastingEnvironment): Long = 2270 + variant match 2271 + case i: ItemVariant => storage.extract(i, amount, summon) 2272 + case _ => 0 2273 + override def entities(using TransactionContext): Set[Entity] = summon[World].getOtherEntities(null, MCBox.of(pos.toCenterPos, 0.5, 0.5, 0.5), _ => true).toSet 2274 + override def teleportEntity(ent: Entity)(using TransactionContext, CastingEnvironment): Boolean = super.teleportEntity(ent) 2275 + ) 2254 2276 2255 2277 object BoxedView extends IotaType[BoxedView.Instance]: 2256 2278 InventoryView