Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 1 | package li.cil.oc.util |
| 2 | |
Florian Nücke | 1561854 | 2015-02-03 23:34:35 +0100 | [diff] [blame] | 3 | import li.cil.oc.api.internal.MultiTank |
Florian Nücke | 5d908fc | 2014-09-19 19:54:44 +0200 | [diff] [blame] | 4 | import li.cil.oc.api.machine.Arguments |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 5 | import net.minecraft.inventory.IInventory |
Florian Nücke | cf521f4 | 2014-06-19 16:23:26 +0200 | [diff] [blame] | 6 | import net.minecraftforge.common.util.ForgeDirection |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 7 | |
Florian Nücke | e3d3e3c | 2014-06-25 22:10:34 +0200 | [diff] [blame] | 8 | import scala.language.implicitConversions |
| 9 | |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 10 | object ExtendedArguments { |
| 11 | |
Florian Nücke | f4258aa | 2014-11-12 11:08:13 +0100 | [diff] [blame] | 12 | implicit def extendedArguments(args: Arguments): ExtendedArguments = new ExtendedArguments(args) |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 13 | |
| 14 | class ExtendedArguments(val args: Arguments) { |
| 15 | def optionalItemCount(n: Int) = |
| 16 | if (args.count > n && args.checkAny(n) != null) { |
| 17 | math.max(0, math.min(64, args.checkInteger(n))) |
| 18 | } |
| 19 | else 64 |
| 20 | |
Florian Nücke | 21488bc | 2014-10-01 14:35:21 +0200 | [diff] [blame] | 21 | def optionalFluidCount(n: Int) = |
| 22 | if (args.count > n && args.checkAny(n) != null) { |
| 23 | math.max(0, args.checkInteger(n)) |
| 24 | } |
| 25 | else 1000 |
| 26 | |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 27 | def checkSlot(inventory: IInventory, n: Int) = { |
| 28 | val slot = args.checkInteger(n) - 1 |
| 29 | if (slot < 0 || slot >= inventory.getSizeInventory) { |
| 30 | throw new IllegalArgumentException("invalid slot") |
| 31 | } |
| 32 | slot |
Florian Nücke | bd0ae24 | 2014-10-14 22:25:08 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | def optSlot(inventory: IInventory, n: Int, default: Int) = { |
| 36 | if (n >= 0 && n < args.count()) checkSlot(inventory, n) |
| 37 | else default |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Florian Nücke | c5e79df | 2014-12-15 21:38:54 +0100 | [diff] [blame] | 40 | def checkTank(multi: MultiTank, n: Int) = { |
Florian Nücke | 21488bc | 2014-10-01 14:35:21 +0200 | [diff] [blame] | 41 | val tank = args.checkInteger(n) - 1 |
Florian Nücke | c5e79df | 2014-12-15 21:38:54 +0100 | [diff] [blame] | 42 | if (tank < 0 || tank >= multi.tankCount) { |
Florian Nücke | 21488bc | 2014-10-01 14:35:21 +0200 | [diff] [blame] | 43 | throw new IllegalArgumentException("invalid tank index") |
| 44 | } |
| 45 | tank |
| 46 | } |
| 47 | |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 48 | def checkSideForAction(n: Int) = checkSide(n, ForgeDirection.SOUTH, ForgeDirection.UP, ForgeDirection.DOWN) |
| 49 | |
| 50 | def checkSideForMovement(n: Int) = checkSide(n, ForgeDirection.SOUTH, ForgeDirection.NORTH, ForgeDirection.UP, ForgeDirection.DOWN) |
| 51 | |
| 52 | def checkSideForFace(n: Int, facing: ForgeDirection) = checkSide(n, ForgeDirection.VALID_DIRECTIONS.filter(_ != facing.getOpposite): _*) |
| 53 | |
Florian Nücke | f7540d4 | 2014-07-11 14:40:03 +0200 | [diff] [blame] | 54 | def checkSide(n: Int, allowed: ForgeDirection*) = { |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 55 | val side = args.checkInteger(n) |
| 56 | if (side < 0 || side > 5) { |
| 57 | throw new IllegalArgumentException("invalid side") |
| 58 | } |
| 59 | val direction = ForgeDirection.getOrientation(side) |
| 60 | if (allowed.isEmpty || (allowed contains direction)) direction |
| 61 | else throw new IllegalArgumentException("unsupported side") |
| 62 | } |
| 63 | } |
| 64 | |
Florian Nücke | d7143f5 | 2014-05-20 16:00:04 +0200 | [diff] [blame] | 65 | } |