mirror of
https://github.com/MassiveCraft/Factions.git
synced 2024-11-25 19:56:23 +01:00
Renovate the class AsciiCompass to the current standards.
This commit is contained in:
parent
6f782b8503
commit
46c02e33f4
@ -11,8 +11,6 @@ import java.util.Map.Entry;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentSkipListMap;
|
import java.util.concurrent.ConcurrentSkipListMap;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
|
|
||||||
import com.massivecraft.factions.Const;
|
import com.massivecraft.factions.Const;
|
||||||
import com.massivecraft.factions.Factions;
|
import com.massivecraft.factions.Factions;
|
||||||
import com.massivecraft.factions.RelationParticipator;
|
import com.massivecraft.factions.RelationParticipator;
|
||||||
@ -385,7 +383,7 @@ public class Board extends Entity<Board> implements BoardInterface
|
|||||||
PS topLeftPs = centerPs.plusChunkCoords(-halfWidth, -halfHeight);
|
PS topLeftPs = centerPs.plusChunkCoords(-halfWidth, -halfHeight);
|
||||||
|
|
||||||
// Get the compass
|
// Get the compass
|
||||||
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Txt.parse("<a>"));
|
List<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees);
|
||||||
|
|
||||||
// Make room for the list of names
|
// Make room for the list of names
|
||||||
height--;
|
height--;
|
||||||
|
@ -1,97 +1,56 @@
|
|||||||
package com.massivecraft.factions.util;
|
package com.massivecraft.factions.util;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import com.massivecraft.massivecore.collections.MassiveList;
|
||||||
|
|
||||||
|
import static com.massivecraft.factions.util.AsciiCompassDirection.*;
|
||||||
|
|
||||||
public class AsciiCompass
|
public class AsciiCompass
|
||||||
{
|
{
|
||||||
public enum Point
|
// -------------------------------------------- //
|
||||||
|
// COMPASS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public static List<String> getAsciiCompass(double degrees)
|
||||||
{
|
{
|
||||||
N('N'),
|
return getAsciiCompass(AsciiCompassDirection.getByDegrees(degrees));
|
||||||
NE('/'),
|
|
||||||
E('E'),
|
|
||||||
SE('\\'),
|
|
||||||
S('S'),
|
|
||||||
SW('/'),
|
|
||||||
W('W'),
|
|
||||||
NW('\\');
|
|
||||||
|
|
||||||
public final char asciiChar;
|
|
||||||
|
|
||||||
private Point(final char asciiChar)
|
|
||||||
{
|
|
||||||
this.asciiChar = asciiChar;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString()
|
|
||||||
{
|
|
||||||
return String.valueOf(this.asciiChar);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString(boolean isActive, ChatColor colorActive, String colorDefault)
|
|
||||||
{
|
|
||||||
return (isActive ? colorActive : colorDefault)+String.valueOf(this.asciiChar);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AsciiCompass.Point getCompassPointForDirection(double inDegrees)
|
private static List<String> getAsciiCompass(AsciiCompassDirection directionFacing)
|
||||||
{
|
{
|
||||||
double degrees = (inDegrees - 180) % 360 ;
|
// Create
|
||||||
if (degrees < 0)
|
List<String> ret = new MassiveList<>();
|
||||||
degrees += 360;
|
|
||||||
|
|
||||||
if (0 <= degrees && degrees < 22.5)
|
// Fill
|
||||||
return AsciiCompass.Point.N;
|
ret.add(visualizeRow(directionFacing, NW, N, NE));
|
||||||
else if (22.5 <= degrees && degrees < 67.5)
|
ret.add(visualizeRow(directionFacing, W, NONE, E));
|
||||||
return AsciiCompass.Point.NE;
|
ret.add(visualizeRow(directionFacing, SW, S, SE));
|
||||||
else if (67.5 <= degrees && degrees < 112.5)
|
|
||||||
return AsciiCompass.Point.E;
|
|
||||||
else if (112.5 <= degrees && degrees < 157.5)
|
|
||||||
return AsciiCompass.Point.SE;
|
|
||||||
else if (157.5 <= degrees && degrees < 202.5)
|
|
||||||
return AsciiCompass.Point.S;
|
|
||||||
else if (202.5 <= degrees && degrees < 247.5)
|
|
||||||
return AsciiCompass.Point.SW;
|
|
||||||
else if (247.5 <= degrees && degrees < 292.5)
|
|
||||||
return AsciiCompass.Point.W;
|
|
||||||
else if (292.5 <= degrees && degrees < 337.5)
|
|
||||||
return AsciiCompass.Point.NW;
|
|
||||||
else if (337.5 <= degrees && degrees < 360.0)
|
|
||||||
return AsciiCompass.Point.N;
|
|
||||||
else
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ArrayList<String> getAsciiCompass(Point point, ChatColor colorActive, String colorDefault)
|
|
||||||
{
|
|
||||||
ArrayList<String> ret = new ArrayList<String>();
|
|
||||||
String row;
|
|
||||||
|
|
||||||
row = "";
|
// Return
|
||||||
row += Point.NW.toString(Point.NW == point, colorActive, colorDefault);
|
|
||||||
row += Point.N.toString(Point.N == point, colorActive, colorDefault);
|
|
||||||
row += Point.NE.toString(Point.NE == point, colorActive, colorDefault);
|
|
||||||
ret.add(row);
|
|
||||||
|
|
||||||
row = "";
|
|
||||||
row += Point.W.toString(Point.W == point, colorActive, colorDefault);
|
|
||||||
row += colorDefault+"+";
|
|
||||||
row += Point.E.toString(Point.E == point, colorActive, colorDefault);
|
|
||||||
ret.add(row);
|
|
||||||
|
|
||||||
row = "";
|
|
||||||
row += Point.SW.toString(Point.SW == point, colorActive, colorDefault);
|
|
||||||
row += Point.S.toString(Point.S == point, colorActive, colorDefault);
|
|
||||||
row += Point.SE.toString(Point.SE == point, colorActive, colorDefault);
|
|
||||||
ret.add(row);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<String> getAsciiCompass(double inDegrees, ChatColor colorActive, String colorDefault)
|
// -------------------------------------------- //
|
||||||
|
// VISUALIZE ROW
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private static String visualizeRow(AsciiCompassDirection directionFacing, AsciiCompassDirection... cardinals)
|
||||||
{
|
{
|
||||||
return getAsciiCompass(getCompassPointForDirection(inDegrees), colorActive, colorDefault);
|
// Catch
|
||||||
|
if (cardinals == null) throw new NullPointerException("cardinals");
|
||||||
|
|
||||||
|
// Create
|
||||||
|
StringBuilder ret = new StringBuilder(cardinals.length);
|
||||||
|
|
||||||
|
// Fill
|
||||||
|
for (AsciiCompassDirection asciiCardinal : cardinals)
|
||||||
|
{
|
||||||
|
ret.append(asciiCardinal.visualize(directionFacing));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,88 @@
|
|||||||
|
package com.massivecraft.factions.util;
|
||||||
|
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
public enum AsciiCompassDirection
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// ENUM
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
N('N'),
|
||||||
|
NE('/'),
|
||||||
|
E('E'),
|
||||||
|
SE('\\'),
|
||||||
|
S('S'),
|
||||||
|
SW('/'),
|
||||||
|
W('W'),
|
||||||
|
NW('\\'),
|
||||||
|
NONE('+'),
|
||||||
|
|
||||||
|
// END OF LIST
|
||||||
|
;
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// FIELDS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private final char asciiChar;
|
||||||
|
public char getAsciiChar() { return this.asciiChar; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// CONSTANTS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public static final ChatColor ACTIVE = ChatColor.RED;
|
||||||
|
public static final ChatColor INACTIVE = ChatColor.YELLOW;
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
AsciiCompassDirection(final char asciiChar)
|
||||||
|
{
|
||||||
|
this.asciiChar = asciiChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// VISUALIZE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public String visualize(AsciiCompassDirection directionFacing)
|
||||||
|
{
|
||||||
|
boolean isFacing = this.isFacing(directionFacing);
|
||||||
|
ChatColor color = this.getColor(isFacing);
|
||||||
|
|
||||||
|
return color.toString() + this.getAsciiChar();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isFacing(AsciiCompassDirection directionFacing)
|
||||||
|
{
|
||||||
|
return this == directionFacing;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ChatColor getColor(boolean active)
|
||||||
|
{
|
||||||
|
return active ? ACTIVE : INACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// GET BY DEGREES
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public static AsciiCompassDirection getByDegrees(double degrees)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
// The conversion from bukkit to usable degrees is (degrees - 180) % 360
|
||||||
|
// But we reduced the 180 to 157 (-23) because it makes the math easier that follows.
|
||||||
|
degrees = (degrees - 157) % 360;
|
||||||
|
if (degrees < 0) degrees += 360;
|
||||||
|
|
||||||
|
// Get ordinal
|
||||||
|
int ordinal = (int) Math.floor(degrees / 45);;
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return AsciiCompassDirection.values()[ordinal];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user