SR-Game/src/main/java/me/unurled/sacredrealms/sr/utils/SRPlayerUtils.java
unurled a3fbcbf32a
Some checks failed
Build / build (push) Failing after 56s
Format / formatting (push) Failing after 1m2s
fix: switch to 2.0 invui and translation
2025-06-25 23:18:12 +02:00

132 lines
4.6 KiB
Java

package me.unurled.sacredrealms.sr.utils;
import static me.unurled.sacredrealms.sr.utils.component.Component.comp;
import java.util.List;
import java.util.Random;
import me.unurled.sacredrealms.sr.Manager;
import me.unurled.sacredrealms.sr.SR;
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Display;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TextDisplay;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
public class SRPlayerUtils {
private static final Random RANDOM = new Random();
private SRPlayerUtils() {}
public static void syncPlayerToSR(@NotNull Player p, @NotNull SRPlayer sr) {
sr.setInventory(p.getInventory());
sr.setPotionEffects((List<PotionEffect>) p.getActivePotionEffects());
}
public static void syncSRToPlayer(@NotNull SRPlayer sr, @NotNull Player p) {
p.getInventory().setContents(sr.getInventory().getContents());
// agility (speed)
// default is 100 so 0.2
double items = sr.getTotalItemAttribute(Attribute.AGILITY);
double speed = sr.getAttribute(Attribute.AGILITY);
float value = (float) ((speed + items) / 500.0f);
value = Math.min(value, 1.0f);
value = Math.max(value, 0f);
AttributeInstance attribute1 = p.getAttribute(org.bukkit.attribute.Attribute.MOVEMENT_SPEED);
if (attribute1 != null) attribute1.setBaseValue(value);
AttributeInstance attribute2 = p.getAttribute(org.bukkit.attribute.Attribute.FLYING_SPEED);
if (attribute2 != null) attribute2.setBaseValue(value);
p.setFlySpeed(value);
p.setWalkSpeed(value);
for (PotionEffect pe : sr.getPotionEffects()) {
p.addPotionEffect(pe);
}
// health
// default is 100 so 20
double health =
(sr.getAttribute(Attribute.HEALTH) + sr.getTotalItemAttribute(Attribute.HEALTH)) / 5;
AttributeInstance attribute = p.getAttribute(org.bukkit.attribute.Attribute.MAX_HEALTH);
if (attribute != null) attribute.setBaseValue(health);
p.sendHealthUpdate();
p.updateInventory();
}
public static void updateActionBar(@NotNull Player p) {
PlayerManager pm = Manager.getInstance(PlayerManager.class);
if (pm == null) return;
SRPlayer sr = pm.getPlayer(p.getUniqueId());
if (sr == null) return;
int maxHealth = (int) sr.getAttribute(Attribute.HEALTH);
int health = (int) sr.getHealth();
int mana = (int) sr.getAttribute(Attribute.MANA);
net.kyori.adventure.text.Component text =
comp("<red>❤ " + health + "/" + maxHealth + " <blue>❈ " + mana);
p.sendActionBar(text);
}
/**
* <a
* href="https://github.com/DragonL0508/D-Damage-Indicators/blob/master/src/main/java/me/dragonl/damageIndicators/GlobalListener.java">author</a>
*/
public static @NotNull TextDisplay spawnIndicator(
@NotNull Entity eventEntity, boolean isHeal, double number) {
// vector operation
Vector location = eventEntity.getLocation().toVector();
Vector facingVector = eventEntity.getFacing().getDirection();
Vector spawnVector = location.add(facingVector.multiply(0.75));
// summon entity
double randomX = RANDOM.nextDouble() * 0.5 - 0.25;
double randomZ = RANDOM.nextDouble() * 0.5 - 0.25;
spawnVector.add(new Vector(randomX, 0, randomZ));
TextDisplay indicators =
eventEntity
.getWorld()
.spawn(
spawnVector.toLocation(eventEntity.getWorld()).add(0.0, 0.5, 0.0),
TextDisplay.class);
String prefix = "<red>-";
if (isHeal) prefix = "<green>+";
indicators.text(comp(prefix + number));
indicators.setBillboard(Display.Billboard.CENTER);
indicators.setShadowed(true);
indicators.setTeleportDuration(2);
indicatorAnimation(indicators);
return indicators;
}
/**
* <a
* href="https://github.com/DragonL0508/D-Damage-Indicators/blob/master/src/main/java/me/dragonl/damageIndicators/GlobalListener.java">author</a>
*/
private static void indicatorAnimation(TextDisplay indicator) {
new BukkitRunnable() {
int height = 0;
@Override
public void run() {
indicator.teleport(indicator.getLocation().add(0, 0.1, 0));
if (height == 15) {
indicator.remove();
this.cancel();
}
height++;
}
}.runTaskTimer(SR.plugin(), 0, 1);
}
}