package eu.univento.core.api; import java.sql.ResultSet; import java.sql.SQLException; import eu.univento.core.Core; /** * messages from database * @author joethei * @version 1.0 */ public class Messages { public static String PREFIX; public static String CONSOLE_PREFIX; public static String UNKNOWN_ERROR; public static String NOT_A_PLAYER; public static String NO_PERMS; public static String ERROR; public static String NOT_ONLINE; public static String KICK_RESTART; public static String KICK_FULL; public static String COMMAND_NOT_FOUND; /** * write default data * @throws ClassNotFoundException Class couldn't be found * @throws SQLException SQL server not available or throwing error */ public static void writeDefault() throws ClassNotFoundException, SQLException { //editable messages will be set here, but do not edit this messages. MySQL sql = Core.returnSQL(); sql.openConnection(); if(!isInDatabase("Prefix")) writeData("INSERT INTO Messages(Ident, Message) values ('Prefix', '§aunivento §8»');"); if(!isInDatabase("ConsolePrefix")) writeData("INSERT INTO Messages(Ident, Message) values ('ConsolePrefix', '[univento Core]');"); if(!isInDatabase("UnknownError")) writeData("INSERT INTO Messages(Ident, Message) values ('UnknownError', '§cEs ist leider ein unbekannter Fehler aufgetreten');"); if(!isInDatabase("Commands.NoPlayer")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NoPlayer', '§cDu bist leider kein Spieler');"); if(!isInDatabase("Commands.NoPerms")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NoPerms', '§cDu hast keine Berechtigungen diesen Befehl auszuführen')"); if(!isInDatabase("Commands.Error")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.Error', '§cBeim ausführen dieses Befehls ist ein Fehler aufgetreten');"); if(!isInDatabase("Commands.NotOnline")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NotOnline', '§cDer Spieler $player ist nicht online');"); if(!isInDatabase("Commands.Unknown")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.Unknown', '§6Dieser Befehl konnte leider nicht gefunden werden');"); if(!isInDatabase("Kick.Restart")) writeData("INSERT INTO Messages(Ident, Message) values ('Kick.Restart', '§cDer Server startet gerade neu');"); if(!isInDatabase("Kick.Full")) writeData("INSERT INTO Messages(Ident, Message) values ('Kick.Full', '§cDer Server ist leider schon voll')"); sql.closeConnection(); } private static void writeData(String data) { MySQL sql = Core.returnSQL(); try { sql.openConnection(); sql.getConnection().createStatement().execute(data); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } /** * checks if message is in database * @param ident identifier of message * @return true/false */ private static boolean isInDatabase(String ident) { MySQL sql = Core.returnSQL(); try { sql.openConnection(); ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM Messages WHERE Ident='" + ident + "';"); if (!rs.next()) { sql.closeConnection(); return false; } sql.closeConnection(); return true; } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); return false; } } /** * reads all string to variables * @throws ClassNotFoundException Class couldn't be found * @throws SQLException SQL server not available or throwing error */ public static void readStrings() throws ClassNotFoundException, SQLException { PREFIX = readString("Prefix") + " "; CONSOLE_PREFIX = readString("ConsolePrefix") + " "; UNKNOWN_ERROR = PREFIX + readString("UnknownError"); NOT_A_PLAYER = readString("Commands.NoPlayer") ; NO_PERMS = PREFIX + readString("Commands.NoPerms"); ERROR = PREFIX + readString("Commands.Error"); NOT_ONLINE = PREFIX + readString("Commands.NotOnline"); KICK_RESTART = readString("Kick.Restart"); KICK_FULL = readString("Kick.Full"); COMMAND_NOT_FOUND = PREFIX + readString("Commands.Unkown"); } /** * reads single message from database * @param path path * @return String */ public static String readString(String path) { MySQL sql = Core.returnSQL(); try { sql.openConnection(); ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM Messages WHERE Ident='" + path + "';"); if (!rs.next()) { sql.closeConnection(); return null; } String message = rs.getString("Message"); sql.closeConnection(); return message; } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); return null; } } }