Core/src/eu/univento/core/api/Messages.java

123 lines
4.8 KiB
Java
Raw Normal View History

2015-12-06 11:04:47 +01:00
package eu.univento.core.api;
2015-12-05 12:26:39 +01:00
import java.sql.ResultSet;
import java.sql.SQLException;
2015-12-06 11:04:47 +01:00
import eu.univento.core.Core;
2015-12-05 12:26:39 +01:00
/**
* 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;
2015-12-05 15:22:24 +01:00
public static String COMMAND_NOT_FOUND;
2015-12-05 12:26:39 +01:00
/**
* 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', '<27>aunivento <20>8<EFBFBD>');");
if(!isInDatabase("ConsolePrefix")) writeData("INSERT INTO Messages(Ident, Message) values ('ConsolePrefix', '[univento Core]');");
if(!isInDatabase("UnknownError")) writeData("INSERT INTO Messages(Ident, Message) values ('UnknownError', '<27>cEs ist leider ein unbekannter Fehler aufgetreten');");
if(!isInDatabase("Commands.NoPlayer")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NoPlayer', '<27>cDu bist leider kein Spieler');");
if(!isInDatabase("Commands.NoPerms")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NoPerms', '<27>cDu hast keine Berechtigungen diesen Befehl auszuf<75>hren')");
if(!isInDatabase("Commands.Error")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.Error', '<27>cBeim ausf<73>hren dieses Befehls ist ein Fehler aufgetreten');");
if(!isInDatabase("Commands.NotOnline")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.NotOnline', '<27>cDer Spieler $player ist nicht online');");
if(!isInDatabase("Commands.Unknown")) writeData("INSERT INTO Messages(Ident, Message) values ('Commands.Unknown', '<27>6Dieser Befehl konnte leider nicht gefunden werden');");
if(!isInDatabase("Kick.Restart")) writeData("INSERT INTO Messages(Ident, Message) values ('Kick.Restart', '<27>cDer Server startet gerade neu');");
if(!isInDatabase("Kick.Full")) writeData("INSERT INTO Messages(Ident, Message) values ('Kick.Full', '<27>cDer Server ist leider schon voll')");
2015-12-05 12:26:39 +01:00
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();
}
}
2015-12-05 15:22:24 +01:00
/**
* 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;
}
}
2015-12-05 12:26:39 +01:00
/**
* 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");
2015-12-05 15:22:24 +01:00
NOT_A_PLAYER = readString("Commands.NoPlayer") ;
NO_PERMS = PREFIX + readString("Commands.NoPerms");
ERROR = PREFIX + readString("Commands.Error");
NOT_ONLINE = PREFIX + readString("Commands.NotOnline");
2015-12-05 15:22:24 +01:00
KICK_RESTART = readString("Kick.Restart");
KICK_FULL = readString("Kick.Full");
COMMAND_NOT_FOUND = PREFIX + readString("Commands.Unkown");
2015-12-05 12:26:39 +01:00
}
/**
* 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");
2015-12-05 12:26:39 +01:00
sql.closeConnection();
return message;
2015-12-05 12:26:39 +01:00
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
}