-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Dev0Louis/dev
4.0.0 Finish.
- Loading branch information
Showing
20 changed files
with
416 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,126 @@ | ||
package dev.louis.nebula; | ||
|
||
import dev.louis.nebula.api.manager.entrypoint.RegisterManaManagerEntrypoint; | ||
import dev.louis.nebula.api.manager.entrypoint.RegisterSpellManagerEntrypoint; | ||
import dev.louis.nebula.api.manager.registerable.ManaManagerRegistrableView; | ||
import dev.louis.nebula.api.manager.registerable.SpellManagerRegistrableView; | ||
import dev.louis.nebula.mana.manager.ManaManager; | ||
import dev.louis.nebula.mana.manager.NebulaManaManager; | ||
import dev.louis.nebula.spell.manager.NebulaSpellManager; | ||
import dev.louis.nebula.spell.manager.SpellManager; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.fabricmc.loader.api.metadata.ModMetadata; | ||
import net.fabricmc.loader.api.ModContainer; | ||
import net.fabricmc.loader.api.entrypoint.EntrypointContainer; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
|
||
public class NebulaManager { | ||
private static final String JSON_KEY_CONTAINS_MANA_MANAGER = "nebula:contains_mana_manager"; | ||
private static final String JSON_KEY_CONTAINS_SPELL_MANAGER = "nebula:contains_spell_manager"; | ||
public static final NebulaManager INSTANCE = new NebulaManager(); | ||
private NebulaManager(){} | ||
|
||
private ManaManager.Factory<?> manaManagerFactory; | ||
private SpellManager.Factory<?> spellManagerFactory; | ||
private boolean loadManaManager = true; | ||
private boolean loadSpellManager = true; | ||
public void init() { | ||
load(); | ||
if(loadManaManager)registerManaManagerFactory(NebulaManaManager::new); | ||
if(loadSpellManager)registerSpellManagerFactory(NebulaSpellManager::new); | ||
import java.util.List; | ||
|
||
public class NebulaManager implements ManaManagerRegistrableView, SpellManagerRegistrableView { | ||
private static ModContainer manaManagerMod; | ||
private static ManaManager.Factory<?> manaManagerFactory; | ||
private static ModContainer spellManagerMod; | ||
private static SpellManager.Factory<?> spellManagerFactory; | ||
private static boolean isLocked = false; | ||
|
||
private NebulaManager() {} | ||
|
||
public static void init() { | ||
if(NebulaManager.isLocked) throw new IllegalStateException("Registration of Managers is locked!"); | ||
NebulaManager nebulaManager = new NebulaManager(); | ||
nebulaManager.runEntrypointsOrThrow(); | ||
nebulaManager.lock(); | ||
nebulaManager.printInfo(); | ||
} | ||
|
||
public static ManaManager.Factory<?> getManaManagerFactory() { | ||
return manaManagerFactory; | ||
} | ||
|
||
public static SpellManager.Factory<?> getSpellManagerFactory() { | ||
return spellManagerFactory; | ||
} | ||
private void load() { | ||
FabricLoader.getInstance().getAllMods().forEach(mod -> { | ||
final ModMetadata metadata = mod.getMetadata(); | ||
if(metadata.getId().equals(Nebula.MOD_ID))return; | ||
var loadManaManager = metadata.getCustomValue(JSON_KEY_CONTAINS_MANA_MANAGER); | ||
if (loadManaManager != null && loadManaManager.getAsBoolean()) this.loadManaManager = false; | ||
var loadSpellManager = metadata.getCustomValue(JSON_KEY_CONTAINS_SPELL_MANAGER); | ||
if (loadSpellManager != null && loadSpellManager.getAsBoolean()) this.loadSpellManager = false; | ||
}); | ||
|
||
public static ManaManager createManaManager(PlayerEntity player) { | ||
return getManaManagerFactory().createPlayerManaManager(player); | ||
} | ||
|
||
public static SpellManager createSpellManager(PlayerEntity player) { | ||
return getSpellManagerFactory().createSpellKnowledgeManager(player); | ||
} | ||
|
||
public void registerManaManagerFactory(ManaManager.Factory<?> manaManager) { | ||
if (manaManager == null) { | ||
throw new NullPointerException("Attempt to register a NULL ManaManager"); | ||
public void lock() { | ||
if(spellManagerFactory == null) { | ||
spellManagerFactory = NebulaSpellManager::new; | ||
spellManagerMod = FabricLoader.getInstance().getModContainer(Nebula.MOD_ID).orElseThrow(); | ||
} | ||
if (this.manaManagerFactory != null) { | ||
throw new UnsupportedOperationException("A second ManaManager attempted to register. Multiple ManaManagers are not supported."); | ||
if(manaManagerFactory == null) { | ||
manaManagerFactory = NebulaManaManager::new; | ||
manaManagerMod = FabricLoader.getInstance().getModContainer(Nebula.MOD_ID).orElseThrow(); | ||
} | ||
this.manaManagerFactory = manaManager; | ||
isLocked = true; | ||
} | ||
|
||
@Override | ||
public void registerManaManagerFactory(ManaManager.Factory<?> manaManagerFactory) { | ||
NebulaManager.manaManagerFactory = manaManagerFactory; | ||
} | ||
|
||
@Override | ||
public void registerSpellManagerFactory(SpellManager.Factory<?> spellManagerFactory) { | ||
NebulaManager.spellManagerFactory = spellManagerFactory; | ||
} | ||
|
||
public void registerSpellManagerFactory(SpellManager.Factory<?> spellKnowledgeManager) { | ||
if (spellKnowledgeManager == null) { | ||
throw new NullPointerException("Attempt to register a NULL SpellManager"); | ||
/** | ||
* Throws an exception if multiple mods want to override the ManaManager or SpellManager. | ||
*/ | ||
private void runEntrypointsOrThrow() { | ||
var manaManagerEntrypoints = FabricLoader.getInstance().getEntrypointContainers("registerManaManager", RegisterManaManagerEntrypoint.class) | ||
.stream().filter(container -> container.getEntrypoint().shouldRegister()).toList(); | ||
|
||
var spellManagerEntrypoints = FabricLoader.getInstance().getEntrypointContainers("registerSpellManager", RegisterSpellManagerEntrypoint.class) | ||
.stream().filter(container -> container.getEntrypoint().shouldRegister()).toList(); | ||
|
||
var manaManagerMods = manaManagerEntrypoints.stream().map(EntrypointContainer::getProvider).toList(); | ||
var spellManagerMods = spellManagerEntrypoints.stream().map(EntrypointContainer::getProvider).toList(); | ||
|
||
if(manaManagerEntrypoints.size() > 1 && spellManagerEntrypoints.size() > 1) { | ||
throw new IllegalStateException("Multiple Mods want to override the ManaManager and SpellManager!\n " + | ||
"Mods overriding ManaManager: " + manaManagerMods + ",\n" + | ||
"Mods overriding SpellManager: " + spellManagerMods); | ||
} | ||
if (this.spellManagerFactory != null) { | ||
throw new UnsupportedOperationException("A SpellManager plug-in attempted to register. Multiple SpellManagers are not supported."); | ||
|
||
if(manaManagerEntrypoints.size() > 1) { | ||
throw new IllegalStateException("Multiple Mods want to override the ManaManager! Mods: " + manaManagerMods); | ||
} | ||
this.spellManagerFactory = spellKnowledgeManager; | ||
} | ||
|
||
public ManaManager.Factory<?> getManaManagerFactory() { | ||
return manaManagerFactory; | ||
if(spellManagerEntrypoints.size() > 1) { | ||
throw new IllegalStateException("Multiple Mods want to override the SpellManager! Mods: " + spellManagerMods); | ||
} | ||
|
||
runEntryPoints(getFirstOrNull(manaManagerEntrypoints), getFirstOrNull(spellManagerEntrypoints)); | ||
} | ||
public SpellManager.Factory<?> getSpellManagerFactory() { | ||
return spellManagerFactory; | ||
|
||
private void runEntryPoints( | ||
EntrypointContainer<RegisterManaManagerEntrypoint> manaManagerEntrypointEntrypointContainer, | ||
EntrypointContainer<RegisterSpellManagerEntrypoint> spellManagerEntrypointEntrypointContainer | ||
) { | ||
if(manaManagerEntrypointEntrypointContainer != null) { | ||
manaManagerEntrypointEntrypointContainer.getEntrypoint().registerSpell(this); | ||
NebulaManager.manaManagerMod = manaManagerEntrypointEntrypointContainer.getProvider(); | ||
} | ||
|
||
if(spellManagerEntrypointEntrypointContainer != null) { | ||
spellManagerEntrypointEntrypointContainer.getEntrypoint().registerSpell(this); | ||
NebulaManager.spellManagerMod = spellManagerEntrypointEntrypointContainer.getProvider(); | ||
} | ||
} | ||
public static ManaManager createManaManager(PlayerEntity player) { | ||
return INSTANCE.getManaManagerFactory().createPlayerManaManager(player); | ||
|
||
private void printInfo() { | ||
Nebula.LOGGER.info("ManaManager is registered by: " + NebulaManager.manaManagerMod.getMetadata().getName()); | ||
Nebula.LOGGER.info("SpellManager is registered by: " + NebulaManager.spellManagerMod.getMetadata().getName()); | ||
} | ||
public static SpellManager createSpellManager(PlayerEntity player) { | ||
return INSTANCE.getSpellManagerFactory().createSpellKnowledgeManager(player); | ||
|
||
private static <T> T getFirstOrNull(List<T> list) { | ||
return list.isEmpty() ? null : list.get(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/louis/nebula/api/manager/entrypoint/RegisterManaManagerEntrypoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.louis.nebula.api.manager.entrypoint; | ||
|
||
import dev.louis.nebula.api.manager.registerable.SpellManagerRegistrableView; | ||
|
||
public interface RegisterManaManagerEntrypoint { | ||
void registerSpell(SpellManagerRegistrableView spellManagerRegistrableView); | ||
|
||
default boolean shouldRegister() { | ||
return true; | ||
}; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/louis/nebula/api/manager/entrypoint/RegisterSpellManagerEntrypoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.louis.nebula.api.manager.entrypoint; | ||
|
||
import dev.louis.nebula.api.manager.registerable.SpellManagerRegistrableView; | ||
|
||
public interface RegisterSpellManagerEntrypoint { | ||
void registerSpell(SpellManagerRegistrableView spellManagerRegistrableView); | ||
|
||
default boolean shouldRegister() { | ||
return true; | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/dev/louis/nebula/api/manager/registerable/ManaManagerRegistrableView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.louis.nebula.api.manager.registerable; | ||
|
||
import dev.louis.nebula.mana.manager.ManaManager; | ||
|
||
public interface ManaManagerRegistrableView { | ||
void registerManaManagerFactory(ManaManager.Factory<?> manaManagerFactory); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/dev/louis/nebula/api/manager/registerable/SpellManagerRegistrableView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.louis.nebula.api.manager.registerable; | ||
|
||
import dev.louis.nebula.spell.manager.SpellManager; | ||
|
||
public interface SpellManagerRegistrableView { | ||
void registerSpellManagerFactory(SpellManager.Factory<?> manaManagerFactory); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.