Created
September 9, 2022 01:03
-
-
Save ilsubyeega/74bf167ae6fa22059c198c937f9715c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package win.roto.skriptmaintenance; | |
| import ch.njol.skript.variables.SerializedVariable; | |
| import ch.njol.skript.variables.Variables; | |
| import ch.njol.skript.variables.VariablesStorage; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.Method; | |
| import java.util.List; | |
| import java.util.concurrent.BlockingQueue; | |
| /** | |
| * License: GPL v3 | |
| * | |
| * Save Skript Variables. | |
| * Use this when: | |
| * - the `saveThread` is dead and cannot save variables into db. | |
| * - `saveThread` is dead but the connection from storages is alive. | |
| * | |
| * After using, PLEASE consider updating your skript plugin into the latest if available. | |
| */ | |
| public final class SkriptMaintenance extends JavaPlugin { | |
| @Override | |
| public void onEnable() { | |
| try { | |
| Field storagesField = Variables.class.getDeclaredField("storages"); | |
| storagesField.setAccessible(true); | |
| Field saveQueueField = Variables.class.getDeclaredField("saveQueue"); | |
| saveQueueField.setAccessible(true); | |
| List<VariablesStorage> storages = (List<VariablesStorage>)storagesField.get(null); | |
| BlockingQueue<SerializedVariable> saveQueue = (BlockingQueue<SerializedVariable>)saveQueueField.get(null); | |
| Method variableStorageAcceptMethod = VariablesStorage.class.getDeclaredMethod("accept", String.class); | |
| Method variableStorageSaveMethod = VariablesStorage.class.getDeclaredMethod("save", SerializedVariable.class); | |
| variableStorageAcceptMethod.setAccessible(true); | |
| variableStorageSaveMethod.setAccessible(true); | |
| Bukkit.getScheduler().runTaskAsynchronously(this, () -> { | |
| try { | |
| getLogger().info("[VARIABLE-FIX] QUEUE SIZE: " + saveQueue.size()); | |
| while (true) { | |
| SerializedVariable v = saveQueue.take(); | |
| for (VariablesStorage s : storages) { | |
| if ((boolean)variableStorageAcceptMethod.invoke(s, v.name)) { | |
| variableStorageSaveMethod.invoke(s, v); | |
| getLogger().info("[VARIABLE-FIX] LOGGED: " + v.name); | |
| break; | |
| } | |
| } | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| }); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment