| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.commands.CacheableFunction;
|
| 9 | import net.minecraft.commands.CommandSourceStack;
|
| 10 | import net.minecraft.commands.functions.CommandFunction;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.HolderSet;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.MinecraftServer;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 19 | import net.minecraft.sounds.SoundEvents;
|
| 20 | import net.minecraft.sounds.SoundSource;
|
| 21 | import net.minecraft.util.Prediction;
|
| 22 | import net.minecraft.world.entity.item.ItemEntity;
|
| 23 | import net.minecraft.world.item.ItemStack;
|
| 24 | import net.minecraft.world.item.crafting.Recipe;
|
| 25 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 26 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 27 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 28 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 29 |
|
| 30 | public record AdvancementRewards(int experience, HolderSet<LootTable> loot, List<ResourceKey<Recipe<?>>> recipes, Optional<CacheableFunction> function) {
|
| 31 | public static final Codec<AdvancementRewards> CODEC = RecordCodecBuilder.create(
|
| 32 | i -> i.group(
|
| 33 | Codec.INT.optionalFieldOf("experience", 0).forGetter(AdvancementRewards::experience),
|
| 34 | LootTable.LIST_CODEC.optionalFieldOf("loot", HolderSet.empty()).forGetter(AdvancementRewards::loot),
|
| 35 | Recipe.KEY_CODEC.listOf().optionalFieldOf("recipes", List.of()).forGetter(AdvancementRewards::recipes),
|
| 36 | CacheableFunction.CODEC.optionalFieldOf("function").forGetter(AdvancementRewards::function)
|
| 37 | )
|
| 38 | .apply(i, AdvancementRewards::new)
|
| 39 | );
|
| 40 | public static final AdvancementRewards EMPTY = new AdvancementRewards(0, HolderSet.empty(), List.of(), Optional.empty());
|
| 41 |
|
| 42 | public void grant(final ServerPlayer player) {
|
| 43 | player.giveExperiencePoints(this.experience);
|
| 44 | ServerLevel level = player.level();
|
| 45 | MinecraftServer server = level.getServer();
|
| 46 | LootParams params = new LootParams.Builder(level)
|
| 47 | .withParameter(LootContextParams.THIS_ENTITY, player)
|
| 48 | .withParameter(LootContextParams.ORIGIN, player.position())
|
| 49 | .create(LootContextParamSets.ADVANCEMENT_REWARD);
|
| 50 | boolean changes = false;
|
| 51 |
|
| 52 | for (Holder<LootTable> lootTable : this.loot) {
|
| 53 | for (ItemStack itemStack : lootTable.value().getRandomItems(params)) {
|
| 54 | if (player.addItem(itemStack)) {
|
| 55 | level.playSound(
|
| 56 | null,
|
| 57 | player.getX(),
|
| 58 | player.getY(),
|
| 59 | player.getZ(),
|
| 60 | SoundEvents.ITEM_PICKUP,
|
| 61 | SoundSource.PLAYERS,
|
| 62 | 0.2F,
|
| 63 | ((player.getRandom().nextFloat() - player.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F
|
| 64 | );
|
| 65 | changes = true;
|
| 66 | } else {
|
| 67 | ItemEntity drop = player.drop(itemStack, false, Prediction.SERVER_ONLY);
|
| 68 | if (drop != null) {
|
| 69 | drop.setNoPickUpDelay();
|
| 70 | drop.setTarget(player.getUUID());
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | if (changes) {
|
| 77 | player.containerMenu.broadcastChanges();
|
| 78 | }
|
| 79 |
|
| 80 | if (!this.recipes.isEmpty()) {
|
| 81 | player.awardRecipesByKey(this.recipes);
|
| 82 | }
|
| 83 |
|
| 84 | this.function
|
| 85 | .flatMap(function -> function.get(server.getFunctions()))
|
| 86 | .ifPresent(
|
| 87 | function -> server.getFunctions()
|
| 88 | .execute(
|
| 89 | (CommandFunction<CommandSourceStack>)function,
|
| 90 | player.createCommandSourceStack().withSuppressedOutput().withPermission(LevelBasedPermissionSet.GAMEMASTER)
|
| 91 | )
|
| 92 | );
|
| 93 | }
|
| 94 |
|
| 95 | public static class Builder {
|
| 96 | private int experience;
|
| 97 | private final ImmutableList.Builder<Holder<LootTable>> loot = ImmutableList.builder();
|
| 98 | private final ImmutableList.Builder<ResourceKey<Recipe<?>>> recipes = ImmutableList.builder();
|
| 99 | private Optional<Identifier> function = Optional.empty();
|
| 100 |
|
| 101 | public static AdvancementRewards.Builder experience(final int amount) {
|
| 102 | return new AdvancementRewards.Builder().addExperience(amount);
|
| 103 | }
|
| 104 |
|
| 105 | public AdvancementRewards.Builder addExperience(final int amount) {
|
| 106 | this.experience += amount;
|
| 107 | return this;
|
| 108 | }
|
| 109 |
|
| 110 | public static AdvancementRewards.Builder loot(final Holder<LootTable> id) {
|
| 111 | return new AdvancementRewards.Builder().addLootTable(id);
|
| 112 | }
|
| 113 |
|
| 114 | public AdvancementRewards.Builder addLootTable(final Holder<LootTable> id) {
|
| 115 | this.loot.add(id);
|
| 116 | return this;
|
| 117 | }
|
| 118 |
|
| 119 | public static AdvancementRewards.Builder recipe(final ResourceKey<Recipe<?>> id) {
|
| 120 | return new AdvancementRewards.Builder().addRecipe(id);
|
| 121 | }
|
| 122 |
|
| 123 | public AdvancementRewards.Builder addRecipe(final ResourceKey<Recipe<?>> id) {
|
| 124 | this.recipes.add(id);
|
| 125 | return this;
|
| 126 | }
|
| 127 |
|
| 128 | public static AdvancementRewards.Builder function(final Identifier id) {
|
| 129 | return new AdvancementRewards.Builder().runs(id);
|
| 130 | }
|
| 131 |
|
| 132 | public AdvancementRewards.Builder runs(final Identifier function) {
|
| 133 | this.function = Optional.of(function);
|
| 134 | return this;
|
| 135 | }
|
| 136 |
|
| 137 | public AdvancementRewards build() {
|
| 138 | return new AdvancementRewards(this.experience, HolderSet.direct(this.loot.build()), this.recipes.build(), this.function.map(CacheableFunction::new));
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|