From 6681bf94387ff892dcf171c5e1edd167b1542bf9 Mon Sep 17 00:00:00 2001 From: Noktuska Date: Fri, 22 Mar 2024 03:13:41 +0100 Subject: [PATCH 1/3] Fixed Beekeeper module not working with GT IApiary --- dependencies.gradle | 25 ++- settings.gradle | 2 +- .../forestry/UpgradeBeekeeper.scala | 134 +++--------- .../forestry/UpgradeBeekeeperUtil.java | 202 ++++++++++++++++++ 4 files changed, 249 insertions(+), 114 deletions(-) create mode 100644 src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java diff --git a/dependencies.gradle b/dependencies.gradle index 4494f21a9..615507bf9 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -7,28 +7,28 @@ dependencies { compile("com.google.code.findbugs:jsr305:3.0.2") - compileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-328-GTNH:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-343-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:EnderStorage:1.5.0:dev") {transitive = false } - compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.45.84:dev") {transitive = false } - compile("com.github.GTNewHorizons:ForestryMC:4.8.4:dev") {transitive = false } - compileOnly("com.github.GTNewHorizons:Railcraft:9.15.6:dev") {transitive = false } - compile("com.github.GTNewHorizons:NotEnoughItems:2.5.23-GTNH:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.45.116:dev") {transitive = false } + compile("com.github.GTNewHorizons:ForestryMC:4.8.7:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:Railcraft:9.15.7:dev") {transitive = false } + compile("com.github.GTNewHorizons:NotEnoughItems:2.5.25-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:ForgeMultipart:1.4.8:dev") {transitive = false } compile("com.github.GTNewHorizons:CodeChickenLib:1.2.1:dev") {transitive = false } compile("com.github.GTNewHorizons:CodeChickenCore:1.2.1:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:waila:1.7.1:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:Galacticraft:3.1.3-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:TinkersMechworks:0.3.1:dev") {transitive = false } - compileOnly("com.github.GTNewHorizons:ProjectRed:4.9.4-GTNH:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:ProjectRed:4.9.5-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:BloodMagic:1.5.1:dev") {transitive = false } - compileOnly("com.github.GTNewHorizons:ThaumicEnergistics:1.6.2-GTNH:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:ThaumicEnergistics:1.6.4-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:ExtraCells2:2.5.34:dev") {transitive = false } - compileOnly('com.github.GTNewHorizons:AE2FluidCraft-Rework:1.2.19-gtnh:dev') {transitive = false } + compileOnly('com.github.GTNewHorizons:AE2FluidCraft-Rework:1.2.22-gtnh:dev') {transitive = false } compile("com.github.GTNewHorizons:EnderIO:2.7.1:dev") { - compile("com.github.GTNewHorizons:EnderCore:0.4.5:dev") + compile("com.github.GTNewHorizons:EnderCore:0.4.6:dev") transitive = false } - compileOnly("com.github.GTNewHorizons:Avaritiaddons:1.7.0-GTNH:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:Avaritiaddons:1.7.1-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:gendustry:1.7.0-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:WirelessRedstone-CBE:1.5.0:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:BuildCraft:7.1.39:dev") {transitive = false } @@ -47,7 +47,7 @@ dependencies { compileOnly("curse.maven:agricraft-225635:2284133") {transitive = false } compileOnly("curse.maven:stargatetech-2-226769:2230351") {transitive = false } - compileOnlyApi("com.github.GTNewHorizons:Angelica:1.0.0-alpha31:api") { transitive = false } + compileOnlyApi("com.github.GTNewHorizons:Angelica:1.0.0-alpha35:api") { transitive = false } compileOnly(deobf("https://immibis.com/mcmoddl/files/redlogic-59.1.13.jar")) compileOnly files("dependencies/ic2classic-api.zip") //curseforge one does NOT work ... @@ -62,4 +62,7 @@ dependencies { testCompile "org.mockito:mockito-all:1.10.19" testCompile "org.scalactic:scalactic_2.11:2.2.6" testCompile "org.scalatest:scalatest_2.11:2.2.6" + + compileOnly("com.github.GTNewHorizons:ModularUI:1.1.42:dev") {transitive = false} + compileOnly("com.github.GTNewHorizons:StructureLib:1.3.0:dev") {transitive = false} } diff --git a/settings.gradle b/settings.gradle index 37468c7ad..c52d518d3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,7 +17,7 @@ pluginManagement { } plugins { - id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.15' + id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.19' } diff --git a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala index d19038550..113bdff7b 100644 --- a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala +++ b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala @@ -1,8 +1,6 @@ package li.cil.oc.integration.forestry import java.util - -import cpw.mods.fml.common.Loader import forestry.api.apiculture.{BeeManager, IBeeHousing} import forestry.plugins.PluginApiculture import li.cil.oc.Constants @@ -15,10 +13,6 @@ import li.cil.oc.server.component.result import li.cil.oc.server.component.traits.{NetworkAware, SideRestricted, WorldAware} import li.cil.oc.util.{BlockPosition, InventoryUtils} import li.cil.oc.util.ExtendedArguments.extendedArguments -import li.cil.oc.util.ExtendedWorld._ -import net.bdew.gendustry.api.blocks.IIndustrialApiary -import net.minecraft.inventory.ISidedInventory -import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTTagCompound import net.minecraftforge.common.util.ForgeDirection @@ -41,49 +35,37 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr override def position: BlockPosition = BlockPosition(host) override protected def checkSideForAction(args: Arguments, n: Int): ForgeDirection = args.checkSideAny(n) - private def withApiary(side: ForgeDirection, f: IBeeHousing => Array[AnyRef]) = - if (host.mainInventory.getSizeInventory > 0) { - position.world.get.getTileEntity(position.offset(side)) match { - case housing: IBeeHousing => f(housing) - case _ => result(false, "Not facing an apiary") - } - } - else result(false, "No inventory?") @Callback(doc = """function(side:number):boolean -- Swap the queen from the selected slot with the apiary at the specified side.""") def swapQueen(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) - withApiary(facing, housing => { - val selected = host.mainInventory.getStackInSlot(host.selectedSlot) - val oldQueen = housing.getBeeInventory.getQueen - housing.getBeeInventory.setQueen(selected) - host.mainInventory.setInventorySlotContents(host.selectedSlot, oldQueen) - result(true) - }) + val pos = position.offset(facing) + result(UpgradeBeekeeperUtil.swapQueen(pos, host.mainInventory(), host.selectedSlot())) } @Callback(doc = """function(side:number):boolean -- Swap the drone from the selected slot with the apiary at the specified side.""") def swapDrone(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) - withApiary(facing, housing => { - val selected = host.mainInventory.getStackInSlot(host.selectedSlot) - val oldQueen = housing.getBeeInventory.getDrone - housing.getBeeInventory.setDrone(selected) - host.mainInventory.setInventorySlotContents(host.selectedSlot, oldQueen) - result(true) - }) + val pos = position.offset(facing) + result(UpgradeBeekeeperUtil.swapDrone(pos, host.mainInventory(), host.selectedSlot())) } @Callback(doc = """function(side:number):number -- Get current progress percent for the apiary at the specified side.""") def getBeeProgress(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) - withApiary(facing, housing => result(housing.getBeekeepingLogic.getBeeProgressPercent)) + val housing = UpgradeBeekeeperUtil.getBeeHousingAt(position.offset(facing)) + if (housing == null) + return result(false, "No bee housing found") + result(housing.getBeekeepingLogic.getBeeProgressPercent) } @Callback(doc = """function(side:number):boolean -- Checks if current bee in the apiary at the specified side can work now.""") def canWork(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) - withApiary(facing, housing => result(housing.getBeekeepingLogic.canWork)) + val housing = UpgradeBeekeeperUtil.getBeeHousingAt(position.offset(facing)) + if (housing == null) + return result(false, "No bee housing found") + result(housing.getBeekeepingLogic.canWork) } @Callback(doc = """function(honeyslot:number):boolean -- Analyzes bee in selected slot, uses honey from the specified slot.""") @@ -112,86 +94,34 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr result(true) } - private def findSameStack(upgrade: ItemStack, inv: ISidedInventory):Int = { - for (slot <- 2 to 5) { - val u = inv.getStackInSlot(slot) - if (u != null && u.getItem == upgrade.getItem && upgrade.getItemDamage == u.getItemDamage) - return slot - } - 0 - } - - private def findEmptySlot(inv: ISidedInventory):Int = { - for (slot <- 2 to 5) { - if (inv.getStackInSlot(slot) == null) - return slot - } - 0 - } - - @Callback(doc = """function(side:number):boolean -- Tries to add industrial upgrade from the selected slot to industrial apiary at the given side.""") + @Callback(doc = """function(side:number[,amount:number]):number -- Tries to add amount many or all industrial upgrades from the selected slot to industrial apiary at the given side.""") def addIndustrialUpgrade(context: Context, args: Arguments): Array[AnyRef] = { - if (!Loader.isModLoaded("gendustry")) return result(false, "Gendustry not loaded!") - val inventory = host.mainInventory val facing = checkSideForAction(args, 0) - val upgrade = inventory.getStackInSlot(host.selectedSlot) - if (upgrade == null) return result(false, "No upgrade in selected slot") - position.world.get.getTileEntity(position.offset(facing)) match { - case ia : IIndustrialApiary => - val inv = ia.asInstanceOf[ISidedInventory] - if (!inv.isItemValidForSlot(2, upgrade)) - return result(false, "Upgrade does not fit") - - var slot = findSameStack(upgrade, inv) - if (slot == 0) - slot = findEmptySlot(inv) - - val u = inv.getStackInSlot(slot) - if (u == null) - inv.setInventorySlotContents(slot, upgrade) - else { - u.stackSize += upgrade.stackSize - inv.setInventorySlotContents(slot, u) - } - inventory.setInventorySlotContents(host.selectedSlot, null) - result(true) - - case _ => result(false, "Not facing an industrial apiary") - } + val pos = position.offset(facing) + var amount = 999 + if (args.count() > 1) + amount = args.checkInteger(1) + result(UpgradeBeekeeperUtil.addIndustrialUpgrade(pos, host.mainInventory(), host.selectedSlot(), amount)) } @Callback(doc = """function(side:number, slot: number):table -- Get industrial upgrade in the given slot of the industrial apiary at the given side.""") def getIndustrialUpgrade(context: Context, args: Arguments): Array[AnyRef] = { - if (!Loader.isModLoaded("gendustry")) return result(Unit, "Gendustry not loaded!") val facing = checkSideForAction(args, 0) - position.world.get.getTileEntity(position.offset(facing)) match { - case ia: IIndustrialApiary => - val inv = ia.asInstanceOf[ISidedInventory] - val slot = args.checkInteger(1) + 1 - if (slot < 2 || slot > 5) - return result(Unit, "Wrong slot index (should be 1-4)") - - result(inv.getStackInSlot(slot)) - - case _ => result(Unit, "Not facing an industrial apiary") - } + val pos = position.offset(facing) + val slot = args.checkInteger(1) + if (slot < 1 || slot > 4) + return result(Unit, "Wrong slot index (should be 1-4)") + result(UpgradeBeekeeperUtil.getIndustrialUpgrade(pos, slot)) } - @Callback(doc = """function(side:number, slot: number):boolean -- Remove industrial upgrade from the given slot of the industrial apiary at the given side.""") + @Callback(doc = """function(side:number, slot: number[, amount: number]):boolean -- Remove industrial upgrade from the given slot of the industrial apiary at the given side.""") def removeIndustrialUpgrade(context: Context, args: Arguments): Array[AnyRef] = { - if (!Loader.isModLoaded("gendustry")) return result(false, "Gendustry not loaded!") val facing = checkSideForAction(args, 0) - position.world.get.getTileEntity(position.offset(facing)) match { - case ia: IIndustrialApiary => - val inv = ia.asInstanceOf[ISidedInventory] - val slot = args.checkInteger(1) + 1 - if (slot < 2 || slot > 5) - return result(false, "Wrong slot index (should be 1-4)") - - val u = inv.getStackInSlot(slot) - val res = InventoryUtils.insertIntoInventory(u, host.mainInventory) - inv.setInventorySlotContents(slot, if (u.stackSize > 0) u else null) - result(res) - - case _ => result(false, "Not facing an industrial apiary") - } + val pos = position.offset(facing) + val slot = args.checkInteger(1) + var amount = 999 + if (args.count() > 2) + amount = args.checkInteger(2) + if (slot < 1 || slot > 4) + return result(false, "Wrong slot index (should be 1-4)") + result(UpgradeBeekeeperUtil.removeIndustrialUpgrade(pos, host.mainInventory(), host.selectedSlot(), slot, amount)) } } diff --git a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java new file mode 100644 index 000000000..ad0177f28 --- /dev/null +++ b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java @@ -0,0 +1,202 @@ +package li.cil.oc.integration.forestry; + +import forestry.api.apiculture.IBeeHousing; +import gregtech.api.metatileentity.BaseMetaTileEntity; +import gregtech.api.util.GT_ApiaryUpgrade; +import gregtech.api.util.GT_Utility; +import gregtech.common.tileentities.machines.basic.GT_MetaTileEntity_IndustrialApiary; +import li.cil.oc.util.BlockPosition; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +/** + * Wrapper class for UpgradeBeekeeper item. Scala doesn't seem to like GT machines, + * so we wrap most functionality in a static java class + * */ +public final class UpgradeBeekeeperUtil { + + // First upgrade slot index of Industrial Apiaries because it's private for some reason + private static final int UPGRADE_INDEX = 7; + private UpgradeBeekeeperUtil() {} + + /** Returns an IBeeHousing TileEntity at position pos. Can be an Industrial Apiary */ + public static IBeeHousing getBeeHousingAt(BlockPosition pos) { + if (pos.world().isEmpty()) + return null; + + World world = pos.world().get(); + TileEntity te = world.getTileEntity(pos.x(), pos.y(), pos.z()); + if (te == null) + return null; + if (te instanceof IBeeHousing) + return (IBeeHousing)te; + // Scala doesn't compile if these checks are of the form (a instanceof B b) + if (!(te instanceof BaseMetaTileEntity)) + return null; + BaseMetaTileEntity mte = (BaseMetaTileEntity)te; + if (!(mte.getMetaTileEntity() instanceof GT_MetaTileEntity_IndustrialApiary)) + return null; + return (IBeeHousing)mte.getMetaTileEntity(); + } + + /** Returns a Tile Entity for Industrial Apiaries at position pos, or null if none exist */ + public static GT_MetaTileEntity_IndustrialApiary getGTIApiaryAt(BlockPosition pos) { + if (pos.world().isEmpty()) + return null; + + World world = pos.world().get(); + TileEntity te = world.getTileEntity(pos.x(), pos.y(), pos.z()); + if (te == null) + return null; + if (!(te instanceof BaseMetaTileEntity)) + return null; + BaseMetaTileEntity mte = (BaseMetaTileEntity)te; + if (!(mte.getMetaTileEntity() instanceof GT_MetaTileEntity_IndustrialApiary)) + return null; + return (GT_MetaTileEntity_IndustrialApiary)mte.getMetaTileEntity(); + } + + public static boolean swapQueen(BlockPosition pos, IInventory hostInv, int slot) { + IBeeHousing housing = getBeeHousingAt(pos); + if (housing == null) + return false; + + ItemStack newQueen = hostInv.getStackInSlot(slot); + ItemStack oldQueen = housing.getBeeInventory().getQueen(); + housing.getBeeInventory().setQueen(newQueen); + hostInv.setInventorySlotContents(slot, oldQueen); + return true; + } + + public static boolean swapDrone(BlockPosition pos, IInventory hostInv, int slot) { + IBeeHousing housing = getBeeHousingAt(pos); + if (housing == null) + return false; + + ItemStack newDrone = hostInv.getStackInSlot(slot); + ItemStack oldDrone = housing.getBeeInventory().getDrone(); + housing.getBeeInventory().setDrone(newDrone); + hostInv.setInventorySlotContents(slot, oldDrone); + return true; + } + + public static int addIndustrialUpgrade(BlockPosition pos, IInventory hostInv, int slot, int amount) { + GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); + if (iapiary == null) + return 0; + + ItemStack stackToInstall = hostInv.getStackInSlot(slot); + if (stackToInstall == null || !GT_ApiaryUpgrade.isUpgrade(stackToInstall)) + return 0; + amount = Math.min(amount, stackToInstall.stackSize); + + for (int i = UPGRADE_INDEX; i < UPGRADE_INDEX + 4; i++) { + // isItemValidForSlot ensures merging existing stacks + if (!iapiary.isItemValidForSlot(i, stackToInstall)) + continue; + + int maxStackSize = GT_ApiaryUpgrade.getUpgrade(stackToInstall).getMaxNumber(); + ItemStack stack = iapiary.getStackInSlot(i); + // Push into empty slot + if (stack == null) { + amount = Math.min(amount, maxStackSize); + iapiary.setInventorySlotContents(i, stackToInstall.splitStack(amount)); + if (stackToInstall.stackSize <= 0) + hostInv.setInventorySlotContents(slot, null); + return amount; + } + // Merge stacks + if (!GT_Utility.areStacksEqual(stackToInstall, stack)) + continue; + amount = Math.max(Math.min(amount, maxStackSize - stack.stackSize), 0); + if (amount == 0) + return 0; + stack.stackSize += amount; + stackToInstall.stackSize -= amount; + if (stackToInstall.stackSize <= 0) + hostInv.setInventorySlotContents(slot, null); + return amount; + } + return 0; + } + + public static ItemStack getIndustrialUpgrade(BlockPosition pos, int index){ + if (index < 1 || index > 4) + return null; + GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); + if (iapiary == null) + return null; + return iapiary.getStackInSlot(index - 1 + UPGRADE_INDEX); + } + + public static int removeIndustrialUpgrade(BlockPosition pos, IInventory hostInv, int slot, int index, int amount) { + if (index < 1 || index > 4) + return 0; + GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); + if (iapiary == null) + return 0; + + index = index - 1 + UPGRADE_INDEX; + ItemStack stack = iapiary.getStackInSlot(index); + if (stack == null) + return 0; + amount = Math.min(amount, stack.stackSize); + int moved = insertIntoHostInv(hostInv, slot, stack.splitStack(amount)); + // If less items were moved than planned, move the unmoved items back + stack.stackSize += (amount - moved); + if (stack.stackSize <= 0) + iapiary.setInventorySlotContents(index, null); + return moved; + } + + private static int insertIntoHostInv(IInventory hostInv, int slot, ItemStack stack) { + if (stack == null) + return 0; + + final int initialStackSize = stack.stackSize; + + // Try putting in selected slot first + insertIntoSlot(hostInv, slot, stack); + + if (stack.stackSize <= 0) + return initialStackSize; + + // Find any stacks to merge with + for (int i = 0; i < hostInv.getSizeInventory(); i++) { + ItemStack stackInSlot = hostInv.getStackInSlot(i); + if (!GT_Utility.areStacksEqual(stack, stackInSlot)) + continue; + insertIntoSlot(hostInv, i, stack); + + if (stack.stackSize <= 0) + return initialStackSize; + } + + // Try pushing any remaining items + for (int i = 0; i < hostInv.getSizeInventory(); i++) { + insertIntoSlot(hostInv, i, stack); + + if (stack.stackSize <= 0) + return initialStackSize; + } + + return initialStackSize - stack.stackSize; + } + + private static void insertIntoSlot(IInventory inv, int slot, ItemStack stack) { + ItemStack stackInSlot = inv.getStackInSlot(slot); + int maxStackSize = Math.min(inv.getInventoryStackLimit(), stack.getMaxStackSize()); + if (stackInSlot == null) { + inv.setInventorySlotContents(slot, stack.splitStack(Math.min(maxStackSize, stack.stackSize))); + } else if (GT_Utility.areStacksEqual(stack, stackInSlot)) { + int toMove = Math.min(stack.stackSize, Math.max(0, maxStackSize - stackInSlot.stackSize)); + if (toMove > 0) { + stackInSlot.stackSize += toMove; + stack.stackSize -= toMove; + } + } + } + +} From 3fb7b472785ed03589aecd98a10f9bf9e48a6ca9 Mon Sep 17 00:00:00 2001 From: Noktuska Date: Mon, 25 Mar 2024 03:41:04 +0100 Subject: [PATCH 2/3] Fixed Beekeeper module not working with GT IApiary --- .../forestry/UpgradeBeekeeper.scala | 16 +++-- .../forestry/UpgradeBeekeeperUtil.java | 71 ++++++++----------- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala index 113bdff7b..ee011c6ec 100644 --- a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala +++ b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeper.scala @@ -31,6 +31,8 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr DeviceAttribute.Product -> "Breeding bees for you (almost)" ) + private final val defaultStackSize = 64 + override def getDeviceInfo: util.Map[String, String] = deviceInfo override def position: BlockPosition = BlockPosition(host) override protected def checkSideForAction(args: Arguments, n: Int): ForgeDirection = args.checkSideAny(n) @@ -98,7 +100,7 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr def addIndustrialUpgrade(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) val pos = position.offset(facing) - var amount = 999 + var amount = defaultStackSize if (args.count() > 1) amount = args.checkInteger(1) result(UpgradeBeekeeperUtil.addIndustrialUpgrade(pos, host.mainInventory(), host.selectedSlot(), amount)) @@ -108,8 +110,9 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr val facing = checkSideForAction(args, 0) val pos = position.offset(facing) val slot = args.checkInteger(1) - if (slot < 1 || slot > 4) - return result(Unit, "Wrong slot index (should be 1-4)") + val maxIndex = UpgradeBeekeeperUtil.getMaxIndustrialUpgradeCount + if (slot < 1 || slot > maxIndex) + return result(Unit, "Wrong slot index (should be 1-" + maxIndex + ")") result(UpgradeBeekeeperUtil.getIndustrialUpgrade(pos, slot)) } @Callback(doc = """function(side:number, slot: number[, amount: number]):boolean -- Remove industrial upgrade from the given slot of the industrial apiary at the given side.""") @@ -117,11 +120,12 @@ class UpgradeBeekeeper(val host: EnvironmentHost with internal.Agent) extends pr val facing = checkSideForAction(args, 0) val pos = position.offset(facing) val slot = args.checkInteger(1) - var amount = 999 + val maxIndex = UpgradeBeekeeperUtil.getMaxIndustrialUpgradeCount + var amount = defaultStackSize if (args.count() > 2) amount = args.checkInteger(2) - if (slot < 1 || slot > 4) - return result(false, "Wrong slot index (should be 1-4)") + if (slot < 1 || slot > maxIndex) + return result(false, "Wrong slot index (should be 1-" + maxIndex + ")") result(UpgradeBeekeeperUtil.removeIndustrialUpgrade(pos, host.mainInventory(), host.selectedSlot(), slot, amount)) } } diff --git a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java index ad0177f28..87f227b59 100644 --- a/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java +++ b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java @@ -1,8 +1,8 @@ package li.cil.oc.integration.forestry; +import cpw.mods.fml.common.Loader; import forestry.api.apiculture.IBeeHousing; import gregtech.api.metatileentity.BaseMetaTileEntity; -import gregtech.api.util.GT_ApiaryUpgrade; import gregtech.api.util.GT_Utility; import gregtech.common.tileentities.machines.basic.GT_MetaTileEntity_IndustrialApiary; import li.cil.oc.util.BlockPosition; @@ -17,8 +17,7 @@ import net.minecraft.world.World; * */ public final class UpgradeBeekeeperUtil { - // First upgrade slot index of Industrial Apiaries because it's private for some reason - private static final int UPGRADE_INDEX = 7; + private static final boolean GT_LOADED = Loader.isModLoaded("gregtech"); private UpgradeBeekeeperUtil() {} /** Returns an IBeeHousing TileEntity at position pos. Can be an Industrial Apiary */ @@ -32,6 +31,10 @@ public final class UpgradeBeekeeperUtil { return null; if (te instanceof IBeeHousing) return (IBeeHousing)te; + + if (!GT_LOADED) + return null; + // Scala doesn't compile if these checks are of the form (a instanceof B b) if (!(te instanceof BaseMetaTileEntity)) return null; @@ -43,7 +46,7 @@ public final class UpgradeBeekeeperUtil { /** Returns a Tile Entity for Industrial Apiaries at position pos, or null if none exist */ public static GT_MetaTileEntity_IndustrialApiary getGTIApiaryAt(BlockPosition pos) { - if (pos.world().isEmpty()) + if (!GT_LOADED || pos.world().isEmpty()) return null; World world = pos.world().get(); @@ -82,72 +85,56 @@ public final class UpgradeBeekeeperUtil { return true; } + public static int getMaxIndustrialUpgradeCount() { + if (!GT_LOADED) + return 0; + return GT_MetaTileEntity_IndustrialApiary.getMaxUpgradeCount(); + } + public static int addIndustrialUpgrade(BlockPosition pos, IInventory hostInv, int slot, int amount) { GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); - if (iapiary == null) + if (iapiary == null || amount <= 0) return 0; ItemStack stackToInstall = hostInv.getStackInSlot(slot); - if (stackToInstall == null || !GT_ApiaryUpgrade.isUpgrade(stackToInstall)) + if (stackToInstall == null) return 0; amount = Math.min(amount, stackToInstall.stackSize); - for (int i = UPGRADE_INDEX; i < UPGRADE_INDEX + 4; i++) { - // isItemValidForSlot ensures merging existing stacks - if (!iapiary.isItemValidForSlot(i, stackToInstall)) - continue; + ItemStack stackToTryPush = stackToInstall.splitStack(amount); + iapiary.addUpgrade(stackToTryPush); + int itemsPushed = amount - stackToTryPush.stackSize; + // Any Upgrades that weren't pushed go back into host inventory + stackToInstall.stackSize += stackToTryPush.stackSize; - int maxStackSize = GT_ApiaryUpgrade.getUpgrade(stackToInstall).getMaxNumber(); - ItemStack stack = iapiary.getStackInSlot(i); - // Push into empty slot - if (stack == null) { - amount = Math.min(amount, maxStackSize); - iapiary.setInventorySlotContents(i, stackToInstall.splitStack(amount)); - if (stackToInstall.stackSize <= 0) - hostInv.setInventorySlotContents(slot, null); - return amount; - } - // Merge stacks - if (!GT_Utility.areStacksEqual(stackToInstall, stack)) - continue; - amount = Math.max(Math.min(amount, maxStackSize - stack.stackSize), 0); - if (amount == 0) - return 0; - stack.stackSize += amount; - stackToInstall.stackSize -= amount; - if (stackToInstall.stackSize <= 0) - hostInv.setInventorySlotContents(slot, null); - return amount; - } - return 0; + return itemsPushed; } public static ItemStack getIndustrialUpgrade(BlockPosition pos, int index){ - if (index < 1 || index > 4) + if (index < 1 || index > getMaxIndustrialUpgradeCount()) return null; GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); if (iapiary == null) return null; - return iapiary.getStackInSlot(index - 1 + UPGRADE_INDEX); + return iapiary.getUpgrade(index - 1); } public static int removeIndustrialUpgrade(BlockPosition pos, IInventory hostInv, int slot, int index, int amount) { - if (index < 1 || index > 4) + if (index < 1 || index > getMaxIndustrialUpgradeCount() || amount <= 0) return 0; GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); if (iapiary == null) return 0; - index = index - 1 + UPGRADE_INDEX; - ItemStack stack = iapiary.getStackInSlot(index); + ItemStack stack = iapiary.getUpgrade(index - 1); if (stack == null) return 0; + stack = stack.copy(); + amount = Math.min(amount, stack.stackSize); int moved = insertIntoHostInv(hostInv, slot, stack.splitStack(amount)); - // If less items were moved than planned, move the unmoved items back - stack.stackSize += (amount - moved); - if (stack.stackSize <= 0) - iapiary.setInventorySlotContents(index, null); + + iapiary.removeUpgrade(index - 1, moved); return moved; } From 20be9ea622ccb2282e170acc1a87f709eceae69f Mon Sep 17 00:00:00 2001 From: Martin Robertz Date: Mon, 25 Mar 2024 08:36:47 +0100 Subject: [PATCH 3/3] Update dependencies.gradle --- dependencies.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dependencies.gradle b/dependencies.gradle index 615507bf9..9144bb7db 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -9,7 +9,7 @@ dependencies { compileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-343-GTNH:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:EnderStorage:1.5.0:dev") {transitive = false } - compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.45.116:dev") {transitive = false } + compileOnly("com.github.GTNewHorizons:GT5-Unofficial:5.09.45.128:dev") {transitive = false } compile("com.github.GTNewHorizons:ForestryMC:4.8.7:dev") {transitive = false } compileOnly("com.github.GTNewHorizons:Railcraft:9.15.7:dev") {transitive = false } compile("com.github.GTNewHorizons:NotEnoughItems:2.5.25-GTNH:dev") {transitive = false }