Thread: Array or HashMap question

Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1 Array or HashMap question 
    🍕

    Linus's Avatar
    Join Date
    Dec 2008
    Age
    31
    Posts
    2,779
    Thanks given
    974
    Thanks received
    411
    Rep Power
    0
    So I'm creating a spell where the player uses the spell on a ground item to create a fire.
    I need the coords to be saved in either a hashmap or an array so that I can make the fire deal damage if you stand on it.
    How would I go on about doing that?

    Code:
    public HashMap<Integer, Integer> burningFires = new HashMap<Integer, Integer>();
    How would I add to that, remove from that and read from that?

    Quote Originally Posted by Linus
    Hello, I'm going to trust you with the code that I've made so far since I have not really came down to the quality of the code yet.
    Things that are missing; all players that step on the fire gets hurt
    all players can see the fire
    only one fire coords are stored.



    Code:
    package server.model.players.content;
    
    import server.Server;
    import server.event.CycleEvent;
    import server.event.CycleEventContainer;
    import server.event.CycleEventHandler;
    import server.model.players.Client;
    
    public class CreateFlame {
    
    	//player uses spell on ground item, ground item is removed and a fire is made. 
    	//standing on the fire will cause 1-2 damage per client.process() cycle. This
    	//can only be done in wilderness.
    	public static int igniteId = 1502;
    	
    	public static void spellCast(final Client c, final int itemId, final int itemX, final int itemY){
    		if(c == null)return;
    		if(itemId == igniteId){
    			if(c.ignitedFireId != -1){ 
    				c.sendMessage("You can only have one fire started at a time.");
    				return;
    			}
    			c.igniteStage = 0;
    			c.turnPlayerTo(itemX, itemY);
    			c.startAnimation(708);//iban emote
    			c.gfx100(246);//dragon battleaxe gfx
    			//88 - iban skull projectile
    			//ibans ashes = 1502
    			CycleEventHandler.getSingleton().addEvent(c, new CycleEvent() {
    				@Override
    				public void execute(CycleEventContainer container) {
    					if(c.igniteStage == 0){
    						c.getPA().createPlayersProjectile(itemX, itemY, 1, 1, 50, 550/*speed*/, 88/*id*/, 43, 31, -c.getId() - 1, 65);
    					}
    					if(c.igniteStage == 1){
    						Server.itemHandler.removeGroundItem(c, igniteId, itemX, itemY, false);
    						c.getPA().checkObjectSpawn(11404, itemX, itemY, 2, 10);
    				    	c.ignitedFireLength = System.currentTimeMillis();
    				    	c.ignitedFireId = 1;
    				    	c.burningFires.put(0, itemX);
    				    	c.burningFires.put(1, itemY);
    						c.sendMessage(c.burningFires.get(0)+"");
    						c.sendMessage(c.burningFires.get(1)+"");
    					}
    					if(c.igniteStage == 10){
    						container.stop();
    						return;
    					}
    					c.igniteStage++;
    				}
    				@Override
    				public void stop() {
    					c.getPA().checkObjectSpawn(-1, itemX, itemY, 2, 10);
    			    	c.ignitedFireLength = 0;
    					c.ignitedFireId = -1;
    					c.sendMessage("Your fire burns out..");
    					c.burningFires.clear();
    				}
    			}, 2); 
    		} else {
    			c.sendMessage("Nothing interesting happens.");
    		}
    	}
    }
    Client process
    Code:
    		if(burningFires.get(0) != null && burningFires.get(1) != null)
    			if(absX == burningFires.get(0) && absY == burningFires.get(1)){
    				if(lastBurn+1000 >= System.currentTimeMillis() || lastBurn == 0){
    					sendMessage("You are burned by the fire!");
    					lastBurn = System.currentTimeMillis();
    				}
    				 // System.out.println("key: " + key + " value: " + burningFires.get(key));
    				//}
    			}
    	}
    	public HashMap<Integer, Integer> burningFires = new HashMap<Integer, Integer>();
    player
    Code:
    	public int igniteStage = -1;
    	public int ignitedFireId = -1;
    	public long ignitedFireLength = 0;
    	public long lastBurn = 0;
    I can't figure the storing of a fire id & coords part out. If you think you can program it feel free to do so, you may release with some credits to me.

    ╠╬╣
    ╦╦
    ╠╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╚╩╩╩╩╩╩╝

    ╠╬╬╬╣



    Reply With Quote  
     

  2. #2  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    28
    Posts
    4,132
    Thanks given
    1,077
    Thanks received
    1,137
    Rep Power
    5000
    Why array or hashmap? lol... Why would you even use a hashmap for this? lol, I'm not really sure what you're trying to do here tbh.
    Reply With Quote  
     

  3. #3  
    🍕

    Linus's Avatar
    Join Date
    Dec 2008
    Age
    31
    Posts
    2,779
    Thanks given
    974
    Thanks received
    411
    Rep Power
    0
    Quote Originally Posted by RSAddict512 View Post
    Why array or hashmap? lol... Why would you even use a hashmap for this? lol, I'm not really sure what you're trying to do here tbh.
    I already told you what I'm doing. I'm trying to add 2 new ints in an array so that I can load and remove them later on. I don't know the first thing about hashmaps

    ╠╬╣
    ╦╦
    ╠╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╚╩╩╩╩╩╩╝

    ╠╬╬╬╣



    Reply With Quote  
     

  4. #4  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    28
    Posts
    4,132
    Thanks given
    1,077
    Thanks received
    1,137
    Rep Power
    5000
    Quote Originally Posted by Linus View Post
    I already told you what I'm doing. I'm trying to add 2 new ints in an array so that I can load and remove them later on.
    What are the two variables?
    Reply With Quote  
     

  5. #5  
    🍕

    Linus's Avatar
    Join Date
    Dec 2008
    Age
    31
    Posts
    2,779
    Thanks given
    974
    Thanks received
    411
    Rep Power
    0
    Quote Originally Posted by RSAddict512 View Post
    What are the two variables?
    coordX & Y of the created ground object.

    ╠╬╣
    ╦╦
    ╠╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╚╩╩╩╩╩╩╝

    ╠╬╬╬╣



    Reply With Quote  
     

  6. #6  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    28
    Posts
    4,132
    Thanks given
    1,077
    Thanks received
    1,137
    Rep Power
    5000
    Quote Originally Posted by Linus View Post
    coordX & Y of the created ground object.
    Ah. You don't want to be using a Map of any sort for that then. Do you not have a Position class like in Hyperion (iirc)? Do you just need to store the position values or the item as well?
    Reply With Quote  
     

  7. #7  
    Registered Member Mr Jason's Avatar
    Join Date
    Dec 2013
    Posts
    384
    Thanks given
    37
    Thanks received
    113
    Rep Power
    0
    Quote Originally Posted by Linus View Post
    coordX & Y of the created ground object.
    Declare
    Code:
    public static Map<Integer, String> burningFires = new HashMap<Integer, String>(5);
    To store the values inside the hashmap you would do
    Code:
                     burningFires.put(0, c.absX);
                     burningFires.put(1, c.absY);
    To read them you would do something like,

    Code:
    burningFires.get(0); //would return c.absX
    burningFires.get(1); //would return c.absY
    To clear everything inside the hashmap you would do
    Code:
    burningFires.clear();



    'Our greatest glory is not in never failing, but in rising up every time we fail', ~Ralph Waldo Emerson.
    Reply With Quote  
     

  8. #8  
    🍕

    Linus's Avatar
    Join Date
    Dec 2008
    Age
    31
    Posts
    2,779
    Thanks given
    974
    Thanks received
    411
    Rep Power
    0
    Quote Originally Posted by RSAddict512 View Post
    Ah. You don't want to be using a Map of any sort for that then. Do you not have a Position class like in Hyperion (iirc)? Do you just need to store the position values or the item as well?
    I'm not sure what you're trying to help me with, I just need the info on how to store & load the x & y coord of my fire.

    ╠╬╣
    ╦╦
    ╠╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╠╬╬╬╬╬╬╣
    ╚╩╩╩╩╩╩╝

    ╠╬╬╬╣



    Reply With Quote  
     

  9. #9  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    28
    Posts
    4,132
    Thanks given
    1,077
    Thanks received
    1,137
    Rep Power
    5000
    Quote Originally Posted by JasonRTM View Post
    Declare
    Code:
    public static Map<Integer, String> burningFires = new HashMap<Integer, String>(5);
    To store the values inside the hashmap you would do
    Code:
                     burningFires.put(0, c.absX);
                     burningFires.put(1, c.absY);
    To read them you would do something like,

    Code:
    burningFires.get(0); //would return c.absX
    burningFires.get(1); //would return c.absY
    To clear everything inside the hashmap you would do
    Code:
    burningFires.clear();
    Just use a 2 dimensional Array then
    Reply With Quote  
     

  10. #10  
    Banned

    Join Date
    Oct 2010
    Posts
    716
    Thanks given
    82
    Thanks received
    131
    Rep Power
    0
    Quote Originally Posted by RSAddict512 View Post
    Still retarded lol. (not to offend you)
    Let him program the style he prefers? Atleast he wants to learn about Collections, so why break him down on that?
    There's plenty of people just sitting there leeching, and I applaud Linus for taking the effort to LEARN, and you should too.
    Reply With Quote  
     

  11. Thankful user:


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

Similar Threads

  1. Replies: 0
    Last Post: 04-27-2012, 05:09 PM
  2. Question about HashMaps
    By Bridget7298 in forum Help
    Replies: 4
    Last Post: 04-21-2009, 06:41 PM
  3. Replies: 8
    Last Post: 03-01-2009, 04:46 AM
  4. Replies: 8
    Last Post: 01-11-2009, 11:27 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •