| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.datafixers.DataFixUtils;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.function.UnaryOperator;
|
| 12 | import net.minecraft.commands.CommandSourceStack;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.component.DataComponentType;
|
| 15 | import net.minecraft.core.component.DataComponents;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.chat.ComponentSerialization;
|
| 18 | import net.minecraft.network.chat.ComponentUtils;
|
| 19 | import net.minecraft.network.chat.ResolutionContext;
|
| 20 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 21 | import net.minecraft.util.StringRepresentable;
|
| 22 | import net.minecraft.util.context.ContextKey;
|
| 23 | import net.minecraft.world.entity.Entity;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 26 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class SetNameFunction extends LootItemConditionalFunction {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | public static final MapCodec<SetNameFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 33 | i -> commonFields(i)
|
| 34 | .and(
|
| 35 | i.group(
|
| 36 | ComponentSerialization.CODEC.optionalFieldOf("name").forGetter(f -> f.name),
|
| 37 | LootContext.EntityTarget.CODEC.optionalFieldOf("entity").forGetter(f -> f.resolutionContext),
|
| 38 | SetNameFunction.Target.CODEC.optionalFieldOf("target", SetNameFunction.Target.CUSTOM_NAME).forGetter(f -> f.target)
|
| 39 | )
|
| 40 | )
|
| 41 | .apply(i, SetNameFunction::new)
|
| 42 | );
|
| 43 | private final Optional<Component> name;
|
| 44 | private final Optional<LootContext.EntityTarget> resolutionContext;
|
| 45 | private final SetNameFunction.Target target;
|
| 46 |
|
| 47 | private SetNameFunction(
|
| 48 | final Optional<Holder<LootItemCondition>> condition,
|
| 49 | final Optional<Component> name,
|
| 50 | final Optional<LootContext.EntityTarget> resolutionContext,
|
| 51 | final SetNameFunction.Target target
|
| 52 | ) {
|
| 53 | super(condition);
|
| 54 | this.name = name;
|
| 55 | this.resolutionContext = resolutionContext;
|
| 56 | this.target = target;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public MapCodec<SetNameFunction> codec() {
|
| 61 | return MAP_CODEC;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 66 | return DataFixUtils.orElse(this.resolutionContext.map(target -> Set.of(target.contextParam())), Set.of());
|
| 67 | }
|
| 68 |
|
| 69 | public static UnaryOperator<Component> createResolver(final LootContext context, final LootContext.@Nullable EntityTarget entityTarget) {
|
| 70 | if (entityTarget != null) {
|
| 71 | Entity entity = context.getOptional(entityTarget.contextParam());
|
| 72 | if (entity != null) {
|
| 73 | CommandSourceStack commandSourceStack = entity.createCommandSourceStackForNameResolution(context.getLevel())
|
| 74 | .withPermission(LevelBasedPermissionSet.GAMEMASTER);
|
| 75 | ResolutionContext resolutionContext = ResolutionContext.create(commandSourceStack);
|
| 76 | return line -> {
|
| 77 | try {
|
| 78 | return ComponentUtils.resolve(resolutionContext, line);
|
| 79 | } catch (CommandSyntaxException e) {
|
| 80 | LOGGER.warn("Failed to resolve text component", e);
|
| 81 | return line;
|
| 82 | }
|
| 83 | };
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | return line -> line;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 92 | this.name.ifPresent(name -> itemStack.set(this.target.component(), createResolver(context, this.resolutionContext.orElse(null)).apply(name)));
|
| 93 | return itemStack;
|
| 94 | }
|
| 95 |
|
| 96 | public static LootItemConditionalFunction.Builder<?> setName(final Component value, final SetNameFunction.Target target) {
|
| 97 | return simpleBuilder(conditions -> new SetNameFunction(conditions, Optional.of(value), Optional.empty(), target));
|
| 98 | }
|
| 99 |
|
| 100 | public static LootItemConditionalFunction.Builder<?> setName(
|
| 101 | final Component value, final SetNameFunction.Target target, final LootContext.EntityTarget resolutionContext
|
| 102 | ) {
|
| 103 | return simpleBuilder(conditions -> new SetNameFunction(conditions, Optional.of(value), Optional.of(resolutionContext), target));
|
| 104 | }
|
| 105 |
|
| 106 | public enum Target implements StringRepresentable {
|
| 107 | CUSTOM_NAME("custom_name"),
|
| 108 | ITEM_NAME("item_name");
|
| 109 |
|
| 110 | public static final Codec<SetNameFunction.Target> CODEC = StringRepresentable.fromEnum(SetNameFunction.Target::values);
|
| 111 | private final String name;
|
| 112 |
|
| 113 | Target(final String name) {
|
| 114 | this.name = name;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public String getSerializedName() {
|
| 119 | return this.name;
|
| 120 | }
|
| 121 |
|
| 122 | public DataComponentType<Component> component() {
|
| 123 | return switch (this) {
|
| 124 | case CUSTOM_NAME -> DataComponents.CUSTOM_NAME;
|
| 125 | case ITEM_NAME -> DataComponents.ITEM_NAME;
|
| 126 | };
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|