Spaces:
Build error
Build error
File size: 25,333 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
package net.minecraft.server.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.stream.Stream;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
public class AttributeCommand {
private static final DynamicCommandExceptionType ERROR_NOT_LIVING_ENTITY = new DynamicCommandExceptionType(
p_308625_ -> Component.translatableEscape("commands.attribute.failed.entity", p_308625_)
);
private static final Dynamic2CommandExceptionType ERROR_NO_SUCH_ATTRIBUTE = new Dynamic2CommandExceptionType(
(p_308616_, p_308617_) -> Component.translatableEscape("commands.attribute.failed.no_attribute", p_308616_, p_308617_)
);
private static final Dynamic3CommandExceptionType ERROR_NO_SUCH_MODIFIER = new Dynamic3CommandExceptionType(
(p_308629_, p_308630_, p_308631_) -> Component.translatableEscape("commands.attribute.failed.no_modifier", p_308630_, p_308629_, p_308631_)
);
private static final Dynamic3CommandExceptionType ERROR_MODIFIER_ALREADY_PRESENT = new Dynamic3CommandExceptionType(
(p_308626_, p_308627_, p_308628_) -> Component.translatableEscape("commands.attribute.failed.modifier_already_present", p_308628_, p_308627_, p_308626_)
);
public static void register(CommandDispatcher<CommandSourceStack> p_251026_, CommandBuildContext p_250936_) {
p_251026_.register(
Commands.literal("attribute")
.requires(p_212441_ -> p_212441_.hasPermission(2))
.then(
Commands.argument("target", EntityArgument.entity())
.then(
Commands.argument("attribute", ResourceArgument.resource(p_250936_, Registries.ATTRIBUTE))
.then(
Commands.literal("get")
.executes(
p_248109_ -> getAttributeValue(
p_248109_.getSource(),
EntityArgument.getEntity(p_248109_, "target"),
ResourceArgument.getAttribute(p_248109_, "attribute"),
1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
p_248104_ -> getAttributeValue(
p_248104_.getSource(),
EntityArgument.getEntity(p_248104_, "target"),
ResourceArgument.getAttribute(p_248104_, "attribute"),
DoubleArgumentType.getDouble(p_248104_, "scale")
)
)
)
)
.then(
Commands.literal("base")
.then(
Commands.literal("set")
.then(
Commands.argument("value", DoubleArgumentType.doubleArg())
.executes(
p_248102_ -> setAttributeBase(
p_248102_.getSource(),
EntityArgument.getEntity(p_248102_, "target"),
ResourceArgument.getAttribute(p_248102_, "attribute"),
DoubleArgumentType.getDouble(p_248102_, "value")
)
)
)
)
.then(
Commands.literal("get")
.executes(
p_248112_ -> getAttributeBase(
p_248112_.getSource(),
EntityArgument.getEntity(p_248112_, "target"),
ResourceArgument.getAttribute(p_248112_, "attribute"),
1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
p_248106_ -> getAttributeBase(
p_248106_.getSource(),
EntityArgument.getEntity(p_248106_, "target"),
ResourceArgument.getAttribute(p_248106_, "attribute"),
DoubleArgumentType.getDouble(p_248106_, "scale")
)
)
)
)
.then(
Commands.literal("reset")
.executes(
p_374867_ -> resetAttributeBase(
p_374867_.getSource(),
EntityArgument.getEntity(p_374867_, "target"),
ResourceArgument.getAttribute(p_374867_, "attribute")
)
)
)
)
.then(
Commands.literal("modifier")
.then(
Commands.literal("add")
.then(
Commands.argument("id", ResourceLocationArgument.id())
.then(
Commands.argument("value", DoubleArgumentType.doubleArg())
.then(
Commands.literal("add_value")
.executes(
p_341134_ -> addModifier(
p_341134_.getSource(),
EntityArgument.getEntity(p_341134_, "target"),
ResourceArgument.getAttribute(p_341134_, "attribute"),
ResourceLocationArgument.getId(p_341134_, "id"),
DoubleArgumentType.getDouble(p_341134_, "value"),
AttributeModifier.Operation.ADD_VALUE
)
)
)
.then(
Commands.literal("add_multiplied_base")
.executes(
p_341141_ -> addModifier(
p_341141_.getSource(),
EntityArgument.getEntity(p_341141_, "target"),
ResourceArgument.getAttribute(p_341141_, "attribute"),
ResourceLocationArgument.getId(p_341141_, "id"),
DoubleArgumentType.getDouble(p_341141_, "value"),
AttributeModifier.Operation.ADD_MULTIPLIED_BASE
)
)
)
.then(
Commands.literal("add_multiplied_total")
.executes(
p_341139_ -> addModifier(
p_341139_.getSource(),
EntityArgument.getEntity(p_341139_, "target"),
ResourceArgument.getAttribute(p_341139_, "attribute"),
ResourceLocationArgument.getId(p_341139_, "id"),
DoubleArgumentType.getDouble(p_341139_, "value"),
AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL
)
)
)
)
)
)
.then(
Commands.literal("remove")
.then(
Commands.argument("id", ResourceLocationArgument.id())
.suggests(
(p_374868_, p_374869_) -> SharedSuggestionProvider.suggestResource(
getAttributeModifiers(
EntityArgument.getEntity(p_374868_, "target"),
ResourceArgument.getAttribute(p_374868_, "attribute")
),
p_374869_
)
)
.executes(
p_341140_ -> removeModifier(
p_341140_.getSource(),
EntityArgument.getEntity(p_341140_, "target"),
ResourceArgument.getAttribute(p_341140_, "attribute"),
ResourceLocationArgument.getId(p_341140_, "id")
)
)
)
)
.then(
Commands.literal("value")
.then(
Commands.literal("get")
.then(
Commands.argument("id", ResourceLocationArgument.id())
.suggests(
(p_374862_, p_374863_) -> SharedSuggestionProvider.suggestResource(
getAttributeModifiers(
EntityArgument.getEntity(p_374862_, "target"),
ResourceArgument.getAttribute(p_374862_, "attribute")
),
p_374863_
)
)
.executes(
p_341142_ -> getAttributeModifier(
p_341142_.getSource(),
EntityArgument.getEntity(p_341142_, "target"),
ResourceArgument.getAttribute(p_341142_, "attribute"),
ResourceLocationArgument.getId(p_341142_, "id"),
1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
p_341135_ -> getAttributeModifier(
p_341135_.getSource(),
EntityArgument.getEntity(p_341135_, "target"),
ResourceArgument.getAttribute(p_341135_, "attribute"),
ResourceLocationArgument.getId(p_341135_, "id"),
DoubleArgumentType.getDouble(p_341135_, "scale")
)
)
)
)
)
)
)
)
)
);
}
private static AttributeInstance getAttributeInstance(Entity p_252177_, Holder<Attribute> p_249942_) throws CommandSyntaxException {
AttributeInstance attributeinstance = getLivingEntity(p_252177_).getAttributes().getInstance(p_249942_);
if (attributeinstance == null) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(p_252177_.getName(), getAttributeDescription(p_249942_));
} else {
return attributeinstance;
}
}
private static LivingEntity getLivingEntity(Entity p_136440_) throws CommandSyntaxException {
if (!(p_136440_ instanceof LivingEntity)) {
throw ERROR_NOT_LIVING_ENTITY.create(p_136440_.getName());
} else {
return (LivingEntity)p_136440_;
}
}
private static LivingEntity getEntityWithAttribute(Entity p_252105_, Holder<Attribute> p_248921_) throws CommandSyntaxException {
LivingEntity livingentity = getLivingEntity(p_252105_);
if (!livingentity.getAttributes().hasAttribute(p_248921_)) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(p_252105_.getName(), getAttributeDescription(p_248921_));
} else {
return livingentity;
}
}
private static int getAttributeValue(CommandSourceStack p_251776_, Entity p_249647_, Holder<Attribute> p_250986_, double p_251395_) throws CommandSyntaxException {
LivingEntity livingentity = getEntityWithAttribute(p_249647_, p_250986_);
double d0 = livingentity.getAttributeValue(p_250986_);
p_251776_.sendSuccess(() -> Component.translatable("commands.attribute.value.get.success", getAttributeDescription(p_250986_), p_249647_.getName(), d0), false);
return (int)(d0 * p_251395_);
}
private static int getAttributeBase(CommandSourceStack p_248780_, Entity p_251083_, Holder<Attribute> p_250388_, double p_250194_) throws CommandSyntaxException {
LivingEntity livingentity = getEntityWithAttribute(p_251083_, p_250388_);
double d0 = livingentity.getAttributeBaseValue(p_250388_);
p_248780_.sendSuccess(() -> Component.translatable("commands.attribute.base_value.get.success", getAttributeDescription(p_250388_), p_251083_.getName(), d0), false);
return (int)(d0 * p_250194_);
}
private static int getAttributeModifier(CommandSourceStack p_136464_, Entity p_136465_, Holder<Attribute> p_250680_, ResourceLocation p_342802_, double p_136468_) throws CommandSyntaxException {
LivingEntity livingentity = getEntityWithAttribute(p_136465_, p_250680_);
AttributeMap attributemap = livingentity.getAttributes();
if (!attributemap.hasModifier(p_250680_, p_342802_)) {
throw ERROR_NO_SUCH_MODIFIER.create(p_136465_.getName(), getAttributeDescription(p_250680_), p_342802_);
} else {
double d0 = attributemap.getModifierValue(p_250680_, p_342802_);
p_136464_.sendSuccess(
() -> Component.translatable(
"commands.attribute.modifier.value.get.success", Component.translationArg(p_342802_), getAttributeDescription(p_250680_), p_136465_.getName(), d0
),
false
);
return (int)(d0 * p_136468_);
}
}
private static Stream<ResourceLocation> getAttributeModifiers(Entity p_377287_, Holder<Attribute> p_377560_) throws CommandSyntaxException {
AttributeInstance attributeinstance = getAttributeInstance(p_377287_, p_377560_);
return attributeinstance.getModifiers().stream().map(AttributeModifier::id);
}
private static int setAttributeBase(CommandSourceStack p_248556_, Entity p_248620_, Holder<Attribute> p_249456_, double p_252212_) throws CommandSyntaxException {
getAttributeInstance(p_248620_, p_249456_).setBaseValue(p_252212_);
p_248556_.sendSuccess(() -> Component.translatable("commands.attribute.base_value.set.success", getAttributeDescription(p_249456_), p_248620_.getName(), p_252212_), false);
return 1;
}
private static int resetAttributeBase(CommandSourceStack p_377147_, Entity p_377320_, Holder<Attribute> p_377314_) throws CommandSyntaxException {
LivingEntity livingentity = getLivingEntity(p_377320_);
if (!livingentity.getAttributes().resetBaseValue(p_377314_)) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(p_377320_.getName(), getAttributeDescription(p_377314_));
} else {
double d0 = livingentity.getAttributeBaseValue(p_377314_);
p_377147_.sendSuccess(() -> Component.translatable("commands.attribute.base_value.reset.success", getAttributeDescription(p_377314_), p_377320_.getName(), d0), false);
return 1;
}
}
private static int addModifier(
CommandSourceStack p_136470_,
Entity p_136471_,
Holder<Attribute> p_251636_,
ResourceLocation p_342914_,
double p_136475_,
AttributeModifier.Operation p_136476_
) throws CommandSyntaxException {
AttributeInstance attributeinstance = getAttributeInstance(p_136471_, p_251636_);
AttributeModifier attributemodifier = new AttributeModifier(p_342914_, p_136475_, p_136476_);
if (attributeinstance.hasModifier(p_342914_)) {
throw ERROR_MODIFIER_ALREADY_PRESENT.create(p_136471_.getName(), getAttributeDescription(p_251636_), p_342914_);
} else {
attributeinstance.addPermanentModifier(attributemodifier);
p_136470_.sendSuccess(
() -> Component.translatable("commands.attribute.modifier.add.success", Component.translationArg(p_342914_), getAttributeDescription(p_251636_), p_136471_.getName()),
false
);
return 1;
}
}
private static int removeModifier(CommandSourceStack p_136459_, Entity p_136460_, Holder<Attribute> p_250830_, ResourceLocation p_344364_) throws CommandSyntaxException {
AttributeInstance attributeinstance = getAttributeInstance(p_136460_, p_250830_);
if (attributeinstance.removeModifier(p_344364_)) {
p_136459_.sendSuccess(
() -> Component.translatable(
"commands.attribute.modifier.remove.success", Component.translationArg(p_344364_), getAttributeDescription(p_250830_), p_136460_.getName()
),
false
);
return 1;
} else {
throw ERROR_NO_SUCH_MODIFIER.create(p_136460_.getName(), getAttributeDescription(p_250830_), p_344364_);
}
}
private static Component getAttributeDescription(Holder<Attribute> p_250602_) {
return Component.translatable(p_250602_.value().getDescriptionId());
}
} |