From b0280324de867363df76c6de80eeefe8b213a504 Mon Sep 17 00:00:00 2001 From: D1p4k <82884007+D1p4k@users.noreply.github.com> Date: Sun, 17 Mar 2024 16:46:37 +0100 Subject: [PATCH] Maybe? --- .../louis/nebulo/spell/CloudJumpSpell.java | 5 ++-- src/main/java/dev/louis/nebula/Nebula.java | 1 + .../nebula/api/manager/mana/ManaManager.java | 2 -- .../dev/louis/nebula/api/spell/Spell.java | 28 +++-------------- .../dev/louis/nebula/api/spell/SpellType.java | 7 +++-- .../nebula/{ => manager}/NebulaManager.java | 3 +- .../manager/spell/NebulaSpellManager.java | 19 ++++-------- .../dev/louis/nebula/mixin/PlayerMixin.java | 2 +- .../nebula/networking/SpellCastC2SPacket.java | 30 +++++++++++-------- 9 files changed, 38 insertions(+), 59 deletions(-) rename src/main/java/dev/louis/nebula/{ => manager}/NebulaManager.java (98%) diff --git a/nebulo/src/main/java/dev/louis/nebulo/spell/CloudJumpSpell.java b/nebulo/src/main/java/dev/louis/nebulo/spell/CloudJumpSpell.java index 6ebc848..39cd079 100644 --- a/nebulo/src/main/java/dev/louis/nebulo/spell/CloudJumpSpell.java +++ b/nebulo/src/main/java/dev/louis/nebulo/spell/CloudJumpSpell.java @@ -2,14 +2,15 @@ import dev.louis.nebula.api.spell.Spell; import dev.louis.nebula.api.spell.SpellType; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.particle.ParticleTypes; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; public class CloudJumpSpell extends Spell { - public CloudJumpSpell(SpellType spellType) { - super(spellType); + public CloudJumpSpell(SpellType spellType, PlayerEntity player) { + super(spellType, player); } @Override diff --git a/src/main/java/dev/louis/nebula/Nebula.java b/src/main/java/dev/louis/nebula/Nebula.java index c5c2087..f32b940 100644 --- a/src/main/java/dev/louis/nebula/Nebula.java +++ b/src/main/java/dev/louis/nebula/Nebula.java @@ -3,6 +3,7 @@ import com.mojang.logging.LogUtils; import dev.louis.nebula.api.spell.SpellType; import dev.louis.nebula.command.NebulaCommand; +import dev.louis.nebula.manager.NebulaManager; import dev.louis.nebula.networking.SpellCastC2SPacket; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; diff --git a/src/main/java/dev/louis/nebula/api/manager/mana/ManaManager.java b/src/main/java/dev/louis/nebula/api/manager/mana/ManaManager.java index 1bbe3f2..1002dfa 100644 --- a/src/main/java/dev/louis/nebula/api/manager/mana/ManaManager.java +++ b/src/main/java/dev/louis/nebula/api/manager/mana/ManaManager.java @@ -47,8 +47,6 @@ public interface ManaManager { */ boolean hasEnoughMana(int mana); - //Thinking about removal as it would isolate the ManaManager from anything Spell related :) - @Deprecated /** * @param spellType The SpellType which should be checked. * @return If enough mana is available for the specified SpellType. diff --git a/src/main/java/dev/louis/nebula/api/spell/Spell.java b/src/main/java/dev/louis/nebula/api/spell/Spell.java index d723e84..b9e515e 100644 --- a/src/main/java/dev/louis/nebula/api/spell/Spell.java +++ b/src/main/java/dev/louis/nebula/api/spell/Spell.java @@ -2,7 +2,6 @@ import dev.louis.nebula.api.manager.spell.SpellManager; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; /** @@ -12,16 +11,17 @@ public abstract class Spell { private static final int DEFAULT_SPELL_AGE = 3 * 20; - private final SpellType spellType; - private PlayerEntity caster; + protected final SpellType spellType; + protected final PlayerEntity caster; protected boolean wasInterrupted; protected boolean stopped; public int age = 0; - public Spell(SpellType spellType) { + public Spell(SpellType spellType, PlayerEntity caster) { this.spellType = spellType; + this.caster = caster; } /** @@ -53,10 +53,6 @@ public SpellType getType() { return this.spellType; } - public int getAge() { - return this.age; - } - public int getDuration() { return DEFAULT_SPELL_AGE; } @@ -69,10 +65,6 @@ public int getDuration() { public void finish() { } - public void setCaster(PlayerEntity caster) { - this.caster = caster; - } - /** * Used to check if the spell should be stopped.
* It is important to check super or check if the spell was interrupted {@link Spell#wasInterrupted()}.
@@ -98,8 +90,6 @@ public final void interrupt() { *
* Unlike {@link Spell#interrupt()} if you call this method it is expected that the spell has finished execution.
*/ - //Thinking about possible change. - @Deprecated protected final void stop() { this.stopped = true; } @@ -121,16 +111,6 @@ public boolean wasInterrupted() { return this.wasInterrupted; } - /** - * Write additional casting data about the spell to the buf. - * @param buf The buf to be written to. - * @return The buf after being written to. - */ - public PacketByteBuf writeBuf(PacketByteBuf buf) { - return buf; - } - - @Override public String toString() { return this.getClass().getSimpleName() + diff --git a/src/main/java/dev/louis/nebula/api/spell/SpellType.java b/src/main/java/dev/louis/nebula/api/spell/SpellType.java index 827c3c9..63bb882 100644 --- a/src/main/java/dev/louis/nebula/api/spell/SpellType.java +++ b/src/main/java/dev/louis/nebula/api/spell/SpellType.java @@ -68,8 +68,8 @@ public int getManaCost() { return manaCost; } - public T create() { - return this.spellFactory.create(this); + public T create(PlayerEntity caster) { + return this.spellFactory.create(this, caster); } @Override @@ -134,7 +134,8 @@ public SpellType build() { @FunctionalInterface public interface SpellFactory { - T create(SpellType spellType); + + T create(SpellType spellType, PlayerEntity caster); } @FunctionalInterface diff --git a/src/main/java/dev/louis/nebula/NebulaManager.java b/src/main/java/dev/louis/nebula/manager/NebulaManager.java similarity index 98% rename from src/main/java/dev/louis/nebula/NebulaManager.java rename to src/main/java/dev/louis/nebula/manager/NebulaManager.java index ea7157e..955f1bb 100644 --- a/src/main/java/dev/louis/nebula/NebulaManager.java +++ b/src/main/java/dev/louis/nebula/manager/NebulaManager.java @@ -1,5 +1,6 @@ -package dev.louis.nebula; +package dev.louis.nebula.manager; +import dev.louis.nebula.Nebula; import dev.louis.nebula.api.manager.mana.ManaManager; import dev.louis.nebula.api.manager.mana.entrypoint.RegisterManaManagerEntrypoint; import dev.louis.nebula.api.manager.mana.registerable.ManaManagerRegistrableView; diff --git a/src/main/java/dev/louis/nebula/manager/spell/NebulaSpellManager.java b/src/main/java/dev/louis/nebula/manager/spell/NebulaSpellManager.java index f9a861c..aba7370 100644 --- a/src/main/java/dev/louis/nebula/manager/spell/NebulaSpellManager.java +++ b/src/main/java/dev/louis/nebula/manager/spell/NebulaSpellManager.java @@ -73,18 +73,9 @@ public boolean forgetSpell(SpellType spellType) { return shouldSync; } - /** - * This Method does not return an unmodifiable set. - * So that it could theoretically be modified. - */ - protected Set> getCastableSpells() { - return learnedSpells; - } - @Override public boolean cast(SpellType spellType) { - var spell = spellType.create(); - spell.setCaster(this.player); + var spell = spellType.create(this.player); return this.cast(spell); } @@ -97,9 +88,9 @@ public boolean cast(Spell spell) { if (this.isServer()) { spell.applyCost(); spell.cast(); - this.activeSpells.add(spell); + if (spell.getDuration() > 0) this.activeSpells.add(spell); } else { - ClientPlayNetworking.send(new SpellCastC2SPacket(spell)); + ClientPlayNetworking.send(new SpellCastC2SPacket(spell.getType())); } return true; } @@ -171,9 +162,9 @@ public static boolean receiveSync(UpdateSpellCastabilityS2CPacket packet, Client @Override public void writeNbt(NbtCompound nbt) { NbtList nbtList = new NbtList(); - for (SpellType spell : getCastableSpells()) { + for (SpellType spellType : this.getLearnedSpells()) { NbtCompound nbtCompound = new NbtCompound(); - nbtCompound.putString(SPELL_NBT_KEY, spell.getId().toString()); + nbtCompound.putString(SPELL_NBT_KEY, spellType.getId().toString()); nbtList.add(nbtCompound); } diff --git a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java index 5458c0e..67b6261 100644 --- a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java @@ -1,9 +1,9 @@ package dev.louis.nebula.mixin; -import dev.louis.nebula.NebulaManager; import dev.louis.nebula.api.NebulaPlayer; import dev.louis.nebula.api.manager.mana.ManaManager; import dev.louis.nebula.api.manager.spell.SpellManager; +import dev.louis.nebula.manager.NebulaManager; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.damage.DamageSource; diff --git a/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java b/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java index 24f7cde..4a7f586 100644 --- a/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java +++ b/src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java @@ -1,7 +1,6 @@ package dev.louis.nebula.networking; import dev.louis.nebula.Nebula; -import dev.louis.nebula.api.spell.Spell; import dev.louis.nebula.api.spell.SpellType; import net.fabricmc.fabric.api.networking.v1.FabricPacket; import net.fabricmc.fabric.api.networking.v1.PacketSender; @@ -13,19 +12,23 @@ /** * Not a FabricPacket because of Restriction in the api. */ -public record SpellCastC2SPacket(Spell spell) implements FabricPacket { - public static final Identifier ID = new Identifier(Nebula.MOD_ID, "spellcast"); +public class SpellCastC2SPacket implements FabricPacket { + private static final Identifier ID = new Identifier(Nebula.MOD_ID, "spellcast"); public static final PacketType TYPE = PacketType.create(ID, SpellCastC2SPacket::read); + public final SpellType spellType; + + public SpellCastC2SPacket(SpellType spellType) { + this.spellType = spellType; + } public static SpellCastC2SPacket read(PacketByteBuf buf) { SpellType spellType = buf.readRegistryValue(SpellType.REGISTRY); - if(spellType == null) throw new IllegalStateException("Spell type not found in registry"); - Spell spell = spellType.create(); - return new SpellCastC2SPacket(spell); + if (spellType == null) throw new IllegalStateException("Spell type not found in registry"); + return new SpellCastC2SPacket(spellType); } public void write(PacketByteBuf buf) { - buf.writeRegistryValue(SpellType.REGISTRY, spell.getType()); + buf.writeRegistryValue(SpellType.REGISTRY, spellType); } @Override @@ -34,10 +37,13 @@ public PacketType getType() { } public static void receive(SpellCastC2SPacket packet, ServerPlayerEntity player, PacketSender responseSender) { - var spell = packet.spell(); - spell.setCaster(player); - player.server.executeSync(() -> { - player.getSpellManager().cast(spell); - }); + player.server.executeSync(() -> player.getSpellManager().cast(packet.spellType)); + } + + @Override + public String toString() { + return "SpellCastC2SPacket[" + + "spellType=" + spellType + ']'; } + }