From f2ba3018808e536b52a115f2c14b0dd2bfba6565 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Thu, 19 Mar 2020 15:56:29 +0200 Subject: [PATCH] Made CapabilityService more resilient Affects issues: - Fixed #1356 --- .../plan/capability/CapabilityService.java | 23 ++++++---------- .../plan/capability/CapabilitySvc.java | 27 +------------------ 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/Plan/api/src/main/java/com/djrapitops/plan/capability/CapabilityService.java b/Plan/api/src/main/java/com/djrapitops/plan/capability/CapabilityService.java index 0f41b0c3b..e75b04be9 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/capability/CapabilityService.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/capability/CapabilityService.java @@ -16,7 +16,8 @@ */ package com.djrapitops.plan.capability; -import java.util.Optional; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; /** @@ -39,8 +40,7 @@ public interface CapabilityService { * @throws IllegalStateException If Plan is installed, but not enabled. */ static CapabilityService getInstance() { - return Optional.ofNullable(Holder.service) - .orElseThrow(() -> new IllegalStateException("CapabilityService has not been initialised yet.")); + return new CapabilityService() {}; } /** @@ -48,7 +48,9 @@ public interface CapabilityService { * * @param isEnabledListener The boolean given to the method tells if Plan has enabled successfully. */ - void registerEnableListener(Consumer isEnabledListener); + default void registerEnableListener(Consumer isEnabledListener) { + ListHolder.ENABLE_LISTENERS.add(isEnabledListener); + } /** * Check if the API on the current version provides a capability. @@ -61,16 +63,7 @@ public interface CapabilityService { return Capability.getByName(capabilityName).isPresent(); } - class Holder { - static CapabilityService service; - - private Holder() { - /* Static variable holder */ - } - - static void set(CapabilityService service) { - Holder.service = service; - } + class ListHolder { + static List> ENABLE_LISTENERS = new ArrayList<>(); } - } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/capability/CapabilitySvc.java b/Plan/common/src/main/java/com/djrapitops/plan/capability/CapabilitySvc.java index 193a74453..946ec142c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/capability/CapabilitySvc.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/capability/CapabilitySvc.java @@ -16,8 +16,6 @@ */ package com.djrapitops.plan.capability; -import java.util.ArrayList; -import java.util.List; import java.util.function.Consumer; /** @@ -29,32 +27,9 @@ import java.util.function.Consumer; */ public class CapabilitySvc implements CapabilityService { - private final List> enableListeners; - - private CapabilitySvc() { - Holder.set(this); - enableListeners = new ArrayList<>(); - } - - private static CapabilitySvc get() { - if (Holder.service == null) { - return new CapabilitySvc(); - } - return (CapabilitySvc) Holder.service; - } - - public static void initialize() { - get(); - } - public static void notifyAboutEnable(boolean isEnabled) { - for (Consumer enableListener : get().enableListeners) { + for (Consumer enableListener : CapabilityService.ListHolder.ENABLE_LISTENERS) { enableListener.accept(isEnabled); } } - - @Override - public void registerEnableListener(Consumer enableListener) { - enableListeners.add(enableListener); - } } \ No newline at end of file