Skip to content

Commit 43bc24a

Browse files
committed
Improve Lua api
1 parent 504b404 commit 43bc24a

13 files changed

Lines changed: 210 additions & 19 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.nekiplay.hypixelcry.mixins.gui;
2+
3+
4+
import net.minecraft.client.gui.screen.ingame.AbstractSignEditScreen;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
@Mixin(AbstractSignEditScreen.class)
9+
public interface AbstractSignEditScreenAccessor {
10+
@Accessor("messages")
11+
String[] getMessages();
12+
}

src/main/java/com/nekiplay/hypixelcry/utils/ItemUtils.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.nekiplay.hypixelcry.utils;
22

3+
import com.mojang.authlib.properties.Property;
34
import net.minecraft.component.ComponentHolder;
45
import net.minecraft.component.DataComponentTypes;
56
import net.minecraft.component.type.AttributeModifierSlot;
7+
import net.minecraft.component.type.LoreComponent;
68
import net.minecraft.component.type.NbtComponent;
9+
import net.minecraft.component.type.ProfileComponent;
710
import net.minecraft.entity.EquipmentSlot;
811
import net.minecraft.entity.LivingEntity;
912
import net.minecraft.item.ItemStack;
13+
import net.minecraft.item.Items;
1014
import net.minecraft.nbt.NbtCompound;
15+
import net.minecraft.text.Text;
1116
import org.jetbrains.annotations.NotNull;
1217

13-
import java.util.List;
14-
import java.util.Optional;
18+
import java.util.*;
1519

1620
public class ItemUtils {
1721
public static final String ID = "id";
@@ -22,10 +26,59 @@ public class ItemUtils {
2226
* @param stack the item stack to get the internal name from
2327
* @return the Skyblock item id of the item stack, or an empty string if the item stack does not have a Skyblock id
2428
*/
29+
public static @NotNull String getItemUuid(@NotNull ComponentHolder stack) {
30+
return getCustomData(stack).getString("uuid", "");
31+
}
32+
2533
public static @NotNull String getItemId(@NotNull ComponentHolder stack) {
2634
return getCustomData(stack).getString(ID, "");
2735
}
2836

37+
public static @NotNull Boolean isRecombobulated(@NotNull ComponentHolder stack) {
38+
return getCustomData(stack).getInt("rarity_upgrades", 0) > 0;
39+
}
40+
41+
public static @NotNull Boolean isMuseumDonated(@NotNull ComponentHolder stack) {
42+
return getCustomData(stack).getBoolean("donated_museum", false);
43+
}
44+
45+
public static @NotNull String getReforgeModifier(@NotNull ComponentHolder stack) {
46+
return getCustomData(stack).getString("modifier", "");
47+
}
48+
49+
public static @NotNull String getHeadTexture(@NotNull ItemStack stack) {
50+
if (!stack.isOf(Items.PLAYER_HEAD) || !stack.contains(DataComponentTypes.PROFILE)) return "";
51+
52+
ProfileComponent profile = stack.get(DataComponentTypes.PROFILE);
53+
if (profile == null) return "";
54+
55+
return profile.properties().get("textures").stream().filter(Objects::nonNull)
56+
.map(Property::value)
57+
.findFirst()
58+
.orElse("");
59+
}
60+
61+
public static @NotNull List<Text> getLore(ItemStack stack) {
62+
return stack.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines();
63+
}
64+
65+
public static @NotNull Map<String, Integer> getHypixelEnchantments(ItemStack itemStack) {
66+
Map<String, Integer> result = new HashMap<>();
67+
NbtCompound extraAttributes = getCustomData(itemStack);
68+
69+
Optional<NbtCompound> enchantmentsOpt = extraAttributes.getCompound("enchantments");
70+
if (enchantmentsOpt.isEmpty()) {
71+
return result;
72+
}
73+
74+
NbtCompound enchantments = enchantmentsOpt.get();
75+
76+
for (String key : enchantments.getKeys()) {
77+
result.put(key, enchantments.getInt(key, 0));
78+
}
79+
80+
return result;
81+
}
2982

3083
/**
3184
* Gets the nbt in the custom data component of the item stack.

src/main/kotlin/com/nekiplay/hypixelcry/features/lua/LuaManager.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.nekiplay.hypixelcry.utils.misc.input.KeyAction
1111
import kotlinx.io.files.FileNotFoundException
1212
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext
1313
import net.fabricmc.loader.api.FabricLoader
14+
import net.minecraft.network.message.SignedMessage
15+
import net.minecraft.text.Text
1416
import org.luaj.vm2.Globals
1517
import org.luaj.vm2.LuaValue
1618
import org.luaj.vm2.lib.jse.JsePlatform
@@ -33,6 +35,7 @@ class LuaManager() {
3335
private val clientTickCallbacks = ArrayList<LuaValue>()
3436
private val renderWorldCallbacks = ArrayList<LuaValue>()
3537
private val keyEventCallbacks = ArrayList<LuaValue>()
38+
private val messageEventCallbacks = ArrayList<LuaValue>()
3639

3740
// Synchronize only when needed
3841
@Volatile private var callbacksLock = Any()
@@ -107,6 +110,12 @@ class LuaManager() {
107110
}
108111
})
109112

113+
globals.set("registerMessageEvent", object : OneArgFunction() {
114+
override fun call(callback: LuaValue): LuaValue {
115+
return LuaValue.valueOf(addMessageCallback(callback))
116+
}
117+
})
118+
110119
globals.set("unregisterClientTick", object : OneArgFunction() {
111120
override fun call(callback: LuaValue): LuaValue {
112121
return LuaValue.valueOf(removeClientTickCallback(callback))
@@ -123,6 +132,12 @@ class LuaManager() {
123132
}
124133
})
125134

135+
globals.set("unregisterMessageEvent", object : OneArgFunction() {
136+
override fun call(callback: LuaValue): LuaValue {
137+
return LuaValue.valueOf(removeMessageEventCallback(callback))
138+
}
139+
})
140+
126141
globals.set("require", object : OneArgFunction() {
127142
override fun call(modname: LuaValue): LuaValue {
128143
val moduleName = modname.checkjstring()
@@ -233,6 +248,13 @@ class LuaManager() {
233248
}
234249
}
235250

251+
fun addMessageCallback(callback: LuaValue): Boolean {
252+
if (!callback.isfunction()) return false
253+
synchronized(callbacksLock) {
254+
return messageEventCallbacks.add(callback)
255+
}
256+
}
257+
236258
// Methods for removing callbacks
237259
fun removeClientTickCallback(callback: LuaValue): Boolean {
238260
synchronized(callbacksLock) {
@@ -251,12 +273,19 @@ class LuaManager() {
251273
}
252274
}
253275

276+
fun removeMessageEventCallback(callback: LuaValue): Boolean {
277+
synchronized(callbacksLock) {
278+
return messageEventCallbacks.remove(callback)
279+
}
280+
}
281+
254282
// Methods to clear all callbacks
255283
fun clearAllCallbacks() {
256284
synchronized(callbacksLock) {
257285
clientTickCallbacks.clear()
258286
renderWorldCallbacks.clear()
259287
keyEventCallbacks.clear()
288+
messageEventCallbacks.clear()
260289
}
261290
}
262291

@@ -305,6 +334,20 @@ class LuaManager() {
305334
}
306335
}
307336

337+
fun onChatMessageEvent(text: Text, message: SignedMessage?) {
338+
val callbacks = synchronized(callbacksLock) {
339+
messageEventCallbacks.toTypedArray()
340+
}
341+
342+
for (callback in callbacks) {
343+
try {
344+
callback.call(LuaValue.valueOf(text.string), LuaValue.valueOf(message?.content?.string))
345+
} catch (e: Exception) {
346+
HypixelCry.LOGGER.error("Error in message callback: ${e.message}")
347+
}
348+
}
349+
}
350+
308351
fun executeScript(file: File): Any {
309352
if (!file.exists() || !file.isFile) {
310353
throw FileNotFoundException("Script file not found: ${file.path}")

src/main/kotlin/com/nekiplay/hypixelcry/features/lua/objects/player/InventoryObject.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.nekiplay.hypixelcry.features.lua.objects.player
22

3+
import com.nekiplay.hypixelcry.HypixelCry
34
import com.nekiplay.hypixelcry.features.lua.utils.ItemStackUtils
5+
import com.nekiplay.hypixelcry.mixins.gui.AbstractSignEditScreenAccessor
46
import com.nekiplay.hypixelcry.pathfinder.utils.mc
57
import com.nekiplay.hypixelcry.utils.InventoryUtils
68
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen
9+
import net.minecraft.client.gui.screen.ingame.SignEditScreen
710
import org.luaj.vm2.LuaValue
811
import org.luaj.vm2.lib.OneArgFunction
12+
import org.luaj.vm2.lib.TwoArgFunction
913
import org.luaj.vm2.lib.ZeroArgFunction
1014

1115
class InventoryObject: LuaValue() {
@@ -15,18 +19,72 @@ class InventoryObject: LuaValue() {
1519

1620
override fun get(key: LuaValue): LuaValue {
1721
return when (key.tojstring()) {
22+
"isSignOpened" -> IsSignOpenedFunction()
1823
"isChestOpened" -> IsChestOpenedFunction()
1924
"isDoubleChestOpened" -> IsDoubleChestOpenedFunction()
2025
"getChestTitle" -> GetChestTitleFunction()
2126

2227
"getStack" -> GetStackFunction()
28+
"getSignText" -> GetSignTextFunction()
29+
"setSignText" -> SetSignTextFunction()
2330

2431
"leftClick" -> LeftClickFunction()
2532
"rightClick" -> RightClickFunction()
33+
34+
"closeScreen" -> CloseScreenFunction()
2635
else -> NIL
2736
} as LuaValue
2837
}
2938

39+
private inner class SetSignTextFunction : TwoArgFunction() {
40+
override fun call(arg: LuaValue, arg2: LuaValue): LuaValue {
41+
if (arg.isnumber() && arg2.isstring()) {
42+
val screen = mc.currentScreen
43+
if (screen is SignEditScreen) {
44+
val sign = screen as AbstractSignEditScreenAccessor
45+
sign.messages[arg.toint()] = arg2.tojstring()
46+
return TRUE
47+
} else {
48+
return FALSE
49+
}
50+
}
51+
return NIL
52+
}
53+
}
54+
55+
private inner class GetSignTextFunction : OneArgFunction() {
56+
override fun call(arg: LuaValue): LuaValue {
57+
if (arg.isnumber()) {
58+
val screen = mc.currentScreen
59+
if (screen is SignEditScreen) {
60+
val sign = screen as AbstractSignEditScreenAccessor
61+
return valueOf(sign.messages[arg.toint()])
62+
} else {
63+
return FALSE
64+
}
65+
}
66+
return NIL
67+
}
68+
}
69+
70+
private inner class CloseScreenFunction : ZeroArgFunction() {
71+
override fun call(): LuaValue {
72+
mc.player?.closeHandledScreen()
73+
return TRUE
74+
}
75+
}
76+
77+
private inner class IsSignOpenedFunction : ZeroArgFunction() {
78+
override fun call(): LuaValue {
79+
val screen = mc.currentScreen
80+
return if (screen is SignEditScreen) {
81+
TRUE
82+
} else {
83+
FALSE
84+
}
85+
}
86+
}
87+
3088
private inner class GetChestTitleFunction : ZeroArgFunction() {
3189
override fun call(): LuaValue {
3290
val screen = mc.currentScreen

src/main/kotlin/com/nekiplay/hypixelcry/features/lua/utils/ItemStackUtils.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,37 @@ object ItemStackUtils {
1818
table.set("count", LuaValue.valueOf(itemStack.count.toDouble()))
1919
table.set("max_count", LuaValue.valueOf(itemStack.maxCount.toDouble()))
2020
table.set("name", LuaValue.valueOf(itemStack.item.name.string))
21+
table.set("name", LuaValue.valueOf(itemStack.item.name.string))
22+
23+
table.set("head_texture", LuaValue.valueOf(ItemUtils.getHeadTexture(itemStack)))
2124
table.set("skyblock_id", LuaValue.valueOf(ItemUtils.getItemId(itemStack)))
2225

26+
table.set("reforge_modifier", LuaValue.valueOf(ItemUtils.getReforgeModifier(itemStack)))
27+
2328
// Дополнительные свойства
24-
table.set("is_damageable", LuaValue.valueOf(itemStack.isDamageable))
2529
table.set("is_stackable", LuaValue.valueOf(itemStack.isStackable))
26-
table.set("is_enchantable", LuaValue.valueOf(itemStack.isEnchantable))
30+
table.set("is_recombobulated", LuaValue.valueOf(ItemUtils.isRecombobulated(itemStack)))
31+
table.set("is_museum_donated", LuaValue.valueOf(ItemUtils.isMuseumDonated(itemStack)))
32+
33+
val loreTable = LuaValue.tableOf();
34+
val loreList = ItemUtils.getLore(itemStack)
35+
loreList.forEachIndexed { index, line ->
36+
loreTable.set(index + 1, line.string)
37+
}
38+
table.set("lore",loreTable )
39+
40+
val enchantsTable = LuaValue.tableOf();
41+
val enchantmentsList = ItemUtils.getHypixelEnchantments(itemStack)
42+
var index = 1
43+
enchantmentsList.forEach { (id, level) ->
44+
val enchantTable = LuaValue.tableOf();
45+
enchantTable.set("name", id)
46+
enchantTable.set("level", level)
2747

28-
// Если предмет имеет повреждения
29-
if (itemStack.isDamageable) {
30-
table.set("damage", LuaValue.valueOf(itemStack.damage.toDouble()))
31-
table.set("max_damage", LuaValue.valueOf(itemStack.maxDamage.toDouble()))
48+
enchantsTable.set(index, enchantTable)
49+
index++
3250
}
51+
table.set("enchantmets", enchantsTable)
3352
return table
3453
}
3554
}

src/main/kotlin/com/nekiplay/hypixelcry/features/modules/ModuleManager.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ object ModuleManager {
1818
fun registerInbuilt() {
1919
val modules = arrayOf(
2020
/* Macros */
21-
AspectOfTheTeleport,
22-
HealingWands,
23-
RogueSword,
24-
WitherCloak,
25-
ZombieSword,
21+
AspectOfTheTeleport(),
22+
HealingWands(),
23+
RogueSword(),
24+
WitherCloak(),
25+
ZombieSword(),
2626

2727
/* Misc */
2828
LuaEvents

src/main/kotlin/com/nekiplay/hypixelcry/features/modules/impl/macros/AspectOfTheTeleport.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
88
import net.minecraft.client.MinecraftClient
99
import net.minecraft.component.ComponentHolder
1010

11-
object AspectOfTheTeleport : BindableClientModule() {
11+
class AspectOfTheTeleport : BindableClientModule() {
1212
private val WAND_IDS = setOf(
1313
"ASPECT_OF_THE_VOID",
1414
"ASPECT_OF_THE_END"

src/main/kotlin/com/nekiplay/hypixelcry/features/modules/impl/macros/HealingWands.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.nekiplay.hypixelcry.sugar.silentUse
66
import com.nekiplay.hypixelcry.utils.ItemUtils
77
import net.minecraft.component.ComponentHolder
88

9-
object HealingWands : BindableClientModule() {
9+
class HealingWands : BindableClientModule() {
1010
private val WAND_IDS = setOf(
1111
"WAND_OF_ATONEMENT",
1212
"WAND_OF_RESTORATION",

src/main/kotlin/com/nekiplay/hypixelcry/features/modules/impl/macros/RogueSword.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.nekiplay.hypixelcry.sugar.silentUse
66
import com.nekiplay.hypixelcry.utils.ItemUtils
77
import net.minecraft.component.ComponentHolder
88

9-
object RogueSword : BindableClientModule() {
9+
class RogueSword : BindableClientModule() {
1010
private val WAND_IDS = setOf(
1111
"ROGUE_SWORD"
1212
)

src/main/kotlin/com/nekiplay/hypixelcry/features/modules/impl/macros/WitherCloak.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.nekiplay.hypixelcry.sugar.silentUse
66
import com.nekiplay.hypixelcry.utils.ItemUtils
77
import net.minecraft.component.ComponentHolder
88

9-
object WitherCloak : BindableClientModule() {
9+
class WitherCloak : BindableClientModule() {
1010
private val WAND_IDS = setOf(
1111
"WITHER_CLOAK"
1212
)

0 commit comments

Comments
 (0)