Unicopia/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java

67 lines
3.1 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.command;
2020-01-27 11:05:22 +01:00
import java.util.Arrays;
import java.util.stream.Stream;
import com.google.common.collect.Streams;
import com.minelittlepony.unicopia.entity.player.Pony;
2020-01-27 11:05:22 +01:00
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
2020-05-03 22:42:28 +02:00
import com.mojang.brigadier.exceptions.CommandSyntaxException;
2020-01-27 11:05:22 +01:00
import net.minecraft.command.argument.EntityArgumentType;
2020-01-27 11:05:22 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
2022-06-25 00:19:55 +02:00
import net.minecraft.text.Text;
2021-08-07 22:32:05 +02:00
import net.minecraft.world.GameRules;
2020-01-27 11:05:22 +01:00
class GravityCommand {
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("gravity").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.literal("get")
.executes(context -> get(context.getSource(), context.getSource().getPlayer(), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> get(context.getSource(), EntityArgumentType.getPlayer(context, "target"), false))
))
.then(CommandManager.literal("set")
.then(CommandManager.argument("gravity", FloatArgumentType.floatArg(-99, 99))
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), FloatArgumentType.getFloat(context, "gravity"), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), FloatArgumentType.getFloat(context, "gravity"), false))
)));
2020-01-27 11:05:22 +01:00
}
2020-05-03 22:42:28 +02:00
static int get(ServerCommandSource source, PlayerEntity player, boolean isSelf) throws CommandSyntaxException {
sendFeedback(source, player, "get", false, Pony.of(player).getPhysics().getGravityModifier());
2020-01-27 11:05:22 +01:00
return 0;
}
static int set(ServerCommandSource source, PlayerEntity player, float gravity, boolean isSelf) {
2020-04-15 18:12:00 +02:00
Pony iplayer = Pony.of(player);
2020-01-27 11:05:22 +01:00
iplayer.getPhysics().setBaseGravityModifier(gravity);
iplayer.setDirty();
2020-01-27 11:05:22 +01:00
sendFeedback(source, player, "set", true, gravity);
return 0;
}
static void sendFeedback(ServerCommandSource source, PlayerEntity player, String key, boolean notifyTarget, Object...arguments) {
String translationKey = "commands.gravity." + key;
2021-08-07 22:32:05 +02:00
if (source.getEntity() == player) {
2023-06-03 13:40:54 +02:00
source.sendFeedback(() -> Text.translatable(translationKey + ".self", arguments), true);
2021-08-07 22:32:05 +02:00
} else {
if (notifyTarget && source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
player.sendMessage(Text.translatable(translationKey, arguments));
2021-08-07 22:32:05 +02:00
}
2020-01-27 11:05:22 +01:00
2023-06-03 13:40:54 +02:00
source.sendFeedback(() -> Text.translatable(translationKey + ".other", Streams.concat(Stream.of(player.getDisplayName()), Arrays.stream(arguments)).toArray()), true);
2021-08-07 22:32:05 +02:00
}
2020-01-27 11:05:22 +01:00
}
}