This will teach you how to add a new file to your server which will handle specifically arrays. Although this isn't the best of an idea, this is just an example - and if you read through this whole tutorial, you could actually learn something. But some may not know what an Array is?
Definition:What you will learn: Arrays, Integers, Static Methods, Creating a file.
Now, perhaps some wonder if this is a Java tutorial or even a server tutorial. The truth is, it's kind of both.
Lets start by creating a new file.
Every file must begin with:
public class
We will make this file public because we want it to be seen by other files. If what so ever you don't, then you would make it private.
Let's begin to add the first line to the file:
public class ClientArray {
As you can see, you already know what the file will be called. (
ClientArray). I've named it that because this file will only handle arrays from the client class. If you want to modify it, you can change the name.
Although there isn't much to this file, I'll assume that most sources have at least one array already built.
Lets begin to add this, don't worry - it'll change just a bit but it won't give us errors.
public static final int PACKET_SIZES[] = {
You may wonder why it's in captials this time?
Well, the proper way to do it is whenever a constant is equal to 'public static final', the value should be in capitals.
This isn't full, but already it tells us that this array will hold integers (numbers), and is static. (Although, static can only be called to a static, this will ease things up by just referring to the class, then method.
Anyways, the full method is this:
Now, we are missing one bracket to end the file.
So, at the end of the file, add one more bracket.
}
Now, this is the time where we delete this array from the client class.
In
client.java, find this:
public static final int packetSizes[] = {
Erase the whole thing, as we added it to the file we've just created.
But it still won't work yet, since we've erased it, the method that calls it doesn't know where it is.
So in the same file, search for:
packetSizes
You will only get one search result for that.
Now, replace 'packetSizes' with:
ClientArray.PACKET_SIZES
We add this because it will tell the file to go to ClientArray.PACKET_SIZES.
As said before, we need to first give the Class we are going in, and the methods name.
(
For those who don't know what the line should look like, here it is:)
PACKET_SIZES = ClientArray.PACKET_SIZES[packetType];
Alright, but let's say you wanted to create your own array, not just take from whats already there.
As said before, arrays can hold multiple values.
But lets focus on the arrays that hold integers (numbers).
If you're adding for the file we've created, then it would look like this:
public static int[] example = {
If your doing something for the client class, then it wouldn't need a static.
public int[] example = {
Let's say that we wanted this array to represent untradable items.
Well, what we would first have to do for the array, is add some numbers.
But we can't tell an array what the numbers mean, we can just add the values.
Example for the static array: public static int[] untradable = { //Example
1507, 995
};
Example for the non-static array: public int[] untradable = { //Example
1507, 995
};
For those who are in
ClientArray.java, you can close it and open up
Client.java. And those who did this example in
Client.java, well - you're in the right place.
As I said before, we are doing 'untradable items' as an example.
We'll have to create a new method, so at the end of the file but before the last bracket add this:
public boolean isUntradable(int item) {
for(int i = 0; i < ClientArray.untradable.length; i++) {
if(ClientArray.untradable[i] == item)
return true;
}
return false;
}
Maybe in my next lesson I'll go a bit further off topic from Array's, but lets focus on this stuff first.
By looking at this method, already you might have guessed that it has something to do with going into the ClientArray class, and going to the untradable method.
(How do you know?)ClientArray.untradable.length
Well, it first sets an integer to 0, but any number that is higher then what we've added in the array, it'll then do:
if(ClientArray.untradable[i] == item)
What that means is basically, if any number from the array equals an items ID number, then it will return a true statement. And in this example, if it's true - then the item is untradable, if it's false then it isn't.
Alright, now you've created your own array, but now its your turn to call upon the method. (isUntradable)
Anyways, good luck with this - and I hope you learned something new...
-Ikram