Thread: [508] Firemaking with Events - nearly 100%

Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1. #1 [508] Firemaking with Events - nearly 100% 
    zeroeh
    Guest
    Purpose: Ability to dig into more Object Orientated Programming with event manager and arraylists.

    Difficulty: 6

    Tested On: Espeon, eventmanager

    Lets get started


    create a file called Remove.java

    We will be using this to remove fires later on.

    Code:
    /**
     * @author Zeroeh <[email protected]> <rune-server.ee>
     */
    package net.com.espeon.model.skills;
    import java.util.ArrayList;
    
    public class Remove {
    	
    
    
    }
    its a basic dry class with our imports of arraylist. so lets add in some stuff


    add in the following

    Code:
    	private static ArrayList<Fire> fires = new ArrayList<Fire>();
    	
    	public static ArrayList<Fire> getFires() {
        	      return fires;
    	}
    This is how we will be storing our fires in an arraylist.


    Code:
    /**
     * @author Zeroeh <[email protected]> <rune-server.ee>
     */
    package net.com.espeon.model.skills;
    import java.util.ArrayList;
    
    public class Remove {
    	
    	private static ArrayList<Fire> fires = new ArrayList<Fire>();
    	
    	public static ArrayList<Fire> getFires() {
        	      return fires;
    	}
    
    }
    I will not be explaining this part.
    Code:
    
    /**
     * @author Zeroeh <[email protected]> <rune-server.ee>
     */
    package net.com.espeon.model.skills;
    
    import net.com.espeon.*;
    import net.com.espeon.model.*;
    
    public class Fire {
    
    	/**
    	 * Simple variable to call Player class by using constructor
    	 * which has the parameter called p.
    	 */
    	 
    	Player p;
    
    	private int id = 0;
    
    	private int absX = 0; // Should these be -1?
    	private int absY = 0; // should these be -1?
    	private int height = 0;
    	
    	private long timeCreated = 0;
    
    	private Player creator = null;
    
    	public Fire(int id, long timeCreated, int absX, int absY, int height, Player creator) {
    		this.id = id;
    		this.timeCreated = timeCreated;
    		this.absX = absX;
    		this.absY = absY;
    		this.height = height;
    		this.creator = creator;
    	}
    
    	public int getID() {
    		return id;
    	}
    		
    	public long getTimeCreated() {
    		return timeCreated;
    	}
    	
    	public int getX() {
    		return absX;
    	}
    	
    	public int getY() {
    		return absY;
    	}
    	
    	public int getHeight() {
    		return height;
    	}
    	
    	public Player getCreator() {
    		return creator;
    	}
    }


    okay now we got those out of the way lets start on the firemaking base.



    create a file in skills folder called Firemaking.java (screwed up conventions but w/e)


    add this into it

    Code:
    
    /**
     * @author Zeroeh <[email protected]> <rune-server.ee>
     */
    package net.com.espeon.model.skills;
    
    import net.com.espeon.*;
    import net.com.espeon.model.Player;
    import net.com.espeon.model.items.PlayerItems;
    import net.com.espeon.EventManager.*;
    import java.util.ArrayList;
    
    public class Firemaking {
    
    }

    Okay lets start going line by line


    Code:
    	/*
    	Declaring our methods
    	*/
        PlayerItems pi = new PlayerItems(); // for deleting  logs
    	public int logType; // to get th e log type
    	Player p; // self exp...
    	public Firemaking(Player p) {
    		this.p = p;
    	}

    we import playerItems for deleting logs from inv
    declare logType to get the id of the log we are burning

    everything else is self-exp.


    Now lets start creating methods first XP

    Code:
    	/* 
    	* We will set XP here for firemaking - Zeroeh
    	*/
    	int getXP() {
    		switch(logType) {
    		case 1511: // reg logs
    			return 250;
    		case 1521: // oak
    			return 350;
    		case 1519: // willow
    			return 450;
    		case 1517: //  maple
    			return 550;
    		case 1515: // yew
    			return 650;
    		case 1513: // magic
    			return 950;
    		default:
    			return -1;
    		}		
        }
    the cases are the id of the log being burnt

    the return is the xp amount per log.

    LEts now create a givexp method

    Code:
           /*
    	* We will give exp in this method
            */
    	public void giveXP(int amt) {
    		p.appendExperience(amt,11); // skill 11 = firemaking.
    	}

    Now lets create a lvl requirement to burn logs

    Code:
    	int getLvlReq() {
    		switch(logType) {
    		case 1511: // reg logs
    			return 1;
    		case 1521:
    			return 15;
    		case 1519:
    			return 30;
    		case 1517:
    			return 45;
    		case 1515:
    			return 60;
    		case 1513:
    			return 75;
    		default:
    			return 1;
    		}		 
        }
    again cases are the log ids
    returns are the XP gained.


    so lets get back to removing the fire

    first we need to set a burnlog timer

    Code:
    	/* 
    	* This will be used for fireburning tiiming
    	*/
    	public static long getBurningTime() {
            return 10000; // this is in milliseconds.
        }
    Now we are going to create an event that kills the fires from the array... so lets go

    Code:
    	/* 
    	* This checks fires
    	*/
    	public void checkFires() {  
    		EventManager.getSingleton().addEvent(new Event() {
    				@Override
    			public void execute(EventContainer c) {
    				for(int fire = 0; fire < Remove.getFires().size(); fire++) {
    					if(System.currentTimeMillis() - Remove.getFires().get(fire).getTimeCreated() >= getBurningTime()){
    						p.getActionSender().sendMessage(p,"Your fire has burnt out!");
    						p.getActionSender().deleteStaticObject(0, Remove.getFires().get(fire).getX(), Remove.getFires().get(fire).getY(), 10); // lets remove the gfire
    						Engine.items.createGroundItem(592, 1, Remove.getFires().get(fire).getX(), Remove.getFires().get(fire).getY(), Remove.getFires().get(fire).getHeight(), p.username);
    						Remove.getFires().remove(fire);
    					        p.appearanceUpdateReq = true;
    				                p.updateReq = true;
    					}
    				}
    			}
    		}, 4000); // check every 4 secs
    now what this method is doing is


    first is going through our arraylist and finding the arrays that have a fire in it.
    then we are checking if time is up for it to stop burning
    send a message to a user
    destory the fire in the world
    creates ashes were it was
    remove it from the arraylist
    repeat every 4 seconds.



    okay lets now create the main method


    Code:
    
    	/* 
    	* Lets create a firemaking event. We will be using event manager to do all the timing.
    	*/
    	
    	public void createFiremakingEvent(int logTypes){
    		if (p == null) {
    			return;
    		}
    		
    		logType = logTypes;
    		
    		if (p.skillLvl[11] < getLvlReq()) {
    			p.getActionSender().sendMessage(p, "You need an Firemaking level of "+getLvlReq()+" to make this burn");
    			return;
    		}
    			int objectX = p.absX;
    			int objectY = p.absY;
    			p.requestAnim(9068,0);
    			p.getActionSender().addStaticObject(2732, p.heightLevel, p.absX, p.absY, -1, 10);
    			int firstX = objectX - (p.mapRegionX - 6) * 8;
    		    int firstY = objectY - (p.mapRegionY - 6) * 8;
    		    Engine.playerMovement.resetWalkingQueue(p);
    			Engine.playerMovement.addToWalkingQueue(p, firstX - 1, firstY);
    			p.getActionSender().sendMessage(p,"You light the logs.");
    			Remove.getFires().add(new Fire(logType, System.currentTimeMillis(), p.absX, p.absY, p.heightLevel, p)); // add into our array list.
    			giveXP(getXP());
    			pi.deleteItem(p, logType, 1);
    			EventManager.getSingleton().addEvent(new Event() { // create the event to stop anims
    				@Override
    				public void execute(EventContainer c) {
    						p.requestAnim(-1,0); // our anim
    				}
    			}, 1000);
    				checkFires(); // lets start the checkfires timer. 
    	}

    Its pretty easy to understand whats going on in this method. I commented things that maybe are a bit unclear.


    now open itemonitem.java


    add

    Firemaking firemaking = new Firemaking(player);


    and usage:

    Code:
    		/*
    		* Start of firemaking here - Zeroeh
    		*/
    		if (itemUsed == 590 && usedWith == 1511 || itemUsed == 1511 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1511);
    		} 
    		
    		if (itemUsed == 590 && usedWith == 1521 || itemUsed == 1521 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1521);
    		} 
    		
    		if (itemUsed == 590 && usedWith == 1513 || itemUsed == 1513 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1513);
    		}
    		if (itemUsed == 590 && usedWith == 1515 || itemUsed == 1515 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1515);
    		}
    		if (itemUsed == 590 && usedWith == 1517 || itemUsed == 1517 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1517);
    		}
    		if (itemUsed == 590 && usedWith == 1519 || itemUsed == 1519 && usedWith == 590) {
    			firemaking.createFiremakingEvent(1519);
    		} 
    		/*
    		* end of firemaking here - Zeroeh
    		*/
    firemaking.createFiremakingEvent(log id);


    pictures:
    soon
    Last edited by zeroeh; 07-28-2009 at 12:22 AM. Reason: had forum links in post.
    Reply With Quote  
     

  2. #2  
    Aza
    Aza is offline
    Banned

    Join Date
    May 2008
    Posts
    2,583
    Thanks given
    99
    Thanks received
    22
    Rep Power
    0
    Beautifully done and explained. Very nice 5/5 and repped
    Reply With Quote  
     

  3. #3  
    Registered Member
    josue992222's Avatar
    Join Date
    Mar 2009
    Posts
    922
    Thanks given
    11
    Thanks received
    5
    Rep Power
    460
    amazing dude might think of replacing my fm.



    Reply With Quote  
     

  4. #4  
    Aza
    Aza is offline
    Banned

    Join Date
    May 2008
    Posts
    2,583
    Thanks given
    99
    Thanks received
    22
    Rep Power
    0
    Could you post your
    Code:
     deleteStaticObject
    method?
    Reply With Quote  
     

  5. #5  
    Tierney
    Guest
    HOly shit! nice dude! :cookie: and rep for you
    Reply With Quote  
     

  6. #6  
    Aza
    Aza is offline
    Banned

    Join Date
    May 2008
    Posts
    2,583
    Thanks given
    99
    Thanks received
    22
    Rep Power
    0
    Does anyone have the deletestaticobject method..?
    Reply With Quote  
     

  7. #7  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,141
    Thanks given
    256
    Thanks received
    1,247
    Rep Power
    3636
    Quote Originally Posted by Aza View Post
    Does anyone have the deletestaticobject method..?
    Use Nathan's delete object method

    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  8. #8  
    Aza
    Aza is offline
    Banned

    Join Date
    May 2008
    Posts
    2,583
    Thanks given
    99
    Thanks received
    22
    Rep Power
    0
    Quote Originally Posted by 'Mystic Flow View Post
    Use Nathan's delete object method
    Where is it =_=
    Reply With Quote  
     

  9. #9  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,141
    Thanks given
    256
    Thanks received
    1,247
    Rep Power
    3636
    Quote Originally Posted by Aza View Post
    Where is it =_=
    Go find it?

    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  10. #10  
    Aza
    Aza is offline
    Banned

    Join Date
    May 2008
    Posts
    2,583
    Thanks given
    99
    Thanks received
    22
    Rep Power
    0
    Quote Originally Posted by 'Mystic Flow View Post
    Go find it?
    Fien.

    Thanks mysty
    Reply With Quote  
     

Page 1 of 2 12 LastLast

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
  •