experience sharing when killing mob

This commit is contained in:
unurled 2024-03-14 18:24:33 +01:00
parent 62f036b2af
commit ea28a9ce69
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
3 changed files with 71 additions and 1 deletions

View file

@ -4,8 +4,13 @@ import static me.unurled.sacredrealms.sr.utils.Component.comp;
import static me.unurled.sacredrealms.sr.utils.SRPlayerUtils.spawnIndicator;
import static me.unurled.sacredrealms.sr.utils.SRPlayerUtils.updateActionBar;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.UUID;
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
import me.unurled.sacredrealms.sr.components.entity.EntityManager;
import me.unurled.sacredrealms.sr.components.entity.SREntity;
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
import me.unurled.sacredrealms.sr.managers.Manager;
@ -24,6 +29,8 @@ import org.jetbrains.annotations.NotNull;
@SuppressWarnings("EmptyMethod")
public class CombatManager extends Manager {
private final HashMap<UUID, HashMap<Player, Double>> historyMobDamage = new HashMap<>();
@EventHandler
public void onDamage(@NotNull EntityDamageByBlockEvent e) {
// TODO: Implement
@ -104,8 +111,44 @@ public class CombatManager extends Manager {
}
// apply damage
if (historyMobDamage.containsKey(entity.getUniqueId())) {
HashMap<Player, Double> history =
historyMobDamage.getOrDefault(entity.getUniqueId(), new HashMap<>());
if (history.containsKey(d)) {
history.put(d, history.get(d) + damage);
} else {
history.put(d, damage);
}
} else {
HashMap<Player, Double> history = new HashMap<>();
history.put(d, damage);
historyMobDamage.put(entity.getUniqueId(), history);
}
entity.damage(damage);
EntityManager em = EntityManager.getInstance(EntityManager.class);
SREntity ent = em.getEntity(entity.getUniqueId());
if (entity.getHealth() <= 0) {
// entity is dead, give experience to player
SRPlayer player1 = pm.getPlayer(d.getUniqueId());
if (player1 != null && ent != null) {
// calculate experience % based on damage
HashMap<Player, Double> playerDoubleHashMap =
historyMobDamage.get(entity.getUniqueId());
if (playerDoubleHashMap == null) {
return;
}
for (Entry<Player, Double> map : playerDoubleHashMap.entrySet()) {
Double exp = ent.getExperience() * (map.getValue() / ent.getHealth());
SRPlayer tmp = pm.getPlayer(map.getKey().getUniqueId());
if (tmp != null) {
tmp.setExperience((long) (tmp.getExperience() + exp));
}
}
}
}
spawnIndicator(entity, false, damage);
// TODO: check for status effects (apply status effects depending item of damager)
}

View file

@ -6,6 +6,8 @@ import org.jetbrains.annotations.NotNull;
public class SREntity {
private final UUID uuid;
private double health = 100;
private Long experience = 0L;
public SREntity(@NotNull Entity e) {
uuid = e.getUniqueId();
@ -14,4 +16,20 @@ public class SREntity {
public UUID getUuid() {
return uuid;
}
public Long getExperience() {
return experience;
}
public void setExperience(Long experience) {
this.experience = experience;
}
public double getHealth() {
return health;
}
public void setHealth(double health) {
this.health = health;
}
}

View file

@ -23,7 +23,8 @@ import org.jetbrains.annotations.Nullable;
public class SRPlayer {
@Expose private UUID uuid = null;
@Expose private int level = 0;
@Expose private int level = 1;
@Expose private Long experience = 0L;
private double health = 100;
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>();
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>();
@ -235,4 +236,12 @@ public class SRPlayer {
public void setHealth(double health) {
this.health = health;
}
public Long getExperience() {
return experience;
}
public void setExperience(Long experience) {
this.experience = experience;
}
}