From ae3677807f11a5f07a0144a9423d84322d60deca Mon Sep 17 00:00:00 2001 From: D1p4k <82884007+D1p4k@users.noreply.github.com> Date: Thu, 2 Nov 2023 00:41:36 +0100 Subject: [PATCH] 3.2.2 --- gradle.properties | 2 +- src/main/java/dev/louis/nebula/Nebula.java | 7 +-- .../dev/louis/nebula/api/NebulaPlayer.java | 11 ----- .../nebula/mana/manager/ManaManager.java | 2 + .../mana/manager/NebulaManaManager.java | 27 +++++++---- .../dev/louis/nebula/mixin/PlayerMixin.java | 43 ++++++----------- .../nebula/mixin/ServerPlayerEntityMixin.java | 3 -- ...{MultiTickSpell.java => TickingSpell.java} | 18 +++++--- .../spell/manager/NebulaSpellManager.java | 46 +++++++++++++++++-- .../nebula/spell/manager/SpellManager.java | 17 ++++++- src/main/resources/fabric.mod.json | 2 +- 11 files changed, 109 insertions(+), 69 deletions(-) rename src/main/java/dev/louis/nebula/spell/{MultiTickSpell.java => TickingSpell.java} (52%) diff --git a/gradle.properties b/gradle.properties index 15b77d6..6d972a8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.20.2 yarn_mappings=1.20.2+build.1 loader_version=0.14.22 # Mod Properties -mod_version=3.2.1 +mod_version=3.2.2 maven_group=dev.louis archives_base_name=Nebula # Dependencies diff --git a/src/main/java/dev/louis/nebula/Nebula.java b/src/main/java/dev/louis/nebula/Nebula.java index a63a658..968e597 100644 --- a/src/main/java/dev/louis/nebula/Nebula.java +++ b/src/main/java/dev/louis/nebula/Nebula.java @@ -1,5 +1,6 @@ package dev.louis.nebula; +import com.mojang.logging.LogUtils; import dev.louis.nebula.command.NebulaCommand; import dev.louis.nebula.networking.SpellCastC2SPacket; import dev.louis.nebula.spell.SpellType; @@ -12,17 +13,13 @@ import net.minecraft.registry.SimpleRegistry; import net.minecraft.util.Identifier; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class Nebula implements ModInitializer { public static final String MOD_ID = "nebula"; - public static final Logger LOGGER = LoggerFactory.getLogger("Nebula"); - - public static Nebula INSTANCE; + public static final Logger LOGGER = LogUtils.getLogger(); @Override public void onInitialize() { - INSTANCE = this; SpellType.init(); registerPacketReceivers(); NebulaRegistries.init(); diff --git a/src/main/java/dev/louis/nebula/api/NebulaPlayer.java b/src/main/java/dev/louis/nebula/api/NebulaPlayer.java index 9b8f0fb..96cb644 100644 --- a/src/main/java/dev/louis/nebula/api/NebulaPlayer.java +++ b/src/main/java/dev/louis/nebula/api/NebulaPlayer.java @@ -1,11 +1,8 @@ package dev.louis.nebula.api; import dev.louis.nebula.mana.manager.ManaManager; -import dev.louis.nebula.spell.MultiTickSpell; import dev.louis.nebula.spell.manager.SpellManager; -import java.util.Collection; - public interface NebulaPlayer { default ManaManager getManaManager() { throw new UnsupportedOperationException("Injected Interface method was not overridden!"); @@ -22,12 +19,4 @@ default SpellManager getSpellManager() { default SpellManager setSpellManager(SpellManager spellManager) { throw new UnsupportedOperationException("Injected Interface method was not overridden!"); } - - default Collection setMultiTickSpells(Collection multiTickSpells) { - throw new UnsupportedOperationException("Injected Interface method was not overridden!"); - } - - default Collection getMultiTickSpells() { - throw new UnsupportedOperationException("Injected Interface method was not overridden!"); - } } diff --git a/src/main/java/dev/louis/nebula/mana/manager/ManaManager.java b/src/main/java/dev/louis/nebula/mana/manager/ManaManager.java index 8853145..b5b454e 100644 --- a/src/main/java/dev/louis/nebula/mana/manager/ManaManager.java +++ b/src/main/java/dev/louis/nebula/mana/manager/ManaManager.java @@ -3,6 +3,7 @@ import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; @@ -19,6 +20,7 @@ public interface ManaManager { void writeNbt(NbtCompound nbt); void readNbt(NbtCompound nbt); void copyFrom(PlayerEntity oldPlayer, boolean alive); + void onDeath(DamageSource damageSource); @FunctionalInterface interface Factory { T createPlayerManaManager(PlayerEntity player); diff --git a/src/main/java/dev/louis/nebula/mana/manager/NebulaManaManager.java b/src/main/java/dev/louis/nebula/mana/manager/NebulaManaManager.java index c89af3e..4289b55 100644 --- a/src/main/java/dev/louis/nebula/mana/manager/NebulaManaManager.java +++ b/src/main/java/dev/louis/nebula/mana/manager/NebulaManaManager.java @@ -6,6 +6,7 @@ import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; @@ -17,8 +18,7 @@ public NebulaManaManager(PlayerEntity player) { this.player = player; } private int mana = 0; - //TODO: Implement this: - //private int lastSyncedMana = getMana(); + private int lastSyncedMana = -1; @Override @@ -53,12 +53,18 @@ public int getMaxMana() { @Override public boolean sendSync() { - if(this.player instanceof ServerPlayerEntity serverPlayerEntity && serverPlayerEntity.networkHandler != null) { - ServerPlayNetworking.send( - serverPlayerEntity, - new SynchronizeManaAmountS2CPacket(player.getManaManager().getMana()) - ); - return true; + if (this.player instanceof ServerPlayerEntity serverPlayerEntity) { + if (serverPlayerEntity.networkHandler != null) { + int mana = this.player.getManaManager().getMana(); + if (mana == this.lastSyncedMana) return true; + ServerPlayNetworking.send( + serverPlayerEntity, + new SynchronizeManaAmountS2CPacket(mana) + ); + this.lastSyncedMana = mana; + return true; + } + Nebula.LOGGER.error("sendSync was called to early for " + serverPlayerEntity.getEntityName()); } return false; } @@ -91,6 +97,11 @@ public void copyFrom(PlayerEntity oldPlayer, boolean alive) { } } + @Override + public void onDeath(DamageSource damageSource) { + //Nothing here :) + } + public NebulaManaManager setPlayer(PlayerEntity player) { this.player = player; return this; diff --git a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java index fdd0049..614ead4 100644 --- a/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/PlayerMixin.java @@ -3,7 +3,6 @@ import dev.louis.nebula.NebulaManager; import dev.louis.nebula.api.NebulaPlayer; import dev.louis.nebula.mana.manager.ManaManager; -import dev.louis.nebula.spell.MultiTickSpell; import dev.louis.nebula.spell.manager.SpellManager; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; @@ -12,15 +11,15 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.util.ArrayList; -import java.util.Collection; - @Mixin(PlayerEntity.class) public abstract class PlayerMixin extends LivingEntity implements NebulaPlayer { + @Shadow public abstract boolean shouldDamagePlayer(PlayerEntity player); + protected PlayerMixin(EntityType entityType, World world) { super(entityType, world); } @@ -53,12 +52,18 @@ public ManaManager setManaManager(ManaManager manaManager) { return this.manaManager = manaManager; } + @Inject(method = "onDeath", at = @At("HEAD")) + public void onDeathManaManger(DamageSource damageSource, CallbackInfo ci) { + this.manaManager.onDeath(damageSource); + } + // Mana End // Spell Start private SpellManager spellManager = NebulaManager.createSpellManager((PlayerEntity) (Object) this); + @Inject(method = "writeCustomDataToNbt", at = @At(value = "TAIL")) public void addSpellsToNbt(NbtCompound nbt, CallbackInfo ci) { this.spellManager.writeNbt(nbt); @@ -68,43 +73,25 @@ public void addSpellsToNbt(NbtCompound nbt, CallbackInfo ci) { public void getSpellsFromNbt(NbtCompound nbt, CallbackInfo ci) { spellManager.readNbt(nbt); } + @Inject(method = "tick", at = @At("RETURN")) public void tickSpellManager(CallbackInfo ci) { spellManager.tick(); } + @Override public SpellManager getSpellManager() { return this.spellManager; } + @Override public SpellManager setSpellManager(SpellManager spellManager) { return this.spellManager = spellManager; } -// Spell End - -// MultiTickSpell Start - private Collection multiTickSpells = new ArrayList<>(); - @Inject(method = "tick", at = @At("HEAD")) - public void tickMultiTickSpells(CallbackInfo ci) { - multiTickSpells.removeIf(multiTickSpell -> !multiTickSpell.shouldContinue()); - for (MultiTickSpell multiTickSpell : multiTickSpells) { - multiTickSpell.tick(); - } - } @Inject(method = "onDeath", at = @At("HEAD")) - public void stopMultiTickSpellsOnDeaths(DamageSource damageSource, CallbackInfo ci) { - for(MultiTickSpell multiTickSpell : multiTickSpells) { - multiTickSpell.stop(true); - } - } - - public Collection getMultiTickSpells() { - return this.multiTickSpells; - } - - public Collection setMultiTickSpells(Collection multiTickSpells) { - return this.multiTickSpells = multiTickSpells; + public void onDeathSpellManager(DamageSource damageSource, CallbackInfo ci) { + this.spellManager.onDeath(damageSource); } -// MultiTickSpell End +// Spell End } diff --git a/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java b/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java index 3561ade..48d3ea1 100644 --- a/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/dev/louis/nebula/mixin/ServerPlayerEntityMixin.java @@ -21,9 +21,6 @@ public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile public void copyNebulaStuffFrom(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) { this.getSpellManager().copyFrom(oldPlayer, alive); this.getManaManager().copyFrom(oldPlayer, alive); - if(alive) { - this.setMultiTickSpells(oldPlayer.getMultiTickSpells()); - } } @Inject(method = "onSpawn", at = @At("RETURN")) diff --git a/src/main/java/dev/louis/nebula/spell/MultiTickSpell.java b/src/main/java/dev/louis/nebula/spell/TickingSpell.java similarity index 52% rename from src/main/java/dev/louis/nebula/spell/MultiTickSpell.java rename to src/main/java/dev/louis/nebula/spell/TickingSpell.java index 5531055..751e69d 100644 --- a/src/main/java/dev/louis/nebula/spell/MultiTickSpell.java +++ b/src/main/java/dev/louis/nebula/spell/TickingSpell.java @@ -2,16 +2,16 @@ import net.minecraft.entity.player.PlayerEntity; -public class MultiTickSpell extends Spell { +public class TickingSpell extends Spell { protected int spellAge = 0; - private boolean shouldRun = true; - public MultiTickSpell(SpellType spellType, PlayerEntity caster) { + private boolean shouldContinue = true; + public TickingSpell(SpellType spellType, PlayerEntity caster) { super(spellType, caster); } @Override public void cast() { - this.getCaster().getMultiTickSpells().add(this); + this.getCaster().getSpellManager().startTickingSpell(this); } /** @@ -22,12 +22,18 @@ public void tick() { spellAge++; } public void stop(boolean fromDeath) { - shouldRun = false; + shouldContinue = false; } + /** + * This method stops the spell! + * The Spell will no longer be ticked. + * Calling this method assumes the Caster is still alive. + * If that is not the case call {@link #stop(boolean)}. + */ public void stop() { stop(false); } public boolean shouldContinue() { - return shouldRun; + return shouldContinue; } } diff --git a/src/main/java/dev/louis/nebula/spell/manager/NebulaSpellManager.java b/src/main/java/dev/louis/nebula/spell/manager/NebulaSpellManager.java index 2407512..0c3b433 100644 --- a/src/main/java/dev/louis/nebula/spell/manager/NebulaSpellManager.java +++ b/src/main/java/dev/louis/nebula/spell/manager/NebulaSpellManager.java @@ -5,10 +5,12 @@ import dev.louis.nebula.networking.UpdateSpellCastabilityS2CPacket; import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; +import dev.louis.nebula.spell.TickingSpell; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtList; @@ -24,7 +26,7 @@ public class NebulaSpellManager implements SpellManager { PlayerEntity player; - + private Set tickingSpells = new HashSet<>(); private Set> castableSpells = new HashSet<>(); public NebulaSpellManager(PlayerEntity player) { @@ -33,17 +35,47 @@ public NebulaSpellManager(PlayerEntity player) { @Override - public void tick() {} + public void tick() { + this.tickingSpells.removeIf(multiTickSpell -> !multiTickSpell.shouldContinue()); + for (TickingSpell tickingSpell : this.tickingSpells) { + tickingSpell.tick(); + } + } + + public Set getTickingSpells() { + return this.tickingSpells; + } + + public boolean startTickingSpell(TickingSpell tickingSpell) { + return tickingSpells.add(tickingSpell); + } + + public boolean stopTickingSpell(TickingSpell tickingSpell) { + return tickingSpells.add(tickingSpell); + } + + public void setTickingSpells(Set tickingSpells) { + this.tickingSpells = tickingSpells; + } + + public boolean isSpellTicking(SpellType spellType) { + return tickingSpells.stream().anyMatch(tickingSpell -> tickingSpell.getType().equals(spellType)); + } + + public boolean isSpellTicking(TickingSpell tickingSpell) { + return isSpellTicking(tickingSpell.getType()); + } + @Override - public boolean addSpell(SpellType spellType) { + public boolean learnSpell(SpellType spellType) { castableSpells.add(spellType); sendSync(); return true; } @Override - public boolean removeSpell(SpellType spellType) { + public boolean forgetSpell(SpellType spellType) { castableSpells.remove(spellType); sendSync(); return true; @@ -84,9 +116,15 @@ public void cast(Spell spell) { public void copyFrom(PlayerEntity oldPlayer, boolean alive) { if(alive) { setCastableSpells(getNebulaSpellmanager(oldPlayer).getCastableSpells()); + setTickingSpells(getNebulaSpellmanager(oldPlayer).getTickingSpells()); } } + @Override + public void onDeath(DamageSource damageSource) { + this.tickingSpells.forEach((tickingSpell -> tickingSpell.stop(true))); + } + public boolean isCastable(SpellType spellType) { return hasLearned(spellType) && spellType.hasEnoughMana(this.player); } diff --git a/src/main/java/dev/louis/nebula/spell/manager/SpellManager.java b/src/main/java/dev/louis/nebula/spell/manager/SpellManager.java index c902ffd..797bd35 100644 --- a/src/main/java/dev/louis/nebula/spell/manager/SpellManager.java +++ b/src/main/java/dev/louis/nebula/spell/manager/SpellManager.java @@ -2,24 +2,37 @@ import dev.louis.nebula.spell.Spell; import dev.louis.nebula.spell.SpellType; +import dev.louis.nebula.spell.TickingSpell; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; +import java.util.Set; + public interface SpellManager { void tick(); - boolean addSpell(SpellType spellType); - boolean removeSpell(SpellType spellType); + boolean startTickingSpell(TickingSpell tickingSpell); + + + boolean stopTickingSpell(TickingSpell tickingSpell); + void setTickingSpells(Set tickingSpells); + boolean isSpellTicking(SpellType spellType); + boolean isSpellTicking(TickingSpell tickingSpell); + + boolean learnSpell(SpellType spellType); + boolean forgetSpell(SpellType spellType); void cast(PlayerEntity player, SpellType spellType); void cast(Spell spell); void copyFrom(PlayerEntity oldNebulaPlayer, boolean alive); + void onDeath(DamageSource damageSource); boolean isCastable(SpellType spellType); boolean hasLearned(SpellType spellType); boolean sendSync(); diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 8a8472f..8daaa45 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -25,7 +25,7 @@ "depends": { "fabricloader": ">=0.14.9", "fabric": "*", - "minecraft": ">=1.20" + "minecraft": ">=1.20.2" }, "custom": { "nebula:contains_mana_manager": true,