How would I go about making this code shorter

Discussion in 'Plugin Development' started by tommyhoogstra, Nov 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    tommyhoogstra

    Currently working on a Prison type plugin, and there are 4 classes (D, C, B, A). Each class has there own mining area, where they can break blocks and sell them, and every so often the blocks will respawn, allowing the players to start mining again.

    So i'm doing this from scratch, without any WorldGuard, but just seeing the code for ONE of the classes IF checks makes me think I have some very poor code.

    I am using vectors to define the breakable regions

    A current solution I've been looking at is storing the vectors in a HashMap like so : minVector, maxVector and looping through them. But I don't want to save hashmaps.

    Currently here is my initial check method

    Code:java
    1. public boolean inMine(Player p, Block b, Vector min, Vector max){
    2. Vector v = new Vector(p.getLocation().getX(), p.getLocation().getY(), p.getLocation().getZ());
    3. Vector x = new Vector(b.getLocation().getX(), b.getLocation().getY(), p.getLocation().getZ());
    4. if(v.isInAABB(min, max) && x.isInAABB(min, max)){
    5. return true;
    6. }else{
    7. return false;
    8. }
    9. }


    This is untested, but im positive it will work all good and dandy.

    Now for the long part
    Code:java
    1. if(inMine(e.getPlayer(), e.getBlock(), new Vector(ConfigManager.getInt("Mines.ClassD.MinX"), ConfigManager.getInt("Mines.ClassD.MinY"), ConfigManager.getInt("Mines.ClassD.MinZ")),
    2. new Vector(ConfigManager.getInt("Mines.ClassD.MaxX"), ConfigManager.getInt("Mines.ClassD.MaxY"), ConfigManager.getInt("Mines.ClassD.MaxZ"))));


    And basically, to check if a player is in any of the mines, its going to be that ^ multiplied by 4.

    Laugh away, its 2AM and I currently can't think of any effective/efficient alternatives.
     
  2. Offline

    adam753

    How about if ConfigManager had methods like this?
    Code:java
    1. getDMin() {
    2. return new Vector(config.getInt("Mines.ClassD.MinX"),
    3. config.getInt("Mines.ClassD.MinY"),
    4. config.getInt("Mines.ClassD.MinZ"));
    5. }
     
  3. Offline

    tommyhoogstra

    I too looked at this option, but thats still 8 individual Vector methods ill need, which seems unnecessary.
     
  4. Offline

    adam753

    How about this?
    Code:java
    1. getMin(String m) {
    2. return new Vector(config.getInt("Mines.Class" + m + ".MinX"),
    3. config.getInt("Mines.Class" + m + ".MinY"),
    4. config.getInt("Mines.Class" + m + ".MinZ"));
    5. }
    6.  
    7.  
    8. //Use:
    9. ConfigManager.getMin("D")
     
  5. Offline

    tommyhoogstra



    Brilliant work, i think that will work just fine.
    Thanks!
     
  6. Offline

    teej107

    Code:java
    1. //Code above here
    2. return v.isInAABB(min, max) && x.isInAABB(min, max);
     
  7. Offline

    tommyhoogstra

    That would go in the boolean method?
     
  8. Offline

    teej107

  9. Offline

    tommyhoogstra

  10. Offline

    Syd

    tommyhoogstra
    It seems like your corner Vectors don't change in runtime, so why don't you simply load them on startup and save them in some variables instead of creating a new instance every time you need it?
     
  11. Offline

    thomasb454


    It would be better to create a Mine object.
     
Thread Status:
Not open for further replies.

Share This Page