Skip to content

Commit

Permalink
Merge pull request #165 from 4P5/getters-for-actionbar-and-titles
Browse files Browse the repository at this point in the history
Getters for actionbar, titles, and scoreboards
  • Loading branch information
skyrina committed Jan 19, 2024
2 parents a6a9261 + 6f5b4f3 commit 7979c30
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
77 changes: 77 additions & 0 deletions common/src/main/java/org/figuramc/figura/lua/api/ClientAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.server.IntegratedServer;
Expand All @@ -15,6 +16,10 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.Objective;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Score;
import net.minecraft.world.scores.Scoreboard;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.lua.LuaNotNil;
import org.figuramc.figura.lua.LuaWhitelist;
Expand All @@ -25,6 +30,7 @@
import org.figuramc.figura.lua.docs.LuaTypeDoc;
import org.figuramc.figura.math.vector.FiguraVec2;
import org.figuramc.figura.math.vector.FiguraVec3;
import org.figuramc.figura.mixin.gui.GuiAccessor;
import org.figuramc.figura.mixin.gui.PlayerTabOverlayAccessor;
import org.figuramc.figura.mixin.render.ModelManagerAccessor;
import org.figuramc.figura.utils.*;
Expand Down Expand Up @@ -537,6 +543,77 @@ public static double getFrameTime() {
return Minecraft.getInstance().getFrameTime();
}

@LuaWhitelist
@LuaMethodDoc("client.get_actionbar")
public static Component getActionbar() {
Gui gui = Minecraft.getInstance().gui;
return ((GuiAccessor) gui).getActionbarTime() > 0 ? ((GuiAccessor) gui).getActionbar() : null;
}

@LuaWhitelist
@LuaMethodDoc("client.get_title")
public static Component getTitle() {
Gui gui = Minecraft.getInstance().gui;
return ((GuiAccessor) gui).getTime() > 0 ? ((GuiAccessor) gui).getTitle() : null;
}

@LuaWhitelist
@LuaMethodDoc("client.get_subtitle")
public static Component getSubtitle() {
Gui gui = Minecraft.getInstance().gui;
return ((GuiAccessor) gui).getTime() > 0 ? ((GuiAccessor) gui).getSubtitle() : null;
}

@LuaWhitelist
@LuaMethodDoc("client.get_scoreboard")
public static Map<String, Map<String, Object>> getScoreboard() {
Map<String, Map<String, Object>> map = new HashMap<>();

assert Minecraft.getInstance().level != null;
Scoreboard scoreboard = Minecraft.getInstance().level.getScoreboard();

Map<String, Objective> objectives = new HashMap<>();

// sidebars for different team colours
assert Minecraft.getInstance().player != null;
PlayerTeam playerTeam = scoreboard.getPlayersTeam(Minecraft.getInstance().player.getScoreboardName());
if (playerTeam != null) {
int id = playerTeam.getColor().getId();
if (id >= 0) {
objectives.put("sidebar_team_" + playerTeam.getColor().getName(), scoreboard.getDisplayObjective(3 + id));
}
}

objectives.put("list", scoreboard.getDisplayObjective(0));
objectives.put("sidebar", scoreboard.getDisplayObjective(1));
objectives.put("below_name", scoreboard.getDisplayObjective(2));

for (Map.Entry<String, Objective> entry : objectives.entrySet()) {
String key = entry.getKey();
Objective objective = entry.getValue();

if (objective != null) {
Map<String, Object> objectiveMap = new HashMap<>();

objectiveMap.put("name", objective.getName());
objectiveMap.put("display_name", objective.getFormattedDisplayName());
objectiveMap.put("criteria", objective.getCriteria().getName());
objectiveMap.put("render_type", objective.getRenderType().getSerializedName());

Map<String, Integer> scoreMap = new HashMap<>();
for (Score score : scoreboard.getPlayerScores(objective)) {
scoreMap.put(score.getOwner(), score.getScore());
}

objectiveMap.put("scores", scoreMap);

map.put(key, objectiveMap);
}
}

return map;
}

@LuaWhitelist
@LuaMethodDoc("client.list_atlases")
public static List<String> listAtlases() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.figuramc.figura.mixin.gui;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.ChatScreen;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(Gui.class)
public interface GuiAccessor {
@Intrinsic
@Accessor("title")
Component getTitle();

@Intrinsic
@Accessor("subtitle")
Component getSubtitle();

@Intrinsic
@Accessor("overlayMessageString")
Component getActionbar();

@Intrinsic
@Accessor("titleTime")
int getTime();

@Intrinsic
@Accessor("overlayMessageTime")
int getActionbarTime();
}
4 changes: 4 additions & 0 deletions common/src/main/resources/assets/figura/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@
"figura.docs.client.get_server_data": "Returns a table with information on the currently connected server (also for singleplayer worlds)",
"figura.docs.client.get_date": "Returns a table with information about the client's current time",
"figura.docs.client.get_frame_time": "Returns the current fraction between the last tick and the next tick\nThis is the value used as \"delta\" in the RENDER event",
"figura.docs.client.get_actionbar": "Returns the current actionbar text, or nil if the action bar isn't visible",
"figura.docs.client.get_title": "Returns the current title text, or nil if the title isn't visible",
"figura.docs.client.get_subtitle": "Returns the current subtitle text, or nil if the title or subtitle isn't visible",
"figura.docs.client.get_scoreboard": "Returns data about the current scoreboard(s). Multiple scoreboards can be visible at the same time (sidebar, team sidebar, list, and below name), so each scoreboard is grouped by its display location",
"figura.docs.client.list_atlases": "Returns a list of all registered atlases paths",
"figura.docs.client.get_atlas": "Returns a TextureAtlasAPI object with information about the given atlas\nReturns nil if the atlas was not found",
"figura.docs.client.get_tab_list": "Returns a table with the text shown in the tablist",
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/resources/figura-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"gui.ChatScreenMixin",
"gui.ClickEventActionMixin",
"gui.DebugScreenOverlayMixin",
"gui.GuiAccessor",
"gui.GuiMessageLineMixin",
"gui.GuiMessageMixin",
"gui.GuiMixin",
Expand Down Expand Up @@ -71,7 +72,6 @@
"render.layers.items.ItemInHandLayerMixin",
"render.layers.items.PlayerItemInHandLayerMixin",
"render.renderers.ArrowRendererMixin",
"render.renderers.TridentRendererMixin",
"render.renderers.BlockEntityWithoutLevelRendererMixin",
"render.renderers.EntityRendererMixin",
"render.renderers.HandRenderSelectionAccessor",
Expand All @@ -83,6 +83,7 @@
"render.renderers.SignRendererMixin",
"render.renderers.SimpleVCMixin",
"render.renderers.SkullBlockRendererMixin",
"render.renderers.TridentRendererMixin",
"sound.ChannelHandleMixin",
"sound.SoundEngineMixin",
"sound.SoundManagerAccessor",
Expand Down

0 comments on commit 7979c30

Please sign in to comment.