blob: 780b9f2029a2b9750c813eff0d699bdeacd51443 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package net.girlonthe.simplerenamecommand;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.arguments.ComponentArgument;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.OutgoingChatMessage;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.network.chat.ChatType.Bound;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
public class SimpleRenameCommand implements ModInitializer {
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(
Commands.literal("renameitem")
.then(Commands.argument("itemName", ComponentArgument.textComponent(registryAccess))
.executes(ctx -> {
ServerPlayer src = ctx.getSource().getPlayerOrException();
ItemStack stack = src.getMainHandItem();
if (stack != null) {
stack.set(DataComponents.CUSTOM_NAME, ctx.getArgument("itemName", Component.class));
} else {
src.sendChatMessage(OutgoingChatMessage.create(PlayerChatMessage.system("slur")), true, ChatType.bind(ChatType.CHAT, ctx.getSource()));
}
return Command.SINGLE_SUCCESS;
})));
});
}
}
|