package l2p.gameserver.utils;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import l2p.gameserver.config.OtherConfig;
import l2p.gameserver.data.holder.StringHolder;
import l2p.gameserver.enums.classes.ClassId;
import l2p.gameserver.model.Player;
import l2p.gameserver.model.instances.NpcInstance;
import l2p.gameserver.model.skills.Skill;
import l2p.gameserver.network.components.CustomMessage;
import l2p.gameserver.network.components.NpcString;
import l2p.gameserver.network.components.SysString;
import l2p.gameserver.network.s2c.NpcHtmlMessage;
public class HtmlUtils
{
public static final String PREV_BUTTON = "<button value=\"&$1037;\" action=\"bypass %prev_bypass%\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">";
public static final String NEXT_BUTTON = "<button value=\"&$1038;\" action=\"bypass %next_bypass%\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">";
public static enum LineType
{
BLANK("L2UI.SquareBlank"),
BLACK("L2UI.SquareBlack"),
WHITE("L2UI.SquareWhite"),
GRAY("L2UI.SquareGray"),
MB_ARROW("L2UI_CH3.minibar_arrow"),
MB_FOOD("L2UI_CH3.minibar_food"),
MB_MAGIC("L2UI_CH3.minibar_magic"),
MB_WATER("L2UI_CH3.minibar_water");
private final String _texture;
private LineType(String texture)
{
_texture = texture;
}
public String getTexture()
{
return _texture;
}
}
public static enum ButtonType
{
SMALL("L2UI_CT1.Button_DF_Small", "L2UI_CT1.Button_DF_Small_Down", -1, 24),
NORMAL("L2UI_CT1.Button_DF", "L2UI_CT1.Button_DF_Down", -1, 26),
LARGE("L2UI_CT1.Button_DF_Large", "L2UI_CT1.Button_DF_Large_Down", -1, 35),
UP("L2UI_CH3.UpButton", "L2UI_CH3.upbutton_down", 15, 15),
DOWN("L2UI_CH3.DownButton", "L2UI_CH3.downbutton_down", 15, 15),
PREV("L2UI_CH3.prev1", "L2UI_CH3.prev1_down", 15, 15),
NEXT("L2UI_CH3.next1", "L2UI_CH3.next1_down", 15, 15),
ABOUT("L2UI_CH3.aboutotpicon", "L2UI_CH3.aboutotpicon_down", 15, 15),
INPUT("L2UI_CT1.Button_DF_Input", "L2UI_CT1.Button_DF_Input_Down", 15, 15),
LIST("L2UI_CT1.PostWnd_DF_Btn_List", "L2UI_CT1.PostWnd_DF_Btn_List_Down", 15, 15),
OLYMPIAD_APPLY("L2UI_CT1.OlympiadWnd_DF_Apply", "L2UI_CT1.OlympiadWnd_DF_Apply_Down", 197, 29),
OLYMPIAD_BACK("L2UI_CT1.OlympiadWnd_DF_Back", "L2UI_CT1.OlympiadWnd_DF_Back_Down", 197, 29),
OLYMPIAD_BUY_EQUIP("L2UI_CT1.OlympiadWnd_DF_BuyEquip", "L2UI_CT1.OlympiadWnd_DF_BuyEquip_Down", 197, 29),
OLYMPIAD_BUY_ETC("L2UI_CT1.OlympiadWnd_DF_BuyEtc", "L2UI_CT1.OlympiadWnd_DF_BuyEtc_Down", 197, 29),
OLYMPIAD_FIGHT_1("L2UI_CT1.OlympiadWnd_DF_Fight1None", "L2UI_CT1.OlympiadWnd_DF_Fight1None_Down", 197, 29),
OLYMPIAD_FIGHT_3("L2UI_CT1.OlympiadWnd_DF_Fight3None", "L2UI_CT1.OlympiadWnd_DF_Fight3None_Down", 197, 29),
OLYMPIAD_HERO_CONFIRM("L2UI_CT1.OlympiadWnd_DF_HeroConfirm", "L2UI_CT1.OlympiadWnd_DF_HeroConfirm_Down", 197, 29),
OLYMPIAD_INFO("L2UI_CT1.OlympiadWnd_DF_Info", "L2UI_CT1.OlympiadWnd_DF_Info_Down", 197, 29),
OLYMPIAD_REWARD("L2UI_CT1.OlympiadWnd_DF_Reward", "L2UI_CT1.OlympiadWnd_DF_Reward_Down", 197, 29),
OLYMPIAD_WATCH("L2UI_CT1.OlympiadWnd_DF_Watch", "L2UI_CT1.OlympiadWnd_DF_Watch_Down", 197, 29);
private final String _fore;
private final String _back;
private final int _width;
private final int _height;
private ButtonType(String fore, String back, int width, int height)
{
_fore = fore;
_back = back;
_width = width;
_height = height;
}
public String getFore()
{
return _fore;
}
public String getBack()
{
return _back;
}
public int getWidth()
{
return _width;
}
public int getWidth(int width)
{
return _width > 0 ? _width : width;
}
public int getHeight()
{
return _height;
}
public int getHeight(int height)
{
return _height > 0 ? _height : height;
}
}
public static enum BarType
{
LARGE_CP("L2UI_CT1.Gauge_DF_Large_CP_Center", "L2UI_CT1.Gauge_DF_Large_CP_bg_Center", 16),
LARGE_HP("L2UI_CT1.Gauge_DF_Large_HP_Center", "L2UI_CT1.Gauge_DF_Large_HP_bg_Center", 16),
LARGE_MP("L2UI_CT1.Gauge_DF_Large_MP_Center", "L2UI_CT1.Gauge_DF_Large_MP_bg_Center", 16),
LARGE_VP("L2UI_CT1.Gauge_DF_Large_VP_Center", "L2UI_CT1.Gauge_DF_Large_VP_bg_Center", 16),
LARGE_EXP("L2UI_CT1.Gauge_DF_Large_EXP_Center", "L2UI_CT1.Gauge_DF_Large_EXP_bg_Center", 16),
ATTR_FIRE("L2UI_CT1.Gauge_DF_Attribute_Fire_Center", "L2UI_CT1.Gauge_DF_Attribute_Fire_bg_Center", 8),
ATTR_WATER("L2UI_CT1.Gauge_DF_Attribute_Water_Center", "L2UI_CT1.Gauge_DF_Attribute_Water_bg_Center", 8),
ATTR_WIND("L2UI_CT1.Gauge_DF_Attribute_Wind_Center", "L2UI_CT1.Gauge_DF_Attribute_Wind_bg_Center", 8),
ATTR_EARTH("L2UI_CT1.Gauge_DF_Attribute_Earth_Center", "L2UI_CT1.Gauge_DF_Attribute_Earth_bg_Center", 8),
ATTR_DIVINE("L2UI_CT1.Gauge_DF_Attribute_Divine_Center", "L2UI_CT1.Gauge_DF_Attribute_Divine_bg_Center", 8),
ATTR_DARK("L2UI_CT1.Gauge_DF_Attribute_Dark_Center", "L2UI_CT1.Gauge_DF_Attribute_Dark_bg_Center", 8);
private final String _fore;
private final String _back;
private final int _height;
private BarType(String fore, String back, int height)
{
_fore = fore;
_back = back;
_height = height;
}
public String getFore()
{
return _fore;
}
public String getBack()
{
return _back;
}
public int getHeight()
{
return _height;
}
public int getHeight(int height)
{
return height > 0 ? height : _height;
}
}
public static String getResidenceName(int id)
{
return "&%" + id + ";";
}
public static String getResidenceLoc(int id)
{
return "&^" + id + ";";
}
public static String getResidenceDesc(int id)
{
return "&*" + id + ";";
}
public static String getNpcName(int npcId)
{
return "&@" + npcId + ";";
}
public static String getItemName(int itemId)
{
return "&#" + itemId + ";";
}
public static String getSysString(SysString sysString)
{
return getSysString(sysString.getId());
}
public static String getSysString(int id)
{
return "&$" + id + ";";
}
public static String getClassName(int classId)
{
return "<ClassId>" + classId + "</ClassId>";
}
public static String getClassName(ClassId classId)
{
return "<ClassId>" + classId.getId() + "</ClassId>";
}
public static String getNpcString(NpcString id, Object... params)
{
return getNpcString(id.getId(), params);
}
public static String getNpcString(int id, Object... params)
{
String replace = "<fstring";
if (params.length > 0)
for (int i = 0; i < params.length; i++)
replace += " p" + (i + 1) + "=\"" + String.valueOf(params[i]) + "\"";
replace += ">" + id + "</fstring>";
return replace;
}
public static String getItemName(int id, Player player)
{
return StringHolder.getInstance().getItem(id, player, true);
}
public static String getNpcName(int id, Player player)
{
return StringHolder.getInstance().getNpc(id, true);
}
public static String getSkillName(int id, int level, Player player)
{
return StringHolder.getInstance().getSkill(id, level, player);
}
public static String getSkillName(Skill skill, Player player)
{
return StringHolder.getInstance().getSkill(skill.getDisplayedId(), skill.getDisplayedLevel(), player);
}
public static String getQuestName(int id, Player player)
{
return StringHolder.getInstance().getQuest(id, player);
}
/**
* Работает как обычный trim(), но в дополнение заменяет кавычки на нестандартные UTF8, удяляет все двойные пробелы и убирает символы <>
*
* @param str - исходная строка
*
* @return - обработанная строка
*/
public final static String trim(String str)
{
str = str.trim().replaceAll("\"", "״").replaceAll("'", "´").replaceAll("<", StringUtils.EMPTY).replaceAll(">", StringUtils.EMPTY);
int length;
do
{
length = str.length();
str = str.replaceAll(" ", " ");
}
while (length > str.length());
return str;
}
public final static String line(LineType type, int width)
{
return line(type, width, 1);
}
public final static String line(LineType type, int width, int height)
{
return line(type.getTexture(), width, height);
}
public final static String line(String texture, int width, int height)
{
return String.format("<table border=0 cellpadding=0 cellspacing=0><tr><td><img src=\"%s\" width=%d height=%d></td></tr></table>", texture, width, height);
}
public final static String button(ButtonType type, String value, String action)
{
return button(type.getFore(), type.getBack(), value, action, null, null, type.getWidth(), type.getHeight());
}
public final static String button(ButtonType type, String value, String action, int width)
{
return button(type.getFore(), type.getBack(), value, action, null, null, width, type.getHeight());
}
public final static String button(ButtonType type, String value, String action, int width, int height)
{
return button(type.getFore(), type.getBack(), value, action, null, null, width, height);
}
public final static String button(ButtonType type, String value, String action, String msg, String tooltip)
{
return button(type.getFore(), type.getBack(), value, action, msg, tooltip, type.getWidth(), type.getHeight());
}
public final static String button(ButtonType type, String value, String action, String msg, String tooltip, int width)
{
return button(type.getFore(), type.getBack(), value, action, msg, tooltip, width, type.getHeight());
}
public final static String button(ButtonType type, String value, String action, boolean pressed)
{
if (!pressed)
return button(type.getFore(), type.getBack(), value, action, null, null, type.getWidth(), type.getHeight());
else
return pressedButton(type.getBack(), value, type.getWidth(), type.getHeight());
}
public final static String button(ButtonType type, String value, String action, boolean pressed, int width)
{
if (!pressed)
return button(type.getFore(), type.getBack(), value, action, null, null, width, type.getHeight());
else
return pressedButton(type.getBack(), value, width, type.getHeight());
}
public final static String button(ButtonType type, String value, String action, boolean pressed, int width, int height)
{
if (!pressed)
return button(type.getFore(), type.getBack(), value, action, null, null, width, height);
else
return pressedButton(type.getBack(), value, width, height);
}
public final static String button(ButtonType type, String value, String action, String msg, String tooltip, int width, int height)
{
return button(type.getFore(), type.getBack(), value, action, msg, tooltip, type.getWidth(width), type.getHeight(height));
}
public final static String button(String fore, String back, String value, String action, String msg, String tooltip, int width, int height)
{
if (msg != null)
{
if (tooltip != null)
return String.format("<button value=\"%s\" action=\"%s\" msg=\"%s\" tooltip=\"%s\" width=%d height=%d fore=\"%s\" back=\"%s\">", value, action, msg, tooltip, width, height, fore, back);
else
return String.format("<button value=\"%s\" action=\"%s\" msg=\"%s\" width=%d height=%d fore=\"%s\" back=\"%s\">", value, action, msg, width, height, fore, back);
}
else
{
if (tooltip != null)
return String.format("<button value=\"%s\" action=\"%s\" tooltip=\"%s\" width=%d height=%d fore=\"%s\" back=\"%s\">", value, action, tooltip, width, height, fore, back);
else
return String.format("<button value=\"%s\" action=\"%s\" width=%d height=%d fore=\"%s\" back=\"%s\">", value, action, width, height, fore, back);
}
}
public final static String pressedButton(ButtonType type, String text)
{
return pressedButton(type.getBack(), text, type.getWidth(), type.getHeight());
}
public final static String pressedButton(ButtonType type, String text, int width)
{
return pressedButton(type.getBack(), text, width, type.getHeight());
}
public final static String pressedButton(ButtonType type, String text, int width, int height)
{
return pressedButton(type.getBack(), text, type.getWidth(width), type.getHeight(height));
}
public final static String pressedButton(String back, String text, int width, int height)
{
return String.format("<table height=%d border=0 cellpadding=0 cellspacing=0><tr><td width=%d background=\"%s\" align=center>%s</td></tr></table>", height, width, back, text);
}
public final static String checkBox(String action, boolean checked)
{
return checkBox(action, checked, true);
}
public final static String checkBox(String action, boolean checked, boolean enabled)
{
if (enabled)
{
if (checked)
return String.format("<button value=\" \" action=\"%s;%s\" width=15 height=15 fore=\"L2UI.CheckBox_checked\" back=\"L2UI.CheckBox_checked\">", action, 0);
else
return String.format("<button value=\" \" action=\"%s;%s\" width=15 height=15 fore=\"L2UI.CheckBox\" back=\"L2UI.CheckBox\">", action, 1);
}
else
{
if (checked)
return "<img src=\"L2UI.CheckBox_checked\" width=15 height=15>";
else
return "<img src=\"L2UI.CheckBox\" width=15 height=15>";
}
}
public static String bar(BarType type, int width, int current)
{
return bar(type.getFore(), type.getBack(), width, type.getHeight(0), current);
}
public static String bar(BarType type, int width, int height, int current)
{
return bar(type.getFore(), type.getBack(), width, type.getHeight(height), current);
}
public static String bar(String fore, String back, int width, int height, int current)
{
return String.format("<table border=0 cellpadding=0 cellspacing=0><tr><td width=%d height=%d background=\"%s\"><img src=\"%s\" width=%d height=%d></td></tr></table>", width, height + 2, back, fore, current, height);
}
//
//
//
public final static void showFile(Player player, NpcInstance npc, String filename, String... replaces)
{
showFile(player, npc, filename, 0, 0, replaces);
}
public final static void showFile(Player player, NpcInstance npc, String filename, int val, String... replaces)
{
showFile(player, npc, filename, val, 0, replaces);
}
public final static void showFile(Player player, NpcInstance npc, String filename, int val, int type, String... replaces)
{
NpcHtmlMessage html = new NpcHtmlMessage(player, npc, filename, val, type);
if (replaces != null && replaces.length > 0)
{
if (replaces.length % 2 != 0)
{
LogUtils.warn("HTML: Error in replaces definition. HTML file: " + filename + ", npc: " + (npc != null ? npc.getName() : "null"));
return;
}
for (int i = 0; i < replaces.length; i = i + 2)
html.replace(replaces[i], replaces[i + 1]);
}
player.sendPacket(html);
}
//
//
//
public final static void showText(String text, Player player, NpcInstance npc)
{
showText(text, 0, player, npc);
}
public final static void showText(String text, int type, Player player, NpcInstance npc)
{
NpcHtmlMessage html = new NpcHtmlMessage(player, npc, type);
html.setHtml(text);
player.sendPacket(html);
}
//
//
//
public final static void show(String text, Player player, NpcInstance npc, Object... arg)
{
show(text, 0, player, npc, arg);
}
public final static void show(String text, int type, Player player, NpcInstance npc, Object... arg)
{
if (text == null || player == null)
return;
NpcHtmlMessage msg = new NpcHtmlMessage(player, npc, type);
// приводим нашу html-ку в нужный вид
if (text.endsWith(".html") || text.endsWith(".htm"))
msg.setFile(text);
else
msg.setHtml(bbParse(text));
if (arg != null && arg.length % 2 == 0)
for (int i = 0; i < arg.length; i = +2)
msg.replace(String.valueOf(arg[i]), arg[i + 1]);
player.sendPacket(msg);
}
public final static void show(CustomMessage message, Player self)
{
show(message.toString(), self, null);
}
//
//
//
public static String replace(String str, String regex, int flags, String replace)
{
return Pattern.compile(regex, flags).matcher(str).replaceAll(replace);
}
public static boolean matches(String str, String regex, int flags)
{
return Pattern.compile(regex, flags).matcher(str).matches();
}
public static String bbParse(String str)
{
if (str == null)
return null;
str = str.replace("\r", StringUtils.EMPTY);
str = str.replaceAll("(\\s|\"|\'|\\(|^|\n)\\*(.*?)\\*(\\s|\"|\'|\\)|\\?|\\.|!|:|;|,|$|\n)", "$1<font color=\"LEVEL\">$2</font>$3"); // *S1*
str = str.replaceAll("(\\s|\"|\'|\\(|^|\n)\\$(.*?)\\$(\\s|\"|\'|\\)|\\?|\\.|!|:|;|,|$|\n)", "$1<font color=\"00FFFF\">$2</font>$3");// $S1$
//s = replace(s, "^!(.*?)$", Pattern.MULTILINE, "<font color=\"FFFFFF\">$1</font>\n\n");
str = str.replaceAll("%%\\s*\n", "<br1>");
str = str.replaceAll("\n\n+", "<br>");
//s = replace(s, "\\[([^\\]\\|]*?)\\|([^\\]]*?)\\]", Pattern.DOTALL, "<a action=\"bypass -h $1\">$2</a>");
str = replace(str, "\\[([^\\]\\|]*?)\\|([^\\]]*?)\\|([^\\]]*?)\\]", Pattern.DOTALL, "<button ALIGN=LEFT ICON=\"$1\" action=\"bypass -h $2\">$3</button>");
str = replace(str, "\\[([^\\]\\|]*?)\\|([^\\]]*?)\\]", Pattern.DOTALL, "<button ALIGN=LEFT ICON=\"NORMAL\" action=\"bypass -h $1\">$2</button>");
str = str.replaceAll(" @M:", "\" msg=\"");
str = str.replaceAll(" @W:", "\" width=\"");
str = str.replaceAll(" @T:", "\" tooltip=\"");
str = str.replaceAll("%need_dialog%", "<font color=\"00FFFF\">TODO: корректный текст диалога (если он у вас есть - поделитесь с администрацией)</font>");
str = str.replaceAll("%free_teleport_level%", String.valueOf(OtherConfig.FREE_TELEPORT_MAX_LEVEL));
return str;
}
}