mirror of
https://github.com/songoda/UltimateKits.git
synced 2024-12-04 08:03:29 +01:00
fix location data duplication bug
This commit is contained in:
parent
58927a437b
commit
6365f72133
@ -186,6 +186,7 @@ public class UltimateKits extends JavaPlugin {
|
|||||||
/*
|
/*
|
||||||
* On plugin disable.
|
* On plugin disable.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
saveToFile();
|
saveToFile();
|
||||||
dataFile.saveConfig();
|
dataFile.saveConfig();
|
||||||
|
@ -3,6 +3,7 @@ package com.songoda.ultimatekits.command.commands;
|
|||||||
import com.songoda.ultimatekits.UltimateKits;
|
import com.songoda.ultimatekits.UltimateKits;
|
||||||
import com.songoda.ultimatekits.command.AbstractCommand;
|
import com.songoda.ultimatekits.command.AbstractCommand;
|
||||||
import com.songoda.ultimatekits.kit.Kit;
|
import com.songoda.ultimatekits.kit.Kit;
|
||||||
|
import com.songoda.ultimatekits.kit.KitBlockData;
|
||||||
import com.songoda.ultimatekits.utils.Methods;
|
import com.songoda.ultimatekits.utils.Methods;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -24,14 +25,15 @@ public class CommandSet extends AbstractCommand {
|
|||||||
return ReturnType.FAILURE;
|
return ReturnType.FAILURE;
|
||||||
}
|
}
|
||||||
Player player = (Player) sender;
|
Player player = (Player) sender;
|
||||||
String kit = args[1].toLowerCase();
|
Kit kit = instance.getKitManager().getKit(args[1].toLowerCase());
|
||||||
if (instance.getKitManager().getKit(kit) == null) {
|
if (kit == null) {
|
||||||
instance.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(sender);
|
instance.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(sender);
|
||||||
return ReturnType.FAILURE;
|
return ReturnType.FAILURE;
|
||||||
}
|
}
|
||||||
Block b = player.getTargetBlock(null, 200);
|
Block b = player.getTargetBlock(null, 200);
|
||||||
instance.getKitManager().addKitToLocation(instance.getKitManager().getKit(kit), b.getLocation());
|
KitBlockData data = instance.getKitManager().addKitToLocation(kit, b.getLocation());
|
||||||
instance.getLocale().newMessage("&8Kit &a" + kit + " &8set to: &a" + b.getType().toString() + "&8.")
|
UltimateKits.getInstance().getDataManager().createBlockData(data);
|
||||||
|
instance.getLocale().newMessage("&8Kit &a" + kit.getName() + " &8set to: &a" + b.getType().toString() + "&8.")
|
||||||
.sendPrefixedMessage(sender);
|
.sendPrefixedMessage(sender);
|
||||||
return ReturnType.SUCCESS;
|
return ReturnType.SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,8 @@ public class DataManager {
|
|||||||
"displayItems = ?, particles = ?, itemOverride = ? " +
|
"displayItems = ?, particles = ?, itemOverride = ? " +
|
||||||
"WHERE world = ? AND x = ? AND y = ? AND z = ?";
|
"WHERE world = ? AND x = ? AND y = ? AND z = ?";
|
||||||
try (PreparedStatement statement = connection.prepareStatement(updateData)) {
|
try (PreparedStatement statement = connection.prepareStatement(updateData)) {
|
||||||
for (int i = 0; i < blockData.size(); i++) {
|
for (KitBlockData data : blockData.values()) {
|
||||||
KitBlockData data = blockData.get(i);
|
if (data == null || data.getWorld() == null) continue;
|
||||||
if (data == null) continue;
|
|
||||||
statement.setString(1, data.getType().toString());
|
statement.setString(1, data.getType().toString());
|
||||||
statement.setString(2, data.getKit().getName());
|
statement.setString(2, data.getKit().getName());
|
||||||
statement.setBoolean(3, data.showHologram());
|
statement.setBoolean(3, data.showHologram());
|
||||||
@ -51,7 +50,6 @@ public class DataManager {
|
|||||||
statement.setInt(8, data.getX());
|
statement.setInt(8, data.getX());
|
||||||
statement.setInt(9, data.getY());
|
statement.setInt(9, data.getY());
|
||||||
statement.setInt(10, data.getZ());
|
statement.setInt(10, data.getZ());
|
||||||
statement.executeUpdate();
|
|
||||||
statement.addBatch();
|
statement.addBatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +59,7 @@ public class DataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateBlockData(KitBlockData blockData) {
|
public void updateBlockData(KitBlockData blockData) {
|
||||||
|
if (blockData.getWorld() == null) return;
|
||||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
String updateData = "UPDATE " + this.getTablePrefix() + "blockdata SET type = ?, kit = ?, holograms = ?, " +
|
String updateData = "UPDATE " + this.getTablePrefix() + "blockdata SET type = ?, kit = ?, holograms = ?, " +
|
||||||
"displayItems = ?, particles = ?, itemOverride = ? " +
|
"displayItems = ?, particles = ?, itemOverride = ? " +
|
||||||
@ -82,6 +81,7 @@ public class DataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createBlockData(KitBlockData blockData) {
|
public void createBlockData(KitBlockData blockData) {
|
||||||
|
if (blockData.getWorld() == null) return;
|
||||||
this.async(() -> this.databaseConnector.connect(connection -> {
|
this.async(() -> this.databaseConnector.connect(connection -> {
|
||||||
String createData ="INSERT INTO " + this.getTablePrefix() + "blockdata (" +
|
String createData ="INSERT INTO " + this.getTablePrefix() + "blockdata (" +
|
||||||
"type, kit, holograms, displayItems, particles, itemOverride, world, x, y, z)" +
|
"type, kit, holograms, displayItems, particles, itemOverride, world, x, y, z)" +
|
||||||
@ -123,13 +123,14 @@ public class DataManager {
|
|||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
ResultSet result = statement.executeQuery(selectData);
|
ResultSet result = statement.executeQuery(selectData);
|
||||||
while (result.next()) {
|
while (result.next()) {
|
||||||
|
World world = Bukkit.getWorld(result.getString("world"));
|
||||||
|
if (world == null) continue;
|
||||||
KitType type = KitType.valueOf(result.getString("type"));
|
KitType type = KitType.valueOf(result.getString("type"));
|
||||||
String kit = result.getString("kit");
|
String kit = result.getString("kit");
|
||||||
boolean holograms = result.getBoolean("holograms");
|
boolean holograms = result.getBoolean("holograms");
|
||||||
boolean displayItems = result.getBoolean("displayItems");
|
boolean displayItems = result.getBoolean("displayItems");
|
||||||
boolean particles = result.getBoolean("particles");
|
boolean particles = result.getBoolean("particles");
|
||||||
boolean itemOverride = result.getBoolean("itemOverride");
|
boolean itemOverride = result.getBoolean("itemOverride");
|
||||||
World world = Bukkit.getWorld(result.getString("world"));
|
|
||||||
int x = result.getInt("x");
|
int x = result.getInt("x");
|
||||||
int y = result.getInt("y");
|
int y = result.getInt("y");
|
||||||
int z = result.getInt("z");
|
int z = result.getInt("z");
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.songoda.ultimatekits.database;
|
package com.songoda.ultimatekits.database;
|
||||||
|
|
||||||
import com.songoda.ultimatekits.database.migrations._1_InitialMigration;
|
import com.songoda.ultimatekits.database.migrations._1_InitialMigration;
|
||||||
|
import com.songoda.ultimatekits.database.migrations._2_DuplicateMigration;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -11,16 +12,17 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class DataMigrationManager {
|
public class DataMigrationManager {
|
||||||
|
|
||||||
private List<DataMigration> migrations;
|
private final List<DataMigration> migrations;
|
||||||
private DatabaseConnector databaseConnector;
|
private final DatabaseConnector databaseConnector;
|
||||||
private DataManager dataManager;
|
private final DataManager dataManager;
|
||||||
|
|
||||||
public DataMigrationManager(DatabaseConnector databaseConnector, DataManager dataManager) {
|
public DataMigrationManager(DatabaseConnector databaseConnector, DataManager dataManager) {
|
||||||
this.databaseConnector = databaseConnector;
|
this.databaseConnector = databaseConnector;
|
||||||
this.dataManager = dataManager;
|
this.dataManager = dataManager;
|
||||||
|
|
||||||
this.migrations = Arrays.asList(
|
this.migrations = Arrays.asList(
|
||||||
new _1_InitialMigration()
|
new _1_InitialMigration(),
|
||||||
|
new _2_DuplicateMigration(databaseConnector instanceof SQLiteConnector)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.songoda.ultimatekits.database.migrations;
|
package com.songoda.ultimatekits.database.migrations;
|
||||||
|
|
||||||
import com.songoda.ultimatekits.UltimateKits;
|
|
||||||
import com.songoda.ultimatekits.database.DataMigration;
|
import com.songoda.ultimatekits.database.DataMigration;
|
||||||
import com.songoda.ultimatekits.database.MySQLConnector;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@ -16,8 +14,8 @@ public class _1_InitialMigration extends DataMigration {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void migrate(Connection connection, String tablePrefix) throws SQLException {
|
public void migrate(Connection connection, String tablePrefix) throws SQLException {
|
||||||
String autoIncrement = UltimateKits.getInstance().getDatabaseConnector() instanceof
|
//String autoIncrement = UltimateKits.getInstance().getDatabaseConnector() instanceof
|
||||||
MySQLConnector ? " AUTO_INCREMENT" : "";
|
// MySQLConnector ? " AUTO_INCREMENT" : "";
|
||||||
|
|
||||||
// Create plugin settings table
|
// Create plugin settings table
|
||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
@ -28,10 +26,10 @@ public class _1_InitialMigration extends DataMigration {
|
|||||||
"displayItems BOOLEAN NOT NULL," +
|
"displayItems BOOLEAN NOT NULL," +
|
||||||
"particles BOOLEAN NOT NULL," +
|
"particles BOOLEAN NOT NULL," +
|
||||||
"itemOverride BOOLEAN NOT NULL," +
|
"itemOverride BOOLEAN NOT NULL," +
|
||||||
"world TEXT NOT NULL," +
|
"world TEXT NOT NULL," + // PK
|
||||||
"x INTEGER NOT NULL," +
|
"x INTEGER NOT NULL," + // PK
|
||||||
"y INTEGER NOT NULL," +
|
"y INTEGER NOT NULL," + // PK
|
||||||
"z INTEGER NOT NULL " +
|
"z INTEGER NOT NULL " + // PK
|
||||||
")");
|
")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,99 @@
|
|||||||
|
package com.songoda.ultimatekits.database.migrations;
|
||||||
|
|
||||||
|
import com.songoda.ultimatekits.database.DataMigration;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class _2_DuplicateMigration extends DataMigration {
|
||||||
|
|
||||||
|
final boolean sqlite;
|
||||||
|
public _2_DuplicateMigration(boolean sqlite) {
|
||||||
|
super(2);
|
||||||
|
this.sqlite = sqlite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate(Connection connection, String tablePrefix) throws SQLException {
|
||||||
|
// Fix duplicate data caused by old sqlite data duplication bug
|
||||||
|
if(sqlite) {
|
||||||
|
HashMap<String, TempKitData> data = new HashMap();
|
||||||
|
// grab a copy of the unique data values
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
ResultSet allData = statement.executeQuery("SELECT * FROM " + tablePrefix + "blockdata");
|
||||||
|
while (allData.next()) {
|
||||||
|
String world = allData.getString("world");
|
||||||
|
int x = allData.getInt("x");
|
||||||
|
int y = allData.getInt("y");
|
||||||
|
int z = allData.getInt("z");
|
||||||
|
String key = world + ";" + x + ";" + y + ";" + z + ";";
|
||||||
|
if(!data.containsKey(key)) {
|
||||||
|
data.put(key, new TempKitData(
|
||||||
|
allData.getString("type"),
|
||||||
|
allData.getString("kit"),
|
||||||
|
allData.getBoolean("holograms"),
|
||||||
|
allData.getBoolean("displayItems"),
|
||||||
|
allData.getBoolean("particles"),
|
||||||
|
allData.getBoolean("itemOverride"),
|
||||||
|
world, x, y, z
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
allData.close();
|
||||||
|
}
|
||||||
|
if(data.isEmpty()) return;
|
||||||
|
connection.setAutoCommit(false);
|
||||||
|
// first delete old data
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.executeUpdate("DELETE FROM " + tablePrefix + "blockdata");
|
||||||
|
}
|
||||||
|
// then re-add valid unique data
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "blockdata (" +
|
||||||
|
"type, kit, holograms, displayItems, particles, itemOverride, world, x, y, z)" +
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
|
||||||
|
for(TempKitData blockData : data.values()) {
|
||||||
|
statement.setString(1, blockData.type);
|
||||||
|
statement.setString(2, blockData.kit);
|
||||||
|
statement.setBoolean(3, blockData.holograms);
|
||||||
|
statement.setBoolean(4, blockData.displayItems);
|
||||||
|
statement.setBoolean(5, blockData.particles);
|
||||||
|
statement.setBoolean(6, blockData.itemOverride);
|
||||||
|
statement.setString(7, blockData.world);
|
||||||
|
statement.setInt(8, blockData.x);
|
||||||
|
statement.setInt(9, blockData.y);
|
||||||
|
statement.setInt(10, blockData.z);
|
||||||
|
statement.addBatch();
|
||||||
|
}
|
||||||
|
statement.executeBatch();
|
||||||
|
}
|
||||||
|
connection.commit();
|
||||||
|
connection.setAutoCommit(true);
|
||||||
|
// free up disk space (sqlite command)
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.executeUpdate("VACUUM");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TempKitData {
|
||||||
|
final String type, kit, world;
|
||||||
|
final int x, y, z;
|
||||||
|
final boolean holograms, displayItems, particles, itemOverride;
|
||||||
|
|
||||||
|
public TempKitData(String type, String kit, boolean holograms, boolean displayItems, boolean particles, boolean itemOverride, String world, int x, int y, int z) {
|
||||||
|
this.type = type;
|
||||||
|
this.kit = kit;
|
||||||
|
this.world = world;
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.holograms = holograms;
|
||||||
|
this.displayItems = displayItems;
|
||||||
|
this.particles = particles;
|
||||||
|
this.itemOverride = itemOverride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,12 +20,10 @@ public class KitBlockData {
|
|||||||
this.items = items;
|
this.items = items;
|
||||||
this.itemOverride = itemOverride;
|
this.itemOverride = itemOverride;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
UltimateKits.getInstance().getDataManager().createBlockData(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public KitBlockData(Kit kit, Location location) {
|
public KitBlockData(Kit kit, Location location) {
|
||||||
this(kit, location, KitType.PREVIEW, false, false, false, false);
|
this(kit, location, KitType.PREVIEW, false, false, false, false);
|
||||||
UltimateKits.getInstance().getDataManager().createBlockData(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.songoda.ultimatekits.kit;
|
package com.songoda.ultimatekits.kit;
|
||||||
|
|
||||||
import com.songoda.ultimatekits.UltimateKits;
|
import com.songoda.ultimatekits.UltimateKits;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -9,7 +8,7 @@ import java.util.*;
|
|||||||
public final class KitManager {
|
public final class KitManager {
|
||||||
|
|
||||||
private Map<Location, KitBlockData> kitsAtLocations = new HashMap<>();
|
private Map<Location, KitBlockData> kitsAtLocations = new HashMap<>();
|
||||||
private List<Kit> registeredKits = new LinkedList<>();
|
private final List<Kit> registeredKits = new LinkedList<>();
|
||||||
|
|
||||||
public boolean addKit(Kit kit) {
|
public boolean addKit(Kit kit) {
|
||||||
if (kit == null) return false;
|
if (kit == null) return false;
|
||||||
@ -30,15 +29,18 @@ public final class KitManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addKitToLocation(Kit kit, Location location) {
|
public KitBlockData addKitToLocation(Kit kit, Location location) {
|
||||||
KitBlockData data = new KitBlockData(kit, location);
|
KitBlockData data = new KitBlockData(kit, location);
|
||||||
kitsAtLocations.put(roundLocation(location), data);
|
kitsAtLocations.put(roundLocation(location), data);
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addKitToLocation(Kit kit, Location location, KitType type, boolean hologram, boolean particles, boolean items, boolean itemOverride) {
|
public KitBlockData addKitToLocation(Kit kit, Location location, KitType type, boolean hologram, boolean particles, boolean items, boolean itemOverride) {
|
||||||
KitBlockData kitBlockData = kitsAtLocations.put(roundLocation(location), new KitBlockData(kit, location, type, hologram, particles, items, itemOverride));
|
KitBlockData data = new KitBlockData(kit, location, type, hologram, particles, items, itemOverride);
|
||||||
|
kitsAtLocations.put(roundLocation(location), data);
|
||||||
if (UltimateKits.getInstance().getHologram() != null)
|
if (UltimateKits.getInstance().getHologram() != null)
|
||||||
UltimateKits.getInstance().getHologram().update(kitBlockData);
|
UltimateKits.getInstance().getHologram().update(data);
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Kit removeKitFromLocation(Location location) {
|
public Kit removeKitFromLocation(Location location) {
|
||||||
|
Loading…
Reference in New Issue
Block a user