2017-10-13 18:35:11 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.bridge;
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2017-10-13 18:35:11 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.FreedomService;
|
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
2018-02-22 12:55:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2017-10-13 18:35:11 +00:00
|
|
|
import net.coreprotect.CoreProtect;
|
|
|
|
import net.coreprotect.CoreProtectAPI;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.World;
|
2018-02-22 12:55:04 +00:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2017-10-13 18:35:11 +00:00
|
|
|
import org.bukkit.plugin.PluginManager;
|
2018-02-22 12:55:04 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public class CoreProtectBridge extends FreedomService
|
|
|
|
{
|
|
|
|
private CoreProtectAPI coreProtectAPI = null;
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
private final List<String> tables = Arrays.asList("co_sign", "co_session", "co_container", "co_block");
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
private BukkitTask wiper;
|
|
|
|
|
|
|
|
public CoreProtectBridge(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
super(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart()
|
|
|
|
{
|
|
|
|
if (ConfigEntry.COREPROTECT_AUTO_WIPING_ENABLED.getBoolean() && getCoreProtect() != null)
|
|
|
|
{
|
|
|
|
createAutomaticWiper();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoreProtect getCoreProtect()
|
|
|
|
{
|
|
|
|
CoreProtect coreProtect = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final Plugin coreProtectPlugin = Bukkit.getServer().getPluginManager().getPlugin("CoreProtect");
|
|
|
|
|
|
|
|
if (coreProtectPlugin != null && coreProtectPlugin instanceof CoreProtect)
|
|
|
|
{
|
2018-07-31 07:01:29 +00:00
|
|
|
coreProtect = (CoreProtect)coreProtectPlugin;
|
2017-10-13 18:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
return coreProtect;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CoreProtectAPI getCoreProtectAPI()
|
|
|
|
{
|
|
|
|
if (coreProtectAPI == null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final CoreProtect coreProtect = getCoreProtect();
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
coreProtectAPI = coreProtect.getAPI();
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
// Check if the plugin or api is not enabled, if so, return null
|
|
|
|
if (!coreProtect.isEnabled() || !coreProtectAPI.isEnabled())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return coreProtectAPI;
|
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public boolean isEnabled()
|
|
|
|
{
|
|
|
|
final CoreProtect coreProtect = getCoreProtect();
|
|
|
|
|
|
|
|
return coreProtect != null && coreProtect.isEnabled();
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:11:48 +00:00
|
|
|
// Rollback the specified player's edits that were in the last 24 hours.
|
2017-10-13 18:35:11 +00:00
|
|
|
public void rollback(final String name)
|
|
|
|
{
|
|
|
|
final CoreProtectAPI coreProtect = getCoreProtectAPI();
|
|
|
|
|
|
|
|
if (!isEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
coreProtect.performRollback(86400, Arrays.asList(name), null, null, null, null, 0, null);
|
|
|
|
}
|
|
|
|
}.runTaskAsynchronously(plugin);
|
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2018-07-28 07:11:48 +00:00
|
|
|
// Reverts a rollback for the specified player's edits that were in the last 24 hours.
|
2018-07-26 22:55:55 +00:00
|
|
|
public void restore(final String name)
|
2017-10-13 18:35:11 +00:00
|
|
|
{
|
|
|
|
final CoreProtectAPI coreProtect = getCoreProtectAPI();
|
|
|
|
|
|
|
|
if (!isEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
coreProtect.performRestore(86400, Arrays.asList(name), null, null, null, null, 0, null);
|
|
|
|
}
|
|
|
|
}.runTaskAsynchronously(plugin);
|
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public File getDatabase()
|
|
|
|
{
|
|
|
|
if (!isEnabled())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:44:00 +00:00
|
|
|
return (new File(getCoreProtect().getDataFolder(), "database.db"));
|
2017-10-13 18:35:11 +00:00
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
private void createAutomaticWiper()
|
|
|
|
{
|
|
|
|
final long interval = 10 * 20L;
|
|
|
|
final File databaseFile = getDatabase();
|
|
|
|
|
|
|
|
wiper = new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
final CoreProtect coreProtect = getCoreProtect();
|
|
|
|
if (getDBSize() > ConfigEntry.COREPROTECT_FILE_LIMIT.getInteger())
|
|
|
|
{
|
|
|
|
FLog.info("The CoreProtect log file has grown too big for the server to cope, the data is being wiped!");
|
|
|
|
FUtil.bcastMsg("The CoreProtect log file has grown too big for the server to cope, the data is being wiped!", ChatColor.RED);
|
|
|
|
PluginManager pluginManager = server.getPluginManager();
|
|
|
|
pluginManager.disablePlugin(coreProtect);
|
2018-07-25 02:44:00 +00:00
|
|
|
for (World world : Bukkit.getWorlds())
|
2017-10-13 18:35:11 +00:00
|
|
|
{
|
2018-07-25 02:44:00 +00:00
|
|
|
if (!world.equals(plugin.wm.adminworld.getWorld()))
|
2017-10-13 18:35:11 +00:00
|
|
|
{
|
|
|
|
clearDatabase(world);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//check if still too big, if so delete all data
|
2018-07-25 02:44:00 +00:00
|
|
|
if (getDBSize() > ConfigEntry.COREPROTECT_FILE_LIMIT.getInteger())
|
2017-10-13 18:35:11 +00:00
|
|
|
{
|
|
|
|
FUtil.deleteFolder(databaseFile);
|
|
|
|
}
|
|
|
|
pluginManager.enablePlugin(coreProtect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskTimer(plugin, interval, interval);
|
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
public double getDBSize()
|
|
|
|
{
|
|
|
|
double bytes = getDatabase().length();
|
|
|
|
double kilobytes = (bytes / 1024);
|
|
|
|
double megabytes = (kilobytes / 1024);
|
|
|
|
return (megabytes / 1024);
|
|
|
|
}
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
// Wipes DB for the specified world
|
|
|
|
public void clearDatabase(World world)
|
|
|
|
{
|
|
|
|
clearDatabase(world, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipes DB for the specified world
|
|
|
|
public void clearDatabase(World world, Boolean shutdown)
|
|
|
|
{
|
|
|
|
final CoreProtect coreProtect = getCoreProtect();
|
|
|
|
|
|
|
|
if (coreProtect == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:11:48 +00:00
|
|
|
/* As CoreProtect doesn't have an API method for deleting all of the data for a specific world
|
|
|
|
we have to do this manually via SQL */
|
2017-10-13 18:35:11 +00:00
|
|
|
File databaseFile = getDatabase();
|
|
|
|
Connection connection = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile);
|
|
|
|
final Statement statement = connection.createStatement();
|
|
|
|
statement.setQueryTimeout(30);
|
2018-07-25 02:44:00 +00:00
|
|
|
|
2017-10-13 18:35:11 +00:00
|
|
|
// Obtain world ID from CoreProtect database
|
|
|
|
ResultSet resultSet = statement.executeQuery("SELECT id FROM co_world WHERE world = '" + world.getName() + "'");
|
|
|
|
String worldID = null;
|
|
|
|
while (resultSet.next())
|
|
|
|
{
|
|
|
|
worldID = String.valueOf(resultSet.getInt("id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the world ID is not null
|
|
|
|
if (worldID == null)
|
|
|
|
{
|
|
|
|
FLog.warning("Failed to obtain the world ID for the " + world.getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through each table and delete their data if the world ID matches
|
|
|
|
for (String table : tables)
|
|
|
|
{
|
|
|
|
statement.executeUpdate("DELETE FROM " + table + " WHERE wid = " + worldID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This shrinks down the file size
|
|
|
|
statement.executeUpdate("VACUUM");
|
|
|
|
|
|
|
|
connection.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
FLog.warning("Failed to delete the CoreProtect data for the " + world.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This exits for flatlands wipes
|
|
|
|
if (shutdown)
|
|
|
|
{
|
2018-07-25 02:44:00 +00:00
|
|
|
if (plugin.amp.enabled)
|
2018-02-22 12:55:04 +00:00
|
|
|
{
|
|
|
|
plugin.amp.restartServer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
server.shutdown();
|
|
|
|
}
|
2017-10-13 18:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|