Implemented vice versa system for playerpositions

This commit is contained in:
Butzlabben 2019-04-22 21:53:21 +02:00
parent 5bd390fd62
commit a8cd6af9c7
9 changed files with 131 additions and 33 deletions

View File

@ -149,6 +149,7 @@ public class WorldSystem extends JavaPlugin {
if (languages.exists() == false)
languages.mkdirs();
PluginConfig.checkConfig(config);
// Done with #6
MessageConfig.checkConfig(new File(languages, "en.yml"));

View File

@ -189,13 +189,13 @@ public class WSCommand {
if (wp.isOnSystemWorld()) {
// Extra safety for #2
if (PluginConfig.getSpawn().getWorld() == null) {
if (PluginConfig.getSpawn(null).getWorld() == null) {
Bukkit.getConsoleSender().sendMessage(PluginConfig.getPrefix() + "§cThe spawn is not properly set");
p.sendMessage(PluginConfig.getPrefix() + "§cThe spawn is not properly set");
return;
}
p.teleport(PluginConfig.getSpawn());
p.teleport(PluginConfig.getSpawn(p));
p.setGameMode(PluginConfig.getSpawnGamemode());
World w = Bukkit.getWorld(p.getWorld().getName());
SystemWorld.tryUnloadLater(w);

View File

@ -59,7 +59,7 @@ public class WorldAdministrateCommand {
if (a.isOnline()) {
Player t = (Player) a;
if (t.getWorld().getName().equals(new DependenceConfig(p).getWorldname())) {
t.teleport(PluginConfig.getSpawn());
t.teleport(PluginConfig.getSpawn(t));
t.setGameMode(PluginConfig.getSpawnGamemode());
}
}

View File

@ -12,6 +12,7 @@ import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import de.butzlabben.world.util.PlayerPositions;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -20,6 +21,7 @@ import org.bukkit.WorldCreator;
import org.bukkit.World.Environment;
import org.bukkit.WorldType;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import de.butzlabben.world.WorldSystem;
@ -44,7 +46,7 @@ public class PluginConfig {
&& cfg.isString("worldtemplates.default") &&
// Database stuff
cfg.isString("database.type") && cfg.isString("database.table_name")
cfg.isString("database.type") && cfg.isString("database.worlds_table_name") && cfg.isString("database.players_table_name")
&& cfg.isString("database.mysql_settings.host") && cfg.isInt("database.mysql_settings.port")
&& cfg.isString("database.mysql_settings.username") && cfg.isString("database.mysql_settings.password")
&& cfg.isString("database.mysql_settings.database") && cfg.isString("database.sqlite_settings.file") &&
@ -58,6 +60,7 @@ public class PluginConfig {
&& (cfg.isLong("worldgeneration.seed") || cfg.isInt("worldgeneration.seed")) &&
cfg.isString("spawn.spawnpoint.world") && cfg.isInt("spawn.gamemode")
&& cfg.isBoolean("spawn.spawnpoint.use_last_location")
&& (cfg.isDouble("spawn.spawnpoint.x") || cfg.isInt("spawn.spawnpoint.x"))
&& (cfg.isDouble("spawn.spawnpoint.y") || cfg.isInt("spawn.spawnpoint.y"))
&& (cfg.isDouble("spawn.spawnpoint.z") || cfg.isInt("spawn.spawnpoint.z"))
@ -93,7 +96,7 @@ public class PluginConfig {
}
// Should fix #2
if (getSpawn().getWorld() == null) {
if (getSpawn(null).getWorld() == null) {
Bukkit.getConsoleSender().sendMessage(getPrefix() + "§cWorld is null in spawn.world!");
}
}
@ -167,9 +170,10 @@ public class PluginConfig {
return getLocation(getConfig(), "worldspawn.spawnpoint", w);
}
public static Location getSpawn() {
public static Location getSpawn(Player player) {
YamlConfiguration cfg = getConfig();
return getLocation(cfg, "spawn.spawnpoint", Bukkit.getWorld(cfg.getString("spawn.spawnpoint.world", "world")));
Location location = getLocation(cfg, "spawn.spawnpoint", Bukkit.getWorld(cfg.getString("spawn.spawnpoint.world", "world")));
return PlayerPositions.getInstance().injectPlayersLocation(player, location);
}
public static long getSeed() {
@ -249,12 +253,20 @@ public class PluginConfig {
return creator;
}
public static boolean useLastLocation() {
public static boolean useWorldSpawnLastLocation() {
return getConfig().getBoolean("worldspawn.use_last_location");
}
public static String getTableName() {
return getConfig().getString("database.table_name");
public static boolean useSpawnLastLocation() {
return getConfig().getBoolean("spawn.spawnpoint.use_last_location");
}
public static String getWorldsTableName() {
return getConfig().getString("database.worlds_table_name");
}
public static String getPlayersTableName() {
return getConfig().getString("database.players_table_name");
}
public static String getDatabaseType() {

View File

@ -62,9 +62,12 @@ public class CommandListener implements Listener {
// Fix for #18
if (from != to || WorldConfig.exists(from.getName())) {
// Save location for #23
if(WorldConfig.exists(from.getName())) {
if (WorldConfig.exists(from.getName())) {
WorldConfig config = WorldConfig.getWorldConfig(from.getName());
PlayerPositions.getInstance().savePlayerLocation(p, config);
PlayerPositions.getInstance().saveWorldsPlayerLocation(p, config);
} else {
if (WorldConfig.exists(to.getName()))
PlayerPositions.getInstance().savePlayerLocation(p);
}
GameMode gameMode = PluginConfig.getSpawnGamemode();
if (WorldConfig.exists(to.getName())) {

View File

@ -26,7 +26,7 @@ public class PlayerListener implements Listener {
if (dc.hasWorld()) {
SystemWorld sw = SystemWorld.getSystemWorld(dc.getWorldname());
if (sw != null && !sw.isLoaded()) {
e.getPlayer().teleport(PluginConfig.getSpawn());
e.getPlayer().teleport(PluginConfig.getSpawn(e.getPlayer()));
}
}
}
@ -40,7 +40,7 @@ public class PlayerListener implements Listener {
// Save last location for #23
if (player.isOnSystemWorld()) {
WorldConfig config = WorldConfig.getWorldConfig(player.getWorldname());
PlayerPositions.getInstance().savePlayerLocation(p, config);
PlayerPositions.getInstance().saveWorldsPlayerLocation(p, config);
}
SystemWorld.tryUnloadLater(w);
}

View File

@ -6,7 +6,9 @@ import de.butzlabben.world.config.WorldConfig;
import de.butzlabben.world.util.database.DatabaseRepository;
import de.butzlabben.world.util.database.DatabaseUtil;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.sql.PreparedStatement;
@ -17,7 +19,6 @@ import java.util.UUID;
/*
Class for implementing #23
*/
@Getter
public class PlayerPositions {
@Getter
@ -25,8 +26,9 @@ public class PlayerPositions {
private DatabaseUtil util = DatabaseRepository.getInstance().getUtil();
public Location injectLocation(Player player, WorldConfig config, Location location) {
if (!PluginConfig.useLastLocation())
public Location injectWorldsLocation(Player player, WorldConfig config, Location location) {
if (!PluginConfig.useWorldSpawnLastLocation())
return location;
Preconditions.checkNotNull(player);
@ -35,7 +37,7 @@ public class PlayerPositions {
UUID uuid = player.getUniqueId();
int id = config.getId();
UUID owner = config.getOwner();
String tableName = PluginConfig.getTableName();
String tableName = PluginConfig.getWorldsTableName();
try {
PreparedStatement ps = util.prepareStatement("SELECT * FROM " + tableName + " WHERE player=? AND id=? AND owner=?");
@ -60,8 +62,74 @@ public class PlayerPositions {
return location;
}
public void savePlayerLocation(Player player, WorldConfig config) {
if (!PluginConfig.useLastLocation())
public Location injectPlayersLocation(Player player, Location location) {
if (!PluginConfig.useSpawnLastLocation())
return location;
if(player == null)
return location;
Preconditions.checkNotNull(location);
UUID uuid = player.getUniqueId();
String tableName = PluginConfig.getPlayersTableName();
try {
PreparedStatement ps = util.prepareStatement("SELECT * FROM " + tableName + " WHERE player=?");
ps.setString(1, uuid.toString());
ResultSet rs = util.executeQuery(ps);
if (!rs.next())
return location;
double x = rs.getDouble("x");
double y = rs.getDouble("y");
double z = rs.getDouble("z");
location.setX(x);
location.setY(y);
location.setZ(z);
String locWorldName = location.getWorld().getName();
if (!locWorldName.equals(rs.getString("world"))) {
World world = Bukkit.getWorld(rs.getString("world"));
if (world != null) {
location.setWorld(world);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return location;
}
public void savePlayerLocation(Player player) {
if (!PluginConfig.useSpawnLastLocation())
return;
Preconditions.checkNotNull(player);
String playersTableName = PluginConfig.getPlayersTableName();
UUID uuid = player.getUniqueId();
Location location = player.getLocation();
try {
PreparedStatement ps = util.prepareStatement("REPLACE INTO " + playersTableName +
" (player, world, x, y, z) VALUES (?, ?, ?, ?, ?)");
ps.setString(1, uuid.toString());
ps.setString(2, location.getWorld().getName());
ps.setDouble(3, location.getX());
ps.setDouble(4, location.getY());
ps.setDouble(5, location.getZ());
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void saveWorldsPlayerLocation(Player player, WorldConfig config) {
if (!PluginConfig.useWorldSpawnLastLocation())
return;
Preconditions.checkNotNull(player);
@ -70,7 +138,7 @@ public class PlayerPositions {
int id = config.getId();
UUID owner = config.getOwner();
Location location = player.getLocation();
String tableName = PluginConfig.getTableName();
String tableName = PluginConfig.getWorldsTableName();
try {
PreparedStatement ps = util.prepareStatement("REPLACE INTO " + tableName +
" (player, id, owner, x, y, z) VALUES (?, ?, ?, ?, ?, ?)");
@ -90,7 +158,7 @@ public class PlayerPositions {
public void deletePositions(WorldConfig config) {
Preconditions.checkNotNull(config);
String tableName = PluginConfig.getTableName();
String tableName = PluginConfig.getWorldsTableName();
int id = config.getId();
UUID owner = config.getOwner();
@ -108,9 +176,9 @@ public class PlayerPositions {
}
private void checkTables() {
String tableName = PluginConfig.getTableName();
String worldsTableName = PluginConfig.getWorldsTableName();
try {
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName +
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + worldsTableName +
" ( `player` VARCHAR(36) NOT NULL , `id` INT NOT NULL , `owner` VARCHAR(36) NOT NULL , " +
"`x` DOUBLE NOT NULL , `y` DOUBLE NOT NULL , `z` DOUBLE NOT NULL , PRIMARY KEY (`player`, `id`, `owner`))");
@ -118,6 +186,17 @@ public class PlayerPositions {
} catch (SQLException e) {
e.printStackTrace();
}
String playersTableName = PluginConfig.getPlayersTableName();
try {
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + playersTableName +
"( `player` VARCHAR(36) NOT NULL , `world` TEXT NOT NULL , " +
"`x` DOUBLE NOT NULL , `y` DOUBLE NOT NULL , `z` DOUBLE NOT NULL , PRIMARY KEY (`player`))");
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
private PlayerPositions() {

View File

@ -96,8 +96,8 @@ public class SystemWorld {
WorldConfig config = WorldConfig.getWorldConfig(worldname);
for (Player a : w.getPlayers()) {
PlayerPositions.getInstance().savePlayerLocation(a, config);
a.teleport(PluginConfig.getSpawn());
PlayerPositions.getInstance().saveWorldsPlayerLocation(a, config);
a.teleport(PluginConfig.getSpawn(a));
a.setGameMode(PluginConfig.getSpawnGamemode());
}
if (unloading) {
@ -142,7 +142,7 @@ public class SystemWorld {
c.unload();
}
for (Player a : w.getPlayers()) {
a.teleport(PluginConfig.getSpawn());
a.teleport(PluginConfig.getSpawn(a));
a.setGameMode(PluginConfig.getSpawnGamemode());
}
@ -388,12 +388,12 @@ public class SystemWorld {
WorldConfig config = WorldConfig.getWorldConfig(worldname);
if (config.getHome() != null) {
p.teleport(positions.injectLocation(p, config, config.getHome()));
p.teleport(positions.injectWorldsLocation(p, config, config.getHome()));
} else {
if (PluginConfig.useWorldSpawn()) {
p.teleport(positions.injectLocation(p, config, PluginConfig.getWorldSpawn(w)));
p.teleport(positions.injectWorldsLocation(p, config, PluginConfig.getWorldSpawn(w)));
} else {
p.teleport(positions.injectLocation(p, config, w.getSpawnLocation()));
p.teleport(positions.injectWorldsLocation(p, config, w.getSpawnLocation()));
}
}
if (PluginConfig.isSurvival()) {

View File

@ -49,8 +49,10 @@ database:
# 'mysql' or 'sqlite'
# You need a working mysql database in order to use this option
type: sqlite
# How the table with the saved player positions should be named
table_name: player_positions
# How the table with the saved player positions on the playerworlds should be named
worlds_table_name: worlds_positions
# How the table with the saved player positions on the normal worlds should be named
players_table_name : player_positions
# Configure here your mysql connection
mysql_settings:
host: 127.0.0.1
@ -113,6 +115,7 @@ worldgeneration:
spawn:
gamemode: 2
spawnpoint:
use_last_location: true
world: world
x: 0
y: 20
@ -129,4 +132,4 @@ worldspawn:
y: 20
z: 0
yaw: 0
pitch: 0
pitch: 0