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

69 lines
3.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.command;
2020-01-27 11:05:22 +01:00
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import com.minelittlepony.unicopia.entity.Living;
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;
import net.minecraft.entity.Entity;
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.entity())
.executes(context -> get(context.getSource(), EntityArgumentType.getEntity(context, "target"), false))
))
.then(CommandManager.literal("set")
.then(CommandManager.argument("gravity", FloatArgumentType.floatArg(-99, 99))
.executes(context -> set(context.getSource(), List.of(context.getSource().getPlayer()), FloatArgumentType.getFloat(context, "gravity"), true))
.then(CommandManager.argument("target", EntityArgumentType.entities())
.executes(context -> set(context.getSource(), EntityArgumentType.getEntities(context, "target"), FloatArgumentType.getFloat(context, "gravity"), false))
)));
2020-01-27 11:05:22 +01:00
}
static int get(ServerCommandSource source, Entity target, boolean isSelf) throws CommandSyntaxException {
Living<?> l = Living.living(target);
2020-01-27 11:05:22 +01:00
float gravity = l == null ? 1 : l.getPhysics().getGravityModifier();
if (source.getEntity() == target) {
source.sendFeedback(() -> Text.translatable("commands.gravity.get.self", gravity), true);
} else {
source.sendFeedback(() -> Text.translatable("commands.gravity.get.other", target.getDisplayName(), gravity), true);
}
return 0;
}
static int set(ServerCommandSource source, Collection<? extends Entity> targets, float gravity, boolean isSelf) {
List<Entity> affected = targets.stream().map(Living::living).filter(Objects::nonNull).map(l -> {
l.getPhysics().setBaseGravityModifier(gravity);
if (l.asEntity() instanceof PlayerEntity player) {
if (source.getEntity() == player) {
player.sendMessage(Text.translatable("commands.gravity.set.self", gravity));
} else if (source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
player.sendMessage(Text.translatable("commands.gravity.set.other", l.asEntity().getDisplayName(), gravity));
}
2021-08-07 22:32:05 +02:00
}
return (Entity)l.asEntity();
}).toList();
2020-01-27 11:05:22 +01:00
if (affected.size() == 1) {
source.sendFeedback(() -> Text.translatable("commands.gravity.set.other", affected.get(0).getDisplayName()), true);
} else {
source.sendFeedback(() -> Text.translatable("commands.gravity.set.multiple", affected.size()), true);
2021-08-07 22:32:05 +02:00
}
return 0;
2020-01-27 11:05:22 +01:00
}
}