| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.DoubleArgumentType;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.minecraft.commands.CommandBuildContext;
|
| 11 | import net.minecraft.commands.CommandSourceStack;
|
| 12 | import net.minecraft.commands.Commands;
|
| 13 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 14 | import net.minecraft.commands.arguments.EntityArgument;
|
| 15 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 16 | import net.minecraft.commands.arguments.ResourceArgument;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.core.registries.Registries;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.LivingEntity;
|
| 23 | import net.minecraft.world.entity.ai.attributes.Attribute;
|
| 24 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 25 | import net.minecraft.world.entity.ai.attributes.AttributeMap;
|
| 26 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 27 |
|
| 28 | public class AttributeCommand {
|
| 29 | private static final DynamicCommandExceptionType ERROR_NOT_LIVING_ENTITY = new DynamicCommandExceptionType(
|
| 30 | target -> Component.translatableEscape("commands.attribute.failed.entity", target)
|
| 31 | );
|
| 32 | private static final Dynamic2CommandExceptionType ERROR_NO_SUCH_ATTRIBUTE = new Dynamic2CommandExceptionType(
|
| 33 | (target, attribute) -> Component.translatableEscape("commands.attribute.failed.no_attribute", target, attribute)
|
| 34 | );
|
| 35 | private static final Dynamic3CommandExceptionType ERROR_NO_SUCH_MODIFIER = new Dynamic3CommandExceptionType(
|
| 36 | (target, attribute, modifier) -> Component.translatableEscape("commands.attribute.failed.no_modifier", attribute, target, modifier)
|
| 37 | );
|
| 38 | private static final Dynamic3CommandExceptionType ERROR_MODIFIER_ALREADY_PRESENT = new Dynamic3CommandExceptionType(
|
| 39 | (target, attribute, modifier) -> Component.translatableEscape("commands.attribute.failed.modifier_already_present", modifier, attribute, target)
|
| 40 | );
|
| 41 |
|
| 42 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 43 | dispatcher.register(
|
| 44 | Commands.literal("attribute")
|
| 45 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 46 | .then(
|
| 47 | Commands.argument("target", EntityArgument.entity())
|
| 48 | .then(
|
| 49 | Commands.argument("attribute", ResourceArgument.resource(context, Registries.ATTRIBUTE))
|
| 50 | .then(
|
| 51 | Commands.literal("get")
|
| 52 | .executes(c -> getAttributeValue(c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), 1.0))
|
| 53 | .then(
|
| 54 | Commands.argument("scale", DoubleArgumentType.doubleArg())
|
| 55 | .executes(
|
| 56 | c -> getAttributeValue(
|
| 57 | c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), DoubleArgumentType.getDouble(c, "scale")
|
| 58 | )
|
| 59 | )
|
| 60 | )
|
| 61 | )
|
| 62 | .then(
|
| 63 | Commands.literal("base")
|
| 64 | .then(
|
| 65 | Commands.literal("set")
|
| 66 | .then(
|
| 67 | Commands.argument("value", DoubleArgumentType.doubleArg())
|
| 68 | .executes(
|
| 69 | c -> setAttributeBase(
|
| 70 | c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), DoubleArgumentType.getDouble(c, "value")
|
| 71 | )
|
| 72 | )
|
| 73 | )
|
| 74 | )
|
| 75 | .then(
|
| 76 | Commands.literal("get")
|
| 77 | .executes(c -> getAttributeBase(c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), 1.0))
|
| 78 | .then(
|
| 79 | Commands.argument("scale", DoubleArgumentType.doubleArg())
|
| 80 | .executes(
|
| 81 | c -> getAttributeBase(
|
| 82 | c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), DoubleArgumentType.getDouble(c, "scale")
|
| 83 | )
|
| 84 | )
|
| 85 | )
|
| 86 | )
|
| 87 | .then(
|
| 88 | Commands.literal("reset")
|
| 89 | .executes(c -> resetAttributeBase(c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute")))
|
| 90 | )
|
| 91 | )
|
| 92 | .then(
|
| 93 | Commands.literal("modifier")
|
| 94 | .then(
|
| 95 | Commands.literal("add")
|
| 96 | .then(
|
| 97 | Commands.argument("id", IdentifierArgument.id())
|
| 98 | .then(
|
| 99 | Commands.argument("value", DoubleArgumentType.doubleArg())
|
| 100 | .then(
|
| 101 | Commands.literal("add_value")
|
| 102 | .executes(
|
| 103 | c -> addModifier(
|
| 104 | c.getSource(),
|
| 105 | EntityArgument.getEntity(c, "target"),
|
| 106 | ResourceArgument.getAttribute(c, "attribute"),
|
| 107 | IdentifierArgument.getId(c, "id"),
|
| 108 | DoubleArgumentType.getDouble(c, "value"),
|
| 109 | AttributeModifier.Operation.ADD_VALUE
|
| 110 | )
|
| 111 | )
|
| 112 | )
|
| 113 | .then(
|
| 114 | Commands.literal("add_multiplied_base")
|
| 115 | .executes(
|
| 116 | c -> addModifier(
|
| 117 | c.getSource(),
|
| 118 | EntityArgument.getEntity(c, "target"),
|
| 119 | ResourceArgument.getAttribute(c, "attribute"),
|
| 120 | IdentifierArgument.getId(c, "id"),
|
| 121 | DoubleArgumentType.getDouble(c, "value"),
|
| 122 | AttributeModifier.Operation.ADD_MULTIPLIED_BASE
|
| 123 | )
|
| 124 | )
|
| 125 | )
|
| 126 | .then(
|
| 127 | Commands.literal("add_multiplied_total")
|
| 128 | .executes(
|
| 129 | c -> addModifier(
|
| 130 | c.getSource(),
|
| 131 | EntityArgument.getEntity(c, "target"),
|
| 132 | ResourceArgument.getAttribute(c, "attribute"),
|
| 133 | IdentifierArgument.getId(c, "id"),
|
| 134 | DoubleArgumentType.getDouble(c, "value"),
|
| 135 | AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL
|
| 136 | )
|
| 137 | )
|
| 138 | )
|
| 139 | )
|
| 140 | )
|
| 141 | )
|
| 142 | .then(
|
| 143 | Commands.literal("remove")
|
| 144 | .then(
|
| 145 | Commands.argument("id", IdentifierArgument.id())
|
| 146 | .suggests(
|
| 147 | (c, p) -> SharedSuggestionProvider.suggestResource(
|
| 148 | getAttributeModifiers(EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute")), p
|
| 149 | )
|
| 150 | )
|
| 151 | .executes(
|
| 152 | c -> removeModifier(
|
| 153 | c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), IdentifierArgument.getId(c, "id")
|
| 154 | )
|
| 155 | )
|
| 156 | )
|
| 157 | )
|
| 158 | .then(
|
| 159 | Commands.literal("value")
|
| 160 | .then(
|
| 161 | Commands.literal("get")
|
| 162 | .then(
|
| 163 | Commands.argument("id", IdentifierArgument.id())
|
| 164 | .suggests(
|
| 165 | (c, p) -> SharedSuggestionProvider.suggestResource(
|
| 166 | getAttributeModifiers(EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute")), p
|
| 167 | )
|
| 168 | )
|
| 169 | .executes(
|
| 170 | c -> getAttributeModifier(
|
| 171 | c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), IdentifierArgument.getId(c, "id"), 1.0
|
| 172 | )
|
| 173 | )
|
| 174 | .then(
|
| 175 | Commands.argument("scale", DoubleArgumentType.doubleArg())
|
| 176 | .executes(
|
| 177 | c -> getAttributeModifier(
|
| 178 | c.getSource(),
|
| 179 | EntityArgument.getEntity(c, "target"),
|
| 180 | ResourceArgument.getAttribute(c, "attribute"),
|
| 181 | IdentifierArgument.getId(c, "id"),
|
| 182 | DoubleArgumentType.getDouble(c, "scale")
|
| 183 | )
|
| 184 | )
|
| 185 | )
|
| 186 | )
|
| 187 | )
|
| 188 | )
|
| 189 | )
|
| 190 | )
|
| 191 | )
|
| 192 | );
|
| 193 | }
|
| 194 |
|
| 195 | private static AttributeInstance getAttributeInstance(final Entity target, final Holder<Attribute> attribute) throws CommandSyntaxException {
|
| 196 | AttributeInstance attributeInstance = getLivingEntity(target).getAttributes().getInstance(attribute);
|
| 197 | if (attributeInstance == null) {
|
| 198 | throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getDisplayName(), getAttributeDescription(attribute));
|
| 199 | } else {
|
| 200 | return attributeInstance;
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | private static LivingEntity getLivingEntity(final Entity target) throws CommandSyntaxException {
|
| 205 | if (target instanceof LivingEntity livingEntity) {
|
| 206 | return livingEntity;
|
| 207 | } else {
|
| 208 | throw ERROR_NOT_LIVING_ENTITY.create(target.getDisplayName());
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | private static LivingEntity getEntityWithAttribute(final Entity target, final Holder<Attribute> attribute) throws CommandSyntaxException {
|
| 213 | LivingEntity livingEntity = getLivingEntity(target);
|
| 214 | if (!livingEntity.getAttributes().hasAttribute(attribute)) {
|
| 215 | throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getDisplayName(), getAttributeDescription(attribute));
|
| 216 | } else {
|
| 217 | return livingEntity;
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | private static int getAttributeValue(final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute, final double scale) throws CommandSyntaxException {
|
| 222 | LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
|
| 223 | double result = livingEntity.getAttributeValue(attribute);
|
| 224 | source.sendSuccess(
|
| 225 | () -> Component.translatable("commands.attribute.value.get.success", getAttributeDescription(attribute), target.getDisplayName(), result), false
|
| 226 | );
|
| 227 | return (int)(result * scale);
|
| 228 | }
|
| 229 |
|
| 230 | private static int getAttributeBase(final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute, final double scale) throws CommandSyntaxException {
|
| 231 | LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
|
| 232 | double result = livingEntity.getAttributeBaseValue(attribute);
|
| 233 | source.sendSuccess(
|
| 234 | () -> Component.translatable("commands.attribute.base_value.get.success", getAttributeDescription(attribute), target.getDisplayName(), result), false
|
| 235 | );
|
| 236 | return (int)(result * scale);
|
| 237 | }
|
| 238 |
|
| 239 | private static int getAttributeModifier(
|
| 240 | final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute, final Identifier id, final double scale
|
| 241 | ) throws CommandSyntaxException {
|
| 242 | LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
|
| 243 | AttributeMap attributes = livingEntity.getAttributes();
|
| 244 | if (!attributes.hasModifier(attribute, id)) {
|
| 245 | throw ERROR_NO_SUCH_MODIFIER.create(target.getDisplayName(), getAttributeDescription(attribute), id);
|
| 246 | }
|
| 247 |
|
| 248 | double result = attributes.getModifierValue(attribute, id);
|
| 249 | source.sendSuccess(
|
| 250 | () -> Component.translatable(
|
| 251 | "commands.attribute.modifier.value.get.success", Component.translationArg(id), getAttributeDescription(attribute), target.getDisplayName(), result
|
| 252 | ),
|
| 253 | false
|
| 254 | );
|
| 255 | return (int)(result * scale);
|
| 256 | }
|
| 257 |
|
| 258 | private static Stream<Identifier> getAttributeModifiers(final Entity target, final Holder<Attribute> attribute) throws CommandSyntaxException {
|
| 259 | AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
|
| 260 | return attributeInstance.getModifiers().stream().map(AttributeModifier::id);
|
| 261 | }
|
| 262 |
|
| 263 | private static int setAttributeBase(final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute, final double value) throws CommandSyntaxException {
|
| 264 | getAttributeInstance(target, attribute).setBaseValue(value);
|
| 265 | source.sendSuccess(
|
| 266 | () -> Component.translatable("commands.attribute.base_value.set.success", getAttributeDescription(attribute), target.getDisplayName(), value), false
|
| 267 | );
|
| 268 | return 1;
|
| 269 | }
|
| 270 |
|
| 271 | private static int resetAttributeBase(final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute) throws CommandSyntaxException {
|
| 272 | LivingEntity livingTarget = getLivingEntity(target);
|
| 273 | if (!livingTarget.getAttributes().resetBaseValue(attribute)) {
|
| 274 | throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getDisplayName(), getAttributeDescription(attribute));
|
| 275 | }
|
| 276 |
|
| 277 | double value = livingTarget.getAttributeBaseValue(attribute);
|
| 278 | source.sendSuccess(
|
| 279 | () -> Component.translatable("commands.attribute.base_value.reset.success", getAttributeDescription(attribute), target.getDisplayName(), value), false
|
| 280 | );
|
| 281 | return 1;
|
| 282 | }
|
| 283 |
|
| 284 | private static int addModifier(
|
| 285 | final CommandSourceStack source,
|
| 286 | final Entity target,
|
| 287 | final Holder<Attribute> attribute,
|
| 288 | final Identifier id,
|
| 289 | final double value,
|
| 290 | final AttributeModifier.Operation operation
|
| 291 | ) throws CommandSyntaxException {
|
| 292 | AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
|
| 293 | AttributeModifier modifier = new AttributeModifier(id, value, operation);
|
| 294 | if (attributeInstance.hasModifier(id)) {
|
| 295 | throw ERROR_MODIFIER_ALREADY_PRESENT.create(target.getDisplayName(), getAttributeDescription(attribute), id);
|
| 296 | }
|
| 297 |
|
| 298 | attributeInstance.addPermanentModifier(modifier);
|
| 299 | source.sendSuccess(
|
| 300 | () -> Component.translatable(
|
| 301 | "commands.attribute.modifier.add.success", Component.translationArg(id), getAttributeDescription(attribute), target.getDisplayName()
|
| 302 | ),
|
| 303 | false
|
| 304 | );
|
| 305 | return 1;
|
| 306 | }
|
| 307 |
|
| 308 | private static int removeModifier(final CommandSourceStack source, final Entity target, final Holder<Attribute> attribute, final Identifier id) throws CommandSyntaxException {
|
| 309 | AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
|
| 310 | if (attributeInstance.removeModifier(id)) {
|
| 311 | source.sendSuccess(
|
| 312 | () -> Component.translatable(
|
| 313 | "commands.attribute.modifier.remove.success", Component.translationArg(id), getAttributeDescription(attribute), target.getDisplayName()
|
| 314 | ),
|
| 315 | false
|
| 316 | );
|
| 317 | return 1;
|
| 318 | } else {
|
| 319 | throw ERROR_NO_SUCH_MODIFIER.create(target.getDisplayName(), getAttributeDescription(attribute), id);
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | private static Component getAttributeDescription(final Holder<Attribute> attribute) {
|
| 324 | return Component.translatable(attribute.value().getDescriptionId());
|
| 325 | }
|
| 326 | }
|
| 327 |
|