Skip to content

Commit

Permalink
合并拉取请求 #142
Browse files Browse the repository at this point in the history
/getPlayerSkull可以获取任意玩家头颅
  • Loading branch information
1024-byteeeee authored Sep 26, 2024
2 parents c8d360e + c0e73c0 commit ca21ac7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.command.argument.EntityArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand All @@ -38,16 +39,17 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
CommandManager.literal("getPlayerSkull")
.requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandGetPlayerSkull))
.then(CommandManager.argument("player", EntityArgumentType.players())
.then(CommandManager.argument("player", StringArgumentType.string())
.suggests((context, builder) -> CommandSource.suggestMatching(context.getSource().getServer().getPlayerNames(), builder))
.executes(context -> execute(
context.getSource().getPlayer(), EntityArgumentType.getPlayer(context, "player"))
context.getSource().getPlayer(), StringArgumentType.getString(context, "player"))
))
);
}

private static int execute(PlayerEntity player, PlayerEntity targetPlayer) {
private static int execute(PlayerEntity player, String name) {
ItemStack headStack = new ItemStack(Items.PLAYER_HEAD);
SkullSkinHelper.writeNbtToPlayerSkull(targetPlayer, headStack);
SkullSkinHelper.writeNbtToPlayerSkull(name, headStack);
player.giveItemStack(headStack);
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
//#if MC>=12005
//$$ import net.minecraft.component.DataComponentTypes;
//$$ import net.minecraft.component.type.ProfileComponent;
//$$ import com.mojang.authlib.properties.PropertyMap;
//$$ import java.util.Optional;
//$$ import net.minecraft.nbt.NbtOps;
//$$ import net.minecraft.nbt.NbtString;
//#endif
import net.minecraft.item.ItemStack;

Expand All @@ -36,4 +40,14 @@ public static void writeNbtToPlayerSkull(PlayerEntity player, ItemStack headStac
headStack.getOrCreateNbt().putString("SkullOwner", player.getGameProfile().getName());
//#endif
}

public static void writeNbtToPlayerSkull(String name, ItemStack headStack) {
//#if MC>=12005
//$$ ProfileComponent profileComponent = DataComponentTypes.PROFILE.getCodecOrThrow()
//$$ .parse(NbtOps.INSTANCE, NbtString.of(name)).getOrThrow();
//$$ headStack.set(DataComponentTypes.PROFILE, profileComponent);
//#else
headStack.getOrCreateNbt().putString("SkullOwner", name);
//#endif
}
}

0 comments on commit ca21ac7

Please sign in to comment.