Skip to content

Commit

Permalink
Dev. progress for v3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCSDev committed Feb 1, 2024
1 parent 5bc13c0 commit c03e458
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public class SASConfig implements ACJsonHandler<JsonObject>
// ==================================================
public String[] firstMinedBlocks = new String[] { "diamond_ore", "deepslate_diamond_ore", "ancient_debris", "deepslate_coal_ore", "dragon_egg", "sculk_sensor", "reinforced_deepslate" };
public String[] firstCraftedItems = new String[] { "wooden_pickaxe", "diamond_pickaxe", "beacon", "netherite_block", "ender_eye" };
public boolean announceFirstDeaths = true;
public String[] firstKilledEntities = new String[] { "zombie", "blaze", "enderman", "ender_dragon", "warden", "wither", "player" };
public String[] firstKilledByEntities = new String[] { "ender_dragon", "warden", "wither", "player" };
public String[] firstCustomStats = new String[] { "deaths" };
// ==================================================
public final @Override JsonObject saveToJson()
{
final var json = new JsonObject();
json.add("firstMinedBlocks", stringArrayToJsonArray(this.firstMinedBlocks));
json.add("firstCraftedItems", stringArrayToJsonArray(this.firstCraftedItems));
json.addProperty("announceFirstDeaths", this.announceFirstDeaths);
json.add("firstKilledEntities", stringArrayToJsonArray(this.firstKilledEntities));
json.add("firstKilledByEntities", stringArrayToJsonArray(this.firstKilledByEntities));
json.add("firstCustomStats", stringArrayToJsonArray(this.firstCustomStats));
return json;
}
// --------------------------------------------------
Expand All @@ -32,9 +32,9 @@ public class SASConfig implements ACJsonHandler<JsonObject>
{
if(json.has("firstMinedBlocks")) this.firstMinedBlocks = jsonArrayToStringArray(json.getAsJsonArray("firstMinedBlocks"));
if(json.has("firstCraftedItems")) this.firstCraftedItems = jsonArrayToStringArray(json.getAsJsonArray("firstCraftedItems"));
if(json.has("announceFirstDeaths")) this.announceFirstDeaths = json.getAsJsonPrimitive("announceFirstDeaths").getAsBoolean();
if(json.has("firstKilledEntities")) this.firstKilledEntities = jsonArrayToStringArray(json.getAsJsonArray("firstKilledEntities"));
if(json.has("firstKilledByEntities")) this.firstKilledByEntities = jsonArrayToStringArray(json.getAsJsonArray("firstKilledByEntities"));
if(json.has("firstCustomStats")) this.firstCustomStats = jsonArrayToStringArray(json.getAsJsonArray("firstCustomStats"));
return true;
}
catch(Exception e) { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import static io.github.thecsdev.betterstats.api.util.stats.SUGeneralStat.getGeneralStatText;

/**
* A {@link Class} that handles announcing players doing
Expand All @@ -47,15 +48,17 @@
public static final String TXT_FIRST_DEATH_HC1 = P + "first_death.hc1";
public static final String TXT_FIRST_KILLED = P + "first_kill";
public static final String TXT_FIRST_KILLED_BY = P + "first_death_to";
public static final String TXT_CUSTOM = P + "custom";
//
private static final Text WATERMARK;
private static final BetterStatsConfig BSSC;
private static final SASConfig SASC;
// --------------------------------------------------
public static final HashSet<Block> FIRST_MINED_BLOCKS;
public static final HashSet<Item> FIRST_CRAFTED_ITEMS;
public static final HashSet<Block> FIRST_MINED_BLOCKS;
public static final HashSet<Item> FIRST_CRAFTED_ITEMS;
public static final HashSet<EntityType<?>> FIRST_KILLED_ENTITIES;
public static final HashSet<EntityType<?>> FIRST_KILLED_BY_ENTITIES;
public static final HashSet<Identifier> FIRST_CUSTOM_STATS; //aka "general stats"
// --------------------------------------------------
private StatAnnouncementSystem() {}
static
Expand Down Expand Up @@ -89,6 +92,7 @@ private StatAnnouncementSystem() {}
FIRST_CRAFTED_ITEMS = new HashSet<Item>();
FIRST_KILLED_ENTITIES = new HashSet<EntityType<?>>();
FIRST_KILLED_BY_ENTITIES = new HashSet<EntityType<?>>();
FIRST_CUSTOM_STATS = new HashSet<Identifier>();

//initialize set entries
for(final var fmbId : SASC.firstMinedBlocks)
Expand Down Expand Up @@ -130,6 +134,16 @@ private StatAnnouncementSystem() {}
FIRST_KILLED_BY_ENTITIES.add(fkbe);
}
catch(InvalidIdentifierException e) { continue; }

for(final var fcsId : SASC.firstCustomStats)
try
{
final var id = new Identifier(fcsId);
final @Nullable var fcs = Registries.CUSTOM_STAT.getOrEmpty(id).orElse(null);
if(fcs == null) continue;
FIRST_CUSTOM_STATS.add(fcs);
}
catch(InvalidIdentifierException e) { continue; }
}
// ==================================================
/**
Expand Down Expand Up @@ -168,17 +182,27 @@ else if(oldValue == 0 && newValue > 0)
else if(stat.getType() == Stats.CRAFTED && FIRST_CRAFTED_ITEMS.contains(stat.getValue()))
broadcastFirstCraft(player, (Item)stat.getValue());

//handle "first death"
else if(stat.getType() == Stats.CUSTOM && Objects.equals(stat.getValue(), Stats.DEATHS) && SASC.announceFirstDeaths)
broadcastFirstDeath(player);

//handle first "killed"
else if(stat.getType() == Stats.KILLED && FIRST_KILLED_ENTITIES.contains(stat.getValue()))
broadcastFirstKilled(player, (EntityType<?>)stat.getValue());

//handle first "killed by"
else if(stat.getType() == Stats.KILLED_BY && FIRST_KILLED_BY_ENTITIES.contains(stat.getValue()))
broadcastFirstKilledBy(player, (EntityType<?>)stat.getValue());

//handle "first death"
else if(stat.getType() == Stats.CUSTOM &&
Objects.equals(stat.getValue(), Stats.DEATHS) &&
FIRST_CUSTOM_STATS.contains(stat.getValue()))
broadcastFirstDeath(player);

//handle custom stats
else if(stat.getType() == Stats.CUSTOM && FIRST_CUSTOM_STATS.contains(stat.getValue()))
{
@SuppressWarnings("unchecked")
final var cStat = (Stat<Identifier>)stat;
broadcastFirstCustomStat(player, cStat, stat.format(newValue));
}
}
}
// --------------------------------------------------
Expand Down Expand Up @@ -273,24 +297,6 @@ public static final void broadcastFirstCraft(ServerPlayerEntity player, Item cra
.append(pText).append(" just crafted their first ").append(iText).append("."));
}

/**
* Broadcasts a "first death" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that died for their first time.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstDeath(ServerPlayerEntity player)
{
final var hardcore = player.getServer().isHardcore();
final var key = hardcore ? TXT_FIRST_DEATH_HC1 : TXT_FIRST_DEATH;
final var literalBrightSide = hardcore ? " On the bright side, it likely won't happen again." : "";

final var pText = formatPlayerText(player);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(key, pText)),
literal("").append(WATERMARK).append(" ")
.append(pText).append(" died for their first time." + literalBrightSide));
}

/**
* Broadcasts a "first killed" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that killed an {@link EntityType} for their first time.
Expand Down Expand Up @@ -322,6 +328,44 @@ public static final void broadcastFirstKilledBy(ServerPlayerEntity player, Entit
literal("").append(WATERMARK).append(" ").append(pText)
.append(" just died to a ").append(etText).append(" for their first time."));
}

/**
* Broadcasts a "first death" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that died for their first time.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstDeath(ServerPlayerEntity player)
{
final var hardcore = player.getServer().isHardcore();
final var key = hardcore ? TXT_FIRST_DEATH_HC1 : TXT_FIRST_DEATH;
final var literalBrightSide = hardcore ? " On the bright side, it likely won't happen again." : "";

final var pText = formatPlayerText(player);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(key, pText)),
literal("").append(WATERMARK).append(" ")
.append(pText).append(" died for their first time." + literalBrightSide));
}

/**
* Broadcasts a player increasing the value of a "custom stat" for their first time.
* @param player The {@link ServerPlayerEntity} whose general/custom stat increased.
* @param stat The general/custom {@link Stat} in question.
* @param statValue The new {@link Stat} value.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstCustomStat(
ServerPlayerEntity player, Stat<Identifier> stat, String statValue) throws NullPointerException
{
final var pText = formatPlayerText(player);
final var sText = literal("").append(getGeneralStatText(stat)).formatted(Formatting.GRAY);
final var vText = literal(statValue).formatted(Formatting.GREEN);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(TXT_CUSTOM, pText, sText, vText)),
literal("").append(WATERMARK).append(" ").append(pText)
.append(" just increased their ").append(sText).append(" stat value to ")
.append(vText).append("."));
}
// --------------------------------------------------
/**
* Broadcasts a stat announcement to all users in the server.<br/><br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@
"betterstats.util.stats.statannouncementsystem.first_death": "%s just died for their first time.",
"betterstats.util.stats.statannouncementsystem.first_death.hc1": "%s just died for their first time. On the bright side, it likely won't happen again.",
"betterstats.util.stats.statannouncementsystem.first_kill": "%s just killed a %s for their first time.",
"betterstats.util.stats.statannouncementsystem.first_death_to": "%s just died to a %s for their first time."
"betterstats.util.stats.statannouncementsystem.first_death_to": "%s just died to a %s for their first time.",
"betterstats.util.stats.statannouncementsystem.custom": "%s just increased their '%s' stat value to %s."
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public class SASConfig implements ACJsonHandler<JsonObject>
// ==================================================
public String[] firstMinedBlocks = new String[] { "diamond_ore", "deepslate_diamond_ore", "ancient_debris", "deepslate_coal_ore", "dragon_egg", "sculk_sensor", "reinforced_deepslate" };
public String[] firstCraftedItems = new String[] { "wooden_pickaxe", "diamond_pickaxe", "beacon", "netherite_block", "ender_eye" };
public boolean announceFirstDeaths = true;
public String[] firstKilledEntities = new String[] { "zombie", "blaze", "enderman", "ender_dragon", "warden", "wither", "player" };
public String[] firstKilledByEntities = new String[] { "ender_dragon", "warden", "wither", "player" };
public String[] firstCustomStats = new String[] { "deaths" };
// ==================================================
public final @Override JsonObject saveToJson()
{
final var json = new JsonObject();
json.add("firstMinedBlocks", stringArrayToJsonArray(this.firstMinedBlocks));
json.add("firstCraftedItems", stringArrayToJsonArray(this.firstCraftedItems));
json.addProperty("announceFirstDeaths", this.announceFirstDeaths);
json.add("firstKilledEntities", stringArrayToJsonArray(this.firstKilledEntities));
json.add("firstKilledByEntities", stringArrayToJsonArray(this.firstKilledByEntities));
json.add("firstCustomStats", stringArrayToJsonArray(this.firstCustomStats));
return json;
}
// --------------------------------------------------
Expand All @@ -32,9 +32,9 @@ public class SASConfig implements ACJsonHandler<JsonObject>
{
if(json.has("firstMinedBlocks")) this.firstMinedBlocks = jsonArrayToStringArray(json.getAsJsonArray("firstMinedBlocks"));
if(json.has("firstCraftedItems")) this.firstCraftedItems = jsonArrayToStringArray(json.getAsJsonArray("firstCraftedItems"));
if(json.has("announceFirstDeaths")) this.announceFirstDeaths = json.getAsJsonPrimitive("announceFirstDeaths").getAsBoolean();
if(json.has("firstKilledEntities")) this.firstKilledEntities = jsonArrayToStringArray(json.getAsJsonArray("firstKilledEntities"));
if(json.has("firstKilledByEntities")) this.firstKilledByEntities = jsonArrayToStringArray(json.getAsJsonArray("firstKilledByEntities"));
if(json.has("firstCustomStats")) this.firstCustomStats = jsonArrayToStringArray(json.getAsJsonArray("firstCustomStats"));
return true;
}
catch(Exception e) { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import static io.github.thecsdev.betterstats.api.util.stats.SUGeneralStat.getGeneralStatText;

/**
* A {@link Class} that handles announcing players doing
Expand All @@ -47,15 +48,17 @@
public static final String TXT_FIRST_DEATH_HC1 = P + "first_death.hc1";
public static final String TXT_FIRST_KILLED = P + "first_kill";
public static final String TXT_FIRST_KILLED_BY = P + "first_death_to";
public static final String TXT_CUSTOM = P + "custom";
//
private static final Text WATERMARK;
private static final BetterStatsConfig BSSC;
private static final SASConfig SASC;
// --------------------------------------------------
public static final HashSet<Block> FIRST_MINED_BLOCKS;
public static final HashSet<Item> FIRST_CRAFTED_ITEMS;
public static final HashSet<Block> FIRST_MINED_BLOCKS;
public static final HashSet<Item> FIRST_CRAFTED_ITEMS;
public static final HashSet<EntityType<?>> FIRST_KILLED_ENTITIES;
public static final HashSet<EntityType<?>> FIRST_KILLED_BY_ENTITIES;
public static final HashSet<Identifier> FIRST_CUSTOM_STATS; //aka "general stats"
// --------------------------------------------------
private StatAnnouncementSystem() {}
static
Expand Down Expand Up @@ -89,6 +92,7 @@ private StatAnnouncementSystem() {}
FIRST_CRAFTED_ITEMS = new HashSet<Item>();
FIRST_KILLED_ENTITIES = new HashSet<EntityType<?>>();
FIRST_KILLED_BY_ENTITIES = new HashSet<EntityType<?>>();
FIRST_CUSTOM_STATS = new HashSet<Identifier>();

//initialize set entries
for(final var fmbId : SASC.firstMinedBlocks)
Expand Down Expand Up @@ -130,6 +134,16 @@ private StatAnnouncementSystem() {}
FIRST_KILLED_BY_ENTITIES.add(fkbe);
}
catch(InvalidIdentifierException e) { continue; }

for(final var fcsId : SASC.firstCustomStats)
try
{
final var id = new Identifier(fcsId);
final @Nullable var fcs = Registries.CUSTOM_STAT.getOrEmpty(id).orElse(null);
if(fcs == null) continue;
FIRST_CUSTOM_STATS.add(fcs);
}
catch(InvalidIdentifierException e) { continue; }
}
// ==================================================
/**
Expand Down Expand Up @@ -168,17 +182,27 @@ else if(oldValue == 0 && newValue > 0)
else if(stat.getType() == Stats.CRAFTED && FIRST_CRAFTED_ITEMS.contains(stat.getValue()))
broadcastFirstCraft(player, (Item)stat.getValue());

//handle "first death"
else if(stat.getType() == Stats.CUSTOM && Objects.equals(stat.getValue(), Stats.DEATHS) && SASC.announceFirstDeaths)
broadcastFirstDeath(player);

//handle first "killed"
else if(stat.getType() == Stats.KILLED && FIRST_KILLED_ENTITIES.contains(stat.getValue()))
broadcastFirstKilled(player, (EntityType<?>)stat.getValue());

//handle first "killed by"
else if(stat.getType() == Stats.KILLED_BY && FIRST_KILLED_BY_ENTITIES.contains(stat.getValue()))
broadcastFirstKilledBy(player, (EntityType<?>)stat.getValue());

//handle "first death"
else if(stat.getType() == Stats.CUSTOM &&
Objects.equals(stat.getValue(), Stats.DEATHS) &&
FIRST_CUSTOM_STATS.contains(stat.getValue()))
broadcastFirstDeath(player);

//handle custom stats
else if(stat.getType() == Stats.CUSTOM && FIRST_CUSTOM_STATS.contains(stat.getValue()))
{
@SuppressWarnings("unchecked")
final var cStat = (Stat<Identifier>)stat;
broadcastFirstCustomStat(player, cStat, stat.format(newValue));
}
}
}
// --------------------------------------------------
Expand Down Expand Up @@ -273,24 +297,6 @@ public static final void broadcastFirstCraft(ServerPlayerEntity player, Item cra
.append(pText).append(" just crafted their first ").append(iText).append("."));
}

/**
* Broadcasts a "first death" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that died for their first time.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstDeath(ServerPlayerEntity player)
{
final var hardcore = player.getServer().isHardcore();
final var key = hardcore ? TXT_FIRST_DEATH_HC1 : TXT_FIRST_DEATH;
final var literalBrightSide = hardcore ? " On the bright side, it likely won't happen again." : "";

final var pText = formatPlayerText(player);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(key, pText)),
literal("").append(WATERMARK).append(" ")
.append(pText).append(" died for their first time." + literalBrightSide));
}

/**
* Broadcasts a "first killed" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that killed an {@link EntityType} for their first time.
Expand Down Expand Up @@ -322,6 +328,44 @@ public static final void broadcastFirstKilledBy(ServerPlayerEntity player, Entit
literal("").append(WATERMARK).append(" ").append(pText)
.append(" just died to a ").append(etText).append(" for their first time."));
}

/**
* Broadcasts a "first death" event to all players in the server.
* @param player A {@link ServerPlayerEntity} that died for their first time.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstDeath(ServerPlayerEntity player)
{
final var hardcore = player.getServer().isHardcore();
final var key = hardcore ? TXT_FIRST_DEATH_HC1 : TXT_FIRST_DEATH;
final var literalBrightSide = hardcore ? " On the bright side, it likely won't happen again." : "";

final var pText = formatPlayerText(player);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(key, pText)),
literal("").append(WATERMARK).append(" ")
.append(pText).append(" died for their first time." + literalBrightSide));
}

/**
* Broadcasts a player increasing the value of a "custom stat" for their first time.
* @param player The {@link ServerPlayerEntity} whose general/custom stat increased.
* @param stat The general/custom {@link Stat} in question.
* @param statValue The new {@link Stat} value.
* @throws NullPointerException If an argument is {@code null}.
*/
public static final void broadcastFirstCustomStat(
ServerPlayerEntity player, Stat<Identifier> stat, String statValue) throws NullPointerException
{
final var pText = formatPlayerText(player);
final var sText = literal("").append(getGeneralStatText(stat)).formatted(Formatting.GRAY);
final var vText = literal(statValue).formatted(Formatting.GREEN);
broadcastBssMessage(player.getServer(),
literal("").append(WATERMARK).append(" ").append(translatable(TXT_CUSTOM, pText, sText, vText)),
literal("").append(WATERMARK).append(" ").append(pText)
.append(" just increased their ").append(sText).append(" stat value to ")
.append(vText).append("."));
}
// --------------------------------------------------
/**
* Broadcasts a stat announcement to all users in the server.<br/><br/>
Expand Down
Loading

0 comments on commit c03e458

Please sign in to comment.