Skip to content

Commit

Permalink
Merge pull request #25 from Dev0Louis/dev
Browse files Browse the repository at this point in the history
4.0.3-beta.1 merging :)
  • Loading branch information
Dev0Louis authored Jan 3, 2024
2 parents fb18934 + 260b856 commit 56af686
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ org.gradle.jvmargs=-Xmx1G
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.2
loader_version=0.15.3
# Mod Properties
mod_version=4.0.2
mod_version=4.0.3-beta.1
maven_group=dev.louis
archives_base_name=Nebula
# Dependencies
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/louis/nebula/NebulaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void registerPacketReceivers() {
registerReceiver(SynchronizeManaAmountS2CPacket.getId(), SynchronizeManaAmountS2CPacket::receive);
}

public static void runWithBuf(MinecraftClient client, PacketByteBuf buf, Runnable runnable) {
public static void runSyncWithBuf(MinecraftClient client, PacketByteBuf buf, Runnable runnable) {
retainer.accept(buf);
client.executeSync(() -> {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

import static dev.louis.nebula.NebulaClient.runWithBuf;
import static dev.louis.nebula.NebulaClient.runSyncWithBuf;

public record SynchronizeManaAmountS2CPacket(int mana) implements FabricPacket {
public static final PacketType<SynchronizeManaAmountS2CPacket> PACKET_TYPE = PacketType.create(new Identifier(Nebula.MOD_ID, "synchronizemana"), SynchronizeManaAmountS2CPacket::new);
Expand All @@ -28,7 +28,7 @@ public PacketType<?> getType() {
}

public static void receive(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
runWithBuf(client, buf, () -> client.player.getManaManager().receiveSync(client, handler, buf, responseSender));
runSyncWithBuf(client, buf, () -> client.player.getManaManager().receiveSync(client, handler, buf, responseSender));
}

public static Identifier getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.HashMap;
import java.util.Map;

import static dev.louis.nebula.NebulaClient.runWithBuf;
import static dev.louis.nebula.NebulaClient.runSyncWithBuf;

public record UpdateSpellCastabilityS2CPacket(Map<SpellType<? extends Spell>, Boolean> spells) implements FabricPacket {
public static final PacketType<UpdateSpellCastabilityS2CPacket> PACKET_TYPE = PacketType.create(new Identifier(Nebula.MOD_ID, "updatespellcastability"), UpdateSpellCastabilityS2CPacket::new);
Expand Down Expand Up @@ -59,7 +59,7 @@ public PacketType<?> getType() {
}

public static void receive(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
runWithBuf(client, buf, () -> client.player.getSpellManager().receiveSync(client, handler, buf, responseSender));
runSyncWithBuf(client, buf, () -> client.player.getSpellManager().receiveSync(client, handler, buf, responseSender));
}

public static Identifier getID() {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/dev/louis/nebula/spell/SpellType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
public class SpellType<T extends Spell> {
private final SpellFactory<T> factory;
private final int manaCost;
private final boolean needLearning;

public SpellType(SpellFactory<T> factory, int manaCost) {
public SpellType(SpellFactory<T> factory, int manaCost, boolean needLearning) {
this.factory = factory;
this.manaCost = manaCost;
this.needLearning = needLearning;
}

public static void init() {
Expand All @@ -35,6 +37,10 @@ public boolean isCastable(PlayerEntity player) {
return player.getSpellManager().isCastable(this);
}

public boolean needsLearning() {
return needLearning;
}

public boolean hasLearned(PlayerEntity player) {
return player.getSpellManager().hasLearned(this);
}
Expand All @@ -55,6 +61,7 @@ public String toString() {
public static class Builder<T extends Spell> {
private final SpellFactory<T> factory;
private final int manaCost;
private boolean needsLearning = true;

private Builder(SpellFactory<T> factory, int manaCost) {
this.factory = factory;
Expand All @@ -65,8 +72,13 @@ public static <T extends Spell> Builder<T> create(SpellFactory<T> factory, int m
return new Builder<>(factory, manaCost);
}

public Builder<T> needsLearning(boolean needsLearning) {
this.needsLearning = needsLearning;
return this;
}

public SpellType<T> build() {
return new SpellType<>(this.factory, manaCost);
return new SpellType<>(this.factory, manaCost, needsLearning);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public NebulaSpellManager(PlayerEntity player) {

@Override
public void tick() {
this.tickingSpells.removeIf(multiTickSpell -> {
boolean willBeRemoved = !multiTickSpell.shouldContinue();
if(willBeRemoved) multiTickSpell.stop();
this.tickingSpells.removeIf(tickingSpell -> {
boolean willBeRemoved = !tickingSpell.shouldContinue();
if(willBeRemoved) tickingSpell.stop();
return willBeRemoved;
});
for (TickingSpell tickingSpell : this.tickingSpells) {
Expand Down Expand Up @@ -70,15 +70,15 @@ public boolean isSpellTicking(TickingSpell tickingSpell) {

@Override
public boolean learnSpell(SpellType<?> spellType) {
this.castableSpells.add(spellType);
this.sendSync();
boolean shouldSync = this.castableSpells.add(spellType);
if(shouldSync) this.sendSync();
return true;
}

@Override
public boolean forgetSpell(SpellType<?> spellType) {
this.castableSpells.remove(spellType);
this.sendSync();
boolean shouldSync = this.castableSpells.remove(spellType);
if(shouldSync) this.sendSync();
return true;
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public void onDeath(DamageSource damageSource) {
}

public boolean isCastable(SpellType<?> spellType) {
return hasLearned(spellType) && ((player.getManaManager().getMana() - spellType.getManaCost()) >= 0);
return player.getManaManager().getMana() - spellType.getManaCost() >= 0 && (!spellType.needsLearning() || this.hasLearned(spellType));
}

@Override
Expand All @@ -134,7 +134,7 @@ public boolean hasLearned(SpellType<?> spellType) {
public boolean sendSync() {
if(this.player instanceof ServerPlayerEntity serverPlayerEntity && serverPlayerEntity.networkHandler != null) {
Map<SpellType<?>, Boolean> castableSpells = new HashMap<>();
Nebula.SPELL_REGISTRY.forEach(spellType -> castableSpells.put(spellType, hasLearned(spellType)));
Nebula.SPELL_REGISTRY.forEach(spellType -> castableSpells.put(spellType, this.hasLearned(spellType)));
ServerPlayNetworking.send(serverPlayerEntity, new UpdateSpellCastabilityS2CPacket(castableSpells));
return true;
}
Expand Down

0 comments on commit 56af686

Please sign in to comment.