Skip to content

Commit ef791db

Browse files
committed
feat: Death Datapoint
1 parent 105da7a commit ef791db

6 files changed

Lines changed: 124 additions & 28 deletions

File tree

type.generic/src/main/java/net/swofty/types/generic/data/DataHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ public enum Data {
352352
SACK_OF_SACKS("sack_of_sacks", false, false, false, DatapointSackOfSacks.class, new DatapointSackOfSacks("sack_of_sacks")),
353353
ITEMS_IN_SACKS("items_in_sacks", false, false, false, DatapointItemsInSacks.class, new DatapointItemsInSacks("items_in_sacks")),
354354
BESTIARY("bestiary", false, false, false, DatapointBestiary.class, new DatapointBestiary("bestiary")),
355+
DEATHS("deaths", false, false, false, DatapointDeaths.class, new DatapointDeaths("deaths")),
355356
SKYBLOCK_EXPERIENCE("skyblock_experience", false, false, false, DatapointSkyBlockExperience.class, new DatapointSkyBlockExperience("skyblock_experience")),
356357
BITS("bits", false, false, false, DatapointInteger.class, new DatapointInteger("bits", 0)),
357358
GEMS("gems", false, false, false, DatapointInteger.class, new DatapointInteger("gems", 0)),

type.generic/src/main/java/net/swofty/types/generic/data/datapoints/DatapointBestiary.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
import net.swofty.types.generic.event.SkyBlockEventHandler;
1212
import net.swofty.types.generic.event.custom.BestiaryUpdateEvent;
1313
import net.swofty.types.generic.gui.inventory.inventories.sbmenu.bestiary.BestiaryEntry;
14-
import net.swofty.types.generic.skill.SkillCategories;
1514
import net.swofty.types.generic.user.SkyBlockPlayer;
1615
import org.json.JSONObject;
1716

18-
import java.util.ArrayList;
1917
import java.util.HashMap;
2018
import java.util.List;
2119
import java.util.Map;
@@ -110,6 +108,12 @@ public List<String> getDisplay(List<String> lore, int kills, BestiaryMob mob, Be
110108
double currentProgress = bestiaryData.getKillsToNextTier(mob, kills);
111109
double currentRequirement = bestiaryData.getTotalKillsForNextTier(bracket, tier + 1);
112110
double totalRequirement = bestiaryData.getTotalKillsForMaxTier(mob);
111+
DatapointDeaths.PlayerDeaths playerDeaths = attachedPlayer.getDeathData();
112+
int deaths = 0;
113+
114+
for (BestiaryMob bestiaryMob : bestiaryEntry.getMobs()) {
115+
deaths += playerDeaths.getAmount(bestiaryMob.getMobID());
116+
}
113117

114118
String baseLoadingBar = "─────────────────";
115119
int maxBarLength = baseLoadingBar.length();
@@ -118,7 +122,7 @@ public List<String> getDisplay(List<String> lore, int kills, BestiaryMob mob, Be
118122
lore.add("§7" + bestiaryEntry.getDescription());
119123
lore.add("");
120124
lore.add("§7Kills: §a" + kills);
121-
lore.add("§7Deaths: §a" + "TODO"); //TODO add datapoint for amount of deaths
125+
lore.add("§7Deaths: §a" + deaths);
122126
lore.add("");
123127

124128
if (tier > 0) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package net.swofty.types.generic.data.datapoints;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import net.minestom.server.entity.damage.Damage;
6+
import net.minestom.server.entity.damage.EntityDamage;
7+
import net.swofty.commons.protocol.Serializer;
8+
import net.swofty.types.generic.data.Datapoint;
9+
import net.swofty.types.generic.entity.mob.BestiaryMob;
10+
import org.json.JSONObject;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class DatapointDeaths extends Datapoint<DatapointDeaths.PlayerDeaths> {
16+
17+
public DatapointDeaths(String key, PlayerDeaths value) {
18+
super(key, value, new Serializer<>() {
19+
@Override
20+
public String serialize(PlayerDeaths value) {
21+
JSONObject jsonObject = new JSONObject(value.deaths);
22+
return jsonObject.toString();
23+
}
24+
25+
@Override
26+
public PlayerDeaths deserialize(String json) {
27+
JSONObject jsonObject = new JSONObject(json);
28+
Map<String, Integer> deaths = new HashMap<>();
29+
30+
for (String key : jsonObject.keySet()) {
31+
deaths.put(key, jsonObject.getInt(key));
32+
}
33+
34+
return new PlayerDeaths(deaths);
35+
}
36+
37+
@Override
38+
public PlayerDeaths clone(PlayerDeaths value) {
39+
return new PlayerDeaths(value.deaths == null ? new HashMap<>() : new HashMap<>(value.deaths));
40+
}
41+
});
42+
}
43+
44+
public DatapointDeaths(String key) {
45+
this(key, new PlayerDeaths());
46+
}
47+
48+
@NoArgsConstructor
49+
@Getter
50+
public static class PlayerDeaths {
51+
private Map<String, Integer> deaths = new HashMap<>();
52+
53+
public PlayerDeaths(Map<String, Integer> mobs) {
54+
this.deaths = mobs;
55+
}
56+
57+
private String getCauseAsString(Damage damage) {
58+
return switch (damage.getType().name()) {
59+
case "minecraft:mob_attack" -> ((BestiaryMob) ((EntityDamage) damage).getSource()).getMobID();
60+
default -> damage.getType().name();
61+
};
62+
}
63+
64+
public void set(Damage deathCause, int amount) {
65+
deaths.put(getCauseAsString(deathCause), amount);
66+
}
67+
68+
public void increase(Damage deathCause, Integer amount) {
69+
set(deathCause, getAmount(getCauseAsString(deathCause)) + amount);
70+
}
71+
72+
public void decrease(Damage deathCause, Integer amount) {
73+
set(deathCause, getAmount(getCauseAsString(deathCause)) - amount);
74+
}
75+
76+
public Integer getAmount(String cause) {
77+
return deaths.getOrDefault(cause, 0);
78+
}
79+
}
80+
}

type.generic/src/main/java/net/swofty/types/generic/gui/inventory/inventories/sbmenu/bestiary/GUIBestiaryMob.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public ItemStack.Builder getItem(SkyBlockPlayer player) {
9292
public ItemStack.Builder getItem(SkyBlockPlayer player) {
9393
ArrayList<String> lore = new ArrayList<>();
9494
int kills = getPlayer().getBestiaryData().getAmount(mob);
95+
int deaths = getPlayer().getDeathData().getAmount(mob.getMobID());
9596
OtherLoot otherLoot = mob.getOtherLoot();
9697

9798
List<SkyBlockLootTable.LootRecord> commonLoot = new ArrayList<>();
@@ -116,7 +117,7 @@ public ItemStack.Builder getItem(SkyBlockPlayer player) {
116117
lore.add("§7Xp Orbs: §3" + otherLoot.getXpOrbAmount());
117118
lore.add("");
118119
lore.add("§7Kills: §a" + kills);
119-
lore.add("§7Deaths: §a" + "TODO");
120+
lore.add("§7Deaths: §a" + deaths);
120121
lore.add("");
121122

122123
if (!commonLoot.isEmpty()) {

type.generic/src/main/java/net/swofty/types/generic/user/SkyBlockPlayer.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import net.swofty.types.generic.SkyBlockGenericLoader;
2929
import net.swofty.types.generic.collection.CustomCollectionAward;
3030
import net.swofty.types.generic.data.DataHandler;
31-
import net.swofty.types.generic.data.Datapoint;
3231
import net.swofty.types.generic.data.datapoints.*;
3332
import net.swofty.types.generic.data.mongodb.CoopDatabase;
3433
import net.swofty.types.generic.event.actions.player.ActionPlayerChangeSkyBlockMenuDisplay;
@@ -91,6 +90,8 @@ public class SkyBlockPlayer extends Player {
9190
@Getter
9291
private PlayerHookManager hookManager;
9392

93+
private static final Pattern SACK_PATTERN = Pattern.compile("^(?:(SMALL|MEDIUM|LARGE|ENCHANTED)_)?(.+?)_SACK$");
94+
9495

9596
public SkyBlockPlayer(@NotNull GameProfile gameProfile, @NotNull PlayerConnection playerConnection) {
9697
super(playerConnection, gameProfile);
@@ -486,7 +487,7 @@ public int getMaxSackStorage(ItemType sack) {
486487
int maxStorage = 0;
487488
String sackCategory = "";
488489

489-
Matcher matcher = Pattern.compile("^(?:(SMALL|MEDIUM|LARGE|ENCHANTED)_)?(.+?)_SACK$").matcher(sack.name());
490+
Matcher matcher = SACK_PATTERN.matcher(sack.name());
490491
if (matcher.find()) {
491492
sackCategory = matcher.group(2);
492493
} else {
@@ -495,7 +496,7 @@ public int getMaxSackStorage(ItemType sack) {
495496
}
496497

497498
for (SkyBlockItem skyBlockItem : getAllSacks()) {
498-
matcher = Pattern.compile("^(?:(SMALL|MEDIUM|LARGE|ENCHANTED)_)?(.+?)_SACK$").matcher(skyBlockItem.getAttributeHandler().getTypeAsString());
499+
matcher = SACK_PATTERN.matcher(skyBlockItem.getAttributeHandler().getTypeAsString());
499500
if (matcher.find()) {
500501
String otherCategory = matcher.group(2);
501502
if (sackCategory.equals(otherCategory) && skyBlockItem.hasComponent(SackComponent.class)) {
@@ -687,6 +688,10 @@ public void setBoosterCookieExpirationDate(long timestamp) {
687688
getDataHandler().get(DataHandler.Data.BOOSTER_COOKIE_EXPIRATION_DATE, DatapointLong.class).setValue(timestamp);
688689
}
689690

691+
public boolean isBoosterCookieActive() {
692+
return getBoosterCookieExpirationDate() >= System.currentTimeMillis();
693+
}
694+
690695
public DatapointKat.PlayerKat getKatData() {
691696
return getDataHandler().get(DataHandler.Data.KAT, DatapointKat.class).getValue();
692697
}
@@ -742,6 +747,9 @@ public void addExperience(long value) {
742747
setExperience(getExperience() + value);
743748
}
744749

750+
public DatapointDeaths.PlayerDeaths getDeathData() {
751+
return getDataHandler().get(DataHandler.Data.DEATHS, DatapointDeaths.class).getValue();
752+
}
745753

746754
@Override
747755
public void kill() {
@@ -754,12 +762,14 @@ public void kill() {
754762

755763
sendMessage("§c☠ §7You " + creator.createPersonal());
756764

757-
DatapointDouble coins = getDataHandler().get(DataHandler.Data.COINS, DatapointDouble.class);
758-
coins.setValue(coins.getValue() / 2);
765+
getDeathData().increase(this.lastDamage, 1);
759766

760767
playSound(Sound.sound(Key.key("block.anvil.fall"), Sound.Source.PLAYER, 1.0f, 2.0f));
761768

762-
sendMessage("§cYou died and lost " + StringUtility.decimalify(coins.getValue(), 1) + " coins!");
769+
if (!isBoosterCookieActive()) {
770+
sendMessage("§cYou died and lost " + StringUtility.decimalify(getCoins() / 2, 1) + " coins!");
771+
setCoins(getCoins() / 2);
772+
}
763773

764774
if (!SkyBlockConst.getTypeLoader().getLoaderValues().announceDeathMessages()) return;
765775

type.generic/src/main/java/net/swofty/types/generic/utility/DeathMessageCreator.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@
88
public record DeathMessageCreator(Damage type) {
99
public String createPersonal() {
1010
return switch (type.getType().name()) {
11-
case "entity_source":
12-
yield "were slain by " + ((SkyBlockMob) ((EntityDamage) type).getSource()).getDisplayName();
13-
case "attack.outOfWorld":
14-
yield "fell out of the world";
15-
case "attack.onFire":
16-
yield "burned to death";
17-
case "attack.fall":
18-
yield "fell from a high place";
11+
case "minecraft:mob_attack":
12+
yield "were slain by " + ((SkyBlockMob) ((EntityDamage) type).getSource()).getDisplayName() + ".";
13+
case "minecraft:out_of_world":
14+
yield "fell out of the world.";
15+
case "minecraft:on_fire":
16+
yield "burned to death.";
17+
case "minecraft:fall":
18+
yield "fell from a high place.";
1919
default:
2020
Logger.warn("Unknown death type: " + type.getType().name());
21-
yield "died";
21+
yield "died.";
2222
};
2323
}
2424

2525
public String createOther() {
2626
return switch (type.getType().name()) {
27-
case "entity_source":
28-
yield "was slain by " + ((SkyBlockMob) ((EntityDamage) type).getSource()).getDisplayName();
29-
case "attack.outOfWorld":
30-
yield "fell out of the world";
31-
case "attack.onFire":
32-
yield "burned to death";
33-
case "attack.fall":
34-
yield "fell from a high place";
27+
case "minecraft:mob_attack":
28+
yield "was slain by " + ((SkyBlockMob) ((EntityDamage) type).getSource()).getDisplayName() + ".";
29+
case "minecraft:out_of_world":
30+
yield "fell out of the world.";
31+
case "minecraft:on_fire":
32+
yield "burned to death.";
33+
case "minecraft:fall":
34+
yield "fell from a high place.";
3535
default:
3636
Logger.warn("Unknown death type: " + type.getType().name());
37-
yield "died";
37+
yield "died.";
3838
};
3939
}
4040
}

0 commit comments

Comments
 (0)