diff --git a/dependencies.gradle b/dependencies.gradle index 4494f21a9..9144bb7db 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.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 } 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..ee011c6ec 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 @@ -37,53 +31,43 @@ 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) - 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 +96,36 @@ 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 = defaultStackSize + 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) + 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):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) + val maxIndex = UpgradeBeekeeperUtil.getMaxIndustrialUpgradeCount + var amount = defaultStackSize + if (args.count() > 2) + amount = args.checkInteger(2) + 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 new file mode 100644 index 000000000..87f227b59 --- /dev/null +++ b/src/main/scala/li/cil/oc/integration/forestry/UpgradeBeekeeperUtil.java @@ -0,0 +1,189 @@ +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_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 { + + private static final boolean GT_LOADED = Loader.isModLoaded("gregtech"); + 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; + + 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; + 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 (!GT_LOADED || 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 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 || amount <= 0) + return 0; + + ItemStack stackToInstall = hostInv.getStackInSlot(slot); + if (stackToInstall == null) + return 0; + amount = Math.min(amount, stackToInstall.stackSize); + + 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; + + return itemsPushed; + } + + public static ItemStack getIndustrialUpgrade(BlockPosition pos, int index){ + if (index < 1 || index > getMaxIndustrialUpgradeCount()) + return null; + GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); + if (iapiary == null) + return null; + return iapiary.getUpgrade(index - 1); + } + + public static int removeIndustrialUpgrade(BlockPosition pos, IInventory hostInv, int slot, int index, int amount) { + if (index < 1 || index > getMaxIndustrialUpgradeCount() || amount <= 0) + return 0; + GT_MetaTileEntity_IndustrialApiary iapiary = getGTIApiaryAt(pos); + if (iapiary == null) + return 0; + + 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)); + + iapiary.removeUpgrade(index - 1, moved); + 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; + } + } + } + +}