Disable Litebans Extension on game servers if installed on proxy

Affects issues:
- Close #2178
This commit is contained in:
Risto Lahtela 2022-01-30 20:28:17 +02:00
parent 4bad953fba
commit 036ec23c08
4 changed files with 151 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import com.djrapitops.plan.gathering.timed.ServerTPSCounter;
import com.djrapitops.plan.gathering.timed.SystemUsageBuffer;
import com.djrapitops.plan.settings.upkeep.ConfigStoreTask;
import com.djrapitops.plan.storage.upkeep.DBCleanTask;
import com.djrapitops.plan.storage.upkeep.ExtensionDisableOnGameServerTask;
import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask;
import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask;
import dagger.Binds;
@ -98,4 +99,8 @@ public interface BukkitTaskModule {
@Binds
@IntoSet
TaskSystem.Task bindActiveCookieStoreExpiryTask(ActiveCookieExpiryCleanupTask activeCookieExpiryCleanupTask);
@Binds
@IntoSet
TaskSystem.Task bindExtensionDisableOnGameServerTask(ExtensionDisableOnGameServerTask extensionDisableOnGameServerTask);
}

View File

@ -0,0 +1,66 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.storage.queries;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.storage.database.queries.HasMoreThanZeroQueryStatement;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionPluginTable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
public class HasExtensionDataForPluginQuery extends HasMoreThanZeroQueryStatement {
private final String pluginName;
private final ServerUUID serverUUID;
public HasExtensionDataForPluginQuery(String pluginName, ServerUUID serverUUID) {
super(sql());
this.pluginName = pluginName;
this.serverUUID = serverUUID;
}
private static String sql() {
return SELECT + "COUNT(1) as c" +
FROM + ExtensionPluginTable.TABLE_NAME +
WHERE + ExtensionPluginTable.PLUGIN_NAME + "=?" +
AND + ExtensionPluginTable.SERVER_UUID + "=?";
}
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, pluginName);
statement.setString(2, serverUUID.toString());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
HasExtensionDataForPluginQuery that = (HasExtensionDataForPluginQuery) o;
return Objects.equals(pluginName, that.pluginName) && Objects.equals(serverUUID, that.serverUUID);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), pluginName, serverUUID);
}
}

View File

@ -58,6 +58,10 @@ public class ExtensionSettings {
return section.getBoolean(pluginName + ".Enabled");
}
public void setEnabled(String pluginName, boolean value) {
getPluginsSection().set(pluginName + ".Enabled", value);
}
public Set<String> getDisabled() {
ConfigNode section = getPluginsSection();

View File

@ -0,0 +1,76 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.storage.upkeep;
import com.djrapitops.plan.TaskSystem;
import com.djrapitops.plan.extension.implementation.storage.queries.HasExtensionDataForPluginQuery;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.config.ExtensionSettings;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import net.playeranalytics.plugin.scheduling.RunnableFactory;
import net.playeranalytics.plugin.server.PluginLogger;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class ExtensionDisableOnGameServerTask extends TaskSystem.Task {
private final PlanConfig config;
private final DBSystem dbSystem;
private final PluginLogger logger;
@Inject
public ExtensionDisableOnGameServerTask(PlanConfig config, DBSystem dbSystem, PluginLogger logger) {
this.config = config;
this.dbSystem = dbSystem;
this.logger = logger;
}
@Override
public void register(RunnableFactory runnableFactory) {
runnableFactory.create(this).runTaskAsynchronously();
}
@Override
public void run() {
String pluginName = "Litebans";
checkAndDisableProxyExtensions(pluginName);
}
private void checkAndDisableProxyExtensions(String pluginName) {
Database db = dbSystem.getDatabase();
db.query(ServerQueries.fetchProxyServerInformation())
.map(Server::getUuid)
.ifPresent(proxyUUID -> checkAndDisableProxyExtension(proxyUUID, pluginName));
}
private void checkAndDisableProxyExtension(ServerUUID proxyUUID, String pluginName) {
Database db = dbSystem.getDatabase();
ExtensionSettings extensionSettings = config.getExtensionSettings();
boolean isInstalledOnProxy = db.query(new HasExtensionDataForPluginQuery(pluginName, proxyUUID));
if (isInstalledOnProxy && extensionSettings.isEnabled(pluginName)) {
extensionSettings.setEnabled(pluginName, false);
logger.info("Set " + pluginName + " Extension as disabled in config since it is already enabled on the proxy server. This is to avoid duplicate data.");
}
}
}