So this method can be used if you have a moderately sized array. I guess this could be considered a "multiset" array, assuming that you have multiple (repeat) values within the array.
So what is array indexing? First of all, an array's index is the location of that item in the array. Java always begins with the number 0. So an array of length 5 would look like so:
Now, we can use array indexing to simplify many different things; however, in this example I'll explain how to use it for using an item on another item. Say you want to work on herblore; yet, you don't want to use 20 different if/else statements for the whole skill. We can shorten this to just two if/else statements or even one depending on your deleteItem method. In this example we'll use two and I'll explain why.
Our goal with this example is to produce a working formula to combine potions together. First off you want to create a few arrays. We need the array to hold the Item IDs of all potions. So we need an array to hold all 1 dose values, all 2 dose values, and so on. However, keep in mind that you need to keep the same potions in the same index within each array. So let's say I put a Prayer Potion (2) in array index 4, then I'll need Prayer Potion (1) at the same index in the other array. This'll become more clear later on.
Here's our arrays:
So above we've got all doses of potions at the correct index in their respective arrays so that they match with their respective potions. Now how do we access these arrays so that we can iterate through and find the correct potions used and the correct potion to output? Simple, a for loop. A for each loop could also be used; however, for the sake of simplicity I'll explain this with a regular for loop.
Hopefully this teaches a bit about array indexing. It's a great code shortener; however, I wouldn't recommend using it if your array is huge because what happens is you need to iterate through the whole array to find the value. If your array is big, then you'll be using more memory, however small it be.
Here's all the code for combining potions:
My Combine Potions method:
Any questions, feel free to ask. If anything in here should be changed to represent factual information, just let me know.
Thanks,
So what is array indexing? First of all, an array's index is the location of that item in the array. Java always begins with the number 0. So an array of length 5 would look like so:
Code: [Select]
0 - First value
1
2
3
4 - Fifth valueNow, we can use array indexing to simplify many different things; however, in this example I'll explain how to use it for using an item on another item. Say you want to work on herblore; yet, you don't want to use 20 different if/else statements for the whole skill. We can shorten this to just two if/else statements or even one depending on your deleteItem method. In this example we'll use two and I'll explain why.
Our goal with this example is to produce a working formula to combine potions together. First off you want to create a few arrays. We need the array to hold the Item IDs of all potions. So we need an array to hold all 1 dose values, all 2 dose values, and so on. However, keep in mind that you need to keep the same potions in the same index within each array. So let's say I put a Prayer Potion (2) in array index 4, then I'll need Prayer Potion (1) at the same index in the other array. This'll become more clear later on.
Here's our arrays:
Code: [Select]
/*
* Combining Potions
*/
final int[] singleDose = {119, 125, 131, 137, 143, 149, 155, 161, 167,
173, 179, 185, 193, 2458, 3014, 3022, 3030,
3038, 3046};
final int[] doubleDose = {117, 123, 129, 135, 141, 147, 153, 159, 165,
171, 177, 183, 191, 2456, 3012, 3020, 3028,
3036, 3044};
final int[] tripleDose = {115, 121, 127, 133, 139, 145, 151, 157, 163,
169, 175, 181, 189, 2454, 3010, 3018, 3026,
3034, 3042};
final int[] fourDose = {113, 2428, 2430, 2432, 2434, 2436, 2438, 2440, 2442,
2444, 2446, 2448, 2450, 2452, 3008, 3016, 3024,
3032, 3040};So above we've got all doses of potions at the correct index in their respective arrays so that they match with their respective potions. Now how do we access these arrays so that we can iterate through and find the correct potions used and the correct potion to output? Simple, a for loop. A for each loop could also be used; however, for the sake of simplicity I'll explain this with a regular for loop.
Code: [Select]
for(int i = 0; i < singleDose.length; i++) // Single with single = double
{
if (itemUsed == singleDose[i] && useWith == singleDose[i]) {
c.getPA().combinePotion(c, singleDose[i], itemUsedSlot, singleDose[i], usedWithSlot, doubleDose[i]);
break;
}
}What this code does is iterate through all values in the singleDose array to find out which item is being used. If then proceeds to check if the second potion is also in the singleDose array. If the potions are found within the array, they are combined, deleted, and a new potion from doubleDose is added. This is why we need the corresponding potions to be at the same index in their respective arrays. Say we combined two Prayer Potions (1). If they are at index 0 in the singleDose array, then the loop will also designate that the doubleDose is at index 0. If you put the wrong Item ID at index 0 in doubleDose, then you'll receive the wrong item.Hopefully this teaches a bit about array indexing. It's a great code shortener; however, I wouldn't recommend using it if your array is huge because what happens is you need to iterate through the whole array to find the value. If your array is big, then you'll be using more memory, however small it be.
Here's all the code for combining potions:
Code: [Select]
/*
* Combining Potions
*/
final int[] singleDose = {119, 125, 131, 137, 143, 149, 155, 161, 167,
173, 179, 185, 193, 2458, 3014, 3022, 3030,
3038, 3046};
final int[] doubleDose = {117, 123, 129, 135, 141, 147, 153, 159, 165,
171, 177, 183, 191, 2456, 3012, 3020, 3028,
3036, 3044};
final int[] tripleDose = {115, 121, 127, 133, 139, 145, 151, 157, 163,
169, 175, 181, 189, 2454, 3010, 3018, 3026,
3034, 3042};
final int[] fourDose = {113, 2428, 2430, 2432, 2434, 2436, 2438, 2440, 2442,
2444, 2446, 2448, 2450, 2452, 3008, 3016, 3024,
3032, 3040};
for(int i = 0; i < singleDose.length; i++) // Single with single = double
{
if (itemUsed == singleDose[i] && useWith == singleDose[i]) {
c.getPA().combinePotion(c, singleDose[i], itemUsedSlot, singleDose[i], usedWithSlot, doubleDose[i]);
break;
}
}
for(int i = 0; i < doubleDose.length; i++) // Double with double = quadruple
{
if ( itemUsed == doubleDose[i] && useWith == doubleDose[i]) {
c.getPA().combinePotion(c, doubleDose[i], itemUsedSlot, doubleDose[i], usedWithSlot, fourDose[i]);
break;
}
}
for(int i = 0; i < singleDose.length; i++) // Single with double = triple
{
if (itemUsed == doubleDose[i] && useWith == singleDose[i]) {
c.getPA().combinePotion(c, doubleDose[i], itemUsedSlot, singleDose[i], usedWithSlot, tripleDose[i]);
break;
}
if (itemUsed == singleDose[i] && useWith == doubleDose[i]) {
c.getPA().combinePotion(c, singleDose[i], itemUsedSlot, doubleDose[i], usedWithSlot, tripleDose[i]);
break;
}
}
for(int i = 0; i < singleDose.length; i++) // Single with triple = quadruple
{
if (itemUsed == tripleDose[i] && useWith == singleDose[i]) {
c.getPA().combinePotion(c, tripleDose[i], itemUsedSlot, singleDose[i], usedWithSlot, fourDose[i]);
break;
}
if (itemUsed == singleDose[i] && useWith == tripleDose[i]) {
c.getPA().combinePotion(c, singleDose[i], itemUsedSlot, tripleDose[i], usedWithSlot, fourDose[i]);
break;
}
}My Combine Potions method:
Code: [Select]
public void combinePotion(Client c, int potOne, int itemSlot, int potTwo, int usedWithSlot, int newPot) {
c.getItems().deleteItem(potOne, itemSlot, 1);
c.getItems().deleteItem(potTwo, usedWithSlot, 1);
c.getItems().addItem(newPot, 1);
c.getItems().addItem(229, 1);
c.sendMessage( "You combine the potions." );
}Any questions, feel free to ask. If anything in here should be changed to represent factual information, just let me know.
Thanks,

















comments (0)
Post a Comment