Skip to content

Instantly share code, notes, and snippets.

@justisr
Last active December 21, 2018 17:20
Show Gist options
  • Save justisr/b1d0607b78425f3fcbb55d1b1f4d520e to your computer and use it in GitHub Desktop.
Save justisr/b1d0607b78425f3fcbb55d1b1f4d520e to your computer and use it in GitHub Desktop.
Managing a large project’s systems, and ensuring their enable/disable ordering can be tiresome. Automate the process.
package com.gmail.justisroot.autoecon;
import java.util.ArrayList;
import java.util.List;
import com.gmail.justisroot.autoecon.command.SoftCommand;
import com.gmail.justisroot.autoecon.data.EventProvider;
import com.gmail.justisroot.autoecon.data.FilePool;
import com.gmail.justisroot.autoecon.data.ScheduledSave;
import com.gmail.justisroot.autoecon.data.file.UserData;
import com.gmail.justisroot.autoecon.data.text.PlaceholderProvider;
import com.gmail.justisroot.autoecon.economy.EconomyWrapper;
import com.gmail.justisroot.autoecon.economy.MarketDirector;
import com.gmail.justisroot.autoecon.economy.items.EconItem;
import com.gmail.justisroot.autoecon.inventory.GUIListener;
public enum AESystem {
FILE_POOL(FilePool.install()),
ECON_ITEM(EconItem.install(), FILE_POOL),
EVENT_PROVIDER(EventProvider.install()),
USER_DATA(UserData.install(), EVENT_PROVIDER),
GUI_LISTENER(GUIListener.install(), EVENT_PROVIDER),
MARKET_DIRECTOR(MarketDirector.install(), FILE_POOL, USER_DATA, EVENT_PROVIDER),
ECONOMY_WRAPPER(EconomyWrapper.install(), FILE_POOL, EVENT_PROVIDER),
SCHEDULED_SAVE(ScheduledSave.install(), FILE_POOL, MARKET_DIRECTOR),
PLACEHOLDER_PROVIDER(PlaceholderProvider.install(), MARKET_DIRECTOR, ECONOMY_WRAPPER, ECON_ITEM),
COMMANDS(SoftCommand.install(), FILE_POOL, MARKET_DIRECTOR);
private final Switch swij;
private final AESystem[] dependencies;
private final List<AESystem> requiredBy = new ArrayList<>();
private AESystem(Switch swij, AESystem... dependencies) {
this.swij = swij;
this.dependencies = dependencies;
for (AESystem dependency : dependencies)
dependency.requiredBy.add(this);
}
public static abstract class Switch {
private boolean enabled;
private void enable() {
this.onEnable();
this.enabled = true;
}
private void disable() {
this.onDisable();
this.enabled = true;
}
private boolean isEnabled() {
return this.enabled;
}
public abstract void onEnable();
public abstract void onDisable();
}
public static final void enableAll() {
for (AESystem system : values()) {
for (AESystem dependency : system.dependencies)
if (!dependency.swij.isEnabled())
dependency.swij.enable();
if (!system.swij.isEnabled())
system.swij.enable();
}
}
public static final void disableAll() {
for (AESystem system : values()) {
for (AESystem requiring : system.requiredBy)
if (requiring.swij.isEnabled())
requiring.swij.disable();
if (system.swij.isEnabled())
system.swij.disable();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment