Skip to content

Commit

Permalink
v3.9 dev. progress
Browse files Browse the repository at this point in the history
Pinned entity/mob HUD stats now support modded entity stat types.
  • Loading branch information
TheCSDev committed Feb 16, 2024
1 parent d734a60 commit cb628a5
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.thecsdev.betterstats;

import java.util.Locale;

import io.github.thecsdev.betterstats.util.stats.SASConfig;
import io.github.thecsdev.tcdcommons.api.config.AutoConfig;
import io.github.thecsdev.tcdcommons.api.config.annotation.NonSerialized;
Expand All @@ -17,6 +19,13 @@ public class BetterStatsConfig extends AutoConfig
* MUST NOT AND SHALL NOT BE DONE WITHOUT THE USER'S CONSENT!
*/
public static @NonSerialized boolean CLIENT_NET_CONSENT = false;

/**
* Defines whether or not certain features are available for
* a given user, such as specific API endpoints, and the
* user being shown certain links in GUI interfaces.
*/
public static final @NonSerialized boolean RESTRICTED_MODE;
// --------------------------------------------------
public @SerializedAs("client-guiMobsFollowCursor") boolean guiMobsFollowCursor = true;
public @SerializedAs("client-trustAllServersBssNet") boolean trustAllServersBssNet = true;
Expand All @@ -25,5 +34,12 @@ public class BetterStatsConfig extends AutoConfig
public @SerializedAs("server-sasConfig") SASConfig sasConfig = new SASConfig();
// ==================================================
public BetterStatsConfig(String name) { super(name); }
static
{
//define RESTRICTED_MODE value
final var lc = Locale.getDefault().getCountry().toLowerCase();
final var rc = "cn".equals(lc);
RESTRICTED_MODE = rc; //TODO - How do I check if the client is authenticated with Mojang?
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.api.registry;

import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -15,6 +16,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.stat.StatType;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -62,13 +64,23 @@ private BSRegistries() {}
* @apiNote The {@link Function} must not return {@code null}!
*/
public static final Map<StatType<EntityType<?>>, Function<SUMobStat, Text>> ENTITY_STAT_TEXT_FORMATTER;

/**
* A {@link Map} of {@link Text}s representing "phrases" for each entity stat type.<br/>
* For example:<br/>
* - {@link Stats#KILLED} becomes "Kills"<br>
* - {@link Stats#KILLED_BY} becomes "Died to"<br/>
* - and so on...
*/
public static final Map<StatType<EntityType<?>>, Text> ENTITY_STAT_PHRASE;
// --------------------------------------------------
static
{
//define the registries
ITEM_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
MOB_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
ENTITY_STAT_TEXT_FORMATTER = new HashMap<>();
ENTITY_STAT_PHRASE = new HashMap<>();

//the default Wiki for 'minecraft' is now 'minecraft.wiki'
final String mc = new Identifier("air").getNamespace();
Expand All @@ -90,6 +102,9 @@ private BSRegistries() {}
translatable("stat_type.minecraft.killed_by.none", entityName) :
translatable("stat_type.minecraft.killed_by", entityName, Integer.toString(stat.deaths));
});

ENTITY_STAT_PHRASE.put(Stats.KILLED, translatable("betterstats.api.client.gui.stats.widget.mobstatwidget.kills"));
ENTITY_STAT_PHRASE.put(Stats.KILLED_BY, translatable("betterstats.api.client.gui.stats.widget.mobstatwidget.deaths"));
}
// ==================================================
/**
Expand All @@ -98,7 +113,7 @@ private BSRegistries() {}
* @return {@code null} if the URL is not found.
* @throws NullPointerException If the argument is null.
*/
public static @Nullable String getItemWikiURL(Identifier itemId) throws NullPointerException
public static final @Nullable String getItemWikiURL(Identifier itemId) throws NullPointerException
{
Objects.requireNonNull(itemId);
var supplier = ITEM_WIKIS.get(itemId.getNamespace());
Expand All @@ -112,12 +127,23 @@ private BSRegistries() {}
* @return {@code null} if the URL is not found.
* @throws NullPointerException If the argument is null.
*/
public static @Nullable String getMobWikiURL(Identifier entityId) throws NullPointerException
public static final @Nullable String getMobWikiURL(Identifier entityId) throws NullPointerException
{
Objects.requireNonNull(entityId);
var supplier = MOB_WIKIS.get(entityId.getNamespace());
if(supplier == null) return null;
else return supplier.apply(entityId);
}

/**
* Obtains the {@link Text} representing the "phrase" for a given entity {@link StatType}.
* @param statType The {@link StatType}.
*/
public static final Text getEntityStatTypePhrase(StatType<EntityType<?>> statType)
{
final @Nullable var p = ENTITY_STAT_PHRASE.get(statType);
if(p != null) return p;
else return literal(Objects.toString(Registries.STAT_TYPE.getId(statType)));
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.client.gui.screen.hud.entry;

import static io.github.thecsdev.betterstats.api.registry.BSRegistries.getEntityStatTypePhrase;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

Expand All @@ -11,14 +12,16 @@
import io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget;
import io.github.thecsdev.betterstats.api.client.gui.stats.widget.MobStatWidget;
import io.github.thecsdev.betterstats.api.client.util.io.LocalPlayerStatsProvider;
import io.github.thecsdev.betterstats.api.util.enumerations.MobStatType;
import io.github.thecsdev.betterstats.api.util.io.IStatsProvider;
import io.github.thecsdev.betterstats.api.util.stats.SUMobStat;
import io.github.thecsdev.tcdcommons.api.client.gui.TElement;
import io.github.thecsdev.tcdcommons.api.client.gui.panel.TPanelElement;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen;
import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext;
import net.minecraft.entity.EntityType;
import net.minecraft.registry.Registries;
import net.minecraft.stat.StatType;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;

public final class StatsHudMobEntry extends TWidgetHudScreen.WidgetEntry<TElement>
Expand All @@ -28,7 +31,7 @@ public final class StatsHudMobEntry extends TWidgetHudScreen.WidgetEntry<TElemen
// --------------------------------------------------
protected IStatsProvider statsProvider;
protected final EntityType<?> entityType;
protected MobStatType mode = MobStatType.KILLED;
protected StatType<EntityType<?>> mode = Stats.KILLED;
// ==================================================
public StatsHudMobEntry(SUMobStat stat) throws NullPointerException { this(stat.getStatsProvider(), stat.getEntityType()); }
public StatsHudMobEntry(IStatsProvider statsProvider, EntityType<?> entityType) throws NullPointerException
Expand All @@ -48,11 +51,18 @@ public StatsHudMobEntry(IStatsProvider statsProvider, EntityType<?> entityType)
final var el = new Element();
el.eContextMenu.register((__, cMenu) ->
{
for(final var ist : MobStatType.values())
//entity stat type entries
for(final var statType : Registries.STAT_TYPE)
{
cMenu.addButton(ist.getText(), ___ ->
//check the stat type and if it's compatible
if(statType.getRegistry() != Registries.ENTITY_TYPE)
continue;
final @SuppressWarnings("unchecked") var statTypeE = (StatType<EntityType<?>>)statType;

//create a button that will switch to the given stat type, and refresh
cMenu.addButton(getEntityStatTypePhrase(statTypeE), ___ ->
{
this.mode = ist;
this.mode = statTypeE;
refreshEntry();
});
}
Expand All @@ -64,12 +74,13 @@ public StatsHudMobEntry(IStatsProvider statsProvider, EntityType<?> entityType)
// --------------------------------------------------
private final CustomStatElement createCustomStatElement(SUMobStat stat)
{
//collect info
final int i = ItemStatWidget.SIZE;
@Nullable Text left = this.mode.getText();
@Nullable Text right = literal(Integer.toString(this.mode.getStatValue(stat)));
//prepare variables
if(this.mode == null) this.mode = Stats.KILLED;
@Nullable Text left = getEntityStatTypePhrase(this.mode);
@Nullable Text right = literal(Integer.toString(this.statsProvider.getStatValue(this.mode, this.entityType)));

//create and return
return new CustomStatElement(i, 0, WIDTH - i, left, right);
return new CustomStatElement(ItemStatWidget.SIZE, 0, WIDTH - ItemStatWidget.SIZE, left, right);
}
// ==================================================
private final class Element extends TElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import static io.github.thecsdev.betterstats.BetterStats.URL_MODRINTH;
import static io.github.thecsdev.betterstats.BetterStats.URL_SOURCES;
import static io.github.thecsdev.betterstats.BetterStats.URL_YOUTUBE;
import static io.github.thecsdev.betterstats.BetterStatsConfig.RESTRICTED_MODE;
import static io.github.thecsdev.betterstats.api.client.registry.BSClientRegistries.STATS_TAB;
import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.tcdcommons.api.client.gui.util.GuiUtils.showUrlPrompt;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import java.awt.Color;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Objects;

Expand Down Expand Up @@ -166,18 +166,17 @@ public MenuBarPanel(int x, int y, int width, MenuBarPanelProxy proxy) throws Nul
{
//create the context menu
final var cMenu = new TContextMenuPanel(btn);
final boolean cn = "CN".equalsIgnoreCase(Locale.getDefault().getCountry());

//url-s
if(!cn)
if(!RESTRICTED_MODE)
{
cMenu.addButton(translatable(tr + "menu_about.source"), __ -> showUrlPrompt(URL_SOURCES, false));
cMenu.addButton(translatable("menu.reportBugs"), __ -> showUrlPrompt(URL_ISSUES, false));
cMenu.addSeparator();
}
cMenu.addButton(translatable(tr + "menu_about.curseforge"), __ -> showUrlPrompt(URL_CURSEFORGE, false));
cMenu.addButton(translatable(tr + "menu_about.modrinth"), __ -> showUrlPrompt(URL_MODRINTH, false));
if(!cn)
if(!RESTRICTED_MODE)
{
cMenu.addSeparator();
cMenu.addButton(translatable(tr + "menu_about.youtube"), __ -> showUrlPrompt(URL_YOUTUBE, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"betterstats.api.util.enumerations.filtergroupby.default": "Default",
"betterstats.api.util.enumerations.filtergroupby.mod": "Mod",

"betterstats.api.client.gui.stats.widget.mobstatwidget.kills": "Kills",
"betterstats.api.client.gui.stats.widget.mobstatwidget.deaths": "Deaths",
"betterstats.api.client.gui.stats.widget.mobstatwidget.kills": "Killed",
"betterstats.api.client.gui.stats.widget.mobstatwidget.deaths": "Died to",
"betterstats.api.client.gui.stats.widget.playerbadgestatwidget.obtained": "Obtained",
"betterstats.api.client.gui.stats.widget.generalstatwidget.value": "Value",

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.thecsdev.betterstats;

import java.util.Locale;

import io.github.thecsdev.betterstats.util.stats.SASConfig;
import io.github.thecsdev.tcdcommons.api.config.AutoConfig;
import io.github.thecsdev.tcdcommons.api.config.annotation.NonSerialized;
Expand All @@ -17,6 +19,13 @@ public class BetterStatsConfig extends AutoConfig
* MUST NOT AND SHALL NOT BE DONE WITHOUT THE USER'S CONSENT!
*/
public static @NonSerialized boolean CLIENT_NET_CONSENT = false;

/**
* Defines whether or not certain features are available for
* a given user, such as specific API endpoints, and the
* user being shown certain links in GUI interfaces.
*/
public static final @NonSerialized boolean RESTRICTED_MODE;
// --------------------------------------------------
public @SerializedAs("client-guiMobsFollowCursor") boolean guiMobsFollowCursor = true;
public @SerializedAs("client-trustAllServersBssNet") boolean trustAllServersBssNet = true;
Expand All @@ -25,5 +34,12 @@ public class BetterStatsConfig extends AutoConfig
public @SerializedAs("server-sasConfig") SASConfig sasConfig = new SASConfig();
// ==================================================
public BetterStatsConfig(String name) { super(name); }
static
{
//define RESTRICTED_MODE value
final var lc = Locale.getDefault().getCountry().toLowerCase();
final var rc = "cn".equals(lc);
RESTRICTED_MODE = rc; //TODO - How do I check if the client is authenticated with Mojang?
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thecsdev.betterstats.api.registry;

import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import java.util.HashMap;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.stat.StatType;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -63,13 +65,23 @@ private BSRegistries() {}
* @apiNote The {@link Function} must not return {@code null}!
*/
public static final Map<StatType<EntityType<?>>, Function<SUMobStat, Text>> ENTITY_STAT_TEXT_FORMATTER;

/**
* A {@link Map} of {@link Text}s representing "phrases" for each entity stat type.<br/>
* For example:<br/>
* - {@link Stats#KILLED} becomes "Kills"<br>
* - {@link Stats#KILLED_BY} becomes "Died to"<br/>
* - and so on...
*/
public static final Map<StatType<EntityType<?>>, Text> ENTITY_STAT_PHRASE;
// --------------------------------------------------
static
{
//define the registries
ITEM_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
MOB_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
ENTITY_STAT_TEXT_FORMATTER = new HashMap<>();
ENTITY_STAT_PHRASE = new HashMap<>();

//the default Wiki for 'minecraft' is now 'minecraft.wiki'
final String mc = new Identifier("air").getNamespace();
Expand All @@ -91,6 +103,9 @@ private BSRegistries() {}
translatable("stat_type.minecraft.killed_by.none", entityName) :
translatable("stat_type.minecraft.killed_by", entityName, Integer.toString(stat.deaths));
});

ENTITY_STAT_PHRASE.put(Stats.KILLED, translatable("betterstats.api.client.gui.stats.widget.mobstatwidget.kills"));
ENTITY_STAT_PHRASE.put(Stats.KILLED_BY, translatable("betterstats.api.client.gui.stats.widget.mobstatwidget.deaths"));
}
// ==================================================
/**
Expand All @@ -99,7 +114,7 @@ private BSRegistries() {}
* @return {@code null} if the URL is not found.
* @throws NullPointerException If the argument is null.
*/
public static @Nullable String getItemWikiURL(Identifier itemId) throws NullPointerException
public static final @Nullable String getItemWikiURL(Identifier itemId) throws NullPointerException
{
Objects.requireNonNull(itemId);
var supplier = ITEM_WIKIS.get(itemId.getNamespace());
Expand All @@ -113,12 +128,23 @@ private BSRegistries() {}
* @return {@code null} if the URL is not found.
* @throws NullPointerException If the argument is null.
*/
public static @Nullable String getMobWikiURL(Identifier entityId) throws NullPointerException
public static final @Nullable String getMobWikiURL(Identifier entityId) throws NullPointerException
{
Objects.requireNonNull(entityId);
var supplier = MOB_WIKIS.get(entityId.getNamespace());
if(supplier == null) return null;
else return supplier.apply(entityId);
}

/**
* Obtains the {@link Text} representing the "phrase" for a given entity {@link StatType}.
* @param statType The {@link StatType}.
*/
public static final Text getEntityStatTypePhrase(StatType<EntityType<?>> statType)
{
final @Nullable var p = ENTITY_STAT_PHRASE.get(statType);
if(p != null) return p;
else return literal(Objects.toString(Registries.STAT_TYPE.getId(statType)));
}
// ==================================================
}
Loading

0 comments on commit cb628a5

Please sign in to comment.