Skip to content

Commit afa7a36

Browse files
committed
kill events but better and working now
1 parent be01df1 commit afa7a36

9 files changed

Lines changed: 94 additions & 53 deletions

File tree

autodocs/autodoc.lua

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-01-28T14:45:03.339480400
5+
-- Generated: 2026-01-29T12:14:59.199722800
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion
@@ -144,6 +144,10 @@ function Entity.get_uuid() end
144144
---@return string
145145
function Entity.get_name() end
146146

147+
--- Gets the id of the entity type that this entity is.
148+
---@return string
149+
function Entity.get_type_id() end
150+
147151
--- Returns true if this entity is a LivingEntity.
148152
---@return boolean
149153
function Entity.is_living() end
@@ -152,6 +156,14 @@ function Entity.is_living() end
152156
---@return LivingEntity
153157
function Entity.as_living() end
154158

159+
--- Returns true if this entity is a PlayerEntity.
160+
---@return LivingEntity
161+
function Entity.is_player() end
162+
163+
--- Return this entity as a PlayerEntity.
164+
---@return LivingEntity
165+
function Entity.as_player() end
166+
155167
--- Execute a commmand as this entity.
156168
---@param command string The command to execute.
157169
---@return integer
@@ -203,6 +215,11 @@ local Player = {}
203215
---@return nil
204216
function Player.tell(msg) end
205217

218+
--- Prints a text component to this player's chat.
219+
---@param msg TextComponent Component to display.
220+
---@return nil
221+
function Player.tell(msg) end
222+
206223
--- Gives this player this stack.
207224
---@param stack ItemStack Stack to give.
208225
---@return nil
@@ -319,7 +336,12 @@ function minecraft.get_version() end
319336
--- Prints an unformatted line to the server chat, visible to all players. (similar to /tellraw). Also prints to the console.
320337
---@param message string Message to be printed.
321338
---@return nil
322-
function minecraft.say(message) end
339+
function minecraft.print(message) end
340+
341+
--- Prints a text component to the server chat, visible to all players. (similar to /tellraw). Also prints the raw string to the console.
342+
---@param message string Message to be printed.
343+
---@return nil
344+
function minecraft.print_component(message) end
323345

324346
--- Executes the given command from the server command source. Returns the result of the command.
325347
---@param command string Command to be executed.
@@ -474,16 +496,14 @@ function text.compound(elements) end
474496
-- luafy:tick | Executes every server tick.;
475497
--
476498
-- luafy:entity_takes_damage | Executes after an entity takes damage.;
477-
-- entity: LivingEntity | Living Entity that took damage.
478-
-- attacker: Entity | Entity that dealt damage.
479-
-- damage_taken: number | Damage taken.
480-
-- source: string | Identifier of Damage Source.
481-
-- was_blocked: boolean | If true, the damage was blocked.
499+
-- entity: LivingEntity -> Living Entity that took damage.
500+
-- attacker: Entity | nil -> Entity that dealt damage.
501+
-- damage_taken: number -> Damage taken.
502+
-- was_blocked: boolean -> If true, the damage was blocked.
482503
--
483504
-- luafy:entity_dies | Executes after an entity dies.;
484-
-- entity: LivingEntity | Living Entity that died.
485-
-- attacker: Entity | Entity that killed this one.
486-
-- source: string | Identifier of Damage Source.
505+
-- entity: LivingEntity -> Living Entity that died.
506+
-- attacker: Entity | nil -> Entity that killed this one.
487507
--
488508
--#endregion
489509

src/main/java/dev/diamond/luafy/Luafy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.fabricmc.loader.api.FabricLoader;
1313
import net.fabricmc.loader.impl.FabricLoaderImpl;
1414
import net.minecraft.resources.Identifier;
15+
import net.minecraft.server.MinecraftServer;
1516
import net.minecraft.server.packs.PackType;
1617
import org.slf4j.Logger;
1718
import org.slf4j.LoggerFactory;

src/main/java/dev/diamond/luafy/lua/LuaTableBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private void addInternal(String key, LuaValue value) {
3737
public void add(String key, float f) { addInternal(key, LuaValue.valueOf(f)); }
3838
public void add(String key, double d) { addInternal(key, LuaValue.valueOf(d)); }
3939
public void add(String key, LuaTable tbl) { addInternal(key, tbl); }
40+
public void add(String key, LuaValue val) { addInternal(key, val); }
4041
public void add(String key, ScriptFunction function) { addInternal(key, new VarArgFunction() {
4142
@Override
4243
public Varargs invoke(Varargs args) {

src/main/java/dev/diamond/luafy/registry/ScriptEvents.java

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
77
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
88
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
9-
import net.minecraft.commands.CommandSourceStack;
109
import net.minecraft.core.Registry;
1110
import net.minecraft.core.registries.Registries;
1211
import net.minecraft.world.damagesource.DamageSource;
1312
import net.minecraft.world.entity.LivingEntity;
13+
import org.luaj.vm2.LuaTable;
14+
import org.luaj.vm2.LuaValue;
15+
1416

1517
public class ScriptEvents {
1618

@@ -26,46 +28,30 @@ public class ScriptEvents {
2628

2729
public static ScriptEvent<EntityTakesDamage> ENTITY_TAKES_DAMAGE = new ScriptEvent<>("Executes after an entity takes damage.", b -> {
2830
b.add("entity", ScriptObjects.LIVING_ENTITY, "Living Entity that took damage.");
29-
b.add("attacker", ScriptObjects.ENTITY, "Entity that dealt damage.");
31+
b.add("attacker", Argtypes.maybe(ScriptObjects.ENTITY), "Entity that dealt damage.");
3032
b.add("damage_taken", Argtypes.NUMBER, "Damage taken.");
31-
b.add("source", Argtypes.STRING, "Identifier of Damage Source.");
3233
b.add("was_blocked", Argtypes.BOOLEAN, "If true, the damage was blocked.");
3334
}, (b, ctx, script) -> {
3435
b.add("entity", ScriptObjects.LIVING_ENTITY.provideTable(ctx.e, script));
35-
b.add("attacker", ScriptObjects.ENTITY.provideTable(ctx.src.getDirectEntity(), script));
36+
b.add("attacker", ctx.src.getEntity() == null ?
37+
LuaValue.NIL :
38+
ScriptObjects.ENTITY.provideTable(ctx.src.getEntity(), script)
39+
);
3640
b.add("damage_taken", ctx.damageTaken);
3741

38-
var v = script
39-
.getSource()
40-
.getServer()
41-
.registryAccess()
42-
.get(Registries.DAMAGE_TYPE)
43-
.orElseThrow()
44-
.value()
45-
.getKey(ctx.src.type());
46-
47-
b.add("source", v.toString());
4842
b.add("was_blocked", ctx.damageTaken);
4943
});
5044

5145
public static ScriptEvent<EntityDies> ENTITY_DIES = new ScriptEvent<>("Executes after an entity dies.", b -> {
5246
b.add("entity", ScriptObjects.LIVING_ENTITY, "Living Entity that died.");
53-
b.add("attacker", ScriptObjects.ENTITY, "Entity that killed this one.");
54-
b.add("source", Argtypes.STRING, "Identifier of Damage Source.");
47+
b.add("attacker", Argtypes.maybe(ScriptObjects.ENTITY), "Entity that killed this one.");
5548
}, (b, ctx, script) -> {
5649
b.add("entity", ScriptObjects.LIVING_ENTITY.provideTable(ctx.e, script));
57-
b.add("attacker", ScriptObjects.ENTITY.provideTable(ctx.src.getDirectEntity(), script));
58-
59-
var v = script
60-
.getSource()
61-
.getServer()
62-
.registryAccess()
63-
.get(Registries.DAMAGE_TYPE)
64-
.orElseThrow()
65-
.value()
66-
.getKey(ctx.src.type());
67-
68-
b.add("source", v.toString());
50+
b.add("attacker", ctx.src.getEntity() == null ?
51+
LuaValue.NIL :
52+
ScriptObjects.ENTITY.provideTable(ctx.src.getEntity(), script)
53+
);
54+
6955
});
7056

7157

@@ -95,24 +81,17 @@ public static void applyEvents() {
9581

9682
// damage
9783
ServerLivingEntityEvents.AFTER_DAMAGE.register((e, src, baseDmgTaken, dmgTaken, isBlocked) -> {
98-
ENTITY_TAKES_DAMAGE.trigger(getCommandSourceStack(e), new ScriptEvents.EntityTakesDamage(e, src, dmgTaken, isBlocked));
84+
ENTITY_TAKES_DAMAGE.trigger(e.level().getServer().createCommandSourceStack(), new ScriptEvents.EntityTakesDamage(e, src, dmgTaken, isBlocked));
9985
});
10086

10187
ServerLivingEntityEvents.AFTER_DEATH.register((e, src) -> {
102-
ENTITY_DIES.trigger(getCommandSourceStack(e), new ScriptEvents.EntityDies(e, src));
88+
ENTITY_DIES.trigger(e.level().getServer().createCommandSourceStack(), new ScriptEvents.EntityDies(e, src));
10389
});
10490

10591

10692

10793
}
10894

109-
public static CommandSourceStack getCommandSourceStack(LivingEntity le) {
110-
if (le.level().getServer() == null) {
111-
throw new RuntimeException("Tried to get the server of an entity that is not in the server (??)");
112-
}
113-
return le.level().getServer().createCommandSourceStack();
114-
}
115-
11695
public record EntityTakesDamage(LivingEntity e, DamageSource src, float damageTaken, boolean blocked) {}
11796
public record EntityDies(LivingEntity e, DamageSource src) {}
11897

src/main/java/dev/diamond/luafy/script/LuaScript.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public Globals getGlobals() {
7171
return globals;
7272
}
7373

74+
public void setSource(@NotNull CommandSourceStack src) {
75+
this.src = src.withSuppressedOutput();
76+
}
7477

7578
private void injectSources() {
7679
for (ScriptPlugin plugin : LuafyRegistries.SCRIPT_PLUGINS) {
@@ -89,7 +92,7 @@ private ScriptExecutionResult executor(@NotNull CommandSourceStack src, @Nullabl
8992
return new ScriptExecutionResult(LuaValue.NIL, compilationError);
9093
}
9194
try {
92-
this.src = src.withSuppressedOutput();
95+
this.setSource(src);
9396
if (ctx == null) {
9497
ctx = LuaTable.tableOf();
9598
}

src/main/java/dev/diamond/luafy/script/api/MinecraftApi.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void addFunctions(ScriptApiBuilder apiBuilder) {
5252
return LuaString.valueOf(SharedConstants.getCurrentVersion().name());
5353
}, "Returns the current Minecraft version string.", args -> {}, Argtypes.STRING);
5454

55-
builder.add("say", args -> {
55+
builder.add("print", args -> {
5656
String s = args.nextString();
5757

5858
for (ServerPlayer spe : script.getSource().getServer().getPlayerList().getPlayers()) {
@@ -67,6 +67,21 @@ public void addFunctions(ScriptApiBuilder apiBuilder) {
6767
args.add("message", Argtypes.STRING, "Message to be printed.");
6868
}, Argtypes.NIL);
6969

70+
builder.add("print_component", args -> {
71+
Component c = args.nextScriptObject(ScriptObjects.TEXT_COMPONENT, script.getSource(), script);
72+
73+
for (ServerPlayer spe : script.getSource().getServer().getPlayerList().getPlayers()) {
74+
spe.sendSystemMessage(c, false);
75+
}
76+
77+
script.getGlobals().STDOUT.print(c.getString());
78+
script.getGlobals().STDOUT.print('\n');
79+
80+
return LuaValue.NIL;
81+
}, "Prints a text component to the server chat, visible to all players. (similar to /tellraw). Also prints the raw string to the console.", args -> {
82+
args.add("message", Argtypes.STRING, "Message to be printed.");
83+
}, Argtypes.NIL);
84+
7085
builder.add("command", args -> {
7186
String s = args.nextString();
7287
var source = script.getSource().getServer().createCommandSourceStack();

src/main/java/dev/diamond/luafy/script/event/ScriptEvent.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public class ScriptEvent<T> implements SimpleAutodocumentable {
2222
private final ContextBuilder<T> ctxBuilder;
2323
private final String desc;
2424
private final ArrayList<ArgDocInfo> argList;
25+
private boolean isEmpty;
2526

2627
public ScriptEvent(String desc, Consumer<ArglistBuilder> arglistBuilder, ContextBuilder<T> ctxBuilder) {
2728
this.entries = new ArrayList<>();
29+
this.isEmpty = true;
2830
this.ctxBuilder = ctxBuilder;
2931

3032
this.desc = desc;
@@ -36,11 +38,13 @@ public ScriptEvent(String desc, Consumer<ArglistBuilder> arglistBuilder, Context
3638
}
3739

3840
public void trigger(@NotNull CommandSourceStack src, T context) {
41+
if (isEmpty) return;
3942

4043
for (ScriptEntry entry : entries) {
4144
if (entry.canExecute()) {
4245
LuaScript script = Luafy.SCRIPT_MANAGER.get(entry.id);
4346
LuaTableBuilder builder = new LuaTableBuilder();
47+
script.setSource(src);
4448
ctxBuilder.build(builder, context, script);
4549
LuaTable ctx = builder.build();
4650

@@ -50,11 +54,13 @@ public void trigger(@NotNull CommandSourceStack src, T context) {
5054
}
5155

5256
public void register(Collection<ScriptEntry> entries) {
57+
this.isEmpty = false;
5358
this.entries.addAll(entries);
5459
}
5560

5661
public void clear() {
57-
entries.clear();
62+
this.isEmpty = true;
63+
this.entries.clear();
5864
}
5965

6066
public String getDesc() {

src/main/java/dev/diamond/luafy/script/object/game/entity/EntityScriptObject.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package dev.diamond.luafy.script.object.game.entity;
22

3-
import com.mojang.brigadier.Command;
43
import dev.diamond.luafy.autodoc.Argtypes;
54
import dev.diamond.luafy.lua.LuaTableBuilder;
65
import dev.diamond.luafy.registry.ScriptObjects;
76
import dev.diamond.luafy.script.LuaScript;
87
import dev.diamond.luafy.script.api.MinecraftApi;
98
import dev.diamond.luafy.script.object.AbstractScriptObject;
109
import net.minecraft.commands.CommandSourceStack;
10+
import net.minecraft.core.registries.BuiltInRegistries;
11+
import net.minecraft.server.level.ServerPlayer;
1112
import net.minecraft.world.entity.Entity;
1213
import net.minecraft.world.entity.LivingEntity;
1314
import org.luaj.vm2.LuaTable;
@@ -20,22 +21,26 @@ public class EntityScriptObject extends AbstractScriptObject<Entity> {
2021
public static final String FUNC_GET_POS = "get_pos";
2122
public static final String FUNC_GET_UUID = "get_uuid";
2223
public static final String FUNC_GET_NAME = "get_name";
24+
public static final String FUNC_GET_TYPE_ID = "get_type_id";
2325
public static final String FUNC_IS_LIVING = "is_living";
2426
public static final String FUNC_AS_LIVING = "as_living";
27+
public static final String FUNC_IS_PLAYER = "is_player";
28+
public static final String FUNC_AS_PLAYER = "as_player";
2529
public static final String FUNC_EXECUTE_AS = "execute_as";
26-
public static final String FUNC_GET_NBT = "get_nbt";
2730

2831
public EntityScriptObject() {
2932
super("An entity.", doc -> {
3033
doc.addFunction(FUNC_GET_POS, "Gets the entity's current position.", args -> {}, ScriptObjects.VEC3D);
3134
doc.addFunction(FUNC_GET_UUID, "Gets the entity's UUID.", args -> {}, Argtypes.STRING);
3235
doc.addFunction(FUNC_GET_NAME, "Gets the entity's name.", args -> {}, Argtypes.STRING);
36+
doc.addFunction(FUNC_GET_TYPE_ID, "Gets the id of the entity type that this entity is.", args -> {}, Argtypes.STRING);
3337
doc.addFunction(FUNC_IS_LIVING, "Returns true if this entity is a LivingEntity.", args -> {}, Argtypes.BOOLEAN);
3438
doc.addFunction(FUNC_AS_LIVING, "Return this entity as a LivingEntity.", args -> {}, ScriptObjects.LIVING_ENTITY);
39+
doc.addFunction(FUNC_IS_PLAYER, "Returns true if this entity is a PlayerEntity.", args -> {}, ScriptObjects.LIVING_ENTITY);
40+
doc.addFunction(FUNC_AS_PLAYER, "Return this entity as a PlayerEntity.", args -> {}, ScriptObjects.LIVING_ENTITY);
3541
doc.addFunction(FUNC_EXECUTE_AS, "Execute a commmand as this entity.", args -> {
3642
args.add("command", Argtypes.STRING, "The command to execute.");
3743
}, Argtypes.INTEGER);
38-
//doc.addFunction(FUNC_GET_NBT, "Get this entity's NBT data.", args -> {}, Argtypes.TABLE);
3944
});
4045
}
4146

@@ -47,8 +52,11 @@ public void toTable(Entity obj, LuaTableBuilder builder, LuaScript script) {
4752
builder.add(FUNC_GET_POS, args -> LuaTableBuilder.provide(b -> ScriptObjects.VEC3D.toTable(obj.position(), b, script)));
4853
builder.add(FUNC_GET_UUID, args -> LuaValue.valueOf(obj.getStringUUID()));
4954
builder.add(FUNC_GET_NAME, args -> LuaValue.valueOf(obj.getPlainTextName()));
55+
builder.add(FUNC_GET_TYPE_ID, args -> LuaValue.valueOf(BuiltInRegistries.ENTITY_TYPE.getKey(obj.getType()).toString()));
5056
builder.add(FUNC_IS_LIVING, args -> LuaValue.valueOf(obj instanceof LivingEntity));
5157
builder.add(FUNC_AS_LIVING, args -> LuaTableBuilder.provide(b -> ScriptObjects.LIVING_ENTITY.toTable((LivingEntity) obj, b, script)));
58+
builder.add(FUNC_IS_PLAYER, args -> LuaValue.valueOf(obj instanceof ServerPlayer));
59+
builder.add(FUNC_AS_PLAYER, args -> LuaTableBuilder.provide(b -> ScriptObjects.PLAYER.toTable((ServerPlayer) obj, b, script)));
5260
builder.add(FUNC_EXECUTE_AS, args -> {
5361
String s = args.nextString();
5462
var source = script.getSource().withEntity(obj);

src/main/java/dev/diamond/luafy/script/object/game/entity/PlayerScriptObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class PlayerScriptObject extends AbstractScriptObject<ServerPlayer> {
1818

1919
public static final String FUNC_TELL = "tell";
20+
public static final String FUNC_TELL_COMPONENT = "tell_component";
2021
public static final String FUNC_GIVE_STACK = "give_stack";
2122

2223
public PlayerScriptObject() {
@@ -25,6 +26,9 @@ public PlayerScriptObject() {
2526
doc.addFunction(FUNC_TELL, "Prints a line to this player's chat.", args -> {
2627
args.add("msg", Argtypes.STRING, "String to display.");
2728
}, Argtypes.NIL);
29+
doc.addFunction(FUNC_TELL, "Prints a text component to this player's chat.", args -> {
30+
args.add("msg", ScriptObjects.TEXT_COMPONENT, "Component to display.");
31+
}, Argtypes.NIL);
2832
doc.addFunction(FUNC_GIVE_STACK, "Gives this player this stack.", args -> {
2933
args.add("stack", ScriptObjects.ITEM_STACK, "Stack to give.");
3034
}, Argtypes.NIL);
@@ -40,6 +44,10 @@ public void toTable(ServerPlayer obj, LuaTableBuilder builder, LuaScript script)
4044
obj.sendSystemMessage(Component.literal(args.nextString()), false);
4145
return LuaValue.NIL;
4246
});
47+
builder.add(FUNC_TELL_COMPONENT, args -> {
48+
obj.sendSystemMessage(args.nextScriptObject(ScriptObjects.TEXT_COMPONENT, script.getSource(), script), false);
49+
return LuaValue.NIL;
50+
});
4351
builder.add(FUNC_GIVE_STACK, args -> {
4452
ItemStack stack = args.nextScriptObject(ScriptObjects.ITEM_STACK, script.getSource(), script);
4553
obj.getInventory().add(stack);

0 commit comments

Comments
 (0)