Skip to content

Commit d0aa403

Browse files
committed
hmmm
1 parent 6bcb76c commit d0aa403

6 files changed

Lines changed: 88 additions & 3 deletions

File tree

autodocs/autodoc.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-02-06T12:35:35.079254700
5+
-- Generated: 2026-03-02T20:11:48.109650600
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion
@@ -582,6 +582,12 @@ function nbtstorage.write(id, table) end
582582
-- entity: LivingEntity -> Living Entity that died.
583583
-- attacker: Entity | nil -> Entity that killed this one.
584584
--
585+
-- luafy:player_joins_server | Executes when a player joins the server.;
586+
-- player: Player -> Player that joined.
587+
--
588+
-- luafy:player_leaves_server | Executes when a player joins the server.;
589+
-- player: Player -> Player that left.
590+
--
585591

586592
--#endregion
587593

src/main/java/dev/diamond/luafy/command/LuafyCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import dev.diamond.luafy.Luafy;
1111
import dev.diamond.luafy.autodoc.SimpleAutodocumentable;
1212
import dev.diamond.luafy.autodoc.generator.AbstractAutodocGenerator;
13+
import dev.diamond.luafy.holder.HolderItem;
1314
import dev.diamond.luafy.lua.LuaTableBuilder;
1415
import dev.diamond.luafy.lua.MetamethodImpl;
1516
import dev.diamond.luafy.registry.LuafyRegistries;
@@ -90,6 +91,9 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher,
9091
)
9192
)
9293
)
94+
).then(
95+
literal("holder")
96+
.executes(LuafyCommand::giveHolderItem)
9397
)
9498
);
9599
}
@@ -215,6 +219,15 @@ private static int execScript(CommandContext<CommandSourceStack> ctx, LuaScript
215219
}
216220

217221

222+
private static int giveHolderItem(CommandContext<CommandSourceStack> ctx) {
223+
// proof of concept
224+
if (ctx.getSource().isPlayer()) {
225+
ctx.getSource().getPlayer().addItem(HolderItem.getItem(ctx.getSource().getLevel()));
226+
}
227+
return 1;
228+
}
229+
230+
218231
private static class ScriptIdsSuggestionProvider implements SuggestionProvider<CommandSourceStack> {
219232
@Override
220233
public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> ctx, SuggestionsBuilder builder) throws CommandSyntaxException {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.diamond.luafy.holder;
2+
3+
import net.minecraft.core.component.DataComponents;
4+
import net.minecraft.nbt.CompoundTag;
5+
import net.minecraft.network.chat.Component;
6+
import net.minecraft.resources.Identifier;
7+
import net.minecraft.server.level.ServerLevel;
8+
import net.minecraft.world.item.ItemStack;
9+
import net.minecraft.world.item.Items;
10+
import net.minecraft.world.item.Rarity;
11+
import net.minecraft.world.item.component.CustomData;
12+
13+
public class HolderItem {
14+
15+
16+
17+
public static ItemStack getItem(ServerLevel level) {
18+
ItemStack itemStack = Items.ENCHANTED_BOOK.getDefaultInstance();
19+
20+
itemStack.set(DataComponents.RARITY, Rarity.UNCOMMON);
21+
itemStack.set(DataComponents.ITEM_NAME, Component.literal("Script Holder"));
22+
itemStack.set(DataComponents.ITEM_MODEL, Identifier.withDefaultNamespace("command_block"));
23+
itemStack.set(DataComponents.MAX_STACK_SIZE, 64);
24+
itemStack.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true);
25+
26+
CompoundTag compoundTag = new CompoundTag();
27+
compoundTag.putBoolean("script_holder_block", true);
28+
CustomData customData = CustomData.of(compoundTag);
29+
itemStack.set(DataComponents.CUSTOM_DATA, customData);
30+
31+
return itemStack;
32+
}
33+
34+
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.diamond.luafy.mixin;
2+
3+
import net.minecraft.world.level.block.CommandBlock;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
6+
@Mixin(CommandBlock.class)
7+
public class CommandBlockMixin {
8+
9+
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import dev.diamond.luafy.script.type.Argtypes;
55
import dev.diamond.luafy.script.event.ScriptEvent;
66
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
7+
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
78
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
89
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
910
import net.minecraft.core.Registry;
11+
import net.minecraft.server.level.ServerPlayer;
1012
import net.minecraft.world.damagesource.DamageSource;
1113
import net.minecraft.world.entity.LivingEntity;
1214
import org.luaj.vm2.LuaValue;
@@ -52,13 +54,27 @@ public class ScriptEvents {
5254

5355
});
5456

57+
public static ScriptEvent<ServerPlayer> PLAYER_JOINS_SERVER = new ScriptEvent<>("Executes when a player joins the server.", b -> {
58+
b.add("player", ScriptObjects.PLAYER, "Player that joined.");
59+
}, (b, ctx, script) -> {
60+
b.add("player", ScriptObjects.PLAYER.provideTable(ctx, script));
61+
});
62+
63+
public static ScriptEvent<ServerPlayer> PLAYER_LEAVES_SERVER = new ScriptEvent<>("Executes when a player joins the server.", b -> {
64+
b.add("player", ScriptObjects.PLAYER, "Player that left.");
65+
}, (b, ctx, script) -> {
66+
b.add("player", ScriptObjects.PLAYER.provideTable(ctx, script));
67+
});
68+
5569

5670

5771
public static void registerAll() {
5872
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("load"), LOAD);
5973
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("tick"), TICK);
6074
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("entity_takes_damage"), ENTITY_TAKES_DAMAGE);
6175
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("entity_dies"), ENTITY_DIES);
76+
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("player_joins_server"), PLAYER_JOINS_SERVER);
77+
Registry.register(LuafyRegistries.SCRIPT_EVENTS, Luafy.id("player_leaves_server"), PLAYER_LEAVES_SERVER);
6278
}
6379

6480

@@ -86,11 +102,16 @@ public static void applyEvents() {
86102
ENTITY_DIES.trigger(e.level().getServer().createCommandSourceStack(), new ScriptEvents.EntityDies(e, src));
87103
});
88104

89-
105+
// join/leave server
106+
ServerPlayerEvents.JOIN.register(player -> {
107+
PLAYER_JOINS_SERVER.trigger(player.level().getServer().createCommandSourceStack(), player);
108+
});
109+
ServerPlayerEvents.LEAVE.register(player -> {
110+
PLAYER_JOINS_SERVER.trigger(player.level().getServer().createCommandSourceStack(), player);
111+
});
90112

91113
}
92114

93115
public record EntityTakesDamage(LivingEntity e, DamageSource src, float damageTaken, boolean blocked) {}
94116
public record EntityDies(LivingEntity e, DamageSource src) {}
95-
96117
}

src/main/resources/luafy.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"package": "dev.diamond.luafy.mixin",
44
"compatibilityLevel": "JAVA_21",
55
"mixins": [
6+
"CommandBlockMixin",
67
"CommandSourceStackAccessor"
78
],
89
"injectors": {

0 commit comments

Comments
 (0)