Files
fabric-carpet/src/main/java/carpet/commands/TickCommand.java
T
altrisi 0a972d6c56 Settings rework and encapsulation (#1330)
* Settings rework and encapsulation

* Settings overhaul 3

Move everything in the new system to a new API package

Require rules using the new system to specify descriptions in their translation files

Implement temporary system on top of current translations to allow that to work properly

* Fix getRules fallback

* Final touches to settings refactor

* Mention that set shouldn't throw and remove comment

* Change translation keys to unify with translation PR

* Deprecate printAllRulesToLog to allow using any PrintStream

* Remove restriction of CarpetRules being Comparable

And fix overreaction in a deprecation message

* Bring back user input to rule observers

* Make Validator deprecation warning not spam on use

* Remove unneeded abstract methods in CarpetRule already inherited from CarpetRule

They literally do nothing, don't even force them to be implemented, and may be confusing as to why they're there or as if they are needed to be implemented

* Fix legacy SettingsManager field not being assigned correctly

* Finish fixing conflicts from future merge

* Actually fix remaining conflicts from future merge (missing imports)

* Import the newer rule in SettingsManager

* Refactor Validators

* Don't spam deprecation warnings on custom managers

* Get out the var

* Fix missing warning in old rule observers

* Update src/main/java/carpet/api/settings/Rule.java

* Remove `@ApiStatus.Experimental` annotations

* Fix incorrect `@deprecated` javadoc tags

By removing them given the class is deprecated anyway and else there's too much noise

* Add missing static import

* Remove TypedField and use a regular field

I liked that class tbh, but it's true that it wasn't really needed

* Move `field` assignment out of for removal

* Fix not following contract of `type()` returning boxed value

Previously handled by TypedField

* Fix few incorrect translation keys

* Remove resaving of comments when saving config files

Comments ignoring is still a thing when reading, so `locked` configs for example will keep comments

* Condense rule load messages in one

* Only print loaded settings message when at least one setting was changed

* Fix legacy validator detector

* Tweak and add more context to response to invalid values

* Fix strict validator not actually erroring on invalid input
2022-06-27 13:12:15 +02:00

169 lines
6.9 KiB
Java

package carpet.commands;
import carpet.CarpetSettings;
import carpet.helpers.TickSpeed;
import carpet.network.ServerNetworkHandler;
import carpet.utils.CarpetProfiler;
import carpet.utils.CommandHelper;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import static com.mojang.brigadier.arguments.FloatArgumentType.floatArg;
import static com.mojang.brigadier.arguments.FloatArgumentType.getFloat;
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
import static net.minecraft.commands.SharedSuggestionProvider.suggest;
public class TickCommand
{
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("tick").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandTick)).
then(literal("rate").
executes((c) -> queryTps(c.getSource())).
then(argument("rate", floatArg(0.1F, 500.0F)).
suggests( (c, b) -> suggest(new String[]{"20.0"},b)).
executes((c) -> setTps(c.getSource(), getFloat(c, "rate"))))).
then(literal("warp").
executes( (c)-> setWarp(c.getSource(), 0, null)).
then(argument("ticks", integer(0)).
suggests( (c, b) -> suggest(new String[]{"3600","72000"},b)).
executes((c) -> setWarp(c.getSource(), getInteger(c,"ticks"), null)).
then(argument("tail command", greedyString()).
executes( (c) -> setWarp(
c.getSource(),
getInteger(c,"ticks"),
getString(c, "tail command")))))).
then(literal("freeze").executes( (c)-> toggleFreeze(c.getSource(), false)).
then(literal("status").executes( (c) -> freezeStatus(c.getSource()))).
then(literal("deep").executes( (c) -> toggleFreeze(c.getSource(), true))).
then(literal("on").executes( (c) -> setFreeze(c.getSource(), false, true)).
then(literal("deep").executes( (c)-> setFreeze(c.getSource(), true, true)))).
then(literal("off").executes( (c) -> setFreeze(c.getSource(), false, false)))).
then(literal("step").
executes((c) -> step(c.getSource(), 1)).
then(argument("ticks", integer(1,72000)).
suggests( (c, b) -> suggest(new String[]{"20"},b)).
executes((c) -> step(c.getSource(), getInteger(c,"ticks"))))).
then(literal("superHot").executes( (c)-> toggleSuperHot(c.getSource()))).
then(literal("health").
executes( (c) -> healthReport(c.getSource(), 100)).
then(argument("ticks", integer(20,24000)).
executes( (c) -> healthReport(c.getSource(), getInteger(c, "ticks"))))).
then(literal("entities").
executes((c) -> healthEntities(c.getSource(), 100)).
then(argument("ticks", integer(20,24000)).
executes((c) -> healthEntities(c.getSource(), getInteger(c, "ticks")))));
dispatcher.register(literalargumentbuilder);
}
private static int setTps(CommandSourceStack source, float tps)
{
TickSpeed.tickrate(tps, true);
queryTps(source);
return (int)tps;
}
private static int queryTps(CommandSourceStack source)
{
Messenger.m(source, "w Current tps is: ",String.format("wb %.1f", TickSpeed.tickrate));
return (int)TickSpeed.tickrate;
}
private static int setWarp(CommandSourceStack source, int advance, String tail_command)
{
ServerPlayer player = null;
try
{
player = source.getPlayerOrException();
}
catch (CommandSyntaxException ignored)
{
}
Component message = TickSpeed.tickrate_advance(player, advance, tail_command, source);
source.sendSuccess(message, false);
return 1;
}
private static int freezeStatus(CommandSourceStack source)
{
if(TickSpeed.isPaused())
{
Messenger.m(source, "gi Freeze Status: Game is "+(TickSpeed.deeplyFrozen()?"deeply ":"")+"frozen");
}
else
{
Messenger.m(source, "gi Freeze Status: Game runs normally");
}
return 1;
}
private static int setFreeze(CommandSourceStack source, boolean isDeep, boolean freeze)
{
TickSpeed.setFrozenState(freeze, isDeep);
if (TickSpeed.isPaused())
{
Messenger.m(source, "gi Game is "+(isDeep?"deeply ":"")+"frozen");
}
else
{
Messenger.m(source, "gi Game runs normally");
}
return 1;
}
private static int toggleFreeze(CommandSourceStack source, boolean isDeep)
{
return setFreeze(source, isDeep, !TickSpeed.isPaused());
}
private static int step(CommandSourceStack source, int advance)
{
TickSpeed.add_ticks_to_run_in_pause(advance);
Messenger.m(source, "gi Stepping " + advance + " tick" + (advance != 1 ? "s" : ""));
return 1;
}
private static int toggleSuperHot(CommandSourceStack source)
{
TickSpeed.is_superHot = !TickSpeed.is_superHot;
ServerNetworkHandler.updateSuperHotStateToConnectedPlayers();
if (TickSpeed.is_superHot)
{
Messenger.m(source, "gi Superhot enabled");
}
else
{
Messenger.m(source, "gi Superhot disabled");
}
return 1;
}
public static int healthReport(CommandSourceStack source, int ticks)
{
CarpetProfiler.prepare_tick_report(source, ticks);
return 1;
}
public static int healthEntities(CommandSourceStack source, int ticks)
{
CarpetProfiler.prepare_entity_report(source, ticks);
return 1;
}
}