mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 09:00:28 +08:00
parent
648cdb1a0c
commit
f2ba301880
@ -16,7 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.capability;
|
package com.djrapitops.plan.capability;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,8 +40,7 @@ public interface CapabilityService {
|
|||||||
* @throws IllegalStateException If Plan is installed, but not enabled.
|
* @throws IllegalStateException If Plan is installed, but not enabled.
|
||||||
*/
|
*/
|
||||||
static CapabilityService getInstance() {
|
static CapabilityService getInstance() {
|
||||||
return Optional.ofNullable(Holder.service)
|
return new CapabilityService() {};
|
||||||
.orElseThrow(() -> new IllegalStateException("CapabilityService has not been initialised yet."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,7 +48,9 @@ public interface CapabilityService {
|
|||||||
*
|
*
|
||||||
* @param isEnabledListener The boolean given to the method tells if Plan has enabled successfully.
|
* @param isEnabledListener The boolean given to the method tells if Plan has enabled successfully.
|
||||||
*/
|
*/
|
||||||
void registerEnableListener(Consumer<Boolean> isEnabledListener);
|
default void registerEnableListener(Consumer<Boolean> isEnabledListener) {
|
||||||
|
ListHolder.ENABLE_LISTENERS.add(isEnabledListener);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the API on the current version provides a capability.
|
* Check if the API on the current version provides a capability.
|
||||||
@ -61,16 +63,7 @@ public interface CapabilityService {
|
|||||||
return Capability.getByName(capabilityName).isPresent();
|
return Capability.getByName(capabilityName).isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
class Holder {
|
class ListHolder {
|
||||||
static CapabilityService service;
|
static List<Consumer<Boolean>> ENABLE_LISTENERS = new ArrayList<>();
|
||||||
|
|
||||||
private Holder() {
|
|
||||||
/* Static variable holder */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void set(CapabilityService service) {
|
|
||||||
Holder.service = service;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.capability;
|
package com.djrapitops.plan.capability;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,32 +27,9 @@ import java.util.function.Consumer;
|
|||||||
*/
|
*/
|
||||||
public class CapabilitySvc implements CapabilityService {
|
public class CapabilitySvc implements CapabilityService {
|
||||||
|
|
||||||
private final List<Consumer<Boolean>> 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) {
|
public static void notifyAboutEnable(boolean isEnabled) {
|
||||||
for (Consumer<Boolean> enableListener : get().enableListeners) {
|
for (Consumer<Boolean> enableListener : CapabilityService.ListHolder.ENABLE_LISTENERS) {
|
||||||
enableListener.accept(isEnabled);
|
enableListener.accept(isEnabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void registerEnableListener(Consumer<Boolean> enableListener) {
|
|
||||||
enableListeners.add(enableListener);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user