mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
add reset subcommand
This commit is contained in:
@@ -16,9 +16,12 @@ import net.lionarius.skinrestorer.util.PlayerUtils;
|
||||
import net.lionarius.skinrestorer.util.Translation;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.arguments.GameProfileArgument;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -32,27 +35,32 @@ public final class SkinCommand {
|
||||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
LiteralArgumentBuilder<CommandSourceStack> base =
|
||||
literal("skin")
|
||||
.then(buildAction("clear", SkinValue.EMPTY::toProviderContext));
|
||||
.then(buildSetAction("clear", SkinValue.EMPTY::toProviderContext))
|
||||
.then(literal("reset")
|
||||
.executes(context -> resetAction(context.getSource()))
|
||||
.then(makeTargetsArgument(
|
||||
(context, profiles) -> resetAction(context.getSource(), profiles, true)
|
||||
)));
|
||||
|
||||
LiteralArgumentBuilder<CommandSourceStack> set = literal("set");
|
||||
|
||||
var providers = SkinRestorer.getProvidersRegistry().getPublicProviders();
|
||||
for (var entry : providers)
|
||||
set.then(buildAction(entry.first(), entry.second()));
|
||||
set.then(buildSetAction(entry.first(), entry.second()));
|
||||
|
||||
base.then(set);
|
||||
|
||||
dispatcher.register(base);
|
||||
}
|
||||
|
||||
private static LiteralArgumentBuilder<CommandSourceStack> buildAction(String name, SkinProvider provider) {
|
||||
private static LiteralArgumentBuilder<CommandSourceStack> buildSetAction(String name, SkinProvider provider) {
|
||||
LiteralArgumentBuilder<CommandSourceStack> action = literal(name);
|
||||
|
||||
if (provider.hasVariantSupport()) {
|
||||
for (SkinVariant variant : SkinVariant.values()) {
|
||||
action.then(
|
||||
literal(variant.toString())
|
||||
.then(buildArgument(
|
||||
.then(buildSetArgument(
|
||||
argument(provider.getArgumentName(), StringArgumentType.string()),
|
||||
context -> {
|
||||
var argument = StringArgumentType.getString(context, provider.getArgumentName());
|
||||
@@ -63,8 +71,9 @@ public final class SkinCommand {
|
||||
}
|
||||
} else {
|
||||
action.then(
|
||||
buildArgument(
|
||||
argument(provider.getArgumentName(), StringArgumentType.string()), context -> {
|
||||
buildSetArgument(
|
||||
argument(provider.getArgumentName(), StringArgumentType.string()),
|
||||
context -> {
|
||||
var argument = StringArgumentType.getString(context, provider.getArgumentName());
|
||||
return new SkinProviderContext(name, argument, null);
|
||||
}
|
||||
@@ -75,43 +84,79 @@ public final class SkinCommand {
|
||||
return action;
|
||||
}
|
||||
|
||||
private static ArgumentBuilder<CommandSourceStack, LiteralArgumentBuilder<CommandSourceStack>> buildAction(
|
||||
private static ArgumentBuilder<CommandSourceStack, LiteralArgumentBuilder<CommandSourceStack>> buildSetAction(
|
||||
String name,
|
||||
Supplier<SkinProviderContext> supplier
|
||||
) {
|
||||
return buildArgument(literal(name), context -> supplier.get());
|
||||
return buildSetArgument(literal(name), context -> supplier.get());
|
||||
}
|
||||
|
||||
private static <T extends ArgumentBuilder<CommandSourceStack, T>> ArgumentBuilder<CommandSourceStack, T> buildArgument(
|
||||
private static <T extends ArgumentBuilder<CommandSourceStack, T>> ArgumentBuilder<CommandSourceStack, T> buildSetArgument(
|
||||
ArgumentBuilder<CommandSourceStack, T> argument,
|
||||
Function<CommandContext<CommandSourceStack>, SkinProviderContext> provider
|
||||
) {
|
||||
return argument
|
||||
.executes(context -> skinAction(
|
||||
.executes(context -> setAction(
|
||||
context.getSource(),
|
||||
provider.apply(context)
|
||||
))
|
||||
.then(makeTargetsArgument(provider));
|
||||
}
|
||||
|
||||
private static RequiredArgumentBuilder<CommandSourceStack, GameProfileArgument.Result> makeTargetsArgument(
|
||||
Function<CommandContext<CommandSourceStack>, SkinProviderContext> provider
|
||||
) {
|
||||
return argument("targets", GameProfileArgument.gameProfile())
|
||||
.requires(source -> source.hasPermission(2))
|
||||
.executes(context -> skinAction(
|
||||
provider.apply(context),
|
||||
context.getSource(),
|
||||
GameProfileArgument.getGameProfiles(context, "targets"),
|
||||
true
|
||||
.then(makeTargetsArgument(
|
||||
(context, targets) -> setAction(
|
||||
context.getSource(),
|
||||
targets,
|
||||
provider.apply(context),
|
||||
true
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
private static int skinAction(
|
||||
SkinProviderContext context,
|
||||
private static RequiredArgumentBuilder<CommandSourceStack, GameProfileArgument.Result> makeTargetsArgument(
|
||||
BiFunction<CommandContext<CommandSourceStack>, Collection<GameProfile>, Integer> consumer
|
||||
) {
|
||||
return argument("targets", GameProfileArgument.gameProfile())
|
||||
.requires(source -> source.hasPermission(2))
|
||||
.executes(context -> consumer.apply(context, GameProfileArgument.getGameProfiles(context, "targets")));
|
||||
}
|
||||
|
||||
private static int resetAction(
|
||||
CommandSourceStack src,
|
||||
Collection<GameProfile> targets,
|
||||
boolean setByOperator
|
||||
) {
|
||||
Collection<ServerPlayer> updatedPlayers = new HashSet<>();
|
||||
for (var profile : targets) {
|
||||
SkinValue skin = null;
|
||||
if (SkinRestorer.getSkinStorage().hasSavedSkin(profile.getId()))
|
||||
skin = SkinRestorer.getSkinStorage().getSkin(profile.getId()).replaceValueWithOriginal();
|
||||
|
||||
if (skin == null)
|
||||
continue;
|
||||
|
||||
var updatedPlayer = SkinRestorer.applySkin(src.getServer(), Collections.singleton(profile), skin);
|
||||
|
||||
SkinRestorer.getSkinStorage().deleteSkin(profile.getId());
|
||||
updatedPlayers.addAll(updatedPlayer);
|
||||
}
|
||||
|
||||
SkinCommand.sendResponse(src, updatedPlayers, setByOperator);
|
||||
|
||||
return targets.size();
|
||||
}
|
||||
|
||||
private static int resetAction(
|
||||
CommandSourceStack src
|
||||
) {
|
||||
if (src.getPlayer() == null)
|
||||
return 0;
|
||||
|
||||
return resetAction(src, Collections.singleton(src.getPlayer().getGameProfile()), false);
|
||||
}
|
||||
|
||||
private static int setAction(
|
||||
CommandSourceStack src,
|
||||
Collection<GameProfile> targets,
|
||||
SkinProviderContext context,
|
||||
boolean setByOperator
|
||||
) {
|
||||
src.sendSystemMessage(Translation.translatableWithFallback(Translation.COMMAND_SKIN_LOADING_KEY));
|
||||
|
||||
@@ -126,39 +171,41 @@ public final class SkinCommand {
|
||||
|
||||
var updatedPlayers = result.getSuccessValue();
|
||||
|
||||
if (updatedPlayers.isEmpty()) {
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_NO_CHANGES_KEY
|
||||
), true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (setByOperator) {
|
||||
var playersComponent = PlayerUtils.createPlayerListComponent(updatedPlayers);
|
||||
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_AFFECTED_PLAYERS_KEY,
|
||||
playersComponent
|
||||
), true);
|
||||
} else {
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_OK_KEY
|
||||
), true);
|
||||
}
|
||||
SkinCommand.sendResponse(src, updatedPlayers, setByOperator);
|
||||
});
|
||||
|
||||
return targets.size();
|
||||
}
|
||||
|
||||
private static int skinAction(
|
||||
private static int setAction(
|
||||
CommandSourceStack src,
|
||||
SkinProviderContext context
|
||||
) {
|
||||
if (src.getPlayer() == null)
|
||||
return 0;
|
||||
|
||||
return skinAction(context, src, Collections.singleton(src.getPlayer().getGameProfile()), false);
|
||||
return setAction(src, Collections.singleton(src.getPlayer().getGameProfile()), context, false);
|
||||
}
|
||||
|
||||
private static void sendResponse(CommandSourceStack src, Collection<ServerPlayer> updatedPlayers, boolean setByOperator) {
|
||||
if (updatedPlayers.isEmpty()) {
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_NO_CHANGES_KEY
|
||||
), true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (setByOperator) {
|
||||
var playersComponent = PlayerUtils.createPlayerListComponent(updatedPlayers);
|
||||
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_AFFECTED_PLAYERS_KEY,
|
||||
playersComponent
|
||||
), true);
|
||||
} else {
|
||||
src.sendSuccess(() -> Translation.translatableWithFallback(
|
||||
Translation.COMMAND_SKIN_OK_KEY
|
||||
), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public class SkinStorage {
|
||||
}
|
||||
|
||||
public void deleteSkin(UUID uuid) {
|
||||
this.removeSkin(uuid, false);
|
||||
this.skinIO.deleteSkin(uuid);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ public record SkinValue(@NotNull String provider, @Nullable String argument, @Nu
|
||||
return new SkinProviderContext(this.provider, this.argument, this.variant);
|
||||
}
|
||||
|
||||
public SkinValue replaceValueWithOriginal() {
|
||||
return new SkinValue(this.provider, this.argument, this.variant, this.originalValue, this.originalValue);
|
||||
}
|
||||
|
||||
public SkinValue setOriginalValue(Property originalValue) {
|
||||
return new SkinValue(this.provider, this.argument, this.variant, this.value, originalValue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user