experience sharing when killing mob
This commit is contained in:
parent
62f036b2af
commit
ea28a9ce69
3 changed files with 71 additions and 1 deletions
|
@ -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.spawnIndicator;
|
||||||
import static me.unurled.sacredrealms.sr.utils.SRPlayerUtils.updateActionBar;
|
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.Random;
|
||||||
|
import java.util.UUID;
|
||||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
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.PlayerManager;
|
||||||
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
import me.unurled.sacredrealms.sr.components.player.SRPlayer;
|
||||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||||
|
@ -24,6 +29,8 @@ import org.jetbrains.annotations.NotNull;
|
||||||
@SuppressWarnings("EmptyMethod")
|
@SuppressWarnings("EmptyMethod")
|
||||||
public class CombatManager extends Manager {
|
public class CombatManager extends Manager {
|
||||||
|
|
||||||
|
private final HashMap<UUID, HashMap<Player, Double>> historyMobDamage = new HashMap<>();
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onDamage(@NotNull EntityDamageByBlockEvent e) {
|
public void onDamage(@NotNull EntityDamageByBlockEvent e) {
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
|
@ -104,8 +111,44 @@ public class CombatManager extends Manager {
|
||||||
}
|
}
|
||||||
// apply damage
|
// 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);
|
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);
|
spawnIndicator(entity, false, damage);
|
||||||
// TODO: check for status effects (apply status effects depending item of damager)
|
// TODO: check for status effects (apply status effects depending item of damager)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class SREntity {
|
public class SREntity {
|
||||||
private final UUID uuid;
|
private final UUID uuid;
|
||||||
|
private double health = 100;
|
||||||
|
private Long experience = 0L;
|
||||||
|
|
||||||
public SREntity(@NotNull Entity e) {
|
public SREntity(@NotNull Entity e) {
|
||||||
uuid = e.getUniqueId();
|
uuid = e.getUniqueId();
|
||||||
|
@ -14,4 +16,20 @@ public class SREntity {
|
||||||
public UUID getUuid() {
|
public UUID getUuid() {
|
||||||
return uuid;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,8 @@ import org.jetbrains.annotations.Nullable;
|
||||||
public class SRPlayer {
|
public class SRPlayer {
|
||||||
|
|
||||||
@Expose private UUID uuid = null;
|
@Expose private UUID uuid = null;
|
||||||
@Expose private int level = 0;
|
@Expose private int level = 1;
|
||||||
|
@Expose private Long experience = 0L;
|
||||||
private double health = 100;
|
private double health = 100;
|
||||||
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>();
|
@Expose private HashMap<Attribute, Double> attributes = new HashMap<>();
|
||||||
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>();
|
private HashMap<Attribute, HashMap<ItemStack, Double>> itemAttributes = new HashMap<>();
|
||||||
|
@ -235,4 +236,12 @@ public class SRPlayer {
|
||||||
public void setHealth(double health) {
|
public void setHealth(double health) {
|
||||||
this.health = health;
|
this.health = health;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getExperience() {
|
||||||
|
return experience;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExperience(Long experience) {
|
||||||
|
this.experience = experience;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue