Skip to content

Commit

Permalink
Added progresstf (redirects to progresstransfur), regresstf, and gett…
Browse files Browse the repository at this point in the history
…f. Modified tf/untf/progresstf to accept multiple players. LtxProgrammer#1113
  • Loading branch information
LtxProgrammer committed Oct 22, 2024
1 parent bef3546 commit 8b74d10
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 17 deletions.
193 changes: 176 additions & 17 deletions src/main/java/net/ltxprogrammer/changed/command/CommandTransfur.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.ltxprogrammer.changed.Changed;
import net.ltxprogrammer.changed.entity.ChangedEntity;
import net.ltxprogrammer.changed.entity.TransfurCause;
import net.ltxprogrammer.changed.entity.TransfurContext;
import net.ltxprogrammer.changed.entity.variant.TransfurVariant;
import net.ltxprogrammer.changed.entity.variant.TransfurVariantInstance;
import net.ltxprogrammer.changed.extension.ChangedCompatibility;
import net.ltxprogrammer.changed.init.ChangedItems;
import net.ltxprogrammer.changed.init.ChangedRegistry;
Expand All @@ -23,9 +25,11 @@
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.commands.arguments.UuidArgument;
import net.minecraft.commands.synchronization.SuggestionProviders;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -34,6 +38,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Mod.EventBusSubscriber
Expand All @@ -57,30 +64,59 @@ public class CommandTransfur {
@SubscribeEvent
public static void registerCommands(RegisterCommandsEvent event) {
var transfurNode = event.getDispatcher().register(Commands.literal("transfur").requires(p -> p.hasPermission(2))
.then(Commands.argument("player", EntityArgument.player())
.then(Commands.argument("players", EntityArgument.player())
.then(Commands.argument("form", ResourceLocationArgument.id()).suggests(SUGGEST_LATEX_FORMS)
.executes(context -> transfurPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
.executes(context -> transfurPlayers(context.getSource(),
EntityArgument.getPlayers(context, "players"),
ResourceLocationArgument.getId(context, "form"),
TransfurCause.GRAB_REPLICATE.name()))
.then(Commands.argument("cause", StringArgumentType.string()).suggests(SUGGEST_TRANSFUR_CAUSE)
.executes(context -> transfurPlayer(context.getSource(),
EntityArgument.getPlayer(context, "player"),
.executes(context -> transfurPlayers(context.getSource(),
EntityArgument.getPlayers(context, "players"),
ResourceLocationArgument.getId(context, "form"),
StringArgumentType.getString(context, "cause"))))
)));
event.getDispatcher().register(Commands.literal("tf").requires(p -> p.hasPermission(2)).redirect(transfurNode));
event.getDispatcher().register(Commands.literal("progresstransfur").requires(p -> p.hasPermission(2))
.then(Commands.argument("player", EntityArgument.player())
var progressTransfurNode = event.getDispatcher().register(Commands.literal("progresstransfur").requires(p -> p.hasPermission(2))
.then(Commands.argument("players", EntityArgument.players())
.then(Commands.argument("form", ResourceLocationArgument.id()).suggests(SUGGEST_LATEX_FORMS)
.then(Commands.argument("progression", FloatArgumentType.floatArg(0.0f))
.executes(context -> progressPlayerTransfur(context.getSource(), EntityArgument.getPlayer(context, "player"), ResourceLocationArgument.getId(context, "form"), FloatArgumentType.getFloat(context, "progression")))
.executes(context -> progressPlayersTransfur(context.getSource(),
EntityArgument.getPlayers(context, "players"),
ResourceLocationArgument.getId(context, "form"),
FloatArgumentType.getFloat(context, "progression"),
TransfurCause.GRAB_REPLICATE.name()))
.then(Commands.argument("cause", StringArgumentType.string()).suggests(SUGGEST_TRANSFUR_CAUSE).executes(context -> progressPlayersTransfur(context.getSource(),
EntityArgument.getPlayers(context, "players"),
ResourceLocationArgument.getId(context, "form"),
FloatArgumentType.getFloat(context, "progression"),
StringArgumentType.getString(context, "cause"))))
))));
event.getDispatcher().register(Commands.literal("progresstf").requires(p -> p.hasPermission(2)).redirect(progressTransfurNode));

var regressTransfurNode = event.getDispatcher().register(Commands.literal("regresstransfur").requires(p -> p.hasPermission(2))
.executes(context -> regressPlayersTransfur(context.getSource(),
Collections.singleton(context.getSource().getPlayerOrException()),
Float.MAX_VALUE))
.then(Commands.argument("players", EntityArgument.players())
.executes(context -> regressPlayersTransfur(context.getSource(),
EntityArgument.getPlayers(context, "players"),
Float.MAX_VALUE))
.then(Commands.argument("regression", FloatArgumentType.floatArg(0.0f))
.executes(context -> regressPlayersTransfur(context.getSource(),
EntityArgument.getPlayers(context, "players"),
FloatArgumentType.getFloat(context, "regression")))
)));
event.getDispatcher().register(Commands.literal("regresstf").requires(p -> p.hasPermission(2)).redirect(regressTransfurNode)
.executes(context -> regressPlayersTransfur(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException()), Float.MAX_VALUE)));

var untransfurNode = event.getDispatcher().register(Commands.literal("untransfur").requires(p -> p.hasPermission(2))
.then(Commands.argument("player", EntityArgument.player())
.executes(context -> untransfurPlayer(context.getSource(), EntityArgument.getPlayer(context, "player")))
.executes(context -> untransfurPlayers(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException())))
.then(Commands.argument("players", EntityArgument.players())
.executes(context -> untransfurPlayers(context.getSource(), EntityArgument.getPlayers(context, "players")))
));
event.getDispatcher().register(Commands.literal("untf").requires(p -> p.hasPermission(2)).redirect(untransfurNode));
event.getDispatcher().register(Commands.literal("untf").requires(p -> p.hasPermission(2)).redirect(untransfurNode)
.executes(context -> untransfurPlayers(context.getSource(), Collections.singleton(context.getSource().getPlayerOrException()))));
if (SharedConstants.IS_RUNNING_IN_IDE || !FMLLoader.isProduction()) {
event.getDispatcher().register(Commands.literal("specialsyringe").requires(p -> p.hasPermission(3))
.then(Commands.argument("uuid", UuidArgument.uuid())
Expand All @@ -93,6 +129,12 @@ public static void registerCommands(RegisterCommandsEvent event) {
})
));
}

event.getDispatcher().register(Commands.literal("gettf").requires(p -> p.hasPermission(1))
.executes(context -> getTransfur(context.getSource(), context.getSource().getPlayerOrException()))
.then(Commands.argument("players", EntityArgument.player())
.executes(context -> getTransfur(context.getSource(), EntityArgument.getPlayer(context, "players")))
));
}

private static int transfurPlayer(CommandSourceStack source, ServerPlayer player, ResourceLocation form, String cause) throws CommandSyntaxException {
Expand Down Expand Up @@ -123,31 +165,148 @@ else if (form.equals(TransfurVariant.SPECIAL_LATEX)) {
return Command.SINGLE_SUCCESS;
}

private static int progressPlayerTransfur(CommandSourceStack source, ServerPlayer player, ResourceLocation form, float progression) throws CommandSyntaxException {
private static int transfurPlayers(CommandSourceStack source, Collection<ServerPlayer> players, ResourceLocation form, String cause) throws CommandSyntaxException {
if (players.size() == 1) {
final ServerPlayer player = players.stream().findFirst().get();
int success = transfurPlayer(source, player, form, cause);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.transfurred.one", player.getScoreboardName(), form), false);
return success;
} else if (players.size() > 1) {
int success = players.stream().map(player -> {
try {
return transfurPlayer(source, player, form, cause);
} catch (CommandSyntaxException e) {
return 0;
}
}).reduce(0, Integer::sum);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.transfurred.many", success, form), false);
return success;
} else
return 0;
}

private static int progressPlayerTransfur(CommandSourceStack source, ServerPlayer player, ResourceLocation form, float progression, String cause) throws CommandSyntaxException {
if (ChangedCompatibility.isPlayerUsedByOtherMod(player))
throw USED_BY_OTHER_MOD.create();

final TransfurCause transfurCause;
try {
transfurCause = TransfurCause.valueOf(cause.toUpperCase());
} catch (IllegalArgumentException ex) {
throw NOT_CAUSE.create();
}
final TransfurContext context;
if (source.getEntity() instanceof Player sourcePlayer)
context = TransfurContext.playerLatexHazard(sourcePlayer, transfurCause);
else if (source.getEntity() instanceof ChangedEntity sourceNpc)
context = TransfurContext.npcLatexHazard(sourceNpc, transfurCause);
else
context = null;

if (TransfurVariant.getPublicTransfurVariants().map(TransfurVariant::getRegistryName).anyMatch(form::equals)) {
ProcessTransfur.progressTransfur(player, progression, ChangedRegistry.TRANSFUR_VARIANT.get().getValue(form));
ProcessTransfur.progressPlayerTransfur(player, progression, ChangedRegistry.TRANSFUR_VARIANT.get().getValue(form), context);
}
else if (form.equals(TransfurVariant.SPECIAL_LATEX)) {
ResourceLocation key = Changed.modResource("special/form_" + player.getUUID());
if (!ChangedRegistry.TRANSFUR_VARIANT.get().containsKey(key))
throw NO_SPECIAL_FORM.create();

ProcessTransfur.progressTransfur(player, progression, ChangedRegistry.TRANSFUR_VARIANT.get().getValue(key));
ProcessTransfur.progressPlayerTransfur(player, progression, ChangedRegistry.TRANSFUR_VARIANT.get().getValue(key), context);
}
else
throw NOT_LATEX_FORM.create();
return Command.SINGLE_SUCCESS;
}

private static int progressPlayersTransfur(CommandSourceStack source, Collection<ServerPlayer> players, ResourceLocation form, float progression, String cause) throws CommandSyntaxException {
if (players.size() == 1) {
final ServerPlayer player = players.stream().findFirst().get();
int success = progressPlayerTransfur(source, player, form, progression, cause);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.progresstf.one", player.getScoreboardName()), false);
return success;
} else if (players.size() > 1) {
int success = players.stream().map(player -> {
try {
return progressPlayerTransfur(source, player, form, progression, cause);
} catch (CommandSyntaxException e) {
return 0;
}
}).reduce(0, Integer::sum);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.progresstf.many", success), false);
return success;
} else
return 0;
}

private static int regressPlayerTransfur(CommandSourceStack source, ServerPlayer player, float regression) throws CommandSyntaxException {
if (ChangedCompatibility.isPlayerUsedByOtherMod(player))
throw USED_BY_OTHER_MOD.create();

float progress = ProcessTransfur.getPlayerTransfurProgress(player);
if (progress > 0.0f)
ProcessTransfur.setPlayerTransfurProgress(player, Math.max(progress - regression, 0.0f));

return Command.SINGLE_SUCCESS;
}

private static int regressPlayersTransfur(CommandSourceStack source, Collection<ServerPlayer> players, float regression) throws CommandSyntaxException {
if (players.size() == 1) {
final ServerPlayer player = players.stream().findFirst().get();
int success = regressPlayerTransfur(source, player, regression);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.regresstf.one", player.getScoreboardName()), false);
return success;
} else if (players.size() > 1) {
int success = players.stream().map(player -> {
try {
return regressPlayerTransfur(source, player, regression);
} catch (CommandSyntaxException e) {
return 0;
}
}).reduce(0, Integer::sum);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.regresstf.many", success), false);
return success;
} else
return 0;
}

private static int untransfurPlayer(CommandSourceStack source, ServerPlayer player) {
ProcessTransfur.ifPlayerTransfurred(player, variant -> {
return ProcessTransfur.ifPlayerTransfurred(player, variant -> {
variant.unhookAll(player);
ProcessTransfur.removePlayerTransfurVariant(player);
ProcessTransfur.setPlayerTransfurProgress(player, 0.0f);
});
return Command.SINGLE_SUCCESS;
}) ? Command.SINGLE_SUCCESS : 0;
}

private static int untransfurPlayers(CommandSourceStack source, Collection<ServerPlayer> players) {
if (players.size() == 1) {
final ServerPlayer player = players.stream().findFirst().get();
int success = untransfurPlayer(source, player);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.untransfurred.one", player.getScoreboardName()), false);
return success;
} else if (players.size() > 1) {
int success = players.stream().map(player -> untransfurPlayer(source, player)).reduce(0, Integer::sum);
if (success > 0)
source.sendSuccess(new TranslatableComponent("command.changed.success.untransfurred.many", success), false);
return success;
} else
return 0;
}

private static int getTransfur(CommandSourceStack source, ServerPlayer player, Predicate<TransfurVariantInstance<?>> test) {
return ProcessTransfur.getPlayerTransfurVariantSafe(player).filter(test).map(instance -> {
source.sendSuccess(new TextComponent(player.getScoreboardName() + ": " + instance.getFormId().toString()), false);
return 1;
}).orElse(0);
}

private static int getTransfur(CommandSourceStack source, ServerPlayer player) {
return getTransfur(source, player, variant -> true);
}
}
9 changes: 9 additions & 0 deletions src/main/resources/assets/changed/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@
"death.attack.changed:fan": "%s was diced by a fan",
"death.attack.changed:fan.player": "%1$s was diced by a fan whilst fighting %2$s",

"command.changed.success.transfurred.one": "Transfurred %s into %s",
"command.changed.success.transfurred.many": "Transfurred %s player(s) into %s",
"command.changed.success.untransfurred.one": "Untransfurred %s",
"command.changed.success.untransfurred.many": "Untransfurred %s player(s)",
"command.changed.success.progresstf.one": "Progressed transfur for %s",
"command.changed.success.progresstf.many": "Progressed transfur for %s player(s)",
"command.changed.success.regresstf.one": "Regressed transfur for %s",
"command.changed.success.regresstf.many": "Regressed transfur for %s player(s)",

"command.changed.error.not_latex_form": "Invalid latex form",
"command.changed.error.not_cause": "Invalid transfur cause",
"command.changed.error.used_by_other_mod": "Player is already used by another mod",
Expand Down

0 comments on commit 8b74d10

Please sign in to comment.