2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.command;
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2024-03-26 18:11:39 +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
|
|
|
|
2020-09-22 15:11:20 +02:00
|
|
|
import net.minecraft.command.argument.EntityArgumentType;
|
2024-03-26 18:11:39 +01:00
|
|
|
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 {
|
|
|
|
|
2023-09-05 00:40:41 +02:00
|
|
|
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))
|
2024-03-26 18:11:39 +01:00
|
|
|
.then(CommandManager.argument("target", EntityArgumentType.entity())
|
|
|
|
.executes(context -> get(context.getSource(), EntityArgumentType.getEntity(context, "target"), false))
|
2023-09-05 00:40:41 +02:00
|
|
|
))
|
|
|
|
.then(CommandManager.literal("set")
|
|
|
|
.then(CommandManager.argument("gravity", FloatArgumentType.floatArg(-99, 99))
|
2024-03-26 18:11:39 +01:00
|
|
|
.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))
|
2023-09-05 00:40:41 +02:00
|
|
|
)));
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 18:11:39 +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
|
|
|
|
2024-03-26 18:11:39 +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);
|
|
|
|
}
|
2023-01-27 15:37:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:11:39 +01:00
|
|
|
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
|
|
|
}
|
2024-03-26 18:11:39 +01:00
|
|
|
return (Entity)l.asEntity();
|
|
|
|
}).toList();
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2024-03-26 18:11:39 +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
|
|
|
}
|
2024-03-26 18:11:39 +01:00
|
|
|
return 0;
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
}
|