Skip to content

Instantly share code, notes, and snippets.

@Magnum97
Created May 9, 2020 07:12
Show Gist options
  • Save Magnum97/9a18bfdc6bd3d073121347e54bd74990 to your computer and use it in GitHub Desktop.
Save Magnum97/9a18bfdc6bd3d073121347e54bd74990 to your computer and use it in GitHub Desktop.
Convert String array to List and frame it
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@NoArgsConstructor
public class YamlHelper {
private String[] header;
public List <String> framedHeader (String... header) {
List <String> stringList = new ArrayList <>();
String border = "# +----------------------------------------------------+ #";
stringList.add(border);
for (String line : header) {
StringBuilder builder = new StringBuilder();
if (line.length() > 50) {
continue;
}
int length = (50 - line.length()) / 2;
StringBuilder finalLine = new StringBuilder(line);
for (int i = 0; i < length; i++) {
finalLine.append(" ");
finalLine.reverse();
finalLine.append(" ");
finalLine.reverse();
}
if (line.length() % 2 != 0) {
finalLine.append(" ");
}
builder.append("# < ").append(finalLine.toString()).append(" > #");
stringList.add(builder.toString());
}
stringList.add(border);
return stringList;
}
}
@Magnum97
Copy link
Author

Magnum97 commented May 9, 2020

Example Code:
		String[] header = {"Config file for BreedablePets",
				"by Magnum1997",
				"This should be self-explanitory. I tried tocomment settings.",
				"For problems, questions, or feedback please visit",
				"https://github.com/Magnum97/BreedablePetsMC/issues"};
		yaml = new Yaml("helper.yml", getDataFolder().toString());
		YamlHelper helper = new YamlHelper();
		yaml.setHeader(helper.framedHeader(header));
Example output:
# +----------------------------------------------------+ #
# <           Config file for BreedablePets            > #
# <                   by Magnum1997                    > #
# < For problems, questions, or feedback please visit  > #
# < https://github.com/Magnum97/BreedablePetsMC/issues > #
# +----------------------------------------------------+ #

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