Code:
package com.rs.game.player;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import com.rs.fs.defs.EnumDef;
import com.rs.game.item.Item;
public class ToolBelt implements Serializable {
private static final long serialVersionUID = 8785635409322322662L;
public static final int TOOLBELT_INTERFACE = 1178;
public static final byte GENERAL = 1, FISHING = 2, CRAFTING = 3, FARMING = 4, SLAYER = 5, INVENTION = 6, DUNGEONEERING = 11, KEYS = 12;
private static final EnumDef GILDED_PICK_ENUM, REGULAR_PICK_ENUM, HATCHET_ENUM, MACHETE_ENUM, GENERAL_ENUM;
private static final EnumDef FISHING_ENUM, CRAFTING_ENUM, FARMING_ENUM, SLAYER_ENUM;
private static final EnumDef DUNGEONEERING_PICKAXES, DUNGEONEERING_HATCHETS, DUNGEONEERING_ENUM, KEYS_ENUM;
private static final int INVENTION_LENGTH;
private static final int CATEGORY_CS_VAR = 1725, SLOT_CS_VAR = 1724;
private static final int[] CATEGORY_LENGTHS;
static {
// General Tools
GILDED_PICK_ENUM = EnumDef.get(7505); REGULAR_PICK_ENUM = EnumDef.get(7501);
HATCHET_ENUM = EnumDef.get(7503);
MACHETE_ENUM = EnumDef.get(10040);
GENERAL_ENUM = EnumDef.get(5350);
// Specified Skilling Tools
FISHING_ENUM = EnumDef.get(5353);
CRAFTING_ENUM = EnumDef.get(5356);
FARMING_ENUM = EnumDef.get(5359);
SLAYER_ENUM = EnumDef.get(10085);
INVENTION_LENGTH = 3;
// Dungeoneering Tools
DUNGEONEERING_PICKAXES = EnumDef.get(5732);
DUNGEONEERING_HATCHETS = EnumDef.get(5733);
DUNGEONEERING_ENUM = EnumDef.get(5731);
KEYS_ENUM = EnumDef.get(5734);
CATEGORY_LENGTHS = new int[] { GENERAL_ENUM.getSize() + 3, FISHING_ENUM.getSize(), CRAFTING_ENUM.getSize(), FARMING_ENUM.getSize(), SLAYER_ENUM.getSize(), INVENTION_LENGTH, DUNGEONEERING_ENUM.getSize() + 2,
KEYS_ENUM.getSize() };
}
private transient Player player;
private Map<Byte, Integer[]> tools;
public ToolBelt() {
tools = new HashMap<>();
for (byte category = INVENTION; category >= GENERAL; category--)
tools.put(category, new Integer[CATEGORY_LENGTHS[category]]);
tools.put(DUNGEONEERING, new Integer[CATEGORY_LENGTHS[INVENTION]]);
tools.put(KEYS, new Integer[CATEGORY_LENGTHS[INVENTION + 1]]);
Integer[] belt = tools.get(INVENTION);
belt[1] = 36367; belt[2] = 36368; belt[3] = 36389;
}
public void setPlayer(Player player) {
this.player = player;
}
public void init() {
refreshAll();
}
public void open(byte category) {
setCategory(category);
refreshCategory(category);
player.getInterfaceManager().sendCentralInterface(TOOLBELT_INTERFACE);
/*player.setCloseInterfacesEvent(()-> {
setCategory(category);
setSlot();
});*/
}
private void setCategory(byte category) {
player.getPackets().sendCSVarInteger(CATEGORY_CS_VAR, category);
}
private void setSlot() {
player.getPackets().sendCSVarInteger(SLOT_CS_VAR, -1);
}
private void refreshAll(byte end, byte start) {
for (byte category = end; category >= start; category--)
refreshCategory(category);
}
private void refreshCategory(byte category) {
Integer[] available = tools.get(category);
for (int slot = available.length - 1; slot >= 0; slot--) {
Integer entry = available[slot];
if (entry == null)
continue;
refresh(category, entry, slot);
}
}
private void refresh(byte category, int entry, int slot) {
EnumDef def = getCategoryEnum(category, slot);
boolean multiMap = isMultiDef(def);
int value = category == INVENTION ? 1 : multiMap ? (int) (def.getKeyForValue(entry)) : 1;
if (category == KEYS) {
player.getVarsManager().sendVar(getVar(category, slot), value);
return;
}
player.getVarsManager().sendVarBit(getVarBit(category, slot), value);
if (multiMap)
player.getVarsManager().sendVarBit(getSecondaryVarBit(category, slot), 1);
}
private static int getSecondaryVarBit(byte category, int slot) {
switch(category) {
case GENERAL:
if (slot == 1)
return 2983;
else if (slot == 4)
return 2985;
else if (slot == 9)
return 3002;
break;
case DUNGEONEERING:
return -1;
}
throw new IllegalStateException();
}
private void refreshAll() {
refreshAll(INVENTION, GENERAL);
}
public boolean addItem(int slot, Item item) {
byte category = getCategory(item.getId());
if (category == -1)
return false;
Integer[] belt = tools.get(category);
int current = (int) getSlotForId(category, item.getId());
Integer entry = belt[current];
if (entry != null && entry >= item.getId()) {
player.getPackets().sendInterfaceMessage(RealInventory.INVENTORY_INTERFACE, 8, 0, slot, "That is already on your tool belt.");
return false;
}
refresh(category, belt[current] = item.getId(), current);
player.getRealInventory().remove(item.getId(), item.getAmount(), slot);
player.getPackets().sendGameMessage("You add the " + item.getDefs().getName() + " to your tool belt.");
return false;
}
public boolean containsTool(int id) {
return search(id, category-> {
Integer[] active = tools.get(category);
for (int index = active.length - 1; index >= 0; index--) {
Integer entry = active[index];
if (entry == null || entry != id)
continue;
return true;
}
return false;
});
}
public boolean isTool(int id) {
return search(id, category-> getSlotForId(category, id) != -1);
}
private boolean search(int id, Predicate<Byte> predicate) {
boolean inDung = player.getDungManager().isInside();
for (byte category = inDung ? KEYS : INVENTION; category >= (inDung ? DUNGEONEERING : GENERAL); category--) {
if (predicate.test(category))
return true;
}
return false;
}
private byte getCategory(int id) {
boolean inDung = player.getDungManager().isInside();
for (byte category = inDung ? KEYS : INVENTION; category >= (inDung ? DUNGEONEERING : GENERAL); category--) {
if (getSlotForId(category, id) != -1)
return category;
}
return -1;
}
private static long getSlotForId(byte category, int id) {
switch(category) {
case GENERAL:
long slot = GENERAL_ENUM.getKeyForValue(id);
if (slot == -1) {
if (REGULAR_PICK_ENUM.getKeyForValue(id) != -1 || GILDED_PICK_ENUM.getKeyForValue(id) != -1) return 1;
else if (HATCHET_ENUM.getKeyForValue(id) != -1) return 4;
else if (MACHETE_ENUM.getKeyForValue(id) != -1) return 9;
}
return slot;
case CRAFTING:
return CRAFTING_ENUM.getKeyForValue(id);
case FARMING:
return FARMING_ENUM.getKeyForValue(id);
case FISHING:
return FISHING_ENUM.getKeyForValue(id);
case SLAYER:
return SLAYER_ENUM.getKeyForValue(id);
case INVENTION:
if (id == 36367 || id == 36368)
return 1;
else if (id == 36389)
return 2;
case DUNGEONEERING:
slot = DUNGEONEERING_ENUM.getKeyForValue(id);
if (slot == -1) {
if (DUNGEONEERING_PICKAXES.getKeyForValue(id) != -1) return 1;
else if (DUNGEONEERING_HATCHETS.getKeyForValue(id) != -1) return 2;
}
return slot;
case KEYS:
return KEYS_ENUM.getKeyForValue(id);
}
throw new IllegalArgumentException("Illegal element: " + category);
}
private static boolean isMultiDef(EnumDef def) {
if (def == null)
return false;
return def.equals(REGULAR_PICK_ENUM) || def.equals(GILDED_PICK_ENUM) || def.equals(HATCHET_ENUM) || def.equals(MACHETE_ENUM) || def.equals(DUNGEONEERING_HATCHETS)
|| def.equals(DUNGEONEERING_PICKAXES);
}
private static EnumDef getCategoryEnum(byte category, int slot) {
switch (category) {
case GENERAL:
if (slot == 1) return REGULAR_PICK_ENUM;
else if (slot == 4) return HATCHET_ENUM;
else if (slot == 9) return MACHETE_ENUM;
return GENERAL_ENUM;
case CRAFTING:
return CRAFTING_ENUM;
case FARMING:
return FARMING_ENUM;
case FISHING:
return FISHING_ENUM;
case SLAYER:
return SLAYER_ENUM;
case INVENTION:
return null;
case DUNGEONEERING:
if (slot == 1)return DUNGEONEERING_PICKAXES;
else if (slot == 2)
return DUNGEONEERING_HATCHETS;
return DUNGEONEERING_ENUM;
case KEYS:
return KEYS_ENUM;
}
throw new IllegalArgumentException("Illegal element: " + category);
}
private static int getVarBit(byte category, int slot) {
if (category == GENERAL) {
switch (slot) {
case 1: return 18521;
case 2: return 2984;
case 3: return 2971;
case 4:return 18522;
case 5:return 2968;
case 6:return 2986;
case 7:return 2988;
case 8:return 2999;
case 9:return 4935;
case 10:return 3004;
case 11:return 3005;
case 12:return 2969;
case 13:return 3007;
case 14:return 685;
case 15:return 28410;
}
} else if (category == FISHING) {
switch (slot) {
case 1:return 2978;
case 2:return 2977;
case 3:return 2982;
case 4:return 3001;
case 5:return 2979;
case 6:return 2980;
case 7:return 2981;
case 8:return 3003;
}
} else if (category == CRAFTING) {
switch (slot) {
case 1:return 2974;
case 2:return 2992;
case 3:return 2970;
case 4:return 2991;
case 5:return 2973;
case 6:return 2975;
case 7:return 2976;
case 8:return 2989;
case 9:return 2990;
case 10:return 2972;
case 11:return 2994;
case 12:return 2993;
case 13:return 3006;
}
} else if (category == FARMING) {
switch (slot) {
case 1:return 2997;
case 2:return 2995;
case 3:return 3000;
case 4:return 2996;
case 5:return 27430;
case 6:return 27431;
}
} else if (category == SLAYER) {
switch (slot) {
case 1:return 28219;
case 2:return 28220;
case 3:return 28221;
case 4:return 28222;
case 5:return 28225;
case 6:return 28223;
case 7:return 28224;
case 8:return 28230;
case 9:return 28226;
case 10:return 28227;
case 11:return 28228;
case 12:return 28229;
}
} else if (category == INVENTION) {
switch(slot) {
case 1:
case 2:return 30224;
case 3:return 30225;
}
} else if (category == DUNGEONEERING) {
switch (slot) {
case 1:return 3008;
case 2:return 3009;
case 3:return 3013;
case 4:return 3010;
case 5:return 3015;
case 6:return 3012;
case 7:return 3014;
case 8:return 3011;
}
}
throw new IllegalArgumentException("Invalid category-slot combo: " + category + ", " + slot + ".");
}
private static int getVar(byte category, int slot) {
switch (slot) {
case 1:return 1812;
case 2:return 1813;
case 3:return 1814;
case 4:return 1815;
case 5:return 1816;
case 6:return 1817;
case 7:return 1818;
case 8:return 1819;
case 9:return 1820;
case 10:return 1821;
case 11:return 1822;
case 12:return 1823;
case 13:return 1824;
case 14:return 1825;
case 15:return 1826;
case 16:return 1827;
case 17:return 1828;
case 18:return 1829;
case 19:return 1830;
case 20:return 1831;
case 21:return 1832;
case 22:return 1833;
case 23:return 1834;
case 24:return 1835;
case 25:return 1836;
case 26:return 1837;
case 27:return 1838;
case 28:return 1839;
case 29:return 1840;
case 30:return 1841;
case 31:return 1842;
case 32:return 1843;
case 33:return 1844;
case 34:return 1845;
case 35:return 1846;
case 36:return 1847;
case 37:return 1848;
case 38:return 1849;
case 39:return 1850;
case 40:return 1851;
case 41:return 1852;
case 42:return 1853;
case 43:return 1854;
case 44:return 1855;
case 45:return 1856;
case 46:return 1857;
case 47:return 1858;
case 48:return 1859;
case 49:return 1860;
case 50:return 1861;
case 51:return 1862;
case 52:return 1863;
case 53:return 1864;
case 54:return 1865;
case 55:return 1866;
case 56:return 1867;
case 57:return 1868;
case 58:return 1869;
case 59:return 1870;
case 60:return 1871;
case 61:return 1872;
case 62:return 1873;
case 63:return 1874;
case 64:return 1875;
}
throw new IllegalArgumentException("Missing var for slot: "+slot);
}
}