aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/girlonthe/simplerenamecommand/SimpleRenameCommand.java
blob: 3cf709fabbde962758554317d34cc310a2bda5aa (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
43
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.ChatFormatting;
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.isEmpty()) {
                                  stack.set(DataComponents.CUSTOM_NAME, ctx.getArgument("itemName", Component.class));
                              } else {
                                  src.sendSystemMessage(Component.literal("You must be holding an item.").withStyle(ChatFormatting.RED));
                              }

                              return Command.SINGLE_SUCCESS;
                          })));
        }); 
    }
}