| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.FloatArgumentType;
|
| 5 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import java.util.Collection;
|
| 9 | import net.minecraft.commands.CommandBuildContext;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.arguments.EntityArgument;
|
| 13 | import net.minecraft.commands.arguments.ParticleArgument;
|
| 14 | import net.minecraft.commands.arguments.coordinates.Vec3Argument;
|
| 15 | import net.minecraft.core.particles.ParticleOptions;
|
| 16 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.server.level.ServerPlayer;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 |
|
| 21 | public class ParticleCommand {
|
| 22 | private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.particle.failed"));
|
| 23 |
|
| 24 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 25 | dispatcher.register(
|
| 26 | Commands.literal("particle")
|
| 27 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 28 | .then(
|
| 29 | Commands.argument("name", ParticleArgument.particle(context))
|
| 30 | .executes(
|
| 31 | c -> sendParticles(
|
| 32 | c.getSource(),
|
| 33 | ParticleArgument.getParticle(c, "name"),
|
| 34 | c.getSource().getPosition(),
|
| 35 | Vec3.ZERO,
|
| 36 | 0.0F,
|
| 37 | 0,
|
| 38 | false,
|
| 39 | c.getSource().getServer().getPlayerList().getPlayers()
|
| 40 | )
|
| 41 | )
|
| 42 | .then(
|
| 43 | Commands.argument("pos", Vec3Argument.vec3())
|
| 44 | .executes(
|
| 45 | c -> sendParticles(
|
| 46 | c.getSource(),
|
| 47 | ParticleArgument.getParticle(c, "name"),
|
| 48 | Vec3Argument.getVec3(c, "pos"),
|
| 49 | Vec3.ZERO,
|
| 50 | 0.0F,
|
| 51 | 0,
|
| 52 | false,
|
| 53 | c.getSource().getServer().getPlayerList().getPlayers()
|
| 54 | )
|
| 55 | )
|
| 56 | .then(
|
| 57 | Commands.argument("delta", Vec3Argument.vec3(false))
|
| 58 | .then(
|
| 59 | Commands.argument("speed", FloatArgumentType.floatArg(0.0F))
|
| 60 | .then(
|
| 61 | Commands.argument("count", IntegerArgumentType.integer(0))
|
| 62 | .executes(
|
| 63 | c -> sendParticles(
|
| 64 | c.getSource(),
|
| 65 | ParticleArgument.getParticle(c, "name"),
|
| 66 | Vec3Argument.getVec3(c, "pos"),
|
| 67 | Vec3Argument.getVec3(c, "delta"),
|
| 68 | FloatArgumentType.getFloat(c, "speed"),
|
| 69 | IntegerArgumentType.getInteger(c, "count"),
|
| 70 | false,
|
| 71 | c.getSource().getServer().getPlayerList().getPlayers()
|
| 72 | )
|
| 73 | )
|
| 74 | .then(
|
| 75 | Commands.literal("force")
|
| 76 | .executes(
|
| 77 | c -> sendParticles(
|
| 78 | c.getSource(),
|
| 79 | ParticleArgument.getParticle(c, "name"),
|
| 80 | Vec3Argument.getVec3(c, "pos"),
|
| 81 | Vec3Argument.getVec3(c, "delta"),
|
| 82 | FloatArgumentType.getFloat(c, "speed"),
|
| 83 | IntegerArgumentType.getInteger(c, "count"),
|
| 84 | true,
|
| 85 | c.getSource().getServer().getPlayerList().getPlayers()
|
| 86 | )
|
| 87 | )
|
| 88 | .then(
|
| 89 | Commands.argument("viewers", EntityArgument.players())
|
| 90 | .executes(
|
| 91 | c -> sendParticles(
|
| 92 | c.getSource(),
|
| 93 | ParticleArgument.getParticle(c, "name"),
|
| 94 | Vec3Argument.getVec3(c, "pos"),
|
| 95 | Vec3Argument.getVec3(c, "delta"),
|
| 96 | FloatArgumentType.getFloat(c, "speed"),
|
| 97 | IntegerArgumentType.getInteger(c, "count"),
|
| 98 | true,
|
| 99 | EntityArgument.getPlayers(c, "viewers")
|
| 100 | )
|
| 101 | )
|
| 102 | )
|
| 103 | )
|
| 104 | .then(
|
| 105 | Commands.literal("normal")
|
| 106 | .executes(
|
| 107 | c -> sendParticles(
|
| 108 | c.getSource(),
|
| 109 | ParticleArgument.getParticle(c, "name"),
|
| 110 | Vec3Argument.getVec3(c, "pos"),
|
| 111 | Vec3Argument.getVec3(c, "delta"),
|
| 112 | FloatArgumentType.getFloat(c, "speed"),
|
| 113 | IntegerArgumentType.getInteger(c, "count"),
|
| 114 | false,
|
| 115 | c.getSource().getServer().getPlayerList().getPlayers()
|
| 116 | )
|
| 117 | )
|
| 118 | .then(
|
| 119 | Commands.argument("viewers", EntityArgument.players())
|
| 120 | .executes(
|
| 121 | c -> sendParticles(
|
| 122 | c.getSource(),
|
| 123 | ParticleArgument.getParticle(c, "name"),
|
| 124 | Vec3Argument.getVec3(c, "pos"),
|
| 125 | Vec3Argument.getVec3(c, "delta"),
|
| 126 | FloatArgumentType.getFloat(c, "speed"),
|
| 127 | IntegerArgumentType.getInteger(c, "count"),
|
| 128 | false,
|
| 129 | EntityArgument.getPlayers(c, "viewers")
|
| 130 | )
|
| 131 | )
|
| 132 | )
|
| 133 | )
|
| 134 | )
|
| 135 | )
|
| 136 | )
|
| 137 | )
|
| 138 | )
|
| 139 | );
|
| 140 | }
|
| 141 |
|
| 142 | private static int sendParticles(
|
| 143 | final CommandSourceStack source,
|
| 144 | final ParticleOptions particle,
|
| 145 | final Vec3 pos,
|
| 146 | final Vec3 delta,
|
| 147 | final float speed,
|
| 148 | final int count,
|
| 149 | final boolean force,
|
| 150 | final Collection<ServerPlayer> players
|
| 151 | ) throws CommandSyntaxException {
|
| 152 | int result = 0;
|
| 153 |
|
| 154 | for (ServerPlayer player : players) {
|
| 155 | if (source.getLevel().sendParticles(player, particle, force, false, pos.x, pos.y, pos.z, count, delta.x, delta.y, delta.z, speed)) {
|
| 156 | result++;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | if (result == 0) {
|
| 161 | throw ERROR_FAILED.create();
|
| 162 | }
|
| 163 |
|
| 164 | source.sendSuccess(() -> Component.translatable("commands.particle.success", BuiltInRegistries.PARTICLE_TYPE.getKey(particle.getType()).toString()), true);
|
| 165 | return result;
|
| 166 | }
|
| 167 | }
|
| 168 |
|