Thread: How to make a perfect handler - Free fishing example!

Results 1 to 4 of 4
  1. #1 How to make a perfect handler - Free fishing example! 
    Registered Member
    Harvey's Avatar
    Join Date
    Mar 2007
    Posts
    965
    Thanks given
    0
    Thanks received
    55
    Rep Power
    296

    How to make a Handled Skill

    Description: In this tutorial i will teach you how to make a propper handled skill with great stability and easy to use.
    I will be giving you and example, fishing.

    Difficulty: 2/10 basic java

    Assumed Knowledge: Java

    Tested Server: WhiteScape V2

    Files/Classes Modified: Client.java, server.java

    Step 1 - Creating the class

    Code:
    public class Fishing extends Thread {
    
    	public void run() {
    
    	}
    
    }
    As you see i have used the extends thread, later on in the tutorial we will be using this thread therefore we are multi-threading.
    This will be used for our timers. The method "run()" is needed when you use a thread, this is our process.

    Step 2 - Possiblities

    Before starting any piece of work we need to think of what the posiblities such as:
    - Does player have correct level?
    - Does player have required item/s?
    - Does player have enough slots?

    Code:
    public class Fishing extends Thread {
    
    	public void fish(int playerId, int reqItem, int reqLvl, int emote, int time, boolean reqFeathers, int fishNumber) {
    		client c = (client) server.playerHandler.players[playerId];
    	}
    
    	public void run() {
    
    	}
    
    }
    int playerId = Will get the playerId from client.java so we know whos client we are controlling (client c = (client) server.playerHandler.players[playerId]
    int fishNumber = Will be used for the end outcome (will explain later)
    The others speak for themselves eh?

    Code:
    
    public class Fishing extends Thread {
    
    //Variables needed
    	public int fish = -1;
    	public int FishTimer = -1;
    	public boolean isFish = false;
    	public boolean feathers = false;
    
    	public void fish(int playerId, int reqItem, int reqLvl, int emote, int time, boolean reqFeathers, int fishNumber)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		if(c.playerHasItem(reqItem)) //If player has required item
    		{
    			if(c.playerLevel[10] >= reqLvl) //If player has required level
    			{
            			if (c.freeSlots() > 0)  //If player has enough slots
            			{
    					if(reqFeathers == true)  //If player needs feathers
    					{
    						if(c.playerHasItem(314))  //If player has feathers
    						{
    							String itemName = c.GetItemName(reqItem); //Not important!
    							String iName = itemName.toLowerCase(); //Not important!
    							c.sendMessage("You place your " + iName + " in the water!"); //Sends a message
    							c.pEmote = emote; //Displays the emote allocated
    							FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    							fish = fishNumber; //Used for outcome
    							isFish = true; //Player is now fishing, true
    							feathers = true; //Needs feathers, true
    						} else {  //If player has no feathers
    							c.sendMessage("You need feathers to perform this task!"); //Sends a message
    						}
    					} else {  //If player does not need feathers
    						c.sendMessage("You place your " + c.GetItemName(reqItem) + " in the water!"); //Sends a message
    						c.pEmote = emote; //Displays the emote allocated
    						FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    						fish = fishNumber; //Used for outcome
    						isFish = true; //Player is now fishing, true
    						feathers = false; //Needs feathers, false
    					}
    				} else {  //If player hasn't enough slots
    					c.sendMessage("Not enough space in your inventory!");
    				}
    			} else {  //If player hasn't the required level
    				c.sendMessage("You need "+reqLvl+" fishing to fish here!");
    			}
    		} else { //If player hasn't the required item
    			c.sendMessage("You need a "+ c.GetItemName(reqItem) + " to fish here!");
    		}
    	}
    
    	public void run() {
    
    	}
    
    }
    Step 3 - Timers

    When you cut a tree, or fish something you never get it straight away, times are random so this will generate the random time and give us our outcome.

    Code:
    
    public class Fishing extends Thread {
    
    //Variables needed
    	public int fish = -1;
    	public int FishTimer = -1;
    	public boolean isFish = false;
    	public boolean feathers = false;
    
    	public void fish(int playerId, int reqItem, int reqLvl, int emote, int time, boolean reqFeathers, int fishNumber)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		if(c.playerHasItem(reqItem)) //If player has required item
    		{
    			if(c.playerLevel[10] >= reqLvl) //If player has required level
    			{
            			if (c.freeSlots() > 0)  //If player has enough slots
            			{
    					if(reqFeathers == true)  //If player needs feathers
    					{
    						if(c.playerHasItem(314))  //If player has feathers
    						{
    							String itemName = c.GetItemName(reqItem); //Not important!
    							String iName = itemName.toLowerCase(); //Not important!
    							c.sendMessage("You place your " + iName + " in the water!"); //Sends a message
    							c.pEmote = emote; //Displays the emote allocated
    							FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    							fish = fishNumber; //Used for outcome
    							isFish = true; //Player is now fishing, true
    							feathers = true; //Needs feathers, true
    						} else {  //If player has no feathers
    							c.sendMessage("You need feathers to perform this task!"); //Sends a message
    						}
    					} else {  //If player does not need feathers
    						c.sendMessage("You place your " + c.GetItemName(reqItem) + " in the water!"); //Sends a message
    						c.pEmote = emote; //Displays the emote allocated
    						FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    						fish = fishNumber; //Used for outcome
    						isFish = true; //Player is now fishing, true
    						feathers = false; //Needs feathers, false
    					}
    				} else {  //If player hasn't enough slots
    					c.sendMessage("Not enough space in your inventory!");
    				}
    			} else {  //If player hasn't the required level
    				c.sendMessage("You need "+reqLvl+" fishing to fish here!");
    			}
    		} else { //If player hasn't the required item
    			c.sendMessage("You need a "+ c.GetItemName(reqItem) + " to fish here!");
    		}
    	}
    
    	public void run() {
    		client c = (client) server.playerHandler.players[server.playerId];
    		if (FishTimer > 0) //If the random timer from the code above is more than zero this will come into action
    		{
    			FishTimer--; //It will then take away 1 every 500ms? so 2 a second until...
    		} 
    		else if (FishTimer == 0) //... it equals 0 which will then...
    		{
    			getFish(server.playerId); //... give us our output
    		}
    	}
    
    }
    Step 4 - The input and output

    So far this has been a waiste, how will this work? Well ill show you!

    Code:
    
    public class Fishing extends Thread {
    
    //Variables needed
    	public int fish = -1;
    	public int FishTimer = -1;
    	public boolean isFish = false;
    	public boolean feathers = false;
    
    //Make it easier to control
    	public int net = 303; //Net id
    	public int netE = 0x26D; //Net emote number
    
    	public void fish(int playerId, int reqItem, int reqLvl, int emote, int time, boolean reqFeathers, int fishNumber)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		if(c.playerHasItem(reqItem)) //If player has required item
    		{
    			if(c.playerLevel[10] >= reqLvl) //If player has required level
    			{
            			if (c.freeSlots() > 0)  //If player has enough slots
            			{
    					if(reqFeathers == true)  //If player needs feathers
    					{
    						if(c.playerHasItem(314))  //If player has feathers
    						{
    							String itemName = c.GetItemName(reqItem); //Not important!
    							String iName = itemName.toLowerCase(); //Not important!
    							c.sendMessage("You place your " + iName + " in the water!"); //Sends a message
    							c.pEmote = emote; //Displays the emote allocated
    							FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    							fish = fishNumber; //Used for outcome
    							isFish = true; //Player is now fishing, true
    							feathers = true; //Needs feathers, true
    						} else {  //If player has no feathers
    							c.sendMessage("You need feathers to perform this task!"); //Sends a message
    						}
    					} else {  //If player does not need feathers
    						c.sendMessage("You place your " + c.GetItemName(reqItem) + " in the water!"); //Sends a message
    						c.pEmote = emote; //Displays the emote allocated
    						FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10); //Timer: (the time integer + 10) - (fishing level / 10)
    						fish = fishNumber; //Used for outcome
    						isFish = true; //Player is now fishing, true
    						feathers = false; //Needs feathers, false
    					}
    				} else {  //If player hasn't enough slots
    					c.sendMessage("Not enough space in your inventory!");
    				}
    			} else {  //If player hasn't the required level
    				c.sendMessage("You need "+reqLvl+" fishing to fish here!");
    			}
    		} else { //If player hasn't the required item
    			c.sendMessage("You need a "+ c.GetItemName(reqItem) + " to fish here!");
    		}
    	}
    
    	public void fishId(int playerId, int fishId) //This is the method and the only method which will be used in client.java
    	{
    		client c = (client) server.playerHandler.players[playerId]; //So we know whos client this is controlling
    		switch(fishId) //NPC (Fish) id
    		{
    			case 316:
    				fish(playerId, net, 1, netE, 15, false, 1); //Example: 
    									    //Requires net, Level Required: 1, 
    									    //the netEmote, Time: 15  + 10 / Level, 
    									    //No feathers needed, 
    									    //Fish number 1 (Case 1 in method getFish)
    				break;
    		}
    	}
    
    	public void getFish(int playerId) //This is called once timer is finished
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		switch(fish)
    		{
    			case 1: //Fish number 1
    				hasFished(playerId, 10, 317); //10 xp and item id 317 (shrimp)
    				break;
    		}
    	}
    
    	public void hasFished(int playerId, int xp, int item) //Outcome
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		c.addSkillXP(xp, 10); //Retrives xp
    		c.addItem(item, 1); //Adds item
    		String itemName2 = c.GetItemName(item);
    		String iName2 = itemName2.toLowerCase();
    		c.sendMessage("You catch "+ iName2 +"."); //Sends message
    		if(feathers) {
    			c.deleteItem(314, c.GetItemSlot(314), 1); //Deletes feathers
    		}
    		resetFish(playerId); //Resets all
    	}
    	
    	public void resetFish(int playerId)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		FishTimer = -1; //Fish timer reset
    		fish = -1; //Fish number reset
    		isFish = false; //Player is not fishing
    		feathers = false; //Resets feathers
    		c.resetAnimation(); //Stops animation
    	}
    
    	public void run() {
    		client c = (client) server.playerHandler.players[server.playerId];
    		if (FishTimer > 0) //If the random timer from the code above is more than zero this will come into action
    		{
    			FishTimer--; //It will then take away 1 every 500ms? so 2 a second until...
    		} 
    		else if (FishTimer == 0) //... it equals 0 which will then...
    		{
    			getFish(server.playerId); //... give us our output
    		}
    	}
    
    }

    Outcome:
    Code:
    /*
    
    Title: Fishing
    Author: XharveyX
    
    */
    
    import java.io.*;
    
    public class Fishing extends Thread {
    
    	public int fish = -1;
    	public int FishTimer = -1;
    	public boolean isFish = false;
    	public boolean feathers = false;
    
    	public int net = 303;
    	public int netE = 0x26D;
    
    	public void fish(int playerId, int reqItem, int reqLvl, int emote, int time, boolean reqFeathers, int fishNumber)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		if(c.playerHasItem(reqItem))
    		{
    			if(c.playerLevel[10] >= reqLvl)
    			{
            			if (c.freeSlots() > 0)
            			{
    					if(reqFeathers == true)
    					{
    						if(c.playerHasItem(314))
    						{
    							String itemName = c.GetItemName(reqItem);
    							String iName = itemName.toLowerCase();
    							c.sendMessage("You place your " + iName + " in the water!");
    							c.pEmote = emote;
    							FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10);
    							fish = fishNumber;
    							isFish = true;
    							feathers = true;
    						} else {
    							c.sendMessage("You need feathers to perform this task!");
    						}
    					} else {						
    						String itemName1 = c.GetItemName(reqItem);
    						String iName1 = itemName1.toLowerCase();
    						c.sendMessage("You place your " + iName1 + " in the water!");
    						c.pEmote = emote;
    						FishTimer = (misc.random(time) + 10) - (c.playerLevel[10] / 10);
    						fish = fishNumber;
    						isFish = true;
    						feathers = false;
    					}
    				} else {
    					c.sendMessage("Not enough space in your inventory!");
    				}
    			} else {
    				c.sendMessage("You need "+reqLvl+" fishing to fish here!");
    			}
    		} else {
    			c.sendMessage("You need a "+ c.GetItemName(reqItem) + " to fish here!");
    		}
    	}
    
    	public void fishId(int playerId, int fishId)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		switch(fishId)
    		{
    			case 316:
    				fish(playerId, net, 1, netE, 15, false, 1); //Example: 
    									    //Requires net, Level Required: 1, 
    									    //the netEmote, Time: 15  + 10 / Level, 
    									    //No feathers needed, 
    									    //Fish number 1 (Case 1 in method getFish)
    				break;
    		}
    	}
    
    	public void getFish(int playerId)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		switch(fish)
    		{
    			case 1:
    				hasFished(playerId, 10, 317);
    				break;
    		}
    	}
    
    	public void hasFished(int playerId, int xp, int item)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		c.addSkillXP(xp, 10);
    		c.addItem(item, 1);
    		String itemName2 = c.GetItemName(item);
    		String iName2 = itemName2.toLowerCase();
    		c.sendMessage("You catch "+ iName2 +".");
    		if(feathers) {
    			c.deleteItem(314, c.GetItemSlot(314), 1);
    		}
    		resetFish(playerId);
    	}
    	
    	public void resetFish(int playerId)
    	{
    		client c = (client) server.playerHandler.players[playerId];
    		FishTimer = -1;
    		fish = -1;
    		isFish = false;
    		feathers = false;
    		c.resetAnimation();
    	}
    
    	public void run()
    	{
    		client c = (client) server.playerHandler.players[server.playerId];
    		if (FishTimer > 0)
    		{
    			FishTimer--;
    		} 
    		else if (FishTimer == 0)
    		{
    			getFish(server.playerId);
    		}
    	}
    }
    Step 5 - Making it work!

    In client.java find case 155 (First client npc packet) and add
    Code:
    server.Fishing.fishId(playerId, NPCID);
    In process add
    Code:
    server.playerId = playerId;
    Add method resetSkills() and make it look like:
    Code:
    	public void resetSkills()
    	{
    		server.Fishing.fish = -1;
    		server.Fishing.FishTimer = -1;
    		server.Fishing.isFish = false;
    		server.Fishing.feathers = false;
    		resetAnimation();
    	}
    And declare that in all walking packets, logout packets etc.

    In server.java in the main method add:
    Code:
    		Fishing = new Fishing();
    and

    Code:
    			(new Thread(Fishing)).start(); //Start fishing thread
    and declare these:

    Code:
    	public static Fishing Fishing = null;
    	public static int playerId;
    Credits: 100% Me (XharveyX)

    Dont like? Dont post. Think i leached? Piss off.

    Thanks,

    Harvey
     

  2. #2  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,074
    Thanks given
    2,625
    Thanks received
    3,579
    Discord
    View profile
    Rep Power
    5000
    Why do you need a thread for that?:

    Code:
    		client c = (client) server.playerHandler.players[server.playerId];
    		if (FishTimer > 0)
    		{
    			FishTimer--;
    		} 
    		else if (FishTimer == 0)
    		{
    			getFish(server.playerId);
    		}
    This code is far from 'perfect'.

    In fact, did you even test it?

    Your thread will infact end as soon as it starts. And you start it as soon as the client logs in.

    This is just a failure tbh.

    Edit: why do you have the Java expert or whatever it is title? You clearly are no expert.
    .
     

  3. #3  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    2,022
    Thanks given
    145
    Thanks received
    871
    Rep Power
    4275
    Quote Originally Posted by Graham View Post
    Why do you need a thread for that?:

    Code:
    		client c = (client) server.playerHandler.players[server.playerId];
    		if (FishTimer > 0)
    		{
    			FishTimer--;
    		} 
    		else if (FishTimer == 0)
    		{
    			getFish(server.playerId);
    		}
    This code is far from 'perfect'.

    In fact, did you even test it?

    Your thread will infact end as soon as it starts. And you start it as soon as the client logs in.

    This is just a failure tbh.
    true, as void run only gets called 1ce at startup
    and client c = (client) server.playerHandler.players[server.playerId];
    isn't needed as client c is never used.
    hella titties
     

  4. #4  
    Registered Member
    Harvey's Avatar
    Join Date
    Mar 2007
    Posts
    965
    Thanks given
    0
    Thanks received
    55
    Rep Power
    296
    Um... i have tested it and it works and yes the thread will start but will do nout. and i needed it for the timer. and if you dont use the client c thing, it will crash.

    Edit: The Client c bit, sorry didnt realise becaus i changed so less is need in client.java, before it used client c.
    Thanks,

    Harvey
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •