diff --git a/runelite-api/pom.xml b/runelite-api/pom.xml index 9c8bdc6bb6..0749f61256 100644 --- a/runelite-api/pom.xml +++ b/runelite-api/pom.xml @@ -42,5 +42,12 @@ api ${project.version} + + + junit + junit + 4.12 + test + diff --git a/runelite-api/src/main/java/net/runelite/api/Client.java b/runelite-api/src/main/java/net/runelite/api/Client.java index 014c00f2a9..1e722833b7 100644 --- a/runelite-api/src/main/java/net/runelite/api/Client.java +++ b/runelite-api/src/main/java/net/runelite/api/Client.java @@ -272,4 +272,11 @@ public class Client { return client.getXteaKeys(); } + + public int getSetting(Varbits varbit) + { + int[] settings = client.getSettings(); + int value = settings[varbit.getIndex()]; + return varbit.get(value); + } } diff --git a/runelite-api/src/main/java/net/runelite/api/Varbits.java b/runelite-api/src/main/java/net/runelite/api/Varbits.java new file mode 100644 index 0000000000..89df90bf69 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/Varbits.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +public enum Varbits +{ + /** + * Runecraft pouches + */ + POUCH_SMALL(486, 0, 2), + POUCH_MEDIUM(486, 3, 8), + POUCH_LARGE(486, 9, 17), + POUCH_GIANT(486, 18, 29); + + private final int index; + private final int leastSignificantBit; + private final int mostSignificantBit; + + private Varbits(int index, int leastSignificantBit, int mostSignificantBit) + { + this.index = index; + this.leastSignificantBit = leastSignificantBit; + this.mostSignificantBit = mostSignificantBit; + } + + public int getIndex() + { + return index; + } + + public int getLeastSignificantBit() + { + return leastSignificantBit; + } + + public int getMostSignificantBit() + { + return mostSignificantBit; + } + + /** + * Get the value of this varbit from the given int + * + * @param value + * @return + */ + public int get(int value) + { + int mask = getMax() - 1; + + return (value >> leastSignificantBit) & mask; + } + + /** + * Set the value of this varbit to a given int + * + * @param varbit The int to set the value on + * @param value The value to set + * @return + */ + public int set(int varbit, int value) + { + int mask = getMax() - 1; + + value &= mask; // Ensure new value is in range + + mask <<= leastSignificantBit; // Align mask to the correct place + + varbit &= ~mask; // And off existing value + + value <<= leastSignificantBit; // Align new value + varbit |= value; + return varbit; + } + + /** + * Get the maximum possible value for this varbit + * + * @return + */ + public int getMax() + { + return 1 << getNumberOfBits(); + } + + /** + * Get the number of bits of storage in this varbit + * @return + */ + public int getNumberOfBits() + { + // leastSignificantBit and mostSignificantBit are inclusive + return (mostSignificantBit - leastSignificantBit) + 1; + } +} diff --git a/runelite-api/src/test/java/net/runelite/api/VarbitsTest.java b/runelite-api/src/test/java/net/runelite/api/VarbitsTest.java new file mode 100644 index 0000000000..4e899f8af7 --- /dev/null +++ b/runelite-api/src/test/java/net/runelite/api/VarbitsTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +import org.junit.Assert; +import org.junit.Test; + +public class VarbitsTest +{ + @Test + public void testGet() + { + // 28 24 20 16 12 8 4 0 + int value = 0b1101_1110_1010_1101_1011_1110_1110_1111; + + // Bit indexes are inclusive + // Small pouch is bits 0-2 + // Medium pouch is bits 3-8 + // Large pouch is bits 9-17 + // Giant pouch is bits 18-29 + Varbits small = Varbits.POUCH_SMALL; + Varbits med = Varbits.POUCH_MEDIUM; + Varbits large = Varbits.POUCH_LARGE; + Varbits giant = Varbits.POUCH_GIANT; + + Assert.assertEquals(7, small.get(value)); + Assert.assertEquals(29, med.get(value)); + Assert.assertEquals(223, large.get(value)); + Assert.assertEquals(1963, giant.get(value)); + } + + @Test + public void testSet() + { + // 28 24 20 16 12 8 4 0 + int value = 0b1101_1110_1010_1101_1011_1110_1110_1111; + int mask = 0b0000_0000_0000_0011_1111_1110_0000_0000; // mask for large pouch + + Varbits large = Varbits.POUCH_LARGE; + + int newValue = large.set(value, 42); + int expect = (value & ~mask) | (42 << large.getLeastSignificantBit()); + Assert.assertEquals(expect, newValue); + } +}