runelite-api: add api method for reading varbits
This commit is contained in:
@@ -42,5 +42,12 @@
|
||||
<artifactId>api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
117
runelite-api/src/main/java/net/runelite/api/Varbits.java
Normal file
117
runelite-api/src/main/java/net/runelite/api/Varbits.java
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Adam <Adam@sigterm.info>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
67
runelite-api/src/test/java/net/runelite/api/VarbitsTest.java
Normal file
67
runelite-api/src/test/java/net/runelite/api/VarbitsTest.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Adam <Adam@sigterm.info>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user