Skip to content

Commit

Permalink
Add extra abstraction for distance commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ricglz committed Jan 24, 2021
1 parent c267ffc commit d964024
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public void run(Player sender, Command command, String label, String[] args)
World world = sender.getWorld();
Location point1 = createLocation(world, args, 0);
Location point2 = args.length == 3 ? sender.getLocation() : createLocation(world, args, 3);
int distance = getDistance(point1, point2);
String message = String.format("The distance between the 2 points is %d units", distance);
sender.sendMessage(message);
getDistanceAndSendMessage(point1, point2, sender);
}

private Location createLocation(World world, String[] args, int start) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public void run(Player sender, Command command, String label, String[] args) thr
final Coordinates coords = discoords.getCoordinates();
Location point1 = coords.getCoordinates(args[0]);
Location point2 = args.length == 1 ? sender.getLocation() : coords.getCoordinates(args[1]);
int distance = getDistance(point1, point2);
String message = String.format("The distance between the 2 points is %d units", distance);
sender.sendMessage(message);
getDistanceAndSendMessage(point1, point2, sender);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.ricglz.discoords.commands;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public class GeneralDistanceCommand extends GeneralCommand {
protected int getDistance(Location point1, Location point2) {
Expand All @@ -10,4 +11,10 @@ protected int getDistance(Location point1, Location point2) {

return (int) Math.sqrt(x * x + y * y + z * z);
}

protected void getDistanceAndSendMessage(Location point1, Location point2, Player sender) {
int distance = getDistance(point1, point2);
String message = String.format("The distance between the 2 points is %d units", distance);
sender.sendMessage(message);
}
}

0 comments on commit d964024

Please sign in to comment.