') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Fix Discord Link RoleSyncManager thread leak by JRoy · Pull Request #6512 · EssentialsX/Essentials · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,9 @@ public void onEnable() {

@Override
public void onDisable() {
if (roleSyncManager != null) {
roleSyncManager.shutdown();
}
if (accounts != null) {
accounts.shutdown();
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.Collections;
Expand All@@ -19,6 +20,7 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import static com.earth2me.essentials.I18n.tlLiteral;
Expand All@@ -28,13 +30,14 @@ public class RoleSyncManager implements Listener {
private final Map<String, InteractionRole> groupToRoleMap = new HashMap<>();
private final Map<String, String> roleIdToGroupMap = new HashMap<>();
private final Semaphore syncSemaphore = new Semaphore(5);
private BukkitTask syncTask;
private int syncCursor = 0;

public RoleSyncManager(final EssentialsDiscordLink ess) {
this.ess = ess;
Bukkit.getPluginManager().registerEvents(this, ess);
onReload();
this.ess.getEss().runTaskTimerAsynchronously(() -> {
this.syncTask = this.ess.getEss().runTaskTimerAsynchronously(() -> {
if (groupToRoleMap.isEmpty() && roleIdToGroupMap.isEmpty()) {
return;
}
Expand DownExpand Up@@ -67,6 +70,12 @@ public RoleSyncManager(final EssentialsDiscordLink ess) {
}, 0, ess.getSettings().getRoleSyncResyncDelay() * 1200L);
}

public void shutdown() {
if (syncTask != null) {
syncTask.cancel();
}
}

public void sync(final UUID uuid, final String discordId) {
final Map<String, InteractionRole> groupToRoleMapCopy = new HashMap<>(groupToRoleMap);
final Map<String, String> roleIdToGroupMapCopy = new HashMap<>(roleIdToGroupMap);
Expand All@@ -81,7 +90,13 @@ public void sync(final Player player, final String discordId, final Map<String,
final List<String> groups = primaryOnly ?
Collections.singletonList(ess.getEss().getPermissionsHandler().getGroup(player)) : ess.getEss().getPermissionsHandler().getGroups(player);
ess.getEss().runTaskAsynchronously(() -> {
syncSemaphore.acquireUninterruptibly();
try {
if (!syncSemaphore.tryAcquire(5, TimeUnit.SECONDS)) {
return;
}
} catch (final InterruptedException e) {
return;
}
ess.getApi().getMemberById(discordId).thenCompose(member -> {
if (member == null) {
if (ess.getSettings().isUnlinkOnLeave()) {
Expand DownExpand Up@@ -151,7 +166,13 @@ public void unSync(final UUID uuid, final String discordId) {
}

ess.getEss().runTaskAsynchronously(() -> {
syncSemaphore.acquireUninterruptibly();
try {
if (!syncSemaphore.tryAcquire(5, TimeUnit.SECONDS)) {
return;
}
} catch (final InterruptedException e) {
return;
}
ess.getApi().getMemberById(discordId).thenCompose(member -> {
// Check if the member is no longer in the guild (null), they don't have any roles anyway.
Comment thread
JRoy marked this conversation as resolved.
if (member == null) {
Expand Down
Loading