Skip to content

Commit

Permalink
Merge pull request #15 from Dev0Louis/dev
Browse files Browse the repository at this point in the history
3.2.2
  • Loading branch information
Dev0Louis authored Nov 1, 2023
2 parents 76af8cf + ae36778 commit 246cf2a
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 69 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/dev/louis/nebula/Nebula.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/dev/louis/nebula/api/NebulaPlayer.java
Original file line number Diff line number Diff line change
@@ -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!");
Expand All @@ -22,12 +19,4 @@ default SpellManager getSpellManager() {
default SpellManager setSpellManager(SpellManager spellManager) {
throw new UnsupportedOperationException("Injected Interface method was not overridden!");
}

default Collection<MultiTickSpell> setMultiTickSpells(Collection<MultiTickSpell> multiTickSpells) {
throw new UnsupportedOperationException("Injected Interface method was not overridden!");
}

default Collection<MultiTickSpell> getMultiTickSpells() {
throw new UnsupportedOperationException("Injected Interface method was not overridden!");
}
}
2 changes: 2 additions & 0 deletions src/main/java/dev/louis/nebula/mana/manager/ManaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 extends ManaManager> {
T createPlayerManaManager(PlayerEntity player);
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/dev/louis/nebula/mana/manager/NebulaManaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 15 additions & 28 deletions src/main/java/dev/louis/nebula/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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<MultiTickSpell> 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<MultiTickSpell> getMultiTickSpells() {
return this.multiTickSpells;
}

public Collection<MultiTickSpell> setMultiTickSpells(Collection<MultiTickSpell> multiTickSpells) {
return this.multiTickSpells = multiTickSpells;
public void onDeathSpellManager(DamageSource damageSource, CallbackInfo ci) {
this.spellManager.onDeath(damageSource);
}
// MultiTickSpell End
// Spell End
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Spell> spellType, PlayerEntity caster) {
private boolean shouldContinue = true;
public TickingSpell(SpellType<? extends TickingSpell> spellType, PlayerEntity caster) {
super(spellType, caster);
}

@Override
public void cast() {
this.getCaster().getMultiTickSpells().add(this);
this.getCaster().getSpellManager().startTickingSpell(this);
}

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +26,7 @@

public class NebulaSpellManager implements SpellManager {
PlayerEntity player;

private Set<TickingSpell> tickingSpells = new HashSet<>();
private Set<SpellType<? extends Spell>> castableSpells = new HashSet<>();

public NebulaSpellManager(PlayerEntity player) {
Expand All @@ -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<TickingSpell> 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<TickingSpell> tickingSpells) {
this.tickingSpells = tickingSpells;
}

public boolean isSpellTicking(SpellType<? extends Spell> spellType) {
return tickingSpells.stream().anyMatch(tickingSpell -> tickingSpell.getType().equals(spellType));
}

public boolean isSpellTicking(TickingSpell tickingSpell) {
return isSpellTicking(tickingSpell.getType());
}


@Override
public boolean addSpell(SpellType<? extends Spell> spellType) {
public boolean learnSpell(SpellType<? extends Spell> spellType) {
castableSpells.add(spellType);
sendSync();
return true;
}

@Override
public boolean removeSpell(SpellType<? extends Spell> spellType) {
public boolean forgetSpell(SpellType<? extends Spell> spellType) {
castableSpells.remove(spellType);
sendSync();
return true;
Expand Down Expand Up @@ -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<? extends Spell> spellType) {
return hasLearned(spellType) && spellType.hasEnoughMana(this.player);
}
Expand Down
Loading

0 comments on commit 246cf2a

Please sign in to comment.