package info.ata4.minecraft.minema.client.modules;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.client.Minecraft;

public abstract class CaptureModule {

	protected static final Minecraft MC = Minecraft.getMinecraft();
	protected static final Logger L = LogManager.getLogger();
	private boolean enabled;

	public String getName() {
		return getClass().getSimpleName();
	}

	public synchronized final boolean isEnabled() {
		return enabled;
	}

	/**
	 * Enables this module if the current configuration says so
	 * 
	 * @throws Exception
	 */
	public synchronized final void enable() throws Exception {
		if (enabled) {
			return;
		}

		if (checkEnable()) {

			enabled = true;
			L.info("Enabling " + getName());
			try {
				doEnable();
			} catch (Exception e) {
				throw new Exception("Cannot enable module", e);
			}

		}
	}

	/**
	 * Disables this module if it was active. Even though it might throw an
	 * exception this module must recover into a state that makes it reusable for
	 * enabling again as if it was freshly instantiated.
	 * 
	 * @throws Exception
	 */
	public synchronized final void disable() throws Exception {
		if (!enabled) {
			return;
		}

		L.info("Disabling " + getName());
		try {
			doDisable();
		} catch (Exception e) {
			throw new Exception("Cannot disable module", e);
		}

		enabled = false;
	}

	protected abstract void doEnable() throws Exception;

	/**
	 * @return True if this module should be enabled given the current configuration
	 */
	protected abstract boolean checkEnable();

	protected abstract void doDisable() throws Exception;
}

@@ -601,37 +601,24 @@
  */%0A%09public 
-synchronized 
 final void e
@@ -640,24 +640,49 @@
 Exception %7B%0A
+%09%09synchronized (this) %7B%0A%09
 %09%09if (enable
@@ -675,38 +675,37 @@
 %0A%09%09%09if (enabled)
- %7B
 %0A%09%09%09
+%09
 return;%0A%09%09%7D%0A%0A%09%09i
@@ -701,18 +701,15 @@
 n;%0A%09
-%09%7D%0A%0A
 %09%09if (
+!
 chec
@@ -718,19 +718,28 @@
 nable())
- %7B%0A
+%0A%09%09%09%09return;
 %0A%09%09%09enab
@@ -751,16 +751,20 @@
  true;%0A%09
+%09%7D%0A%0A
 %09%09L.info
@@ -790,17 +790,16 @@
 ame());%0A
-%09
 %09%09try %7B%0A
@@ -801,17 +801,16 @@
 ry %7B%0A%09%09%09
-%09
 doEnable
@@ -813,17 +813,16 @@
 able();%0A
-%09
 %09%09%7D catc
@@ -839,17 +839,16 @@
 on e) %7B%0A
-%09
 %09%09%09throw
@@ -894,14 +894,8 @@
 e);%0A
-%09%09%09%7D%0A%0A
 %09%09%7D%0A
@@ -1145,37 +1145,24 @@
  */%0A%09public 
-synchronized 
 final void d
@@ -1189,16 +1189,41 @@
 ption %7B%0A
+%09%09synchronized (this) %7B%0A%09
 %09%09if (!e
@@ -1229,19 +1229,18 @@
 enabled)
- %7B
 %0A
+%09
 %09%09%09retur
@@ -1242,16 +1242,36 @@
 return;%0A
+%09%09%09enabled = false;%0A
 %09%09%7D%0A%0A%09%09L
@@ -1412,28 +1412,8 @@
 %0A%09%09%7D
-%0A%0A%09%09enabled = false;
 %0A%09%7D%0A

Fix for modules remaining in enabled state and not recovering