AttributeCommand.java
net.minecraft.server.commands.AttributeCommand
信息
- 全限定名:net.minecraft.server.commands.AttributeCommand
- 类型:public class
- 包:net.minecraft.server.commands
- 源码路径:src/main/java/net/minecraft/server/commands/AttributeCommand.java
- 起始行号:L28
- 职责:
TODO
字段/常量
-
ERROR_NOT_LIVING_ENTITY- 类型:
DynamicCommandExceptionType - 修饰符:
private static final - 源码定位:
L29 - 说明:
TODO
- 类型:
-
ERROR_NO_SUCH_ATTRIBUTE- 类型:
Dynamic2CommandExceptionType - 修饰符:
private static final - 源码定位:
L32 - 说明:
TODO
- 类型:
-
ERROR_NO_SUCH_MODIFIER- 类型:
Dynamic3CommandExceptionType - 修饰符:
private static final - 源码定位:
L35 - 说明:
TODO
- 类型:
-
ERROR_MODIFIER_ALREADY_PRESENT- 类型:
Dynamic3CommandExceptionType - 修饰符:
private static final - 源码定位:
L38 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L42
- 方法名:register
- 源码定位:L42
- 返回类型:void
- 修饰符:public static
参数:
- dispatcher: CommandDispatcher
- context: CommandBuildContext
说明:
TODO
private static AttributeInstance getAttributeInstance(Entity target, Holder<Attribute> attribute) @ L233
- 方法名:getAttributeInstance
- 源码定位:L233
- 返回类型:AttributeInstance
- 修饰符:private static
参数:
- target: Entity
- attribute: Holder
说明:
TODO
private static LivingEntity getLivingEntity(Entity target) @ L242
- 方法名:getLivingEntity
- 源码定位:L242
- 返回类型:LivingEntity
- 修饰符:private static
参数:
- target: Entity
说明:
TODO
private static LivingEntity getEntityWithAttribute(Entity target, Holder<Attribute> attribute) @ L250
- 方法名:getEntityWithAttribute
- 源码定位:L250
- 返回类型:LivingEntity
- 修饰符:private static
参数:
- target: Entity
- attribute: Holder
说明:
TODO
private static int getAttributeValue(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double scale) @ L259
- 方法名:getAttributeValue
- 源码定位:L259
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- scale: double
说明:
TODO
private static int getAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double scale) @ L268
- 方法名:getAttributeBase
- 源码定位:L268
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- scale: double
说明:
TODO
private static int getAttributeModifier(CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id, double scale) @ L277
- 方法名:getAttributeModifier
- 源码定位:L277
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- id: Identifier
- scale: double
说明:
TODO
private static Stream<Identifier> getAttributeModifiers(Entity target, Holder<Attribute> attribute) @ L294
- 方法名:getAttributeModifiers
- 源码定位:L294
- 返回类型:Stream
- 修饰符:private static
参数:
- target: Entity
- attribute: Holder
说明:
TODO
private static int setAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double value) @ L299
- 方法名:setAttributeBase
- 源码定位:L299
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- value: double
说明:
TODO
private static int resetAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute) @ L307
- 方法名:resetAttributeBase
- 源码定位:L307
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
说明:
TODO
private static int addModifier(CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id, double value, AttributeModifier.Operation operation) @ L320
- 方法名:addModifier
- 源码定位:L320
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- id: Identifier
- value: double
- operation: AttributeModifier.Operation
说明:
TODO
private static int removeModifier(CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id) @ L339
- 方法名:removeModifier
- 源码定位:L339
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- attribute: Holder
- id: Identifier
说明:
TODO
private static Component getAttributeDescription(Holder<Attribute> attribute) @ L354
- 方法名:getAttributeDescription
- 源码定位:L354
- 返回类型:Component
- 修饰符:private static
参数:
- attribute: Holder
说明:
TODO
代码
public class AttributeCommand {
private static final DynamicCommandExceptionType ERROR_NOT_LIVING_ENTITY = new DynamicCommandExceptionType(
target -> Component.translatableEscape("commands.attribute.failed.entity", target)
);
private static final Dynamic2CommandExceptionType ERROR_NO_SUCH_ATTRIBUTE = new Dynamic2CommandExceptionType(
(target, attribute) -> Component.translatableEscape("commands.attribute.failed.no_attribute", target, attribute)
);
private static final Dynamic3CommandExceptionType ERROR_NO_SUCH_MODIFIER = new Dynamic3CommandExceptionType(
(target, attribute, modifier) -> Component.translatableEscape("commands.attribute.failed.no_modifier", attribute, target, modifier)
);
private static final Dynamic3CommandExceptionType ERROR_MODIFIER_ALREADY_PRESENT = new Dynamic3CommandExceptionType(
(target, attribute, modifier) -> Component.translatableEscape("commands.attribute.failed.modifier_already_present", modifier, attribute, target)
);
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
dispatcher.register(
Commands.literal("attribute")
.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
.then(
Commands.argument("target", EntityArgument.entity())
.then(
Commands.argument("attribute", ResourceArgument.resource(context, Registries.ATTRIBUTE))
.then(
Commands.literal("get")
.executes(
c -> getAttributeValue(
c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute"), 1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
c -> getAttributeValue(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
DoubleArgumentType.getDouble(c, "scale")
)
)
)
)
.then(
Commands.literal("base")
.then(
Commands.literal("set")
.then(
Commands.argument("value", DoubleArgumentType.doubleArg())
.executes(
c -> setAttributeBase(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
DoubleArgumentType.getDouble(c, "value")
)
)
)
)
.then(
Commands.literal("get")
.executes(
c -> getAttributeBase(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
c -> getAttributeBase(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
DoubleArgumentType.getDouble(c, "scale")
)
)
)
)
.then(
Commands.literal("reset")
.executes(
c -> resetAttributeBase(
c.getSource(), EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute")
)
)
)
)
.then(
Commands.literal("modifier")
.then(
Commands.literal("add")
.then(
Commands.argument("id", IdentifierArgument.id())
.then(
Commands.argument("value", DoubleArgumentType.doubleArg())
.then(
Commands.literal("add_value")
.executes(
c -> addModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id"),
DoubleArgumentType.getDouble(c, "value"),
AttributeModifier.Operation.ADD_VALUE
)
)
)
.then(
Commands.literal("add_multiplied_base")
.executes(
c -> addModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id"),
DoubleArgumentType.getDouble(c, "value"),
AttributeModifier.Operation.ADD_MULTIPLIED_BASE
)
)
)
.then(
Commands.literal("add_multiplied_total")
.executes(
c -> addModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id"),
DoubleArgumentType.getDouble(c, "value"),
AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL
)
)
)
)
)
)
.then(
Commands.literal("remove")
.then(
Commands.argument("id", IdentifierArgument.id())
.suggests(
(c, p) -> SharedSuggestionProvider.suggestResource(
getAttributeModifiers(
EntityArgument.getEntity(c, "target"), ResourceArgument.getAttribute(c, "attribute")
),
p
)
)
.executes(
c -> removeModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id")
)
)
)
)
.then(
Commands.literal("value")
.then(
Commands.literal("get")
.then(
Commands.argument("id", IdentifierArgument.id())
.suggests(
(c, p) -> SharedSuggestionProvider.suggestResource(
getAttributeModifiers(
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute")
),
p
)
)
.executes(
c -> getAttributeModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id"),
1.0
)
)
.then(
Commands.argument("scale", DoubleArgumentType.doubleArg())
.executes(
c -> getAttributeModifier(
c.getSource(),
EntityArgument.getEntity(c, "target"),
ResourceArgument.getAttribute(c, "attribute"),
IdentifierArgument.getId(c, "id"),
DoubleArgumentType.getDouble(c, "scale")
)
)
)
)
)
)
)
)
)
);
}
private static AttributeInstance getAttributeInstance(Entity target, Holder<Attribute> attribute) throws CommandSyntaxException {
AttributeInstance attributeInstance = getLivingEntity(target).getAttributes().getInstance(attribute);
if (attributeInstance == null) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getName(), getAttributeDescription(attribute));
} else {
return attributeInstance;
}
}
private static LivingEntity getLivingEntity(Entity target) throws CommandSyntaxException {
if (!(target instanceof LivingEntity)) {
throw ERROR_NOT_LIVING_ENTITY.create(target.getName());
} else {
return (LivingEntity)target;
}
}
private static LivingEntity getEntityWithAttribute(Entity target, Holder<Attribute> attribute) throws CommandSyntaxException {
LivingEntity livingEntity = getLivingEntity(target);
if (!livingEntity.getAttributes().hasAttribute(attribute)) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getName(), getAttributeDescription(attribute));
} else {
return livingEntity;
}
}
private static int getAttributeValue(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double scale) throws CommandSyntaxException {
LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
double result = livingEntity.getAttributeValue(attribute);
source.sendSuccess(
() -> Component.translatable("commands.attribute.value.get.success", getAttributeDescription(attribute), target.getName(), result), false
);
return (int)(result * scale);
}
private static int getAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double scale) throws CommandSyntaxException {
LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
double result = livingEntity.getAttributeBaseValue(attribute);
source.sendSuccess(
() -> Component.translatable("commands.attribute.base_value.get.success", getAttributeDescription(attribute), target.getName(), result), false
);
return (int)(result * scale);
}
private static int getAttributeModifier(CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id, double scale) throws CommandSyntaxException {
LivingEntity livingEntity = getEntityWithAttribute(target, attribute);
AttributeMap attributes = livingEntity.getAttributes();
if (!attributes.hasModifier(attribute, id)) {
throw ERROR_NO_SUCH_MODIFIER.create(target.getName(), getAttributeDescription(attribute), id);
} else {
double result = attributes.getModifierValue(attribute, id);
source.sendSuccess(
() -> Component.translatable(
"commands.attribute.modifier.value.get.success", Component.translationArg(id), getAttributeDescription(attribute), target.getName(), result
),
false
);
return (int)(result * scale);
}
}
private static Stream<Identifier> getAttributeModifiers(Entity target, Holder<Attribute> attribute) throws CommandSyntaxException {
AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
return attributeInstance.getModifiers().stream().map(AttributeModifier::id);
}
private static int setAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute, double value) throws CommandSyntaxException {
getAttributeInstance(target, attribute).setBaseValue(value);
source.sendSuccess(
() -> Component.translatable("commands.attribute.base_value.set.success", getAttributeDescription(attribute), target.getName(), value), false
);
return 1;
}
private static int resetAttributeBase(CommandSourceStack source, Entity target, Holder<Attribute> attribute) throws CommandSyntaxException {
LivingEntity livingTarget = getLivingEntity(target);
if (!livingTarget.getAttributes().resetBaseValue(attribute)) {
throw ERROR_NO_SUCH_ATTRIBUTE.create(target.getName(), getAttributeDescription(attribute));
} else {
double value = livingTarget.getAttributeBaseValue(attribute);
source.sendSuccess(
() -> Component.translatable("commands.attribute.base_value.reset.success", getAttributeDescription(attribute), target.getName(), value), false
);
return 1;
}
}
private static int addModifier(
CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id, double value, AttributeModifier.Operation operation
) throws CommandSyntaxException {
AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
AttributeModifier modifier = new AttributeModifier(id, value, operation);
if (attributeInstance.hasModifier(id)) {
throw ERROR_MODIFIER_ALREADY_PRESENT.create(target.getName(), getAttributeDescription(attribute), id);
} else {
attributeInstance.addPermanentModifier(modifier);
source.sendSuccess(
() -> Component.translatable(
"commands.attribute.modifier.add.success", Component.translationArg(id), getAttributeDescription(attribute), target.getName()
),
false
);
return 1;
}
}
private static int removeModifier(CommandSourceStack source, Entity target, Holder<Attribute> attribute, Identifier id) throws CommandSyntaxException {
AttributeInstance attributeInstance = getAttributeInstance(target, attribute);
if (attributeInstance.removeModifier(id)) {
source.sendSuccess(
() -> Component.translatable(
"commands.attribute.modifier.remove.success", Component.translationArg(id), getAttributeDescription(attribute), target.getName()
),
false
);
return 1;
} else {
throw ERROR_NO_SUCH_MODIFIER.create(target.getName(), getAttributeDescription(attribute), id);
}
}
private static Component getAttributeDescription(Holder<Attribute> attribute) {
return Component.translatable(attribute.value().getDescriptionId());
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.hasPermission(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
SharedSuggestionProvider.suggestResource()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
EntityArgument.entity(), EntityArgument.getEntity()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
IdentifierArgument.getId(), IdentifierArgument.id()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
ResourceArgument.getAttribute(), ResourceArgument.resource()
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用/返回值 - 关联成员:
Component.translatable(), Component.translatableEscape(), Component.translationArg()
- 引用位置:
-
- 引用位置:
参数/返回值
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
返回值
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
返回值
- 引用位置:
-
- 引用位置:
参数/构造调用 - 关联成员:
AttributeModifier()
- 引用位置: