Modify MapText to actually work, modify UtilText to save the images of characters

This commit is contained in:
libraryaddict 2016-01-07 04:48:44 +13:00
parent a73309fc11
commit c920d4efa1
2 changed files with 61 additions and 128 deletions

View File

@ -17,6 +17,7 @@ import org.bukkit.ChatColor;
public class UtilText
{
private static HashMap<Character, Integer> _characters = new HashMap<Character, Integer>();
private static HashMap<Character, BufferedImage> _characterImages = new HashMap<Character, BufferedImage>();
static
{
@ -263,13 +264,13 @@ public class UtilText
public static String[] splitLinesToArray(String[] strings, LineFormat lineFormat)
{
ArrayList<String> lineList = splitLines(strings, lineFormat);
String[] lineArray = new String[lineList.size()];
lineArray = lineList.toArray(lineArray);
return lineArray;
}
public static ArrayList<String> splitLines(String[] strings, LineFormat lineFormat)
{
ArrayList<String> lines = new ArrayList<String>();
@ -281,35 +282,35 @@ public class UtilText
return lines;
}
public static String[] splitLineToArray(String string, LineFormat lineFormat)
{
ArrayList<String> lineList = splitLine(string, lineFormat);
String[] lineArray = new String[lineList.size()];
lineArray = lineList.toArray(lineArray);
return lineArray;
}
public static ArrayList<String> splitLine(String string, LineFormat lineFormat)
{
ArrayList<String> strings = new ArrayList<String>();
//Ignore lines with #
// Ignore lines with #
if (string.startsWith("#"))
{
strings.add(string.substring(1, string.length()));
return strings;
}
//Empty
// Empty
if (string.equals("") || string.equals(" "))
{
strings.add(string);
return strings;
}
String current = "";
int currentLength = 0;
String[] split = string.split(" ");
@ -453,35 +454,63 @@ public class UtilText
private static void grab(Character character, BufferedImage image, int imageX, int imageY)
{
BufferedImage newImage = image.getSubimage(imageX, imageY, 8, 8);
int width = 8;
if (character == ' ')
{
_characters.put(character, 3);
return;
width = 3;
}
for (int x = 0; x < 8; x++)
else
{
boolean isTransparentLine = true;
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
{
int pixel = image.getRGB(imageX + x, imageY + y);
boolean isTransparentLine = true;
if ((pixel >> 24) != 0x00)
for (int y = 0; y < 8; y++)
{
isTransparentLine = false;
int pixel = image.getRGB(imageX + x, imageY + y);
if ((pixel >> 24) != 0x00)
{
isTransparentLine = false;
break;
}
}
if (isTransparentLine)
{
width = x + 1;
break;
}
}
if (isTransparentLine)
{
_characters.put(character, x);
return;
}
}
_characters.put(character, 8);
newImage = newImage.getSubimage(0, 0, width, 8);
_characterImages.put(character, newImage);
_characters.put(character, width);
}
public static int getLength(char character)
{
if (!_characters.containsKey(character))
{
return 16;
}
return _characters.get(character);
}
public static BufferedImage getImage(char character)
{
if (!_characterImages.containsKey(character))
{
character = '?';
}
return _characterImages.get(character);
}
public static boolean isStringSimilar(String newString, String oldString, float matchRequirement)

View File

@ -2,14 +2,9 @@ package mineplex.core.map;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.imageio.ImageIO;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilText;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -20,86 +15,6 @@ import org.bukkit.map.MapView;
public class MapText
{
private static HashMap<Character, BufferedImage> _characters = new HashMap<Character, BufferedImage>();
private void loadCharacters()
{
try
{
InputStream inputStream = getClass().getResourceAsStream("ascii.png");
BufferedImage image = ImageIO.read(inputStream);
char[] text = new char[]
{
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
'_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
int x = 0;
int y = 16;
for (char c : text)
{
grab(c, image, x, y);
if (x < 15 * 8)
{
x += 8;
}
else
{
x = 0;
y += 8;
}
}
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void grab(Character character, BufferedImage image, int imageX, int imageY)
{
BufferedImage newImage = image.getSubimage(imageX, imageY, 8, 8);
int width = character == ' ' ? 4 : 0;
if (width == 0)
{
for (int x = 0; x < 8; x++)
{
width++;
boolean foundNonTrans = false;
for (int y = 0; y < 8; y++)
{
int pixel = newImage.getRGB(x, y);
if ((pixel >> 24) != 0x00)
{
foundNonTrans = true;
break;
}
}
if (!foundNonTrans)
{
break;
}
}
}
newImage = newImage.getSubimage(0, 0, width, 8);
_characters.put(character, newImage);
}
private ArrayList<String> split(String text)
{
ArrayList<String> returns = new ArrayList<String>();
@ -112,7 +27,7 @@ public class MapText
for (char c : word.toCharArray())
{
length += _characters.get(c).getWidth();
length += UtilText.getImage(c).getWidth();
}
if (lineWidth + length >= 127)
@ -133,11 +48,6 @@ public class MapText
public ItemStack getMap(boolean sendToServer, String... text)
{
if (_characters.isEmpty())
{
loadCharacters();
}
BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
int height = 1;
@ -150,13 +60,7 @@ public class MapText
for (char c : line.toCharArray())
{
BufferedImage img = _characters.get(c);
if (img == null)
{
System.out.print("Error: '" + c + "' has no image associated");
continue;
}
BufferedImage img = UtilText.getImage(c);
g.drawImage(img, length, height, null);
@ -179,7 +83,7 @@ public class MapText
ItemStack item = new ItemStack(Material.MAP);
item.setDurability(map.getId());
if (sendToServer)
{
for (Player player : UtilServer.getPlayers())