Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.io.InputStream;

@CommandAlias("give-sign")
Expand All @@ -24,7 +22,7 @@ public class GiveSignCommand extends BaseCommand {
@Syntax("<material> <color> <font> <text>")
@CommandCompletion("@material @color @font <text>")
@Description("Gives a sign with custom text")
public void giveSign(Player player, String material, String color, String font , String text) throws IOException {
public void giveSign(Player player, String material, String color, String font , String text) {

if(player.getGameMode() != org.bukkit.GameMode.CREATIVE) {
player.sendMessage("You need to be in creative mode to use this command.");
Expand All @@ -48,10 +46,10 @@ public void giveSign(Player player, String material, String color, String font ,

InputStream inputStream;
try {
inputStream = JavaPlugin.getPlugin(SignTextGenerator.class).getDataPath().normalize().resolve(font + ".json").toUri().toURL().openStream();
inputStream = SignTextGenerator.getPlugin().getResource("fonts/" + font + ".json");
}catch (Exception IOException) {
player.sendMessage("Error generating text. Make sure the font exists.");
throw IOException;
return;
}
ObjectMapper objectMapper = new ObjectMapper();
JsonLayout jsonLayout;
Expand Down Expand Up @@ -91,16 +89,12 @@ public void giveSign(Player player, String material, String color, String font ,
signBlock.getSide(Side.FRONT).setGlowingText(true);
meta.setBlockState(signBlock);
meta.displayName(Component.text()
.append(Component.text("§r" + text.substring(0, (i + 1) * maxCharsPerSign - maxCharsPerSign), signColor, TextDecoration.ITALIC))
.append(Component.text(currentChars, signColor, TextDecoration.BOLD, TextDecoration.ITALIC))
.append(Component.text(text.substring(i * maxCharsPerSign + maxCharsPerSign), signColor, TextDecoration.ITALIC))
.append(Component.text("§r" + text.substring(0, (i + 1) * maxCharsPerSign - maxCharsPerSign), signColor))
.append(Component.text(currentChars, signColor, TextDecoration.BOLD))
.append(Component.text(text.substring(i * maxCharsPerSign + maxCharsPerSign), signColor))
.build());
boolean success = sign.setItemMeta(meta);
if (success) {
player.getInventory().addItem(sign);
}else {
player.sendMessage("Error giving sign. Could not set item meta.");
}
sign.setItemMeta(meta);
player.getInventory().addItem(sign);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,28 @@

import co.aikar.commands.PaperCommandManager;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;

public final class SignTextGenerator extends JavaPlugin {

private static SignTextGenerator plugin;

@Override
public void onEnable() {
// Plugin startup logic
// TextGeneration.initCharMap();

List<JarEntry> fontEntries = new ArrayList<>();
URL jarUrl = GiveSignCommand.class.getProtectionDomain().getCodeSource().getLocation();
try {
File file = new File(jarUrl.toURI());
try (JarFile jarFile = new JarFile(file)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().startsWith("fonts/") && !entry.isDirectory()) {
fontEntries.add(entry);
}
}
}
}catch (URISyntaxException | IOException exception){
getLogger().severe("Failed to read font files from plugin jar. No fonts will be available for use. Plugin will be disabled.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}

File dataFolder = getDataFolder();
boolean success = true;
if (!dataFolder.exists()) {
success = dataFolder.mkdirs();
}
if(!success){
getLogger().severe("Failed to create data folder for SignTextGenerator plugin. This is a crucial part of the Plugin.");
getLogger().severe("Due to this error the plugin will be disabled. Please fix the issue and restart the server.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
for(JarEntry font : fontEntries) {
Path dataPath = getDataPath().normalize();
Path fontPath = getDataPath().resolve(font.getName().substring("fonts/".length())).normalize();
if(!fontPath.startsWith(dataPath)) {
getLogger().warning("Font " + font.getName() + " is trying to be extracted outside of the plugin data folder. This is a security risk and the file will not be extracted.");
getLogger().warning("This font will not be available for use");
continue;
}

if(fontPath.getParent() != null && !Files.exists(fontPath.getParent())) {
try {
Files.createDirectories(fontPath.getParent());
}catch (IOException e){
getLogger().warning("Failed to create directories for font file: " + font.getName());
getLogger().warning("This font will not be available for use");
continue;
}
}

if(!Files.exists(fontPath)) {
try (InputStream inputStream = getResource(font.getName());
OutputStream outputStream = Files.newOutputStream(fontPath)) {

if (inputStream == null) {
getLogger().warning("Failed to extract font file: " + font.getName() + ". Stream is empty.");
getLogger().warning("This font will not be available for use");
continue;
}

inputStream.transferTo(outputStream);
}catch (IOException e){
getLogger().warning("Failed to extract font file: " + font.getName() + ". Could not create stream.");
getLogger().warning("This font will not be available for use");
}
}
}
plugin = this;

PaperCommandManager manager = new PaperCommandManager(this);

Expand All @@ -115,28 +46,30 @@ public void onEnable() {
return colorCompletions;
});

Collection<String> fontCompletions = new ArrayList<>();
Path dataPath = getDataPath().normalize();
try (Stream<Path> paths = Files.walk(dataPath)) {
paths.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".json"))
.forEach(path -> {
String fileName = dataPath.relativize(path).toString().replace("\\", "/");
String fontName = fileName.replace(".json", "");
if (!fontName.isEmpty()) {
fontCompletions.add(fontName);
manager.getCommandCompletions().registerAsyncCompletion("font", _ -> {
List<String> fileNames = new ArrayList<>();
URL jarUrl = GiveSignCommand.class.getProtectionDomain().getCodeSource().getLocation();
try {
File jarFile = new File(jarUrl.toURI());
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().startsWith("fonts/") && !entry.isDirectory()) {
fileNames.add(entry.getName().substring("fonts/".length()));
}
});
} catch (IOException e) {
getLogger().warning("Failed to read font files from data folder.");
getLogger().warning("No fonts will be available for use. Plugin will be disabled. Please check permissions.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
}
}
}catch (Exception _){}

getLogger().info("Successfully loaded font files from data folder. Available fonts: " + fontCompletions.toString().substring(1, fontCompletions.toString().length() - 1));
Collection<String> fontCompletions = new ArrayList<>();
for (String fileName : fileNames) {
String fontName = fileName.replace(".json", "");
fontCompletions.add(fontName);
}

manager.getCommandCompletions().registerAsyncCompletion("font", _ -> fontCompletions);
return fontCompletions;
});

manager.registerCommand(new GiveSignCommand());

Expand All @@ -147,4 +80,8 @@ public void onDisable() {
// Plugin shutdown logic
}

public static SignTextGenerator getPlugin() {
return plugin;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.realMorgon.signTextGenerator;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.InputStream;
import java.util.HashMap;
Expand All @@ -13,7 +12,7 @@ public static String[] generateText(String text, String font) {
InputStream inputStream;

try {
inputStream = JavaPlugin.getPlugin(SignTextGenerator.class).getDataPath().normalize().resolve(font + ".json").toUri().toURL().openStream();
inputStream = SignTextGenerator.getPlugin().getResource("fonts/" + font + ".json");
}catch (Exception IOException) {
return null;
}
Expand All @@ -38,10 +37,8 @@ public static String[] generateText(String text, String font) {
for (int j = 0; j < 4; j++) {
if (lines[j] == null) {
lines[j] = charLines[j];
}else if (jsonLayout.ownLetterSeparation) {
lines[j] += charLines[j];
}else {
lines[j] += " " + charLines[j];
lines[j] += "" + charLines[j];
}
}
}
Expand All @@ -53,6 +50,5 @@ public static String[] generateText(String text, String font) {
class JsonLayout {
public int maxCharsPerSign;
public int maxCharsPerHangingSign;
public boolean ownLetterSeparation;
public HashMap<Character, String[]> charMap;
}
94 changes: 94 additions & 0 deletions src/main/resources/fonts/Flitze/2lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"maxCharsPerSign" : 7,
"maxCharsPerHangingSign" : 4,
"ownLetterSeparation": true,
"charMap" : {
" ": [""," "," "," "],
"A": ["",".|'|. ","|'''| ",""],
"B": ["","|.) ","|.) ",""],
"C": ["","/'' ","\\.. ",""],
"D": ["","|'\\ ","|./ ",""],
"E": ["","['' ","L. ",""],
"F": ["","|''' ","|' ",""],
"G": ["","/'.. ","\\/ ",""],
"H": ["","|,,| ","| | ",""],
"I": ["","| ","| ",""],
"J": [""," | ","J ",""],
"K": ["","|/ ","|\\ ",""],
"L": ["","| ","L ",""],
"M": ["","|v| ","| | ",""],
"N": ["","|\\| ","| '| ",""],
"O": ["","/\\ ","\\/ ",""],
"P": ["","I.) ","| ",""],
"Q": ["","/\\ ","\\X ",""],
"R": ["","I.) ","|\\ ",""],
"S": ["","('' ","..) ",""],
"T": ["","''|'' "," | ",""],
"U": ["","| | ","|,,| ",""],
"V": ["","|, ,| "," || ",""],
"W": ["","'|, ,|' "," W ",""],
"X": ["","\\/ ","/\\ ",""],
"Y": ["","\\/ "," | ",""],
"Z": ["","'''7 ","ㄥ... ",""],
"Ä": ["°° ",".|'|. ","|'''| ",""],
"Ö": ["°° ","/'\\ ","\\./ ",""],
"Ü": ["°° ","| | ","\\/ ",""],
"a": [""," , ","(.I "," "],
"b": ["","| ","I.) "," "],
"c": [""," ","C "," "],
"d": [""," | ","(.I "," "],
"e": [""," ","e "," "],
"f": ["","f ","| "," "],
"g": [""," ","() ","J "],
"h": ["","| ","|´`i "," "],
"i": ["","° ","| "," "],
"j": ["","° ","| ","J "],
"k": ["","| ","K "," "],
"l": ["","| ","l "," "],
"m": [""," ","|`'´| "," "],
"n": ["","  ","|´`i "," "],
"o": [""," ","() "," "],
"p": [""," ","I.) ","| "],
"q": [""," ","(.I "," | "],
"r": [""," ","I'' "," "],
"s": ["","  ","S "," "],
"t": ["","..|.. "," l "," "],
"u": [""," ","U "," "],
"v": ["","  ","V "," "],
"w": ["","  ","W "," "],
"x": ["","  ","X "," "],
"y": [""," ","\\.| "," J "],
"z": ["","  ","Z ",""],
"ä": ["","°° ","(.I "," "],
"ö": ["","°° ","() "," "],
"ü": ["","°° ","U "," "],
"0": ["","|''| ","|,,| ",""],
"1": ["","´| "," | ",""],
"2": ["","'') ","ㄥ. ",""],
"3": ["","'') ","..) ",""],
"4": ["","ㄥ| "," | ",""],
"5": ["","[' ","...) ",""],
"6": ["","/ ","(.) ",""],
"7": ["","''7 ","/ ",""],
"8": ["","(.) ","(..) ",""],
"9": ["","(.) ","./ ",""],
".": [""," ","o",""],
",": [""," ","i ",""],
"-": [""," ","'''' ",""],
"+": ["","_|_ "," || ",""],
"'": ["","i "," ",""],
"&": ["","() ","(x ",""],
"*": ["","x ","  ",""],
"/": [""," / ","/ ",""],
"\\": ["","\\ "," \\ ",""],
"|": ["","| ","| ",""],
":": ["","o ","o ",""],
";": ["","o ","|  ",""],
"(": ["","/ ","\\ ",""],
")": ["","\\ ","/ ",""],
"!": ["","| ","° ",""],
"?": ["","´') ","° ",""],
"~": [""," ","~ ",""],
"_": [""," ","_ "," "]
}
}
Loading