This commit is contained in:
Butzlabben 2019-04-22 16:47:36 +02:00
parent 1f02b57adb
commit 0358a634b2
13 changed files with 147 additions and 13 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.butzlabben.world</groupId>
<artifactId>WorldSystem</artifactId>
<version>2.4.4.2</version>
<version>2.4.5</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.number>-</project.build.number>

View File

@ -5,6 +5,7 @@ import de.butzlabben.world.command.*;
import de.butzlabben.world.config.*;
import de.butzlabben.world.listener.*;
import de.butzlabben.world.util.PapiExtension;
import de.butzlabben.world.util.database.DatabaseRepository;
import de.butzlabben.world.wrapper.AsyncCreatorAdapter;
import de.butzlabben.world.wrapper.CreatorAdapter;
import de.butzlabben.world.wrapper.SystemWorld;
@ -83,6 +84,9 @@ public class WorldSystem extends JavaPlugin {
framework.registerCommands(new WorldAdministrateCommand());
// Establish database connection
DatabaseRepository.getInstance().getUtil().connect();
System.setProperty("bstats.relocatecheck", "false");
Metrics m = new Metrics(this);
m.addCustomChart(new Metrics.SingleLineChart("worlds", DependenceConfig::getHighestID));
@ -126,6 +130,9 @@ public class WorldSystem extends JavaPlugin {
}
}
// Close database connection
DatabaseRepository.getInstance().getUtil().close();
Bukkit.getConsoleSender()
.sendMessage(PluginConfig.getPrefix() + "Succesfully disabled WorldSystem v" + version);
}

View File

@ -64,7 +64,7 @@ public class PluginConfig {
&& (cfg.isDouble("spawn.spawnpoint.yaw") || cfg.isInt("spawn.spawnpoint.yaw"))
&& (cfg.isDouble("spawn.spawnpoint.pitch") || cfg.isInt("spawn.spawnpoint.pitch")) &&
cfg.isBoolean("worldspawn.use")
cfg.isBoolean("worldspawn.use") && cfg.isBoolean("worldspawn.use_last_location")
&& (cfg.isDouble("worldspawn.spawnpoint.x") || cfg.isInt("worldspawn.spawnpoint.x"))
&& (cfg.isDouble("worldspawn.spawnpoint.y") || cfg.isInt("worldspawn.spawnpoint.y"))
&& (cfg.isDouble("worldspawn.spawnpoint.z") || cfg.isInt("worldspawn.spawnpoint.z"))
@ -249,7 +249,11 @@ public class PluginConfig {
return creator;
}
public static String getTablename() {
public static boolean useLastLocation() {
return getConfig().getBoolean("worldspawn.use_last_location");
}
public static String getTableName() {
return getConfig().getString("database.table_name");
}

View File

@ -3,6 +3,7 @@ package de.butzlabben.world.listener;
import de.butzlabben.world.config.MessageConfig;
import de.butzlabben.world.config.PluginConfig;
import de.butzlabben.world.config.WorldConfig;
import de.butzlabben.world.util.PlayerPositions;
import de.butzlabben.world.wrapper.SystemWorld;
import de.butzlabben.world.wrapper.WorldPlayer;
import org.bukkit.Bukkit;
@ -60,6 +61,11 @@ public class CommandListener implements Listener {
// Fix for #18
if (from != to || WorldConfig.exists(from.getName())) {
// Save location for #23
if(WorldConfig.exists(from.getName())) {
WorldConfig config = WorldConfig.getWorldConfig(from.getName());
PlayerPositions.getInstance().savePlayerLocation(p, config);
}
GameMode gameMode = PluginConfig.getSpawnGamemode();
if (WorldConfig.exists(to.getName())) {
if (PluginConfig.isSurvival()) {

View File

@ -1,5 +1,8 @@
package de.butzlabben.world.listener;
import de.butzlabben.world.config.WorldConfig;
import de.butzlabben.world.util.PlayerPositions;
import de.butzlabben.world.wrapper.WorldPlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -33,6 +36,12 @@ public class PlayerListener implements Listener {
public void onLeave(PlayerQuitEvent e) {
Player p = e.getPlayer();
World w = p.getWorld();
WorldPlayer player = new WorldPlayer(p);
// Save last location for #23
if (player.isOnSystemWorld()) {
WorldConfig config = WorldConfig.getWorldConfig(player.getWorldname());
PlayerPositions.getInstance().savePlayerLocation(p, config);
}
SystemWorld.tryUnloadLater(w);
}
}

View File

@ -0,0 +1,107 @@
package de.butzlabben.world.util;
import com.google.common.base.Preconditions;
import de.butzlabben.world.config.PluginConfig;
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.Location;
import org.bukkit.entity.Player;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
/*
Class for implementing #23
*/
@Getter
public class PlayerPositions {
@Getter
private static PlayerPositions instance = new PlayerPositions();
private DatabaseUtil util = DatabaseRepository.getInstance().getUtil();
public Location injectLocation(Player player, WorldConfig config, Location location) {
if (!PluginConfig.useLastLocation())
return location;
Preconditions.checkNotNull(player);
Preconditions.checkNotNull(config);
Preconditions.checkNotNull(location);
UUID uuid = player.getUniqueId();
int id = config.getId();
UUID owner = config.getOwner();
String tableName = PluginConfig.getTableName();
try {
PreparedStatement ps = util.prepareStatement("SELECT * FROM " + tableName + " WHERE player=? AND id=? AND owner=?");
ps.setString(1, uuid.toString());
ps.setInt(2, id);
ps.setString(3, owner.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);
} catch (SQLException e) {
e.printStackTrace();
}
return location;
}
public void savePlayerLocation(Player player, WorldConfig config) {
if (!PluginConfig.useLastLocation())
return;
Preconditions.checkNotNull(player);
Preconditions.checkNotNull(config);
UUID uuid = player.getUniqueId();
int id = config.getId();
UUID owner = config.getOwner();
Location location = player.getLocation();
String tableName = PluginConfig.getTableName();
try {
PreparedStatement ps = util.prepareStatement("REPLACE INTO " + tableName +
" (player, id, owner, x, y, z) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, uuid.toString());
ps.setInt(2, id);
ps.setString(3, owner.toString());
ps.setDouble(4, location.getX());
ps.setDouble(5, location.getY());
ps.setDouble(6, location.getZ());
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void checkTables() {
String tableName = PluginConfig.getTableName();
try {
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName +
" ( `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`))");
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
private PlayerPositions() {
checkTables();
}
}

View File

@ -1,6 +1,4 @@
package de.butzlabben.world.util;
import de.butzlabben.world.config.PluginConfig;
package de.butzlabben.world.util.database;
import java.sql.*;

View File

@ -1,4 +1,4 @@
package de.butzlabben.world.util;
package de.butzlabben.world.util.database;
import de.butzlabben.world.config.PluginConfig;
import lombok.Getter;

View File

@ -1,4 +1,4 @@
package de.butzlabben.world.util;
package de.butzlabben.world.util.database;
import java.sql.Connection;
import java.sql.PreparedStatement;

View File

@ -1,4 +1,4 @@
package de.butzlabben.world.util;
package de.butzlabben.world.util.database;
import de.butzlabben.world.config.PluginConfig;

View File

@ -1,4 +1,4 @@
package de.butzlabben.world.util;
package de.butzlabben.world.util.database;
import de.butzlabben.world.config.PluginConfig;

View File

@ -6,6 +6,7 @@ import de.butzlabben.world.config.*;
import de.butzlabben.world.event.WorldCreateEvent;
import de.butzlabben.world.event.WorldLoadEvent;
import de.butzlabben.world.event.WorldUnloadEvent;
import de.butzlabben.world.util.PlayerPositions;
import org.apache.commons.io.FileUtils;
import org.bukkit.*;
import org.bukkit.entity.Player;
@ -370,6 +371,7 @@ public class SystemWorld {
public void teleportToWorldSpawn(Player p) {
Preconditions.checkNotNull(p, "player must not be null");
Preconditions.checkArgument(p.isOnline(), "player must be online");
PlayerPositions positions = PlayerPositions.getInstance();
if (creating) {
p.sendMessage(MessageConfig.getWorldStillCreating());
@ -383,12 +385,12 @@ public class SystemWorld {
WorldConfig config = WorldConfig.getWorldConfig(worldname);
if (config.getHome() != null) {
p.teleport(config.getHome());
p.teleport(positions.injectLocation(p, config, config.getHome()));
} else {
if (PluginConfig.useWorldSpawn()) {
p.teleport(PluginConfig.getWorldSpawn(w));
p.teleport(positions.injectLocation(p, config, PluginConfig.getWorldSpawn(w)));
} else {
p.teleport(w.getSpawnLocation());
p.teleport(positions.injectLocation(p, config, w.getSpawnLocation()));
}
}
if (PluginConfig.isSurvival()) {

View File

@ -122,6 +122,7 @@ spawn:
# Location where you spawn when you join a world
worldspawn:
use_last_location: true
use: false
spawnpoint:
x: 0