Skip to content

Commit

Permalink
Merge pull request #20 from Dev0Louis/dev
Browse files Browse the repository at this point in the history
4.0.0 Finish.
  • Loading branch information
Dev0Louis authored Dec 10, 2023
2 parents 0081622 + 3461335 commit 44de3ce
Show file tree
Hide file tree
Showing 20 changed files with 416 additions and 99 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.3
yarn_mappings=1.20.3+build.1
loader_version=0.15.0
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.1
# Mod Properties
mod_version=4.0.0-beta.1
mod_version=4.0.0-beta.2
maven_group=dev.louis
archives_base_name=Nebula
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.91.1+1.20.3
fabric_version=0.91.2+1.20.4
6 changes: 3 additions & 3 deletions src/main/java/dev/louis/nebula/Nebula.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class Nebula implements ModInitializer {
@Override
public void onInitialize() {
SpellType.init();
this.registerPacketReceivers();
NebulaManager.INSTANCE.init();
NebulaManager.init();
NebulaCommand.init();
LOGGER.info("Nebula has initialized.");
this.registerPacketReceivers();
LOGGER.info("Nebula has been initialized.");
}

public void registerPacketReceivers() {
Expand Down
147 changes: 102 additions & 45 deletions src/main/java/dev/louis/nebula/NebulaManager.java
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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/louis/nebula/api/NebulaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ default SpellManager getSpellManager() {
default SpellManager setSpellManager(SpellManager spellManager) {
throw new UnsupportedOperationException("Injected Interface method was not overridden!");
}

default void createManagersIfNecessary() {
throw new UnsupportedOperationException("Injected Interface method was not overridden!");
};
}
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;
};
}
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;
};
}
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);
}
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);
}
5 changes: 3 additions & 2 deletions src/main/java/dev/louis/nebula/command/NebulaCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ private static void register(CommandDispatcher<ServerCommandSource> dispatcher,
command.then(getManaCommand);
command.then(setManaCommand);


var learnSpellCommand = literal("learnSpell");
Nebula.SPELL_REGISTRY.forEach(spellType -> {
command.then(CommandManager.literal(spellType.getId().toString()).executes(context -> {
learnSpellCommand.then(CommandManager.literal(spellType.getId().toString()).executes(context -> {
if(context.getSource().isExecutedByPlayer()) {
context.getSource().getPlayer().getSpellManager().learnSpell(spellType);
}
return 0;
}));
});

command.then(learnSpellCommand);

dispatcher.register(command);
}
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/dev/louis/nebula/mana/manager/ManaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,94 @@
import net.minecraft.network.PacketByteBuf;

public interface ManaManager {
/**
* This is called every tick the player is ticked.
* This should not be called manually.
*/
void tick();

/**
* Sets the amount of mana.
* @param mana The amount of mana to set.
*/
void setMana(int mana);

/**
* @return The current amount of mana.
*/
int getMana();

/**
* Adds the specified amount of mana.
* @param mana The amount of mana to add.
*/
void addMana(int mana);

/**
* Drains the specified amount of mana.
* @param mana The amount of mana to drain.
*/
void drainMana(int mana);

/**
* Drains amount of mana from {@link SpellType#getManaCost()}
* @param spellType The SpellType where the amount of mana to drain is got from.
*/
void drainMana(SpellType<?> spellType);

/**
* @return The maximum amount of mana that can be stored.
*/
int getMaxMana();

/**
* @param mana The man amount that should be checked.
* @return If enough mana is available.
*/
boolean hasEnoughMana(int mana);

/**
* @param spellType The SpellType which should be checked.
* @return If enough mana is available for the specified SpellType.
*/
boolean hasEnoughMana(SpellType<?> spellType);

/**
* Sends the ManaManager's state to the client.
* @return If the state was successfully send.
*/
boolean sendSync();

/**
* Receives the ManaManager's state for the client. This shall never be called by the server.
* @return If the state was successfully received.
*/
boolean receiveSync(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender);

/**
* Writes the Nbt data of the SpellManager.
* @param nbt The Nbt data that shall be written to.
* @return The Nbt data that has been written to.
*/
void writeNbt(NbtCompound nbt);

/**
* Reads the Nbt data of the ManaManager.
* @param nbt The Nbt data that shall be read.
*/
void readNbt(NbtCompound nbt);

/**
* This is called when the Player dies.
* @param damageSource The DamageSource of the death.
*/
void onDeath(DamageSource damageSource);

/**
* Sets the PlayerEntity of the ManaManager.
* @param player The new PlayerEntity of the ManaManager.
* @return The ManaManager with the new PlayerEntity.
*/
ManaManager setPlayer(PlayerEntity player);

@FunctionalInterface
Expand Down
Loading

0 comments on commit 44de3ce

Please sign in to comment.