diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml new file mode 100644 index 0000000..42eae27 --- /dev/null +++ b/.github/workflows/gradle-publish.yml @@ -0,0 +1,45 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created +# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle + +name: Gradle Package + +on: + release: + types: [created] + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + server-id: github # Value of the distributionManagement/repository/id field of the pom.xml + settings-path: ${{ github.workspace }} # location for the settings.xml file + + - name: Build with Gradle + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: build + + # The USERNAME and TOKEN need to correspond to the credentials environment variables used in + # the publishing section of your build.gradle + - name: Publish to GitHub Packages + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: publish + env: + USERNAME: ${{ github.actor }} + TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/gradle.properties b/gradle.properties index 492320f..9aa1d3f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,11 +4,11 @@ org.gradle.jvmargs=-Xmx1G # check these on https://modmuss50.me/fabric.html minecraft_version=1.19.4 yarn_mappings=1.19.4+build.1 -loader_version=0.14.17 +loader_version=0.14.19 # Mod Properties -mod_version=2.0.0 +mod_version=2.1.0 maven_group=dev.louis archives_base_name=Nebula # Dependencies # check this on https://modmuss50.me/fabric.html -fabric_version=0.76.0+1.19.4 \ No newline at end of file +fabric_version=0.77.0+1.19.4 \ No newline at end of file diff --git a/src/main/java/dev/louis/nebula/Nebula.java b/src/main/java/dev/louis/nebula/Nebula.java index b09a3ac..eb1b292 100644 --- a/src/main/java/dev/louis/nebula/Nebula.java +++ b/src/main/java/dev/louis/nebula/Nebula.java @@ -1,9 +1,6 @@ package dev.louis.nebula; import dev.louis.nebula.event.SpellCastCallback; -import dev.louis.nebula.manamanager.ManaManager; -import dev.louis.nebula.manamanager.NebulaManaManager; -import dev.louis.nebula.manamanager.player.PlayerManaManager; import dev.louis.nebula.networking.SpellCastC2SPacket; import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; @@ -11,10 +8,6 @@ import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; import net.fabricmc.fabric.api.event.registry.RegistryAttribute; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.metadata.ModMetadata; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; @@ -24,74 +17,28 @@ import org.slf4j.LoggerFactory; public class Nebula implements ModInitializer { - private static final String JSON_KEY_DISABLE_NEBULAMANAMANAGER = "nebula:contains_manamanager"; public static final String MOD_ID = "nebula"; public static final Logger LOGGER = LoggerFactory.getLogger("Nebula"); public static Nebula INSTANCE; - private ManaManager activeManaManager; - private boolean hasActiveManaManager = false; - @Override public void onInitialize() { INSTANCE = this; SpellType.init(); registerPacketReceivers(); NebulaRegistries.init(); - loadIfNeeded(); - if(nebulaManaManagerActive) { - registerManaManager(new NebulaManaManager()); - } + NebulaManager.init(); } public void registerPacketReceivers() { - ServerPlayNetworking.registerGlobalReceiver(SpellCastC2SPacket.ID, ((server, player, handler, buf, responseSender) -> { + ServerPlayNetworking.registerGlobalReceiver(SpellCastC2SPacket.getId(), ((server, player, handler, buf, responseSender) -> { Spell spell = SpellCastC2SPacket.read(player, buf).spell(); if (SpellCastCallback.EVENT.invoker().interact(player, spell) == ActionResult.PASS) { spell.cast(); } })); } - private static boolean needsLoad = true; - private static boolean nebulaManaManagerActive = true; - - private static void loadIfNeeded() { - if (needsLoad) { - for (ModContainer container : FabricLoader.getInstance().getAllMods()) { - final ModMetadata meta = container.getMetadata(); - if(meta.getId().equals("nebula"))continue; - if (meta.containsCustomValue(JSON_KEY_DISABLE_NEBULAMANAMANAGER)) { - nebulaManaManagerActive = false; - } - } - - needsLoad = false; - } - } - public void registerManaManager(ManaManager manaManager) { - if (manaManager == null) { - throw new NullPointerException("Attempt to register a NULL rendering plug-in."); - } else if (activeManaManager != null) { - throw new UnsupportedOperationException("A second rendering plug-in attempted to register. Multiple rendering plug-ins are not supported."); - } else { - manaManager.setUp(); - this.activeManaManager = manaManager; - this.hasActiveManaManager = true; - } - } - - public ManaManager getManaManager() { - return activeManaManager; - } - - public boolean hasManaManager() { - return hasActiveManaManager; - } - - public PlayerManaManager createManaManager(PlayerEntity player) { - return getManaManager().createPlayerManaManager(player); - } diff --git a/src/main/java/dev/louis/nebula/NebulaClient.java b/src/main/java/dev/louis/nebula/NebulaClient.java index 7f085ef..99b5318 100644 --- a/src/main/java/dev/louis/nebula/NebulaClient.java +++ b/src/main/java/dev/louis/nebula/NebulaClient.java @@ -7,6 +7,7 @@ import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; public class NebulaClient implements ClientModInitializer { @@ -19,15 +20,12 @@ public void onInitializeClient() { private void registerPacketReceivers() { //Register the Knowledge Packet. - ClientPlayNetworking.registerGlobalReceiver(SynchronizeSpellKnowledgeS2CPacket.ID, (client, handler, buf, responseSender) -> { + ClientPlayNetworking.registerGlobalReceiver(SynchronizeSpellKnowledgeS2CPacket.getID(), (client, handler, buf, responseSender) -> { SynchronizeSpellKnowledgeS2CPacket packet = SynchronizeSpellKnowledgeS2CPacket.read(buf); - client.executeSync(() -> NebulaPlayer.access(client.player).getSpellKnowledge().updateCastableSpell(packet.spells())); + client.executeSync(() -> NebulaPlayer.access(client.player).getSpellKnowledgeManager().updateCastableSpell(packet.spells())); }); //Register the ManaAmount Packet. - ClientPlayNetworking.registerGlobalReceiver(SynchronizeManaAmountS2CPacket.ID, ((client, handler, buf, responseSender) -> { - SynchronizeManaAmountS2CPacket packet = SynchronizeManaAmountS2CPacket.read(buf); - NebulaPlayer.access(client.player).setMana(packet.mana()); - })); + ClientPlayConnectionEvents.INIT.register(((handler, client) -> ClientPlayNetworking.registerReceiver(SynchronizeManaAmountS2CPacket.PACKET_TYPE, SynchronizeManaAmountS2CPacket::receive))); } private void registerAutoConfigIfInstalled() { diff --git a/src/main/java/dev/louis/nebula/NebulaManager.java b/src/main/java/dev/louis/nebula/NebulaManager.java new file mode 100644 index 0000000..5c1d417 --- /dev/null +++ b/src/main/java/dev/louis/nebula/NebulaManager.java @@ -0,0 +1,98 @@ +package dev.louis.nebula; + +import dev.louis.nebula.knowledgemanager.NebulaSpellKnowledgeManager; +import dev.louis.nebula.knowledgemanager.SpellKnowledgeManager; +import dev.louis.nebula.knowledgemanager.player.PlayerSpellKnowledgeManager; +import dev.louis.nebula.manamanager.ManaManager; +import dev.louis.nebula.manamanager.NebulaManaManager; +import dev.louis.nebula.manamanager.player.PlayerManaManager; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; +import net.fabricmc.loader.api.metadata.ModMetadata; +import net.minecraft.entity.player.PlayerEntity; + +import java.util.Optional; + +public class NebulaManager { + private static final String JSON_KEY_CONTAINS_MANAMANAGER = "nebula:contains_manamanager"; + private static final String JSON_KEY_CONTAINS_SPELLKNOWLEDGEMANAGER = "nebula:contains_spellknowledgemanager"; + public static final NebulaManager INSTANCE = new NebulaManager(); + + + private boolean nebulaManaManagerActive = true; + private boolean nebulaSpellManagerActive = true; + private Optional manaManager = Optional.empty(); + private Optional spellKnowledgeManager = Optional.empty(); + + + private NebulaManager() { + + } + + public static void init() { + INSTANCE.internal_init(); + } + private void internal_init() { + load(); + if(nebulaManaManagerActive) { + registerManaManager(new NebulaManaManager()); + } + if(nebulaSpellManagerActive) { + registerSpellKnowledgeManager(new NebulaSpellKnowledgeManager()); + } + } + + + private void load() { + for (ModContainer container : FabricLoader.getInstance().getAllMods()) { + final ModMetadata meta = container.getMetadata(); + if(meta.getId().equals("nebula"))continue; + if (meta.containsCustomValue(JSON_KEY_CONTAINS_MANAMANAGER)) { + nebulaManaManagerActive = false; + } + if (meta.containsCustomValue(JSON_KEY_CONTAINS_SPELLKNOWLEDGEMANAGER)) { + nebulaSpellManagerActive = false; + } + } + } + public void registerManaManager(ManaManager manaManager) { + if (manaManager == null) { + throw new NullPointerException("Attempt to register a NULL ManaManager"); + } else if (this.manaManager.isPresent()) { + throw new UnsupportedOperationException("A second ManaManager attempted to register. Multiple ManaManagers are not supported."); + } else { + this.manaManager = Optional.of(manaManager); + manaManager.setUp(); + } + } + + public void registerSpellKnowledgeManager(SpellKnowledgeManager spellKnowledgeManager) { + if (spellKnowledgeManager == null) { + throw new NullPointerException("Attempt to register a NULL SpellKnowledgeManager"); + } else if (this.spellKnowledgeManager.isPresent()) { + throw new UnsupportedOperationException("A SpellKnowledgeManager plug-in attempted to register. Multiple SpellKnowledgeManagers are not supported."); + } else { + this.spellKnowledgeManager = Optional.of(spellKnowledgeManager); + spellKnowledgeManager.setUp(); + } + } + + + + public ManaManager getManaManager() { + return manaManager.orElseThrow(); + } + + public SpellKnowledgeManager getSpellKnowledgeManager() { + return spellKnowledgeManager.orElseThrow(); + } + + public PlayerManaManager createPlayerManaManager(PlayerEntity player) { + return getManaManager().createPlayerManaManager(player); + } + + public PlayerSpellKnowledgeManager createPlayerSpellKnowledgeManager(PlayerEntity player) { + return getSpellKnowledgeManager().createPlayerSpellKnowledgeManager(player); + } + +} diff --git a/src/main/java/dev/louis/nebula/api/NebulaPlayer.java b/src/main/java/dev/louis/nebula/api/NebulaPlayer.java index 3ac0086..c2c8367 100644 --- a/src/main/java/dev/louis/nebula/api/NebulaPlayer.java +++ b/src/main/java/dev/louis/nebula/api/NebulaPlayer.java @@ -1,6 +1,6 @@ package dev.louis.nebula.api; -import dev.louis.nebula.knowledge.SpellKnowledgeManager; +import dev.louis.nebula.knowledgemanager.player.PlayerSpellKnowledgeManager; import dev.louis.nebula.manamanager.player.PlayerManaManager; import net.minecraft.entity.player.PlayerEntity; import org.jetbrains.annotations.NotNull; @@ -10,7 +10,7 @@ public interface NebulaPlayer { void setPlayerManaManager(PlayerManaManager manaManager); public int getMana(); public void setMana(int mana); - public SpellKnowledgeManager getSpellKnowledge(); + public PlayerSpellKnowledgeManager getSpellKnowledgeManager(); @NotNull static NebulaPlayer access(PlayerEntity player) { diff --git a/src/main/java/dev/louis/nebula/event/SpellKnowledgeAddCallback.java b/src/main/java/dev/louis/nebula/event/SpellKnowledgeAddCallback.java deleted file mode 100644 index 0dac84d..0000000 --- a/src/main/java/dev/louis/nebula/event/SpellKnowledgeAddCallback.java +++ /dev/null @@ -1,24 +0,0 @@ -package dev.louis.nebula.event; - -import dev.louis.nebula.spell.SpellType; -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.util.ActionResult; - -public interface SpellKnowledgeAddCallback { - Event EVENT = EventFactory.createArrayBacked(SpellKnowledgeAddCallback.class, (listeners) -> (player, spellType) -> { - for (SpellKnowledgeAddCallback event : listeners) { - ActionResult result = event.interact(player, spellType); - - if (result != ActionResult.PASS) { - return result; - } - } - - return ActionResult.PASS; - } - ); - - ActionResult interact(PlayerEntity player, SpellType spellType); -} diff --git a/src/main/java/dev/louis/nebula/event/SpellKnowledgeRemoveCallback.java b/src/main/java/dev/louis/nebula/event/SpellKnowledgeUpdateCallback.java similarity index 53% rename from src/main/java/dev/louis/nebula/event/SpellKnowledgeRemoveCallback.java rename to src/main/java/dev/louis/nebula/event/SpellKnowledgeUpdateCallback.java index d3cc2cb..df587cb 100644 --- a/src/main/java/dev/louis/nebula/event/SpellKnowledgeRemoveCallback.java +++ b/src/main/java/dev/louis/nebula/event/SpellKnowledgeUpdateCallback.java @@ -1,15 +1,18 @@ package dev.louis.nebula.event; +import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.ActionResult; -public interface SpellKnowledgeRemoveCallback { - Event EVENT = EventFactory.createArrayBacked(SpellKnowledgeRemoveCallback.class, (listeners) -> (player, removedSpellType) -> { - for (SpellKnowledgeRemoveCallback event : listeners) { - ActionResult result = event.interact(player, removedSpellType); +import java.util.Map; + +public interface SpellKnowledgeUpdateCallback { + Event EVENT = EventFactory.createArrayBacked(SpellKnowledgeUpdateCallback.class, (listeners) -> (player, castableSpells) -> { + for (SpellKnowledgeUpdateCallback event : listeners) { + ActionResult result = event.interact(player, castableSpells); if (result != ActionResult.PASS) { return result; @@ -20,5 +23,5 @@ public interface SpellKnowledgeRemoveCallback { } ); - ActionResult interact(PlayerEntity player, SpellType removedSpellType); + ActionResult interact(PlayerEntity player, Map, Boolean> castableSpells); } diff --git a/src/main/java/dev/louis/nebula/knowledgemanager/NebulaSpellKnowledgeManager.java b/src/main/java/dev/louis/nebula/knowledgemanager/NebulaSpellKnowledgeManager.java new file mode 100644 index 0000000..b22b66e --- /dev/null +++ b/src/main/java/dev/louis/nebula/knowledgemanager/NebulaSpellKnowledgeManager.java @@ -0,0 +1,15 @@ +package dev.louis.nebula.knowledgemanager; + +import dev.louis.nebula.knowledgemanager.player.NebulaPlayerSpellKnowledgeManager; +import net.minecraft.entity.player.PlayerEntity; + +public class NebulaSpellKnowledgeManager implements SpellKnowledgeManager { + + @Override + public NebulaPlayerSpellKnowledgeManager createPlayerSpellKnowledgeManager(PlayerEntity player) { + return new NebulaPlayerSpellKnowledgeManager(player); + } + + @Override + public void setUp() {} +} diff --git a/src/main/java/dev/louis/nebula/knowledgemanager/SpellKnowledgeManager.java b/src/main/java/dev/louis/nebula/knowledgemanager/SpellKnowledgeManager.java new file mode 100644 index 0000000..98b1b13 --- /dev/null +++ b/src/main/java/dev/louis/nebula/knowledgemanager/SpellKnowledgeManager.java @@ -0,0 +1,9 @@ +package dev.louis.nebula.knowledgemanager; + +import dev.louis.nebula.knowledgemanager.player.PlayerSpellKnowledgeManager; +import net.minecraft.entity.player.PlayerEntity; + +public interface SpellKnowledgeManager { + T createPlayerSpellKnowledgeManager(PlayerEntity player); + void setUp(); +} diff --git a/src/main/java/dev/louis/nebula/knowledge/SpellKnowledgeManager.java b/src/main/java/dev/louis/nebula/knowledgemanager/player/NebulaPlayerSpellKnowledgeManager.java similarity index 66% rename from src/main/java/dev/louis/nebula/knowledge/SpellKnowledgeManager.java rename to src/main/java/dev/louis/nebula/knowledgemanager/player/NebulaPlayerSpellKnowledgeManager.java index 7cde62c..42f17da 100644 --- a/src/main/java/dev/louis/nebula/knowledge/SpellKnowledgeManager.java +++ b/src/main/java/dev/louis/nebula/knowledgemanager/player/NebulaPlayerSpellKnowledgeManager.java @@ -1,9 +1,8 @@ -package dev.louis.nebula.knowledge; +package dev.louis.nebula.knowledgemanager.player; import dev.louis.nebula.Nebula; import dev.louis.nebula.api.NebulaPlayer; -import dev.louis.nebula.event.SpellKnowledgeAddCallback; -import dev.louis.nebula.event.SpellKnowledgeRemoveCallback; +import dev.louis.nebula.event.SpellKnowledgeUpdateCallback; import dev.louis.nebula.networking.SynchronizeSpellKnowledgeS2CPacket; import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; @@ -18,11 +17,11 @@ import java.util.*; -public class SpellKnowledgeManager { +public class NebulaPlayerSpellKnowledgeManager implements PlayerSpellKnowledgeManager { PlayerEntity player; - public SpellKnowledgeManager(PlayerEntity player) { + public NebulaPlayerSpellKnowledgeManager(PlayerEntity player) { this.player = player; } @@ -32,61 +31,76 @@ public SpellKnowledgeManager(PlayerEntity player) { /** * @return a copy of the CastableSpells. */ + @Override public Set> getCastableSpells() { return castableSpells; } + @Override + public void tick() {} - /** - * The setCastableSpells function sets the castableSpells field of this object to the given list. - * - * @param castableSpells Set the list of spells that the player can cast - * - */ - private void setCastableSpells(Set> castableSpells) { + @Override + public void setCastableSpells(Set> castableSpells) { this.castableSpells = castableSpells; } - public void addCastableSpell(SpellType... castableSpells) { + + @Override + public void addCastableSpell(SpellType spellType) { + addCastableSpell(List.of(spellType)); + } + + + @Override + public void addCastableSpell(Iterable> castableSpells) { Map, Boolean> spellMaps = new HashMap<>(); for (SpellType spellType : castableSpells) { - if (SpellKnowledgeAddCallback.EVENT.invoker().interact(player, spellType) == ActionResult.PASS) { - spellMaps.put(spellType, true); - } + spellMaps.put(spellType, true); } updateCastableSpell(spellMaps); } public void updateCastableSpell(Map, Boolean> castableSpells) { + if(SpellKnowledgeUpdateCallback.EVENT.invoker().interact(player, castableSpells) != ActionResult.PASS)return; castableSpells.forEach((spellType, knows) -> { - if (knows) this.castableSpells.add(spellType); - else this.castableSpells.remove(spellType); + if (knows) this.castableSpells.add(spellType); + else this.castableSpells.remove(spellType); }); if(player instanceof ServerPlayerEntity serverPlayer) { - ServerPlayNetworking.send(serverPlayer, SynchronizeSpellKnowledgeS2CPacket.ID, new SynchronizeSpellKnowledgeS2CPacket(castableSpells).write(PacketByteBufs.create())); + var buf = PacketByteBufs.create(); + new SynchronizeSpellKnowledgeS2CPacket(castableSpells).write(buf); + ServerPlayNetworking.send(serverPlayer, SynchronizeSpellKnowledgeS2CPacket.getID(), buf); } } - public void removeCastableSpell(SpellType... castableSpells) { + + @Override + public void removeCastableSpell(SpellType spellType) { + removeCastableSpell(List.of(spellType)); + } + + @Override + public void removeCastableSpell(Iterable> castableSpells) { Map, Boolean> spellMaps = new HashMap<>(); for (SpellType spellType : castableSpells) { - if (SpellKnowledgeRemoveCallback.EVENT.invoker().interact(player, spellType) == ActionResult.PASS) { - spellMaps.put(spellType, true); - } + spellMaps.put(spellType, false); } updateCastableSpell(spellMaps); } + @Override public void copyFrom(ServerPlayerEntity oldPlayer, boolean alive) { if(alive) { - setCastableSpells(((NebulaPlayer)oldPlayer).getSpellKnowledge().getCastableSpells()); + setCastableSpells(((NebulaPlayer)oldPlayer).getSpellKnowledgeManager().getCastableSpells()); } } + @Override public boolean doesKnow(SpellType spellType) { return castableSpells.contains(spellType); } + @Override public NbtCompound writeNbt(NbtCompound nbt) { - Set> castableSpells = NebulaPlayer.access(player).getSpellKnowledge().getCastableSpells(); + Set> castableSpells = NebulaPlayer.access(player).getSpellKnowledgeManager().getCastableSpells(); NbtList nbtList = new NbtList(); for (SpellType spell : castableSpells) { @@ -102,6 +116,7 @@ public NbtCompound writeNbt(NbtCompound nbt) { } + @Override public void readNbt(NbtCompound nbt) { NbtList nbtList = (NbtList) nbt.getCompound(Nebula.MOD_ID).get("Spells"); if(nbtList == null)return; diff --git a/src/main/java/dev/louis/nebula/knowledgemanager/player/PlayerSpellKnowledgeManager.java b/src/main/java/dev/louis/nebula/knowledgemanager/player/PlayerSpellKnowledgeManager.java new file mode 100644 index 0000000..1888e30 --- /dev/null +++ b/src/main/java/dev/louis/nebula/knowledgemanager/player/PlayerSpellKnowledgeManager.java @@ -0,0 +1,30 @@ +package dev.louis.nebula.knowledgemanager.player; + +import dev.louis.nebula.spell.Spell; +import dev.louis.nebula.spell.SpellType; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.Map; +import java.util.Set; + +public interface PlayerSpellKnowledgeManager { + void tick(); + + void setCastableSpells(Set> castableSpells); + Set> getCastableSpells(); + + void addCastableSpell(SpellType spellType); + void addCastableSpell(Iterable> castableSpells); + void updateCastableSpell(Map, Boolean> castableSpells); + void removeCastableSpell(SpellType spellType); + void removeCastableSpell(Iterable> castableSpells); + + void copyFrom(ServerPlayerEntity oldPlayer, boolean alive); + + boolean doesKnow(SpellType spellType); + + NbtCompound writeNbt(NbtCompound nbt); + + void readNbt(NbtCompound nbt); +} diff --git a/src/main/java/dev/louis/nebula/manamanager/player/NebulaPlayerManaManager.java b/src/main/java/dev/louis/nebula/manamanager/player/NebulaPlayerManaManager.java index 1a7ba3b..1ab6f1e 100644 --- a/src/main/java/dev/louis/nebula/manamanager/player/NebulaPlayerManaManager.java +++ b/src/main/java/dev/louis/nebula/manamanager/player/NebulaPlayerManaManager.java @@ -1,6 +1,7 @@ package dev.louis.nebula.manamanager.player; import dev.louis.nebula.Nebula; +import dev.louis.nebula.NebulaManager; import dev.louis.nebula.api.NebulaPlayer; import dev.louis.nebula.networking.SynchronizeManaAmountS2CPacket; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; @@ -50,16 +51,19 @@ public int getPlayerMaxMana() { public void setPlayerMaxMana(int max) { if(max > 0)this.maxmana = max; - this.maxmana = Nebula.INSTANCE.getManaManager().getMaxMana(); + this.maxmana = NebulaManager.INSTANCE.getManaManager().getMaxMana(); } @Override public void sync() { if(this.player.getWorld().isClient() || ((ServerPlayerEntity)player).networkHandler == null)return; + var buf = PacketByteBufs.create(); + new SynchronizeManaAmountS2CPacket(((NebulaPlayer)player).getMana()).write(buf); + ServerPlayNetworking.send( (ServerPlayerEntity) this.player, - SynchronizeManaAmountS2CPacket.ID, - new SynchronizeManaAmountS2CPacket(((NebulaPlayer)player).getMana()).write(PacketByteBufs.create()) + SynchronizeManaAmountS2CPacket.getId(), + buf ); } diff --git a/src/main/java/dev/louis/nebula/mixin/PlayerManagerMixin.java b/src/main/java/dev/louis/nebula/mixin/PlayerManagerMixin.java index 0d1cb82..9463de0 100644 --- a/src/main/java/dev/louis/nebula/mixin/PlayerManagerMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/PlayerManagerMixin.java @@ -4,6 +4,7 @@ import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.network.ClientConnection; +import net.minecraft.network.PacketByteBuf; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; import org.spongepowered.asm.mixin.Mixin; @@ -15,6 +16,9 @@ public class PlayerManagerMixin { @Inject(at = @At(value = "TAIL"), method = "onPlayerConnect") private void onPlayerJoin(ClientConnection connection, ServerPlayerEntity player, CallbackInfo info) { - ServerPlayNetworking.send(player, SynchronizeSpellKnowledgeS2CPacket.ID, SynchronizeSpellKnowledgeS2CPacket.create().write(PacketByteBufs.create())); + PacketByteBuf buf = PacketByteBufs.create(); + SynchronizeSpellKnowledgeS2CPacket.create(player).write(buf); + + ServerPlayNetworking.send(player, SynchronizeSpellKnowledgeS2CPacket.getID(), buf); } } diff --git a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java index dba3b61..06dc44b 100644 --- a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java @@ -1,8 +1,8 @@ package dev.louis.nebula.mixin; -import dev.louis.nebula.Nebula; +import dev.louis.nebula.NebulaManager; import dev.louis.nebula.api.NebulaPlayer; -import dev.louis.nebula.knowledge.SpellKnowledgeManager; +import dev.louis.nebula.knowledgemanager.player.PlayerSpellKnowledgeManager; import dev.louis.nebula.manamanager.player.PlayerManaManager; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; @@ -22,7 +22,7 @@ private PlayerMixin() { //Mana Start - public PlayerManaManager playerManaManager = Nebula.INSTANCE.createManaManager((PlayerEntity) (Object) this); + public PlayerManaManager playerManaManager = NebulaManager.INSTANCE.createPlayerManaManager((PlayerEntity) (Object) this); @Inject(method = "writeCustomDataToNbt", at = @At("RETURN")) public void addManaToNbtMixin(NbtCompound nbt, CallbackInfo ci) { @@ -64,20 +64,24 @@ public void setPlayerManaManager(PlayerManaManager manaManager) { // Knowledge Start - private final SpellKnowledgeManager spellKnowledge = new SpellKnowledgeManager((PlayerEntity) (Object) this); + private final PlayerSpellKnowledgeManager spellKnowledgeManager = NebulaManager.INSTANCE.createPlayerSpellKnowledgeManager((PlayerEntity) (Object) this); @Inject(method = "writeCustomDataToNbt", at = @At(value = "TAIL")) public void addKnowledgeToNbtMixin(NbtCompound nbt, CallbackInfo ci) { - this.spellKnowledge.writeNbt(nbt); + this.spellKnowledgeManager.writeNbt(nbt); } @Inject(method = "readCustomDataFromNbt", at = @At("TAIL")) public void addKnowledgeFromNbtMixin(NbtCompound nbt, CallbackInfo ci) { - spellKnowledge.readNbt(nbt); + spellKnowledgeManager.readNbt(nbt); + } + @Inject(method = "tick", at = @At("RETURN")) + public void tickKnowledgeManager(CallbackInfo ci) { + spellKnowledgeManager.tick(); } @Override - public SpellKnowledgeManager getSpellKnowledge() { - return this.spellKnowledge; + public PlayerSpellKnowledgeManager getSpellKnowledgeManager() { + return this.spellKnowledgeManager; } // Knowledge End diff --git a/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java b/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java index 3423a74..317b5ff 100644 --- a/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java @@ -24,7 +24,7 @@ public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile @Inject(method = "copyFrom", at = @At("RETURN")) public void KnowledgeMixin(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) { - ((NebulaPlayer) This()).getSpellKnowledge().copyFrom(oldPlayer, alive); + ((NebulaPlayer) This()).getSpellKnowledgeManager().copyFrom(oldPlayer, alive); } @@ -39,7 +39,9 @@ public void mixin(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) @Inject(method = "onSpawn", at = @At("RETURN")) public void onDeathMixin(CallbackInfo ci) { - ServerPlayNetworking.send(This(), SynchronizeSpellKnowledgeS2CPacket.ID, SynchronizeSpellKnowledgeS2CPacket.create().write(PacketByteBufs.create())); + var buf = PacketByteBufs.create(); + SynchronizeSpellKnowledgeS2CPacket.create(this).write(buf); + ServerPlayNetworking.send(This(), SynchronizeSpellKnowledgeS2CPacket.getID(), buf); } diff --git a/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java b/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java index 1776226..7028ff2 100644 --- a/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java +++ b/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java @@ -2,22 +2,22 @@ import dev.louis.nebula.Nebula; import dev.louis.nebula.spell.Spell; +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; -public class SpellCastC2SPacket { - public static final Identifier ID = new Identifier(Nebula.MOD_ID, "spellcast"); - - private final Spell spell; - - public SpellCastC2SPacket(Spell spell) { - this.spell = spell; - } - public PacketByteBuf write(PacketByteBuf buf) { +public record SpellCastC2SPacket(Spell spell) implements FabricPacket { + public static final PacketType PACKET_TYPE = PacketType.create(new Identifier(Nebula.MOD_ID, "spellcast"), SynchronizeManaAmountS2CPacket::new); + public void write(PacketByteBuf buf) { buf.writeRegistryValue(Nebula.NebulaRegistries.SPELL_TYPE, spell.getType()); spell.writeBuf(buf); - return buf; + } + + @Override + public PacketType getType() { + return PACKET_TYPE; } public static SpellCastC2SPacket read(PlayerEntity caster, PacketByteBuf buf) { @@ -25,8 +25,7 @@ public static SpellCastC2SPacket read(PlayerEntity caster, PacketByteBuf buf) { spell.readBuf(buf); return new SpellCastC2SPacket(spell); } - - public Spell spell() { - return spell; + public static Identifier getId() { + return PACKET_TYPE.getId(); } } diff --git a/src/main/java/dev/louis/nebula/networking/SynchronizeManaAmountS2CPacket.java b/src/main/java/dev/louis/nebula/networking/SynchronizeManaAmountS2CPacket.java index cfde4a7..4cfc07a 100644 --- a/src/main/java/dev/louis/nebula/networking/SynchronizeManaAmountS2CPacket.java +++ b/src/main/java/dev/louis/nebula/networking/SynchronizeManaAmountS2CPacket.java @@ -1,26 +1,35 @@ package dev.louis.nebula.networking; import dev.louis.nebula.Nebula; +import dev.louis.nebula.api.NebulaPlayer; +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.PacketType; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; -public class SynchronizeManaAmountS2CPacket { - public static final Identifier ID = new Identifier(Nebula.MOD_ID, "spellcast"); +public record SynchronizeManaAmountS2CPacket(int mana) implements FabricPacket { + public static final PacketType PACKET_TYPE = PacketType.create(new Identifier(Nebula.MOD_ID, "synchronizemana"), SynchronizeManaAmountS2CPacket::new); - private final int mana; - - public SynchronizeManaAmountS2CPacket(int mana) { - this.mana = mana; + public SynchronizeManaAmountS2CPacket(PacketByteBuf buf) { + this(buf.readVarInt()); } - public PacketByteBuf write(PacketByteBuf buf) { + @Override + public void write(PacketByteBuf buf) { buf.writeVarInt(mana); - return buf; } - public static SynchronizeManaAmountS2CPacket read(PacketByteBuf buf) { - return new SynchronizeManaAmountS2CPacket(buf.readVarInt()); + @Override + public PacketType getType() { + return PACKET_TYPE; + } + + public static void receive(SynchronizeManaAmountS2CPacket packet, ClientPlayerEntity player, PacketSender responseSender) { + NebulaPlayer.access(player).setMana(packet.mana()); } - public int mana() { - return mana; + + public static Identifier getId() { + return PACKET_TYPE.getId(); } } diff --git a/src/main/java/dev/louis/nebula/networking/SynchronizeSpellKnowledgeS2CPacket.java b/src/main/java/dev/louis/nebula/networking/SynchronizeSpellKnowledgeS2CPacket.java index 3343a39..1af1ea6 100644 --- a/src/main/java/dev/louis/nebula/networking/SynchronizeSpellKnowledgeS2CPacket.java +++ b/src/main/java/dev/louis/nebula/networking/SynchronizeSpellKnowledgeS2CPacket.java @@ -3,44 +3,43 @@ import dev.louis.nebula.Nebula; import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketType; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; import java.util.HashMap; import java.util.Map; -public class SynchronizeSpellKnowledgeS2CPacket { - public static final Identifier ID = new Identifier(Nebula.MOD_ID, "syncspellknowledge"); +public record SynchronizeSpellKnowledgeS2CPacket(Map, Boolean> spells) implements FabricPacket { + public static final PacketType PACKET_TYPE = PacketType.create(new Identifier(Nebula.MOD_ID, "syncspellknowledge"), SynchronizeManaAmountS2CPacket::new); - private final Map, Boolean> spells; - - public SynchronizeSpellKnowledgeS2CPacket(Map, Boolean> spells) { - this.spells = Map.copyOf(spells); - } - private SynchronizeSpellKnowledgeS2CPacket(PacketByteBuf buf) { - Map, Boolean> map = new HashMap<>(); - int size = buf.readVarInt(); - for (int i = 0; i < size; i++) { - SpellType spellType = buf.readRegistryValue(Nebula.NebulaRegistries.SPELL_TYPE); - boolean knows = buf.readBoolean(); - map.put(spellType, knows); - } - this.spells = map; + public SynchronizeSpellKnowledgeS2CPacket(PacketByteBuf buf) { + this(readMap(buf)); } - public PacketByteBuf write(PacketByteBuf buf) { + public void write(PacketByteBuf buf) { buf.writeVarInt(spells.size()); spells.forEach((spellType, knows) -> { buf.writeRegistryValue(Nebula.NebulaRegistries.SPELL_TYPE, spellType); buf.writeBoolean(knows); }); - return buf; } - public static SynchronizeSpellKnowledgeS2CPacket create() { + @Override + public PacketType getType() { + return PACKET_TYPE; + } + + public static Identifier getID() { + return PACKET_TYPE.getId(); + } + + public static SynchronizeSpellKnowledgeS2CPacket create(PlayerEntity player) { Map, Boolean> map = new HashMap<>(); Nebula.NebulaRegistries.SPELL_TYPE.forEach(spellType -> { - map.put(spellType, true); + map.put(spellType, spellType.doesKnow(player)); }); return new SynchronizeSpellKnowledgeS2CPacket(map); } @@ -49,6 +48,17 @@ public static SynchronizeSpellKnowledgeS2CPacket read(PacketByteBuf buf) { return new SynchronizeSpellKnowledgeS2CPacket(buf); } + private static Map, Boolean> readMap(PacketByteBuf buf) { + Map, Boolean> spells = new HashMap<>(); + int size = buf.readVarInt(); + for (int i = 0; i < size; i++) { + SpellType spellType = buf.readRegistryValue(Nebula.NebulaRegistries.SPELL_TYPE); + boolean knows = buf.readBoolean(); + spells.put(spellType, knows); + } + return spells; + } + public Map, Boolean> spells() { return spells; }; diff --git a/src/main/java/dev/louis/nebula/spell/SpellType.java b/src/main/java/dev/louis/nebula/spell/SpellType.java index 7814bc0..b33c489 100644 --- a/src/main/java/dev/louis/nebula/spell/SpellType.java +++ b/src/main/java/dev/louis/nebula/spell/SpellType.java @@ -90,7 +90,7 @@ public boolean doesKnow(PlayerEntity player) { return doesKnow(NebulaPlayer.access(player)); } public boolean doesKnow(NebulaPlayer player) { - return player.getSpellKnowledge().doesKnow(this); + return player.getSpellKnowledgeManager().doesKnow(this); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 52d288e..e5eda28 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -28,7 +28,8 @@ "minecraft": ">=1.19.3" }, "custom": { - "nebula:contains_manamanager": true + "nebula:contains_manamanager": true, + "nebula:contains_spellknowledgemanager": true }, "accessWidener": "nebula.accesswidener" }