Skip to content

Commit

Permalink
Merge pull request #842 from Chronoken/main
Browse files Browse the repository at this point in the history
Fix showing mana on the food bar.
  • Loading branch information
Chronoken authored Feb 3, 2024
2 parents 2f5d8e6 + 63a73c0 commit 0f41df4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;

import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;

Expand All @@ -30,6 +31,7 @@
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.spells.BowSpell;
import com.nisovin.magicspells.mana.ManaSystem;

public class CastListener implements Listener {

Expand Down Expand Up @@ -172,6 +174,14 @@ public void onPlayerDrop(PlayerDropItemEvent event) {
noCastUntil.put(event.getPlayer().getName(), System.currentTimeMillis() + 150);
}

@EventHandler(ignoreCancelled = true)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
Player player = (Player) event.getEntity();
if (!(MagicSpells.getManaHandler() instanceof ManaSystem system)) return;
if (!system.usingHungerBar()) return;
MagicSpells.scheduleDelayedTask(() -> system.showMana(player), 1);
}

private void castSpell(Player player, ItemStack item) {
Spellbook spellbook = MagicSpells.getSpellbook(player);
Spell spell = spellbook.getActiveSpell(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ private void showManaInChat(Player player, ManaBar bar) {
}

private void showManaOnHungerBar(Player player, ManaBar bar) {
player.setFoodLevel(Math.round(((float) bar.getMana() / (float) bar.getMaxMana()) * 20));
player.setSaturation(20);
int food = Math.round(((float) bar.getMana() / (float) bar.getMaxMana()) * 20);
MagicSpells.getVolatileCodeHandler().sendStatusUpdate(player, player.getHealth(), food, player.getSaturation());
}

private void showManaOnActionBar(Player player, ManaBar bar) {
Expand Down Expand Up @@ -304,7 +304,7 @@ public void run() {
Iterator<ManaBar> manaBarIterator = manaBars.values().iterator();
ManaBar manaBar;
Player player;
while(manaBarIterator.hasNext()) {
while (manaBarIterator.hasNext()) {
manaBar = manaBarIterator.next();
if (!manaBar.getManaRank().equals(rank)) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public void sendToastEffect(Player receiver, ItemStack icon, Frame frameType, Co

}

@Override
public void sendStatusUpdate(Player player, double health, int food, float saturation) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ public abstract Recipe createSmithingRecipe(

public abstract void sendToastEffect(Player receiver, ItemStack icon, Frame frameType, Component text);

public abstract void sendStatusUpdate(Player player, double health, int food, float saturation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ class VolatileCode1_20_R2(helper: VolatileCodeHelper) : VolatileCodeHandle(helpe
))
}

override fun sendStatusUpdate(player: Player?, health: Double, food: Int, saturation: Float) {
(player as CraftPlayer).handle.connection.send(ClientboundSetHealthPacket(health.toFloat(), food, saturation))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ class VolatileCode1_20_R3(helper: VolatileCodeHelper) : VolatileCodeHandle(helpe
))
}

override fun sendStatusUpdate(player: Player?, health: Double, food: Int, saturation: Float) {
(player as CraftPlayer).handle.connection.send(ClientboundSetHealthPacket(health.toFloat(), food, saturation))
}

}

0 comments on commit 0f41df4

Please sign in to comment.