Skip to content

Commit

Permalink
Add update option for head command
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pink-hacker committed Jan 14, 2024
1 parent f337652 commit 55cf766
Showing 1 changed file with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

public class HeadCommand implements CommandRegistrationCallback {
private static final SimpleCommandExceptionType GIVE_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.head.give.fail"));
Expand All @@ -38,11 +39,11 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
.then(CommandManager.literal("give")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("targets", EntityArgumentType.players())
.then(CommandManager.argument("heads", GameProfileArgumentType.gameProfile())
.then(CommandManager.argument("player", GameProfileArgumentType.gameProfile())
.executes(context -> give(
context.getSource(),
EntityArgumentType.getPlayers(context, "targets"),
GameProfileArgumentType.getProfileArgument(context, "heads")
GameProfileArgumentType.getProfileArgument(context, "player")
))
)
)
Expand All @@ -64,6 +65,23 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
BlockPosArgumentType.getLoadedBlockPos(context, "pos")
))
)
// TODO: Add is up-to-date
)
)
.then(CommandManager.literal("update")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("pos", BlockPosArgumentType.blockPos())
.then(CommandManager.argument("player", GameProfileArgumentType.gameProfile())
.executes(context -> updateHead(
context.getSource(),
BlockPosArgumentType.getLoadedBlockPos(context, "pos"),
GameProfileArgumentType.getProfileArgument(context, "player").stream().findFirst().orElseThrow()
))
)
.executes(context -> updateHead(
context.getSource(),
BlockPosArgumentType.getLoadedBlockPos(context, "pos")
))
)
)
);
Expand Down Expand Up @@ -142,6 +160,39 @@ private static int queryName(ServerCommandSource source, BlockPos pos) {
}

private static Text copyText(String key, String copyText) {
return Text.translatable(key, Texts.bracketed(Text.literal(String.valueOf(copyText)).styled((style) -> style.withColor(Formatting.GREEN).withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, String.valueOf(copyText))).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable(copyText))).withInsertion(String.valueOf(copyText)))));
return Text.translatable(
key,
Texts.bracketed(Text.literal(String.valueOf(copyText))
.styled((style) -> style
.withColor(Formatting.GREEN)
.withClickEvent(new ClickEvent(
ClickEvent.Action.COPY_TO_CLIPBOARD,
String.valueOf(copyText)
))
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.translatable(copyText)
))
.withInsertion(String.valueOf(copyText))
)
)
);
}

private static int updateHead(ServerCommandSource source, BlockPos pos, GameProfile profile) {
int i = 0;

if (source.getWorld().getBlockEntity(pos) instanceof SkullBlockEntity entity) {
// TODO: Fix heads stay cached until reloading the world
entity.setOwner(profile);

i = 1;
}

return i;
}

private static int updateHead(ServerCommandSource source, BlockPos pos) {
return updateHead(source, pos, source.getPlayer().getGameProfile());
}
}

0 comments on commit 55cf766

Please sign in to comment.