Skip to content

Instantly share code, notes, and snippets.

@SpyryBR
Last active December 22, 2023 01:08
Show Gist options
  • Save SpyryBR/e52d3403a8447a6e0c44f7ba1d17e55f to your computer and use it in GitHub Desktop.
Save SpyryBR/e52d3403a8447a6e0c44f7ba1d17e55f to your computer and use it in GitHub Desktop.
Código para gerar caixa de caracteres.
public class AsciiBox {
private int size;
private Boolean fix = false;
private String inside = " ";
private String[] borders = new String[4];
private String[] corners = new String[0];
public String render(String[] content) {
Boolean haveCorners = this.corners.length > 0;
String verticalBorder = this.borders[0];
String border = Strings.repeat(verticalBorder, haveCorners ? this.size - 2 : this.size);
String message = "\n" + (haveCorners ? this.corners[0] + border + this.corners[1] : border);
for (String contentItem: content) {
message += "\n" + centerString(contentItem);
}
message += "\n" + (haveCorners ? this.corners[2] + border + this.corners[3] : border);
return message;
}
public AsciiBox border(String border) {
this.borders[0] = border;
return this;
}
public AsciiBox borders(String vertical, String horizontal) {
this.borders = new String[] { vertical, horizontal };
return this;
}
public AsciiBox corner(String corner) {
this.corners = new String[] { corner, corner, corner, corner };
return this;
}
public AsciiBox corners(String topLeft, String topRight, String bottomLeft, String bottomRight) {
this.corners = new String[] { topLeft, topRight, bottomLeft, bottomRight };
return this;
}
public AsciiBox size(int size) {
this.size = size;
return this;
}
public AsciiBox inside(String inside) {
this.inside = inside;
return this;
}
public AsciiBox fixSide(Boolean side) {
this.fix = side;
return this;
}
private String centerString(String string) {
int stringLenght = ChatColor.stripColor(string).length();
int calc = ((this.size - stringLenght)/2) - 1;
String space = Strings.repeat(this.inside, calc);
String leftSpace = space;
String rightSpace = space;
String horizontalBorder = this.borders.length == 2 ? this.borders[1] : this.borders[0];
if((stringLenght % 2) != 0) {
if (this.fix) {
leftSpace += Strings.repeat(this.inside, 1);
} else {
rightSpace += Strings.repeat(this.inside, 1);
}
}
return horizontalBorder + leftSpace + "§r" + string + "§r" + rightSpace + horizontalBorder;
}
}
@SpyryBR
Copy link
Author

SpyryBR commented Jun 24, 2020

'-'

@drethenrain
Copy link

'-'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment