From dd258a63b7319dc67cd51db62a7137d8bb69d1c2 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Sun, 22 Sep 2024 20:40:32 -0600 Subject: [PATCH 01/11] Update HostAPI.java Add all 3 statistic methods --- .../org/figuramc/figura/lua/api/HostAPI.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 39ebf16af..35148e921 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -12,13 +12,20 @@ import net.minecraft.client.multiplayer.PlayerInfo; import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.NonNullList; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.stats.Stat; +import net.minecraft.stats.Stats; import net.minecraft.world.InteractionHand; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.SlotAccess; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.Block; import org.figuramc.figura.FiguraMod; import org.figuramc.figura.avatar.Avatar; import org.figuramc.figura.avatar.AvatarManager; @@ -28,6 +35,7 @@ import org.figuramc.figura.lua.LuaWhitelist; import org.figuramc.figura.lua.api.entity.EntityAPI; import org.figuramc.figura.lua.api.world.ItemStackAPI; +import org.figuramc.figura.lua.api.world.WorldAPI; import org.figuramc.figura.lua.docs.LuaFieldDoc; import org.figuramc.figura.lua.docs.LuaMethodDoc; import org.figuramc.figura.lua.docs.LuaMethodOverload; @@ -533,6 +541,119 @@ public List> getStatusEffects() { return list; } + @LuaWhitelist + @LuaMethodDoc( + value = "host.get_general_statistics", + aliases = "getGeneralStats" + ) + public Map getGeneralStatistics() { + Map map = new HashMap<>(); + LocalPlayer player = this.minecraft.player; + if (player == null || !isHost()) return map; + for (Stat stat : Stats.CUSTOM) { + System.out.println(stat.getName()); + String name = stat.getName().replace("minecraft.custom:minecraft.",""); // turn registry to translation key + map.put(name, player.getStats().getValue(stat)); + } + return map; + } + @LuaWhitelist + public Map getGeneralStats() { return getGeneralStatistics(); } + + @LuaWhitelist + @LuaMethodDoc( + value = "host.get_item_statistic", + aliases = "getItemStat", + overloads = @LuaMethodOverload( + argumentTypes = ItemStackAPI.class, + argumentNames = "item" + ) + ) + public Map getItemStatistic(Object id) { + Map map = new HashMap<>(); + LocalPlayer player = this.minecraft.player; + if (player == null || !isHost()) return map; + String itemKey; + ItemStackAPI stack; + if (id instanceof String) { + stack = WorldAPI.newItem(id.toString(), 1, null); + itemKey = id.toString(); + } else { + stack = (ItemStackAPI) id; + itemKey = BuiltInRegistries.ITEM.getKey(stack.itemStack.getItem()).toString(); + } + + int timesMined = 0; // init to 0 for if it's not a block + int timesBroken; + int timesCrafted; + int timesUsed; + int timesPickedUp; + int timesDropped; + if (stack.itemStack.getItem() instanceof BlockItem) { + Block block = stack.getBlockstate().blockState.getBlock(); + Stat stat = Stats.BLOCK_MINED.get(block); + timesMined = player.getStats().getValue(stat); + } + + Stat brokenStat = Stats.ITEM_BROKEN.get(stack.itemStack.getItem()); + timesBroken = player.getStats().getValue(brokenStat); + + Stat craftedStat = Stats.ITEM_CRAFTED.get(stack.itemStack.getItem()); + timesCrafted = player.getStats().getValue(craftedStat); + + Stat usedStat = Stats.ITEM_USED.get(stack.itemStack.getItem()); + timesUsed = player.getStats().getValue(usedStat); + + Stat pickedUpStat = Stats.ITEM_PICKED_UP.get(stack.itemStack.getItem()); + timesPickedUp = player.getStats().getValue(pickedUpStat); + + Stat droppedStat = Stats.ITEM_DROPPED.get(stack.itemStack.getItem()); + timesDropped = player.getStats().getValue(droppedStat); + + + map.put("mined", timesMined); + map.put("crafted", timesCrafted); + map.put("used", timesUsed); + map.put("picked_up", timesPickedUp); + map.put("broken", timesBroken); + map.put("dropped", timesDropped); + return map; + } + @LuaWhitelist + public Map getItemStat(Object id) { return getItemStatistic(id); } + + @LuaWhitelist + @LuaMethodDoc( + value = "host.get_entity_statistic", + aliases = "get_entity_stat", + overloads = @LuaMethodOverload( + argumentTypes = EntityAPI.class, + argumentNames = "entity" + ) + ) + public Map getEntityStatistic(String type) { + Map map = new HashMap<>(); + LocalPlayer player = this.minecraft.player; + if (player == null || !isHost()) return map; + Optional> entity = EntityType.byString(type); + if (entity.isPresent()) { + Stat> killedStat = Stats.ENTITY_KILLED.get(entity.get()); + Stat> killedByStat = Stats.ENTITY_KILLED_BY.get(entity.get()); + + int timesKilled = player.getStats().getValue(killedStat); + int timesKilledBy = player.getStats().getValue(killedByStat); + + map.put("killed", timesKilled); + map.put("killed_by", timesKilledBy); + } + else { + throw new LuaError("Invalid entity type " + type); + } + return map; + } + @LuaWhitelist + public Map getEntityStat(String type) { return getEntityStatistic(type); } + @LuaWhitelist @LuaMethodDoc("host.get_clipboard") public String getClipboard() { From e849aa401bc76818125d0a43b1735512cb674cfa Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:31:02 -0600 Subject: [PATCH 02/11] Fix getEntityStatistic alias --- common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 35148e921..49a05dde7 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -625,7 +625,7 @@ public Map getItemStatistic(Object id) { @LuaWhitelist @LuaMethodDoc( value = "host.get_entity_statistic", - aliases = "get_entity_stat", + aliases = "getEntityStat", overloads = @LuaMethodOverload( argumentTypes = EntityAPI.class, argumentNames = "entity" From af302503699ce2b26b2f1d2a5f9c01d6c64f5ae6 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:38:13 -0600 Subject: [PATCH 03/11] embarrassing... someone forgot to remove a debug print --- common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 49a05dde7..c3a164a3e 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -551,7 +551,6 @@ public Map getGeneralStatistics() { LocalPlayer player = this.minecraft.player; if (player == null || !isHost()) return map; for (Stat stat : Stats.CUSTOM) { - System.out.println(stat.getName()); String name = stat.getName().replace("minecraft.custom:minecraft.",""); // turn registry to translation key map.put(name, player.getStats().getValue(stat)); } From 8c1c2ce87474e75659d33edf161514bcf393152a Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Mon, 23 Sep 2024 18:40:12 -0600 Subject: [PATCH 04/11] Docs entries in en_us --- common/src/main/resources/assets/figura/lang/en_us.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index e4b27a47b..05621d69d 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -1088,6 +1088,9 @@ "figura.docs.host.append_chat_history": "Appends the message on the recent chat history", "figura.docs.host.get_chat_message": "Returns a table with information about a chat message\nTakes an index, where 1 means the last message on chat", "figura.docs.host.set_chat_message": "Modifies a chat message with the given text\nTakes an index, were 1 means the last message on chat\nSetting the message to nil will effectively remove it from the chat", + "figura.docs.host.get_general_statistics": "Returns a table with general stats about the player\nThe table contains all values in the \"general\" section", + "figura.docs.host.get_item_statistic": "Returns a table with the item's stats in the \"items\" section\nTakes an ItemStack or a string with the item's id", + "figura.docs.host.get_entity_statistic": "Returns a table with the entity's stats in the \"mobs\" section\nTakes a string with the entity's type", "figura.docs.host.swing_arm": "Animates swinging the player's arm\nIf the boolean is true, then the offhand is the one that swings", "figura.docs.host.is_first_person": "Returns true if the camera is in first person mode", "figura.docs.host.is_camera_backwards": "Returns true if the camera is facing backward", From 0e085e639db3ddd7edac5862ff0975207439e420 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:41:21 -0600 Subject: [PATCH 05/11] Update HostAPI.java fix generalStats to return doubles and have the doubles have the decimal point in the proper place --- .../java/org/figuramc/figura/lua/api/HostAPI.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index c3a164a3e..5bed8a0df 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -7,6 +7,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.Screenshot; import net.minecraft.client.gui.screens.ChatScreen; +import net.minecraft.client.gui.screens.achievement.StatsScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.multiplayer.PlayerInfo; @@ -546,18 +547,20 @@ public List> getStatusEffects() { value = "host.get_general_statistics", aliases = "getGeneralStats" ) - public Map getGeneralStatistics() { - Map map = new HashMap<>(); + public Map getGeneralStatistics() { + Map map = new HashMap<>(); LocalPlayer player = this.minecraft.player; if (player == null || !isHost()) return map; for (Stat stat : Stats.CUSTOM) { String name = stat.getName().replace("minecraft.custom:minecraft.",""); // turn registry to translation key - map.put(name, player.getStats().getValue(stat)); + String formatted = stat.format(player.getStats().getValue(stat)).replaceAll("[^\\d.]", ""); // remove things that aren't digits or periods + double finalValue = Double.parseDouble(formatted); + map.put(name, finalValue); } return map; } @LuaWhitelist - public Map getGeneralStats() { return getGeneralStatistics(); } + public Map getGeneralStats() { return getGeneralStatistics(); } @LuaWhitelist @LuaMethodDoc( @@ -575,7 +578,7 @@ public Map getItemStatistic(Object id) { String itemKey; ItemStackAPI stack; if (id instanceof String) { - stack = WorldAPI.newItem(id.toString(), 1, null); + stack = WorldAPI.newItem(id.toString(), 1, null); // this handles errors for me and im lazy itemKey = id.toString(); } else { stack = (ItemStackAPI) id; @@ -624,7 +627,7 @@ public Map getItemStatistic(Object id) { @LuaWhitelist @LuaMethodDoc( value = "host.get_entity_statistic", - aliases = "getEntityStat", + aliases = "get_entity_stat", overloads = @LuaMethodOverload( argumentTypes = EntityAPI.class, argumentNames = "entity" From 4cea388f4e2d67d986ba09a492a3bffdaa763dda Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:41:52 -0600 Subject: [PATCH 06/11] Update HostAPI.java remove debug import --- common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 5bed8a0df..2a73d5787 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -7,7 +7,6 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.Screenshot; import net.minecraft.client.gui.screens.ChatScreen; -import net.minecraft.client.gui.screens.achievement.StatsScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.multiplayer.PlayerInfo; From 9aa8698301083aa26d4b06ed141aa5e6c3c3f332 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:08:39 -0600 Subject: [PATCH 07/11] Add updateStats function Adds a method that sends a packet which updates the statistics, effectively opening the statistics menu --- .../main/java/org/figuramc/figura/lua/api/HostAPI.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 2a73d5787..4233e36f1 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -13,6 +13,7 @@ import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.NonNullList; import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.protocol.game.ServerboundClientCommandPacket; import net.minecraft.stats.Stat; import net.minecraft.stats.Stats; import net.minecraft.world.InteractionHand; @@ -540,6 +541,15 @@ public List> getStatusEffects() { return list; } + @LuaWhitelist + @LuaMethodDoc( + value = "host.update_statistics", + aliases = "updateStats" + ) + public void updateStatistics() { + this.minecraft.getConnection().send(new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.REQUEST_STATS)); // in theory, this cant nullptr; if it does, ill fix later + } + public void updateStats() {updateStatistics();} @LuaWhitelist @LuaMethodDoc( From 45cd3215a34e0487ff76b35d00a9410603e66a6b Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:09:25 -0600 Subject: [PATCH 08/11] Add updateStats docs entry --- common/src/main/resources/assets/figura/lang/en_us.json | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index 05621d69d..1541ddeeb 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -1088,6 +1088,7 @@ "figura.docs.host.append_chat_history": "Appends the message on the recent chat history", "figura.docs.host.get_chat_message": "Returns a table with information about a chat message\nTakes an index, where 1 means the last message on chat", "figura.docs.host.set_chat_message": "Modifies a chat message with the given text\nTakes an index, were 1 means the last message on chat\nSetting the message to nil will effectively remove it from the chat", + "figura.docs.host.update_statistics": "Updates the player's statistics as if the statistics screen was opened", "figura.docs.host.get_general_statistics": "Returns a table with general stats about the player\nThe table contains all values in the \"general\" section", "figura.docs.host.get_item_statistic": "Returns a table with the item's stats in the \"items\" section\nTakes an ItemStack or a string with the item's id", "figura.docs.host.get_entity_statistic": "Returns a table with the entity's stats in the \"mobs\" section\nTakes a string with the entity's type", From 078496e8b8380af9176582e1b6786039215c4dc5 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:13:37 -0600 Subject: [PATCH 09/11] Add alias to updateStatistics it existed before, just forgot to luawhitelist it lol --- common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 4233e36f1..ec8cff9b5 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -549,6 +549,7 @@ public List> getStatusEffects() { public void updateStatistics() { this.minecraft.getConnection().send(new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.REQUEST_STATS)); // in theory, this cant nullptr; if it does, ill fix later } + @LuaWhitelist public void updateStats() {updateStatistics();} @LuaWhitelist From 6730bfa098eb0b4f4915a4fdf65c2dd28649cd45 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:20:14 -0600 Subject: [PATCH 10/11] Fix overloads for itemStat and entityStat --- .../org/figuramc/figura/lua/api/HostAPI.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index ec8cff9b5..6a76d8961 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -576,10 +576,16 @@ public Map getGeneralStatistics() { @LuaMethodDoc( value = "host.get_item_statistic", aliases = "getItemStat", - overloads = @LuaMethodOverload( - argumentTypes = ItemStackAPI.class, - argumentNames = "item" - ) + overloads = { + @LuaMethodOverload( + argumentTypes = ItemStackAPI.class, + argumentNames = "item" + ), + @LuaMethodOverload( + argumentTypes = {String.class}, + argumentNames = "id" + ) + } ) public Map getItemStatistic(Object id) { Map map = new HashMap<>(); @@ -637,11 +643,7 @@ public Map getItemStatistic(Object id) { @LuaWhitelist @LuaMethodDoc( value = "host.get_entity_statistic", - aliases = "get_entity_stat", - overloads = @LuaMethodOverload( - argumentTypes = EntityAPI.class, - argumentNames = "entity" - ) + aliases = "get_entity_stat" ) public Map getEntityStatistic(String type) { Map map = new HashMap<>(); From 10c83d846761d972feec6621be1f6d06b5a98c20 Mon Sep 17 00:00:00 2001 From: Riftlight <79118830+Riftlight@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:25:12 -0600 Subject: [PATCH 11/11] Fix entityStat overload --- .../main/java/org/figuramc/figura/lua/api/HostAPI.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java index 6a76d8961..d3921e2b8 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/HostAPI.java @@ -643,8 +643,12 @@ public Map getItemStatistic(Object id) { @LuaWhitelist @LuaMethodDoc( value = "host.get_entity_statistic", - aliases = "get_entity_stat" - ) + aliases = "get_entity_stat", + overloads = @LuaMethodOverload( + argumentTypes = String.class, + argumentNames = "type" + ) + ) public Map getEntityStatistic(String type) { Map map = new HashMap<>(); LocalPlayer player = this.minecraft.player;