Skip to content

Commit

Permalink
Store home yaw and pitch #165
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgH93 committed Oct 11, 2020
1 parent 0d0d593 commit 8df85ff
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,29 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import lombok.Getter;

public class Home
{
private final String homeServer, world;
private final double x, y, z;
@Getter private final double x, y, z;
@Getter private final float yaw, pitch;

@Deprecated
public Home(final @NotNull String world, final @Nullable String homeServer, final double x, final double y, final double z)
{
this(world, homeServer, x, y, z, 0, 0);
}

public Home(final @NotNull String world, final @Nullable String homeServer, final double x, final double y, final double z, final float yaw, final float pitch)
{
this.world = world;
this.homeServer = homeServer;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}

/**
Expand All @@ -45,36 +56,6 @@ public Home(final @NotNull String world, final @Nullable String homeServer, fina
return homeServer;
}

/**
* Gets the x position of the represented home.
*
* @return The x position.
*/
public double getX()
{
return x;
}

/**
* Gets the y position of the represented home.
*
* @return The y position.
*/
public double getY()
{
return y;
}

/**
* Gets the z position of the represented home.
*
* @return The z position.
*/
public double getZ()
{
return z;
}

/**
* Gets the world name of the represented home.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ public Home(Location location)

public Home(@NotNull Location location, @Nullable String homeServer)
{
super(location.getWorld().getName(), homeServer, location.getX(), location.getY(), location.getZ());
super(location.getWorld().getName(), homeServer, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
this.location = location;
}

public Home(final @NotNull String world, final @Nullable String server, final double x, final double y, final double z, final float yaw, final float pitch)
{
super(world, server, x, y, z, yaw, pitch);
this.location = new Location(Bukkit.getServer().getWorld(world), x, y, z, yaw, pitch);
}

@Deprecated
public Home(final @NotNull String world, final @Nullable String server, final double x, final double y, final double z)
{
super(world, server, x, y, z);
this.location = new Location(Bukkit.getServer().getWorld(world), x, y, z);
this.location = new Location(Bukkit.getServer().getWorld(world), x, y, z, 0, 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public MarriageHome(Location location, String homeServer)
super(location, homeServer);
}

public MarriageHome(String worldName, double x, double y, double z, String homeServer)
public MarriageHome(String worldName, double x, double y, double z, float yaw, float pitch, String homeServer)
{
super(worldName, homeServer, x, y, z);
super(worldName, homeServer, x, y, z, yaw, pitch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public MarriageData produceMarriage(final @NotNull MarriagePlayerData player1, f
}

@Override
public MarriageHome produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z)
public MarriageHome produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z, float yaw, float pitch)
{
return new MarriageHome(world, x, y, z, server);
return new MarriageHome(world, x, y, z, yaw, pitch, server);
}

@Override
Expand Down Expand Up @@ -90,4 +90,10 @@ public void runAsync(final @NotNull Runnable runnable, final long delay)
}
return connectionProvider;
}

@Override
public @NotNull String getPluginVersion()
{
return plugin.getDescription().getVersion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class PlatformSpecific implements IPlatformSpecific<MarriagePlayerData, M
}

@Override
public @NotNull Home produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z)
public @NotNull Home produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z, float yaw, float pitch)
{
return new Home(world, server, x, y, z);
return new Home(world, server, x, y, z, yaw, pitch);
}

@Override
Expand Down Expand Up @@ -88,4 +88,10 @@ public void runAsync(final @NotNull Runnable runnable, final long delay)
}
return connectionProvider;
}

@Override
public @NotNull String getPluginVersion()
{
return plugin.getDescription().getVersion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public boolean supportsBungeeCord()
}

@Override
protected void buildQuerys()
protected void buildQueries()
{
queryAddPlayer = "INSERT IGNORE INTO {TPlayers} ({FName},{FUUID},{FShareBackpack}) SELECT ?,?,? FROM (SELECT 1) AS `tmp` WHERE NOT EXISTS (SELECT * FROM {TPlayers} WHERE {FUUID}=?);";
super.buildQuerys();
super.buildQueries();
}

void queryDatabaseVersion(final @NotNull Connection connection)
Expand Down Expand Up @@ -115,7 +115,8 @@ protected void checkDatabase()
"CONSTRAINT fk_{TMarriages}_{TPlayers}_{FPlayer2} FOREIGN KEY ({FPlayer2}) REFERENCES {TPlayers} ({FPlayerID}) ON DELETE CASCADE ON UPDATE CASCADE,\n" +
"CONSTRAINT fk_{TMarriages}_{TPlayers}_{FPriest} FOREIGN KEY ({FPriest}) REFERENCES {TPlayers} ({FPlayerID}) ON DELETE SET NULL ON UPDATE CASCADE\n)" + getEngine() + ";"),
queryTHomes = replacePlaceholders("CREATE TABLE IF NOT EXISTS {THomes} (\n{FMarryID} INT UNSIGNED NOT NULL,\n{FHomeX} DOUBLE NOT NULL,\n{FHomeY} DOUBLE NOT NULL,\n{FHomeZ} DOUBLE NOT NULL,\n" +
"{FHomeWorld} VARCHAR(45) NOT NULL DEFAULT 'world',\n" + ((useBungee) ? "{FHomeServer} VARCHAR(45) DEFAULT NULL,\n" : "") + "PRIMARY KEY ({FMarryID}),\n" +
"{FHomeYaw} FLOAT DEFAULT 0,\n{FHomePitch} FLOAT DEFAULT 0,\n{FHomeWorld} VARCHAR(45) NOT NULL DEFAULT 'world',\n" +
((useBungee) ? "{FHomeServer} VARCHAR(45) DEFAULT NULL,\n" : "") + "PRIMARY KEY ({FMarryID}),\n" +
"CONSTRAINT fk_{THomes}_{TMarriages}_{FMarryID} FOREIGN KEY ({FMarryID}) REFERENCES {TMarriages} ({FMarryID}) ON DELETE CASCADE ON UPDATE CASCADE\n)" + getEngine() + ";");
DBTools.updateDB(connection, queryTPlayers);
DBTools.updateDB(connection, queryTMarriages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public abstract class SQL<MARRIAGE_PLAYER extends MarriagePlayerDataBase, MARRIA
//region Field Names
protected String fieldPlayerID = "player_id", fieldName = "name", fieldUUID = "uuid", fieldShareBackpack = "sharebackpack", fieldPriestID = "player_id", fieldSurname = "surname"; // Player
protected String fieldMarryID = "marry_id", fieldPlayer1 = "player1", fieldPlayer2 = "player2", fieldPriest = "priest", fieldPVPState = "pvp_state", fieldDate = "date", fieldColor = "color"; // Marriage
protected String fieldHomeX = "home_x", fieldHomeY = "home_y", fieldHomeZ = "home_z", fieldHomeWorld = "home_world", fieldHomeServer = "home_server"; // Home
protected String fieldHomeX = "home_x", fieldHomeY = "home_y", fieldHomeZ = "home_z", fieldHomeWorld = "home_world", fieldHomeServer = "home_server", fieldHomeYaw = "home_yaw", fieldHomePitch = "home_pitch"; // Home
//endregion
//region Querys
@Language("SQL") protected String queryDelHome = "DELETE FROM {THomes} WHERE {FMarryID}=?;", queryUpdateHome = "REPLACE INTO {THomes} ({FMarryID},{FHomeX},{FHomeY},{FHomeZ},{FHomeWorld},{FHomeServer}) VALUES (?,?,?,?,?,?);";
//region Queries
@Language("SQL") protected String queryDelHome = "DELETE FROM {THomes} WHERE {FMarryID}=?;", queryUpdateHome = "REPLACE INTO {THomes} ({FMarryID},{FHomeX},{FHomeY},{FHomeZ},{FHomeYaw},{FHomePitch},{FHomeWorld},{FHomeServer}) VALUES (?,?,?,?,?,?,?,?);";
@Language("SQL") protected String queryPvPState = "UPDATE {TMarriages} SET {FPvPState}=? WHERE {FMarryID}=?;", querySetSurname = "UPDATE {TMarriages} SET {FSurname}=? WHERE {FMarryID}=?;";
@Language("SQL") protected String queryDelMarriage = "DELETE FROM {TMarriages} WHERE {FMarryID}=?;", querySetBackpackShareState = "UPDATE {TPlayers} SET {FShareBackpack}=? WHERE {FPlayerID}=?;";
@Language("SQL") protected String queryMarry = "INSERT INTO {TMarriages} ({FPlayer1},{FPlayer2},{FPriest},{FPvPState},{FDate}) VALUES (?,?,?,?,?);", queryLoadHome = "SELECT * FROM {THomes} WHERE {FMarryID}=?";
Expand All @@ -79,7 +79,7 @@ protected SQL(final @NotNull IPlatformSpecific<MARRIAGE_PLAYER, MARRIAGE, HOME>
this.dbConfig = dbConfig;
this.connectionProvider = connectionProvider;
loadTableAndFieldNames();
buildQuerys();
buildQueries();
}

@Override
Expand Down Expand Up @@ -144,15 +144,17 @@ protected void loadTableAndFieldNames()
fieldHomeX = dbConfig.getSQLField("HomeX", fieldHomeX);
fieldHomeY = dbConfig.getSQLField("HomeY", fieldHomeY);
fieldHomeZ = dbConfig.getSQLField("HomeZ", fieldHomeZ);
fieldHomeYaw = dbConfig.getSQLField("HomeYaw", fieldHomeYaw);
fieldHomePitch = dbConfig.getSQLField("HomePitch", fieldHomePitch);
fieldHomeWorld = dbConfig.getSQLField("HomeWorld", fieldHomeWorld);
fieldHomeServer = dbConfig.getSQLField("HomeServer", fieldHomeServer);
}

protected void buildQuerys()
protected void buildQueries()
{
if(!useBungee)
{
queryUpdateHome = queryUpdateHome.replaceAll(",\\{FHomeServer}", "").replace("(?,?,?,?,?,?)", "(?,?,?,?,?)");
queryUpdateHome = queryUpdateHome.replaceAll(",\\{FHomeServer}", "").replace("(?,?,?,?,?,?,?,?)", "(?,?,?,?,?,?,?)");
}
if(surnameEnabled)
{
Expand Down Expand Up @@ -196,8 +198,9 @@ protected void setTableAndFieldNames()
.replaceAll("\\{TPlayers}", tablePlayers).replaceAll("\\{TMarriages}", tableMarriages).replaceAll("\\{TPriests}", tablePriests).replaceAll("\\{THomes}", tableHomes) // Table names
.replaceAll("\\{FPlayerID}", fieldPlayerID).replaceAll("\\{FName}", fieldName).replaceAll("\\{FUUID}", fieldUUID).replaceAll("\\{FShareBackpack}", fieldShareBackpack) // Player fields
.replaceAll("\\{FMarryID}", fieldMarryID).replaceAll("\\{FSurname}", fieldSurname).replaceAll("\\{FPlayer1}", fieldPlayer1).replaceAll("\\{FPlayer2}", fieldPlayer2) // Marriage fields
.replaceAll("\\{FPriest}", fieldPriest).replaceAll("\\{FPvPState}", fieldPVPState).replaceAll("\\{FDate}", fieldDate).replaceAll("\\{FHomeServer}", fieldHomeServer).replaceAll("\\{FColor}", fieldColor)
.replaceAll("\\{FHomeX}", fieldHomeX).replaceAll("\\{FHomeY}", fieldHomeY).replaceAll("\\{FHomeZ}", fieldHomeZ).replaceAll("\\{FHomeWorld}", fieldHomeWorld) // Home fields
.replaceAll("\\{FPriest}", fieldPriest).replaceAll("\\{FPvPState}", fieldPVPState).replaceAll("\\{FDate}", fieldDate).replaceAll("\\{FColor}", fieldColor)
.replaceAll("\\{FHomeServer}", fieldHomeServer).replaceAll("\\{FHomeX}", fieldHomeX).replaceAll("\\{FHomeY}", fieldHomeY).replaceAll("\\{FHomeZ}", fieldHomeZ) // Home fields
.replaceAll("\\{FHomeYaw}", fieldHomeYaw).replaceAll("\\{FHomePitch}", fieldHomePitch).replaceAll("\\{FHomeWorld}", fieldHomeWorld)
.replaceAll("\\{FPriestID}", fieldPriestID);
}

Expand Down Expand Up @@ -456,7 +459,7 @@ protected void loadHomes()
while(rs.next())
{
String homeServer = (useBungee) ? rs.getString(fieldHomeServer) : null;
homes.put(rs.getInt(fieldMarryID), platform.produceHome("", rs.getString(fieldHomeWorld), homeServer, rs.getDouble(fieldHomeX), rs.getDouble(fieldHomeY), rs.getDouble(fieldHomeZ)));
homes.put(rs.getInt(fieldMarryID), platform.produceHome("", rs.getString(fieldHomeWorld), homeServer, rs.getDouble(fieldHomeX), rs.getDouble(fieldHomeY), rs.getDouble(fieldHomeZ), rs.getFloat(fieldHomeYaw), rs.getFloat(fieldHomePitch)));
}
for(Object marriage : cache.getLoadedMarriages())
{
Expand Down Expand Up @@ -488,7 +491,7 @@ public void loadHome(final MARRIAGE marriage)
if(rs.next())
{
String homeServer = (useBungee) ? rs.getString(fieldHomeServer) : null;
marriage.setHomeData(platform.produceHome("", rs.getString(fieldHomeWorld), homeServer, rs.getDouble(fieldHomeX), rs.getDouble(fieldHomeY), rs.getDouble(fieldHomeZ)));
marriage.setHomeData(platform.produceHome("", rs.getString(fieldHomeWorld), homeServer, rs.getDouble(fieldHomeX), rs.getDouble(fieldHomeY), rs.getDouble(fieldHomeZ), rs.getFloat(fieldHomeYaw), rs.getFloat(fieldHomePitch)));
}
else
{
Expand Down Expand Up @@ -620,11 +623,11 @@ public void updateHome(final @NotNull MARRIAGE marriage)
{
if(useBungee)
{
runStatementAsyncIncludeKeyFirst(queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getWorldName(), home.getHomeServer());
runStatementAsyncIncludeKeyFirst(queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getYaw(), home.getPitch(), home.getWorldName(), home.getHomeServer());
}
else
{
runStatementAsyncIncludeKeyFirst(queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getWorldName());
runStatementAsyncIncludeKeyFirst(queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getYaw(), home.getPitch(), home.getWorldName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import at.pcgamingfreaks.ConsoleColor;
import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider;
import at.pcgamingfreaks.Database.ConnectionProvider.SQLiteConnectionProvider;
import at.pcgamingfreaks.Database.DBTools;
import at.pcgamingfreaks.MarriageMaster.API.Home;
import at.pcgamingfreaks.MarriageMaster.Database.*;
import at.pcgamingfreaks.Version;
Expand Down Expand Up @@ -61,6 +62,8 @@ protected void checkDatabase()
{
try(Connection connection = getConnection(); Statement statement = connection.createStatement())
{
Version dbVersion = getDatabaseVersion(statement);

statement.execute(replacePlaceholders("CREATE TABLE IF NOT EXISTS {TPlayers} ({FPlayerID} INTEGER PRIMARY KEY NOT NULL, {FName} VARCHAR(16) NOT NULL, {FUUID} CHAR(36) UNIQUE DEFAULT NULL, " +
"{FShareBackpack} TINYINT(1) NOT NULL DEFAULT 0);"));
statement.execute(replacePlaceholders("CREATE TABLE IF NOT EXISTS {TPriests} ({FPlayerID} INTEGER PRIMARY KEY NOT NULL," +
Expand All @@ -76,8 +79,16 @@ protected void checkDatabase()
}
catch(SQLException ignored) {}
statement.execute(replacePlaceholders("CREATE TABLE IF NOT EXISTS {THomes} ({FMarryID} INTEGER PRIMARY KEY NOT NULL,{FHomeX} DOUBLE NOT NULL,{FHomeY} DOUBLE NOT NULL," +
"{FHomeZ} DOUBLE NOT NULL,{FHomeWorld} VARCHAR(45) NOT NULL DEFAULT 'world'," +
"{FHomeZ} DOUBLE NOT NULL, {FHomeYaw} FLOAT DEFAULT 0, {FHomePitch} FLOAT DEFAULT 0,{FHomeWorld} VARCHAR(45) NOT NULL DEFAULT 'world'," +
"CONSTRAINT fk_{THomes}_{TMarriages}_{FMarryID} FOREIGN KEY ({FMarryID}) REFERENCES {TMarriages} ({FMarryID}) ON DELETE CASCADE ON UPDATE CASCADE);"));
try
{
statement.execute(replacePlaceholders("ALTER TABLE {THomes} ADD COLUMN {FHomeYaw} FLOAT DEFAULT 0;"));
statement.execute(replacePlaceholders("ALTER TABLE {THomes} ADD COLUMN {FHomePitch} FLOAT DEFAULT 0;"));
}
catch(SQLException ignored) {}

DBTools.runStatement(connection, "INSERT OR REPLACE INTO `mariagemaster_metadata` (`key`, `value`) VALUES ('db_version',?);", platform.getPluginVersion());
}
catch(SQLException e)
{
Expand All @@ -86,10 +97,20 @@ protected void checkDatabase()
}
}

private @NotNull Version getDatabaseVersion(final @NotNull Statement stmt) throws SQLException
{
stmt.execute("CREATE TABLE IF NOT EXISTS `mariagemaster_metadata` (`key` CHAR(32) PRIMARY KEY NOT NULL, `value` TEXT);");
try(ResultSet rs = stmt.executeQuery("SELECT `value` FROM `mariagemaster_metadata` WHERE `key`='db_version';"))
{
if(rs.next()) return new Version(rs.getString("value"));
}
return new Version("0");
}

@Override
protected void buildQuerys()
protected void buildQueries()
{
super.buildQuerys();
super.buildQueries();
querySetPriest = querySetPriest .replace("REPLACE INTO", "INSERT OR REPLACE INTO").replace(" VALUE ", " VALUES ");
queryUpdateHome = queryUpdateHome.replace("REPLACE INTO", "INSERT OR REPLACE INTO");
queryAddPlayer = queryAddPlayer .replace("INSERT IGNORE INTO", "INSERT OR IGNORE INTO");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public interface IPlatformSpecific<MARRIAGE_PLAYER extends MarriagePlayerDataBas
MARRIAGE_PLAYER producePlayer(final @Nullable UUID uuid, final @NotNull String name, boolean priest, final boolean sharesBackpack, final @Nullable Object databaseKey);
MARRIAGE produceMarriage(final @NotNull MARRIAGE_PLAYER player1, final @NotNull MARRIAGE_PLAYER player2, final @Nullable MARRIAGE_PLAYER priest, final @NotNull Date weddingDate,
final @Nullable String surname, final boolean pvpEnabled, final MessageColor color, final @Nullable HOME home, final @Nullable Object databaseKey);
HOME produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z);
HOME produceHome(final @NotNull String name, final @NotNull String world, final @Nullable String server, final double x, final double y, final double z, final float yaw, final float pitch);

/**
* @param runnable The runnable that should be executed
* @param delay The amount of ticks (20 ticks per second) that should be waited before executing
*/
void runAsync(@NotNull Runnable runnable, long delay);
@Nullable ConnectionProvider getExternalConnectionProvider(final @NotNull String dbType, final @NotNull Logger logger) throws SQLException;
@NotNull String getPluginVersion();
}

0 comments on commit 8df85ff

Please sign in to comment.