1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Attempt to lower redundancy

Discussion in 'Developer Support' started by Geashaw, Mar 5, 2015.

  1. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    An enum with location => ore would be nice, how could I attempt such thing to make the following less redundant?

    Code (Java):
    1. package com.runemate.geashawscripts.LazyAIOMiner.gui;
    2.  
    3. import com.runemate.game.api.hybrid.location.Area;
    4. import com.runemate.game.api.hybrid.location.Coordinate;
    5. import com.runemate.geashawscripts.LazyAIOMiner.LazyAIOMiner;
    6. import javafx.beans.value.ChangeListener;
    7. import javafx.beans.value.ObservableValue;
    8. import javafx.collections.FXCollections;
    9. import javafx.collections.ObservableList;
    10. import javafx.fxml.FXML;
    11. import javafx.scene.control.Button;
    12. import javafx.scene.control.CheckBox;
    13. import javafx.scene.control.ListView;
    14.  
    15. import java.util.Arrays;
    16.  
    17. /**
    18. * Created by Geashaw on 10-2-2015.
    19. */
    20. public class Controller {
    21.  
    22.     @FXML
    23.     private CheckBox checkboxPowermine;
    24.     @FXML
    25.     private CheckBox checkboxDropGems;
    26.     @FXML
    27.     private ListView < String > listLocation;
    28.     @FXML
    29.     private ListView < String > listOre;
    30.     @FXML
    31.     private Button buttonStart;
    32.  
    33.     @FXML
    34.     private ObservableList < String > listLocationData = FXCollections.observableArrayList(
    35.         "North-east Ardougne", "South-east Varrock", "South-west Varrock", "Al Kharid", "Rimmington");@FXML
    36.     private ObservableList < String > listOreData = FXCollections.observableArrayList(
    37.         "Clay", "Tin ore", "Copper ore", "Iron ore", "Coal");@FXML
    38.     private ObservableList < String > listArdougneData = FXCollections.observableArrayList(
    39.         "Iron ore", "Coal");@FXML
    40.     private ObservableList < String > listSouthEastVarrockData = FXCollections.observableArrayList(
    41.         "Tin ore", "Copper ore", "Iron ore");@FXML
    42.     private ObservableList < String > listSouthWestVarrockData = FXCollections.observableArrayList(
    43.         "Clay", "Tin ore", "Copper ore", "Iron ore");@FXML
    44.     private ObservableList < String > listAlKharidData = FXCollections.observableArrayList(
    45.         "Clay", "Tin ore", "Copper ore", "Iron ore", "Coal", "Silver ore", "Gold ore", "Mithril ore", "Adamantite ore");@FXML
    46.     private ObservableList < String > listRimmingtonData = FXCollections.observableArrayList(
    47.         "Clay", "Tin ore", "Copper ore", "Iron ore", "Gold ore");
    48.  
    49.     final Area VarrockEastMineArea = new Area.Rectangular(new Coordinate(3294, 3355, 0), new Coordinate(3276, 3372, 0));
    50.     final Area VarrockEastBankArea = new Area.Rectangular(new Coordinate(3249, 3415, 0), new Coordinate(3257, 3425, 0));
    51.  
    52.     final Area VarrockSouthMineArea = new Area.Rectangular(new Coordinate(3170, 3362, 0), new Coordinate(3185, 3380, 0));
    53.     final Area VarrockSouthBankArea = new Area.Rectangular(new Coordinate(3179, 3432, 0), new Coordinate(3190, 3447, 0));
    54.  
    55.     final Area AlKharidMineArea = new Area.Rectangular(new Coordinate(3286, 3275, 0), new Coordinate(3311, 3320, 0));
    56.     final Area AlKharidBankArea = new Area.Rectangular(new Coordinate(3264, 3160, 0), new Coordinate(3272, 3174, 0));
    57.  
    58.     final Area RimmingtonMineArea = new Area.Circular(new Coordinate(2977, 3238, 0), 14);
    59.     final Area RimmingtonBankArea = new Area.Rectangular(new Coordinate(3008, 3350, 0), new Coordinate(3018, 3359, 0));
    60.  
    61.     final Area ArdougneMineArea = new Area.Rectangular(new Coordinate(2686, 3325, 0), new Coordinate(2717, 3340, 0));
    62.     final Area ArdougneBankArea = new Area.Rectangular(new Coordinate(2647, 3278, 0), new Coordinate(2659, 3288, 0));
    63.  
    64.     Integer[] clay = {
    65.         7481, 7483, 13456, 13457, 13458
    66.     };
    67.     Integer[] tin = {
    68.         7484, 7486, 13447, 13448, 13449
    69.     };
    70.     Integer[] copper = {
    71.         7478, 7479, 7480, 13450, 13451, 13452, 13708
    72.     };
    73.     Integer[] iron = {
    74.         7487, 7488, 7489, 13444, 13445, 13446, 13710, 13711
    75.     };
    76.     Integer[] silver = {
    77.         13716, 13717
    78.     };
    79.     Integer[] coal = {
    80.         13706, 13714
    81.     };
    82.     Integer[] gold = {
    83.         7490, 7492, 13707, 13715
    84.     };
    85.     Integer[] mithril = {
    86.         13718, 13719
    87.     };
    88.     Integer[] adamantite = {
    89.         14168, 13720
    90.     };
    91.  
    92.     public void initialize() {
    93.         listLocation.setItems(listLocationData);
    94.         listOre.setItems(listOreData);
    95.  
    96.         listLocation.getSelectionModel().selectedItemProperty().addListener(new ChangeListener < String > () {@Override
    97.             public void changed(ObservableValue <? extends String > observable, String oldValue, String newValue) {
    98.  
    99.                 if (observable.getValue().equals("North-east Ardougne")) {
    100.                     listOre.setItems(listArdougneData);
    101.                 } else if (observable.getValue().equals("South-east Varrock")) {
    102.                     listOre.setItems(listSouthEastVarrockData);
    103.                 } else if (observable.getValue().equals("South-west Varrock")) {
    104.                     listOre.setItems(listSouthWestVarrockData);
    105.                 } else if (observable.getValue().equals("Al Kharid")) {
    106.                     listOre.setItems(listAlKharidData);
    107.                 } else if (observable.getValue().equals("Rimmington")) {
    108.                     listOre.setItems(listRimmingtonData);
    109.                 }
    110.             }
    111.         });
    112.     }
    113.  
    114.     public void startController() {
    115.  
    116.         // Set the action event.
    117.         buttonStart.setOnAction(event - > {
    118.  
    119.             // Get location and ore.
    120.             String location = listLocation.getSelectionModel().getSelectedItem().toString();
    121.             String ore = listOre.getSelectionModel().getSelectedItem().toString();
    122.  
    123.             // Set location for debugging.
    124.             LazyAIOMiner.location = location;
    125.             LazyAIOMiner.oreName = listOre.getSelectionModel().getSelectedItem().toString();
    126.  
    127.             if (location.equals("North-east Ardougne")) {
    128.                 LazyAIOMiner.bankArea = ArdougneBankArea;
    129.                 LazyAIOMiner.mineArea = ArdougneMineArea;
    130.             } else if (location.equals("South-east Varrock")) {
    131.                 LazyAIOMiner.bankArea = VarrockEastBankArea;
    132.                 LazyAIOMiner.mineArea = VarrockEastMineArea;
    133.             } else if (location.equals("South-west Varrock")) {
    134.                 LazyAIOMiner.bankArea = VarrockSouthBankArea;
    135.                 LazyAIOMiner.mineArea = VarrockSouthMineArea;
    136.             } else if (location.equals("Al Kharid")) {
    137.                 LazyAIOMiner.bankArea = AlKharidBankArea;
    138.                 LazyAIOMiner.mineArea = AlKharidMineArea;
    139.             } else if (location.equals("Rimmington")) {
    140.                 LazyAIOMiner.bankArea = RimmingtonBankArea;
    141.                 LazyAIOMiner.mineArea = RimmingtonMineArea;
    142.             }
    143.  
    144.             if (ore.equals("Clay")) {
    145.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(clay));
    146.             } else if (ore.equals("Tin ore")) {
    147.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(tin));
    148.             } else if (ore.equals("Copper ore")) {
    149.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(copper));
    150.             } else if (ore.equals("Iron ore")) {
    151.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(iron));
    152.             } else if (ore.equals("Silver ore")) {
    153.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(silver));
    154.             } else if (ore.equals("Coal")) {
    155.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(coal));
    156.             } else if (ore.equals("Gold ore")) {
    157.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(gold));
    158.             } else if (ore.equals("Mithril ore")) {
    159.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(mithril));
    160.             } else if (ore.equals("Adamantite ore")) {
    161.                 LazyAIOMiner.oreObjectIds.addAll(Arrays.asList(adamantite));
    162.             }
    163.  
    164.             if (checkboxPowermine != null) {
    165.                 if (checkboxPowermine.isSelected()) {
    166.                     LazyAIOMiner.powermine = true;
    167.                 } else {
    168.                     LazyAIOMiner.powermine = false;
    169.                 }
    170.             }
    171.  
    172.             if (checkboxDropGems != null) {
    173.                 if (checkboxDropGems.isSelected()) {
    174.                     LazyAIOMiner.dropgems = true;
    175.                 } else {
    176.                     LazyAIOMiner.dropgems = false;
    177.                 }
    178.             }
    179.  
    180.             LazyAIOMiner.guiOpen = false;
    181.             // Close the gui.
    182.             Loader.guiStage.close();
    183.         });
    184.     }
    185. }
     
  2. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    What have you done? XD Do a few things for me. Create a class for a custom object that will contain your mappings, i.e. MiningLocationMapping, and give it a meaningful toString method. Make the generic type of ListView from String to MiningLocationMapping. When you need to finally execute based on selection listView...getSelectedItem() will return a MiningLocationMapping that will hold all the information you'll need to configure the bot. Lastly, look into bindings. It's much cleaner.

    P.S. Good job acknowledging there was something bad about this code and congratulations on using software that facilitates proper design paradigms. JavaFX > Swing
     
    #2 Arbiter, Mar 5, 2015
    Last edited: Mar 5, 2015
  3. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    That sounds like a .. "DAYUMMMMN GURL, this is too much code".
    I made that code by analysing how ListViews in java fx works using googles.

    Then I didn't get much further than the code above, which works splendid but is a bit overkill.
     
  4. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    I edited my OP with advice.
     
  5. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    So that would be something along the way Enums work? Returning the toString()?

    Something like;

    Code (Java):
    1. private ListView<String> listOre = MiningLocationMapping.toString();?
    When you need to finally execute based on selection listView...getSelectedItem() will return a MiningLocationMapping that will hold all the information you'll need to configure the bot.

    What would you need binding for in this case? I saw this video;


    As webdeveloper I don't really like redundancy ^^
     
  6. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    More like
    Code (Java):
    1.  
    2. private static final List<MiningLocationMapping> locations = FXCollection.newObservableList();
    3. static {
    4.     locations.add(firstLocation);
    5.     locations.add(secondLocation);
    6.     ...
    7. }
    8. private ListView<MiningLocationMapping> locationsListView = new ListView<>(locations);
    9.  
    and

    Code (Java):
    1. public class MiningLocationMapping {
    2. private String name;
    3. private int blah;
    4. ...
    5.  
    6. // So it renders nicely in the ListView
    7. @Override
    8. public String toString() {
    9.    return name;
    10. }
    11.  
    That's all the code I'll spoonfeed you. The rest you should be able to figure out, including the use case for bindings. Think about how you would want to expand this later. Hint: What if the user wanted to change the mining location while the bot is running? How can you easily achieve this in one line instead of 20 without any re-configuration?
    --- Double Post Merged, Mar 5, 2015, Original Post Date: Mar 5, 2015 ---
    P.S. That's not a valid line of code. Class mismatch; ListView<String> != String
     
  7. Savior

    Savior Java Warlord

    Joined:
    Nov 17, 2014
    Messages:
    4,906
    Likes Received:
    2,748
    Offtopic: Oh you are actually a girl or what? xd
     

Share This Page

Loading...