Skip to content

Commit 5dd223b

Browse files
authored
fix(appeng): use long instead of int for item sizes (#183)
1 parent bd5b1d7 commit 5dd223b

4 files changed

Lines changed: 36 additions & 32 deletions

File tree

src/main/scala/li/cil/oc/integration/appeng/AEStackFactory.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package li.cil.oc.integration.appeng
33
import appeng.api.storage.data.{IAEStack, IAEStackType}
44
import appeng.util.item.{AEFluidStack, AEItemStack}
55
import li.cil.oc.api.driver.Converter
6+
import li.cil.oc.integration.util.MapUtils.MapWrapper
67
import li.cil.oc.integration.vanilla.{ConverterFluidStack, ConverterItemStack}
78

89
import java.util
@@ -24,22 +25,32 @@ object AEStackFactory {
2425

2526
def parse[T <: IAEStack[T]](map: util.Map[_, _])(implicit tag: ClassTag[T]): T = {
2627
classRegistry.get(tag.runtimeClass) match {
27-
case Some(entry) => entry.parser(map).asInstanceOf[T]
28+
case Some(entry) =>
29+
val result = entry.parser(map).asInstanceOf[T]
30+
result.setStackSize(map.getLong("size").get)
31+
result
2832
case None => throw new UnregisteredAETypeException(s"Type ${tag.runtimeClass} hasn't been registered");
2933
}
3034
}
3135

3236
def parse(key: String, map: util.Map[_, _]): IAEStack[_] = {
3337
idRegistry.get(key) match {
34-
case Some(entry) => entry.parser(map)
38+
case Some(entry) =>
39+
val result = entry.parser(map)
40+
result.setStackSize(map.getLong("size").get)
41+
result
3542
case None => throw new UnregisteredAETypeException(s"Type $key hasn't been registered");
3643
}
3744
}
3845

3946
def convert(stack: IAEStack[_], map: util.Map[AnyRef, AnyRef]): Unit = {
40-
classRegistry.get(stack.getClass) match {
41-
case Some(entry) => entry.converter(stack, map)
42-
case None => throw new UnregisteredAETypeException(s"Type ${stack.getClass} hasn't been registered");
47+
val id = stack.getStackType.getId
48+
idRegistry.get(id) match {
49+
case Some(entry) =>
50+
entry.converter(stack, map)
51+
map.put("size", Long.box(stack.getStackSize))
52+
map.put("isCraftable", Boolean.box(stack.isCraftable))
53+
case None => throw new UnregisteredAETypeException(s"Type ${id} hasn't been registered");
4354
}
4455
}
4556

src/main/scala/li/cil/oc/integration/appeng/ModAppEng.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ object ModAppEng extends ModProxy {
3737
Driver.add(DriverBlockInterface)
3838
Driver.add(DriverUpgradeAE)
3939

40-
Driver.add(ConverterAEItemStack)
41-
Driver.add(ConverterAEFluidStack)
40+
Driver.add(ConverterAEStack)
4241
Driver.add(new ConverterCellInventory)
4342
Driver.add(ConverterDimensionalCoord)
4443
Driver.add(ConverterPattern)

src/main/scala/li/cil/oc/integration/appeng/NetworkControl.scala

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import li.cil.oc.integration.ae2fc.Ae2FcUtil
2626
import li.cil.oc.integration.appeng.NetworkControl._
2727
import li.cil.oc.integration.appeng.internal.SubscriptionBase
2828
import li.cil.oc.integration.ec.ECUtil
29-
import li.cil.oc.server.driver.Registry
30-
import li.cil.oc.util.{AE2Bridge, DatabaseAccess}
3129
import li.cil.oc.util.ExtendedArguments._
3230
import li.cil.oc.util.ExtendedNBT._
3331
import li.cil.oc.util.ResultWrapper._
32+
import li.cil.oc.util.{AE2Bridge, DatabaseAccess}
3433
import net.minecraft.item.{Item, ItemStack}
3534
import net.minecraft.nbt.{JsonToNBT, NBTTagCompound}
3635
import net.minecraft.tileentity.TileEntity
@@ -189,7 +188,7 @@ trait NetworkControl[AETile >: Null <: TileEntity with IGridProxyable with IActi
189188
}
190189
DatabaseAccess.withDatabase(node, args.checkString(1), database => {
191190
val items = allItems
192-
.collect { case aeItem if matches(convert(aeItem, tile), filter) => aePotential(aeItem, tile) }.toArray
191+
.collect { case aeItem if matches(convert(aeItem, tile), filter) => aeItem }.toArray
193192
val offset = args.optSlot(database.data, 2, 0)
194193
val count = args.optInteger(3, Int.MaxValue) min (database.size - offset) min items.length
195194
var slot = offset
@@ -641,32 +640,28 @@ object NetworkControl extends AETypes {
641640
}
642641
}
643642

644-
private def aePotential(aeItem: AEStack, tile: TileEntity with IGridProxyable): AEStack = {
645-
if (aeItem.getStackSize > 0 || !aeItem.isCraftable)
646-
aeItem
647-
else
648-
asCraft(aeItem, tile)
649-
}
650-
651-
private def hashConvert(value: java.util.HashMap[_, _]) = {
652-
val hash = new java.util.HashMap[String, AnyRef]
653-
value.collect { case (k: String, v: AnyRef) => hash += k -> v }
643+
private def hashConvert(value: java.util.HashMap[AnyRef, AnyRef]) = {
644+
val hash = new java.util.HashMap[String, AnyRef](((value.size() + 1) / 0.75).toInt)
645+
val it = value.entrySet().iterator()
646+
while (it.hasNext) {
647+
val entry = it.next()
648+
val k = entry.getKey
649+
val v = entry.getValue
650+
k match {
651+
case str: String =>
652+
hash.put(str, v)
653+
case _ =>
654+
}
655+
}
654656
hash
655657
}
656658

657659
def convert(aeItem: AEStack, tile: TileEntity with IGridProxyable): java.util.HashMap[String, AnyRef] = {
658-
val potentialItem = aePotential(aeItem, tile)
659-
val result = Registry.convert(Array[AnyRef](potentialItem))
660-
.collect { case hash: java.util.HashMap[_, _] => hashConvert(hash) }
661-
if (result.length > 0) {
662-
val hash = result(0)
663-
// it would have been nice to put these fields in a registry convert
664-
// but the potential ae item needs the tile and position data
665-
hash.update("size", Long.box(aeItem.getStackSize))
666-
hash.update("isCraftable", Boolean.box(aeItem.isCraftable))
667-
return hash
660+
val converted = new java.util.HashMap[AnyRef, AnyRef]()
661+
try AEStackFactory.convert(aeItem, converted) catch {
662+
case t: Throwable => OpenComputers.log.warn("Type converter threw an exception.", t)
668663
}
669-
null
664+
hashConvert(converted)
670665
}
671666

672667
private def loadController(nbt: NBTTagCompound, f: TileEntity with IGridProxyable with IActionHost => Unit): Unit = {

src/main/scala/li/cil/oc/integration/thaumicenergistics/ModThaumicEnergistics.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ object ModThaumicEnergistics extends ModProxy {
1111
override def initialize(): Unit = {
1212
AEStackFactory.register[AEEssentiaStack](AEEssentiaStackType.ESSENTIA_STACK_TYPE, ConvertAEEssentiaStack.convert, ConvertAEEssentiaStack.parse)
1313

14-
Driver.add(ConvertAEEssentiaStack)
1514
Driver.add(DriverController)
1615
Driver.add(DriverBlockInterface)
1716
Driver.add(DriverEssentiaExportBus)

0 commit comments

Comments
 (0)