Skip to content

Commit

Permalink
Maybe?
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev0Louis committed Mar 17, 2024
1 parent 8ffc948 commit b028032
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/java/dev/louis/nebula/Nebula.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 4 additions & 24 deletions src/main/java/dev/louis/nebula/api/spell/Spell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -53,10 +53,6 @@ public SpellType<? extends Spell> getType() {
return this.spellType;
}

public int getAge() {
return this.age;
}

public int getDuration() {
return DEFAULT_SPELL_AGE;
}
Expand All @@ -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. <br>
* It is important to check super or check if the spell was interrupted {@link Spell#wasInterrupted()}. <br>
Expand All @@ -98,8 +90,6 @@ public final void interrupt() {
* <br>
* Unlike {@link Spell#interrupt()} if you call this method it is expected that the spell has finished execution.<br>
*/
//Thinking about possible change.
@Deprecated
protected final void stop() {
this.stopped = true;
}
Expand All @@ -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() +
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/dev/louis/nebula/api/spell/SpellType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,7 +134,8 @@ public SpellType<T> build() {

@FunctionalInterface
public interface SpellFactory<T extends Spell> {
T create(SpellType<T> spellType);

T create(SpellType<T> spellType, PlayerEntity caster);
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpellType<?>> 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);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/louis/nebula/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/dev/louis/nebula/networking/SpellCastC2SPacket.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SpellCastC2SPacket> 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
Expand All @@ -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 + ']';
}

}

0 comments on commit b028032

Please sign in to comment.