Thread: Better Way of Handling Teleports?

Results 1 to 7 of 7
  1. #1 Better Way of Handling Teleports? 
    Registered Member
    Join Date
    Aug 2016
    Posts
    81
    Thanks given
    5
    Thanks received
    24
    Rep Power
    56
    is there a way to combine these into one line?

    ClickingButtons.java:
    Code:
    	case 5227:
    		player.send(new SendInterface(50000));
    		player.send(new SendString("<shad=-1>Training Teleports", 33337));
    		player.send(new SendString("<shad=-1>Rock Crabs (Relleka)", 33338));
    Code:
    	case 130058:
    		player.getMagic().teleport(TeleportationData.ROCK_CRABS.getPosition(), TeleportTypes.SPELL_BOOK);
    		break;
    TeleportHandler.java:
    Code:
    		ROCK_CRABS(238123, 0, "None", "None", 0, new Location(2674, 3710, 0), false, false),
    rather than making a separate case for each teleport.
    Reply With Quote  
     

  2. #2  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    You oughta give some more information.
    By the way you're writing it, I take it you've got a ton of teleports to write?
    Try to see a pattern within the cases. If you can find one, you can shorten the code as well. However if there's no pattern, you will need to define each and every single one seperately.

    But yeah you should post more than a few lines, not getting the full picture of this right now; Cannot suggest much without seeing it fully.
    Reply With Quote  
     

  3. #3  
    Owner of Dawntained

    Mgt Madness's Avatar
    Join Date
    Oct 2011
    Age
    28
    Posts
    3,380
    Thanks given
    1,429
    Thanks received
    958
    Rep Power
    2168
    Add more fields to the enum so it can store the clickingbutton data and interface data
    Attached image
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Aug 2016
    Posts
    81
    Thanks given
    5
    Thanks received
    24
    Rep Power
    56
    Quote Originally Posted by Mgt Madness View Post
    Add more fields to the enum so it can store the clickingbutton data and interface data
    i'm aware of that but i'm not sure where to start?
    Reply With Quote  
     

  5. #5  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by manures View Post
    i'm aware of that but i'm not sure where to start?
    What he suggested hardly does any good, it just fills the enum with variables it shouldn't really contain.
    What he means is what you change to enum to support more variables, as for example:
    Code:
    ROCK_CRABS(238123, 0, "None", "None", 0, new Location(2674, 3710, 0), false, false, 5227, 130058),//notice I added two more numbers in the end, both are being the numbers relevant to the 'cases'. However there may be more components for the given slot, so it could get ugly there.
    After which you could simply, instead of switching different componentID's (Assuming you're switching components) you could loop through the enum values, for example
    Code:
    for (TeleportData teles : TeleportData.values()) {
    if (teles.getComponentId() == componentId)
    player.getMagic().teleport(teles.getPosition(), TeleportTypes.SPELL_BOOK);
    }
    However I personally wouldn't advise storing data such as component IDs within an enum, it'll just look nasty especially if there are loads of them. If there's a pattern within the components, you could do it much better, for example:

    Say there are components 5000, 5002, 5004, 5006 etc.
    Instead of switching the components, you could simply write something like:
    Code:
    //this is WITHIN the enum
    		private static final HashMap<Integer, TeleportData> TELES = new HashMap<Integer, TeleportData>();
    		
    		static {
    			for (TeleportData t : values())
    				TELES.put(t.ordinal(), t);
    		}
    		
    		public static TeleportData getTeleport(int id) {
    			return TELES.get(id);
    		}
    & in the spot in which it currently switches the components, you could do it as
    if (componentId >= 5000 && componentId <= 5010) {
    player.getMagic().teleport(TeleportData.getTelepor t((componentId - 5010) / 2), TeleportTypes.SPELL_BOOK);
    }
    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Aug 2016
    Posts
    81
    Thanks given
    5
    Thanks received
    24
    Rep Power
    56
    Quote Originally Posted by Eldritch View Post
    What he suggested hardly does any good, it just fills the enum with variables it shouldn't really contain.
    What he means is what you change to enum to support more variables, as for example:
    Code:
    ROCK_CRABS(238123, 0, "None", "None", 0, new Location(2674, 3710, 0), false, false, 5227, 130058),//notice I added two more numbers in the end, both are being the numbers relevant to the 'cases'. However there may be more components for the given slot, so it could get ugly there.
    After which you could simply, instead of switching different componentID's (Assuming you're switching components) you could loop through the enum values, for example
    Code:
    for (TeleportData teles : TeleportData.values()) {
    if (teles.getComponentId() == componentId)
    player.getMagic().teleport(teles.getPosition(), TeleportTypes.SPELL_BOOK);
    }
    However I personally wouldn't advise storing data such as component IDs within an enum, it'll just look nasty especially if there are loads of them. If there's a pattern within the components, you could do it much better, for example:

    Say there are components 5000, 5002, 5004, 5006 etc.
    Instead of switching the components, you could simply write something like:
    Code:
    //this is WITHIN the enum
    		private static final HashMap<Integer, TeleportData> TELES = new HashMap<Integer, TeleportData>();
    		
    		static {
    			for (TeleportData t : values())
    				TELES.put(t.ordinal(), t);
    		}
    		
    		public static TeleportData getTeleport(int id) {
    			return TELES.get(id);
    		}
    & in the spot in which it currently switches the components, you could do it as
    if (componentId >= 5000 && componentId <= 5010) {
    player.getMagic().teleport(TeleportData.getTelepor t((componentId - 5010) / 2), TeleportTypes.SPELL_BOOK);
    }
    i can see what you mean, thank you!
    Reply With Quote  
     

  7. #7  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by manures View Post
    i can see what you mean, thank you!
    Of course I have to say that if you're not very experienced, you should go with the first one. The second method, AKA which I'd use is quite sensitive. The second one solely depends on the order in which the enum entries are, a simple switch or removal of an entry and the entire thing would come collapsing down on you. enum.ordinary() method isn't what you should always depend on. Just sayin'. (If you don't know what it does it, it simply gets the enum entry value, starting off at zero).
    A quick example in case you're not fully understanding yet:
    Code:
    public enum Random {
    FIRST_ENTRY(),//0
    SECOND_ENTRY(),//1
    THIRD_ENTRY(),//2
    FOURTH_ENTRY;//3
    }
    Using the .ordinary() method on the enum will get the values I commented.

    Note: Only drawing this out like that so you'd understand what I meant by 'sensitivity' there.
    Reply With Quote  
     


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. 562 Better way to handle overloads
    By Makar in forum Snippets
    Replies: 17
    Last Post: 11-01-2011, 10:16 PM
  2. What's the best way to handle dialogue?
    By Paketa in forum Help
    Replies: 3
    Last Post: 04-11-2009, 02:37 AM
  3. Adding a nice way to handle objects (emulous)
    By Livinglife in forum Tutorials
    Replies: 15
    Last Post: 03-23-2009, 10:15 PM
  4. Replies: 14
    Last Post: 03-15-2009, 11:07 AM
  5. More Efficient way to load MapData
    By HcoFlame in forum Tutorials
    Replies: 33
    Last Post: 09-16-2008, 11:18 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
  •