自定义战利品对象
战利品表系统较为复杂,会用到多个注册表,模组开发者可以通过它们扩展行为。
与战利品表相关的注册表都遵循类似的模式。添加注册项时,通常先继承某个类或实现某个接口,将功能放入其中。接着定义用于序列化的 codec,再像平时一样用 DeferredRegister 将 codec 注册到对应注册表。这符合多数注册表所采用的“一个基础对象,多个实例”模式,例如方块与方块状态、物品与物品堆叠。
自定义战利品条目
创建自定义战利品条目时,继承 LootPoolEntryContainer,或者它的两个直接子类之一:LootPoolSingletonContainer 或 CompositeEntryBase。这里以创建返回某个实体掉落物的条目类型为例。这仅用于演示,实际使用时直接引用另一个战利品表更合适。首先创建战利品条目的类:
// We extend LootPoolSingletonContainer since we have a "finite" set of drops.
// Some of this code is adapted from NestedLootTable.
public class EntityLootEntry extends LootPoolSingletonContainer {
public static final MapCodec<EntityLootEntry> CODEC = RecordCodecBuilder.mapCodec(inst ->
// Add our own fields.
inst.group(
// A value referencing an entity type id.
BuiltInRegistries.ENTITY_TYPE.holderByNameCodec().fieldOf("entity").forGetter(e -> e.entity)
)
// Add common fields: weight, display, conditions, and functions.
.and(singletonFields(inst))
.apply(inst, EntityLootEntry::new)
);
// A Holder for the entity type we want to roll the other table for.
private final Holder<EntityType<?>> entity;
// It is common practice to have a private constructor and have a static factory method.
// This is because weight, quality, conditions, and functions are supplied by a lambda below.
private EntityLootEntry(Holder<EntityType<?>> entity, int weight, int quality, List<LootItemCondition> conditions, List<LootItemFunction> functions) {
// Pass lambda-provided parameters to super.
super(weight, quality, conditions, functions);
// Set our values.
this.entity = entity;
}
// Static builder method, accepting our custom parameters and combining them with a lambda that supplies the values common to all entries.
public static LootPoolSingletonContainer.Builder<?> entityLoot(Holder<EntityType<?>> entity) {
// Use the static simpleBuilder() method defined in LootPoolSingletonContainer.
return simpleBuilder((weight, quality, conditions, functions) -> new EntityLootEntry(entity, weight, quality, conditions, functions));
}
// This is where the magic happens. To add an item stack, we generally call #accept on the consumer.
// However, in this case, we let #getRandomItems do that for us.
@Override
public void createItemStack(Consumer<ItemStack> consumer, LootContext context) {
// Get the entity's loot table. If it doesn't exist, an empty loot table will be returned, so null-checking is not necessary.
LootTable table = context.getLevel().reloadableRegistries().getLootTable(entity.value().getDefaultLootTable());
// Use the raw version here, because vanilla does it too. :P
// #getRandomItemsRaw calls consumer#accept for us on the results of the roll.
table.getRandomItemsRaw(context, consumer);
}
// Tells the entry what to use for serialization.
@Override
public MapCodec<EntityLootEntry> codec() {
return CODEC;
}
}
接下来在注册时使用 map codec:
public static final DeferredRegister<MapCodec<? extends LootPoolEntryContainer>> LOOT_POOL_ENTRY_TYPES =
DeferredRegister.create(Registries.LOOT_POOL_ENTRY_TYPE, ExampleMod.MOD_ID);
public static final Supplier<MapCodec<EntityLootEntry>> ENTITY_LOOT =
LOOT_POOL_ENTRY_TYPES.register("entity_loot", () -> EntityLootEntry.CODEC);
自定义数字提供器
创建自定义数字提供器时,实现 NumberProvider 接口。这里假设要创建一个将所提供数字的正负号反转的数字提供器:
// We accept another number provider as our base.
public record InvertedSignProvider(NumberProvider base) implements NumberProvider {
public static final MapCodec<InvertedSignProvider> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
NumberProviders.CODEC.fieldOf("base").forGetter(InvertedSignProvider::base)
).apply(inst, InvertedSignProvider::new));
// Return a float value. Use the context and the record parameters as needed.
@Override
public float getFloat(LootContext context) {
return -this.base.getFloat(context);
}
// Return an int value. Use the context and the record parameters as needed.
// Overriding this is optional, the default implementation will round the result of #getFloat.
@Override
public int getInt(LootContext context) {
return -this.base.getInt(context);
}
// Return a set of the loot context params used by this provider. See below for more information.
// Since we have a base value, we just defer to the base.
@Override
public Set<ContextKey<?>> getReferencedContextParams() {
return this.base.getReferencedContextParams();
}
// Tells the provider what to use for serialization.
@Override
public MapCodec<InvertedSignProvider> codec() {
return CODEC;
}
}
与自定义战利品条目一样,接下来使用这个 codec 进行注册:
public static final DeferredRegister<MapCodec<? extends NumberProvider>> LOOT_NUMBER_PROVIDER_TYPES =
DeferredRegister.create(Registries.LOOT_NUMBER_PROVIDER_TYPE, ExampleMod.MOD_ID);
public static final Supplier<MapCodec<? extends NumberProvider>> INVERTED_SIGN =
LOOT_NUMBER_PROVIDER_TYPES.register("inverted_sign", () -> InvertedSignProvider.CODEC);
自定义基于等级的值
在 record 中实现 LevelBasedValue 接口,即可创建自定义 LevelBasedValue。这里仍以反转另一个 LevelBasedValue 的输出为例:
public record InvertedSignLevelBasedValue(LevelBasedValue base) implements LevelBaseValue {
public static final MapCodec<InvertedLevelBasedValue> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
LevelBasedValue.CODEC.fieldOf("base").forGetter(InvertedLevelBasedValue::base)
).apply(inst, InvertedLevelBasedValue::new));
// Perform our operation.
@Override
public float calculate(int level) {
return -this.base.calculate(level);
}
// Tells the value what to use for serialization.
@Override
public MapCodec<InvertedLevelBasedValue> codec() {
return CODEC;
}
}
然后同样使用 codec 进行注册:
public static final DeferredRegister<MapCodec<? extends LevelBasedValue>> LEVEL_BASED_VALUES =
DeferredRegister.create(Registries.ENCHANTMENT_LEVEL_BASED_VALUE_TYPE, ExampleMod.MOD_ID);
public static final Supplier<MapCodec<InvertedSignLevelBasedValue>> INVERTED_SIGN =
LEVEL_BASED_VALUES.register("inverted_sign", () -> InvertedSignLevelBasedValue.CODEC);
自定义战利品条件
首先创建实现 LootItemCondition 的战利品条件类。这里假设只有击杀生物的玩家达到特定经验等级时,条件才通过:
public record HasXpLevelCondition(int level) implements LootItemCondition {
// Add the context we need for this condition. In our case, this will be the xp level the player must have.
public static final MapCodec<HasXpLevelCondition> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
Codec.INT.fieldOf("level").forGetter(HasXpLevelCondition::level)
).apply(inst, HasXpLevelCondition::new));
// Evaluates the condition here. Get the required loot context parameters from the provided LootContext.
// In our case, we want the KILLER_ENTITY to have at least our required level.
@Override
public boolean test(LootContext context) {
@Nullable
Entity entity = context.getOptionalParameter(LootContextParams.KILLER_ENTITY);
return entity instanceof Player player && player.experienceLevel >= level;
}
// Tell the game what parameters we expect from the loot context. Used in validation.
@Override
public Set<ContextKey<?>> getReferencedContextParams() {
return ImmutableSet.of(LootContextParams.KILLER_ENTITY);
}
// Tells the condition what to use for serialization.
@Override
public MapCodec<HasXpLevelCondition> codec() {
return CODEC;
}
}
可以将 map codec 注册到注册表:
public static final DeferredRegister<MapCodec<? extends LootItemCondition>> LOOT_CONDITION_TYPES =
DeferredRegister.create(Registries.LOOT_CONDITION_TYPE, ExampleMod.MOD_ID);
public static final Supplier<MapCodec<HasXpLevelCondition>> MIN_XP_LEVEL =
LOOT_CONDITION_TYPES.register("min_xp_level", () -> HasXpLevelCondition.CODEC);
自定义战利品函数
首先创建继承 LootItemFunction 的类。LootItemFunction 扩展了 BiFunction<ItemStack, LootContext, ItemStack>,所以我们要根据现有物品堆叠和战利品上下文,返回一个修改后的新物品堆叠。不过,几乎所有战利品函数都不会直接继承 LootItemFunction,而是继承 LootItemConditionalFunction。后者内置了为函数应用战利品条件的功能,只有条件满足时才会应用函数。这里以给物品施加指定等级的随机附魔为例:
// Code adapted from vanilla's EnchantRandomlyFunction class.
// LootItemConditionalFunction is an abstract class, not an interface, so we cannot use a record here.
public class RandomEnchantmentWithLevelFunction extends LootItemConditionalFunction {
// Our context: an optional list of enchantments, and a level.
private final Optional<HolderSet<Enchantment>> enchantments;
private final int level;
// Our codec.
public static final MapCodec<RandomEnchantmentWithLevelFunction> CODEC =
// #commonFields adds the conditions field.
RecordCodecBuilder.mapCodec(inst -> commonFields(inst).and(inst.group(
RegistryCodecs.homogeneousList(Registries.ENCHANTMENT).optionalFieldOf("enchantments").forGetter(e -> e.enchantments),
Codec.INT.fieldOf("level").forGetter(e -> e.level)
).apply(inst, RandomEnchantmentWithLevelFunction::new));
public RandomEnchantmentWithLevelFunction(List<LootItemCondition> conditions, Optional<HolderSet<Enchantment>> enchantments, int level) {
super(conditions);
this.enchantments = enchantments;
this.level = level;
}
// Run our enchantment application logic. Most of this is copied from EnchantRandomlyFunction#run.
@Override
public ItemStack run(ItemStack stack, LootContext context) {
RandomSource random = context.getRandom();
List<Holder<Enchantment>> stream = this.enchantments
.map(HolderSet::stream)
.orElseGet(() -> context.getLevel().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).listElements().map(Function.identity()))
.filter(e -> e.value().canEnchant(stack))
.toList();
Optional<Holder<Enchantment>> optional = Util.getRandomSafe(list, random);
if (optional.isEmpty()) {
LOGGER.warn("Couldn't find a compatible enchantment for {}", stack);
} else {
Holder<Enchantment> enchantment = optional.get();
if (stack.is(Items.BOOK)) {
stack = new ItemStack(Items.ENCHANTED_BOOK);
}
stack.enchant(enchantment, Mth.nextInt(random, enchantment.value().getMinLevel(), enchantment.value().getMaxLevel()));
}
return stack;
}
// Tells the function what to use for serialization.
@Override
public MapCodec<RandomEnchantmentWithLevelFunction> codec() {
return CODEC;
}
}
然后可以将 map codec 注册到注册表:
public static final DeferredRegister<MapCodec<? extends LootItemFunction>> LOOT_FUNCTION_TYPES =
DeferredRegister.create(Registries.LOOT_FUNCTION_TYPE, ExampleMod.MOD_ID);
public static final Supplier<MapCodec<RandomEnchantmentWithLevelFunction>> RANDOM_ENCHANTMENT_WITH_LEVEL =
LOOT_FUNCTION_TYPES.register("random_enchantment_with_level", () -> RandomEnchantmentWithLevelFunction.CODEC);
来源: NeoForged/Documentation(26.1)。中文翻译依据提交
4a680cdd4d15。