| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.util.context.ContextKey;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.MapItem;
|
| 17 | import net.minecraft.world.level.StructureManager;
|
| 18 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 19 | import net.minecraft.world.level.levelgen.structure.StructureStart;
|
| 20 | import net.minecraft.world.level.saveddata.maps.MapDecorationType;
|
| 21 | import net.minecraft.world.level.saveddata.maps.MapDecorationTypes;
|
| 22 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 23 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 24 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 25 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 |
|
| 28 | public class ExplorationMapFunction extends LootItemConditionalFunction {
|
| 29 | public static final Holder<MapDecorationType> DEFAULT_DECORATION = MapDecorationTypes.WOODLAND_MANSION;
|
| 30 | public static final byte DEFAULT_ZOOM = 2;
|
| 31 | public static final int DEFAULT_SEARCH_RADIUS = 50;
|
| 32 | public static final boolean DEFAULT_SKIP_EXISTING = true;
|
| 33 | public static final MapCodec<ExplorationMapFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> commonFields(i)
|
| 35 | .and(
|
| 36 | i.group(
|
| 37 | RegistryCodecs.holderSet(Registries.STRUCTURE).fieldOf("destination").forGetter(f -> f.destination),
|
| 38 | MapDecorationType.CODEC.optionalFieldOf("decoration", DEFAULT_DECORATION).forGetter(f -> f.mapDecoration),
|
| 39 | Codec.BYTE.optionalFieldOf("zoom", (byte)2).forGetter(f -> f.zoom),
|
| 40 | Codec.INT.optionalFieldOf("search_radius", 50).forGetter(f -> f.searchRadius),
|
| 41 | Codec.BOOL.optionalFieldOf("skip_existing_chunks", true).forGetter(f -> f.skipKnownStructures)
|
| 42 | )
|
| 43 | )
|
| 44 | .apply(i, ExplorationMapFunction::new)
|
| 45 | );
|
| 46 | private final HolderSet<Structure> destination;
|
| 47 | private final Holder<MapDecorationType> mapDecoration;
|
| 48 | private final byte zoom;
|
| 49 | private final int searchRadius;
|
| 50 | private final boolean skipKnownStructures;
|
| 51 |
|
| 52 | private ExplorationMapFunction(
|
| 53 | final Optional<Holder<LootItemCondition>> condition,
|
| 54 | final HolderSet<Structure> destination,
|
| 55 | final Holder<MapDecorationType> mapDecoration,
|
| 56 | final byte zoom,
|
| 57 | final int searchRadius,
|
| 58 | final boolean skipKnownStructures
|
| 59 | ) {
|
| 60 | super(condition);
|
| 61 | this.destination = destination;
|
| 62 | this.mapDecoration = mapDecoration;
|
| 63 | this.zoom = zoom;
|
| 64 | this.searchRadius = searchRadius;
|
| 65 | this.skipKnownStructures = skipKnownStructures;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public MapCodec<ExplorationMapFunction> codec() {
|
| 70 | return MAP_CODEC;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 75 | return Set.of(LootContextParams.ORIGIN);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 80 | if (itemStack.isEmpty()) {
|
| 81 | return itemStack;
|
| 82 | }
|
| 83 |
|
| 84 | Vec3 lootPos = context.getOptional(LootContextParams.ORIGIN);
|
| 85 | if (lootPos == null) {
|
| 86 | return itemStack;
|
| 87 | }
|
| 88 |
|
| 89 | ServerLevel level = context.getLevel();
|
| 90 | BlockPos lootBlockPos = BlockPos.containing(lootPos);
|
| 91 | if (this.skipKnownStructures) {
|
| 92 | this.claimStructureAt(level, lootBlockPos);
|
| 93 | }
|
| 94 |
|
| 95 | BlockPos nearestMapStructure = level.findNearestMapStructure(this.destination, lootBlockPos, this.searchRadius, this.skipKnownStructures);
|
| 96 | if (nearestMapStructure == null) {
|
| 97 | return itemStack;
|
| 98 | }
|
| 99 |
|
| 100 | MapItem.applyNewSavedData(level, itemStack, nearestMapStructure.getX(), nearestMapStructure.getZ(), this.zoom, true, true);
|
| 101 | MapItem.renderBiomePreviewMap(level, itemStack);
|
| 102 | MapItemSavedData.addTargetDecoration(itemStack, nearestMapStructure, "+", this.mapDecoration);
|
| 103 | return itemStack;
|
| 104 | }
|
| 105 |
|
| 106 | private void claimStructureAt(final ServerLevel level, final BlockPos pos) {
|
| 107 | StructureManager structureManager = level.structureManager();
|
| 108 | StructureStart start = structureManager.getStructureAt(pos, this.destination);
|
| 109 | if (start.isValid() && start.canBeReferenced()) {
|
| 110 | structureManager.addReference(start);
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | public static ExplorationMapFunction.Builder makeExplorationMap(final HolderSet<Structure> destination) {
|
| 115 | return new ExplorationMapFunction.Builder(destination);
|
| 116 | }
|
| 117 |
|
| 118 | public static class Builder extends LootItemConditionalFunction.Builder<ExplorationMapFunction.Builder> {
|
| 119 | private final HolderSet<Structure> destination;
|
| 120 | private Holder<MapDecorationType> mapDecoration = ExplorationMapFunction.DEFAULT_DECORATION;
|
| 121 | private byte zoom = 2;
|
| 122 | private int searchRadius = 50;
|
| 123 | private boolean skipKnownStructures = true;
|
| 124 |
|
| 125 | public Builder(final HolderSet<Structure> destination) {
|
| 126 | this.destination = destination;
|
| 127 | }
|
| 128 |
|
| 129 | protected ExplorationMapFunction.Builder getThis() {
|
| 130 | return this;
|
| 131 | }
|
| 132 |
|
| 133 | public ExplorationMapFunction.Builder setMapDecoration(final Holder<MapDecorationType> mapDecoration) {
|
| 134 | this.mapDecoration = mapDecoration;
|
| 135 | return this;
|
| 136 | }
|
| 137 |
|
| 138 | public ExplorationMapFunction.Builder setZoom(final byte zoom) {
|
| 139 | this.zoom = zoom;
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | public ExplorationMapFunction.Builder setSearchRadius(final int searchRadius) {
|
| 144 | this.searchRadius = searchRadius;
|
| 145 | return this;
|
| 146 | }
|
| 147 |
|
| 148 | public ExplorationMapFunction.Builder setSkipKnownStructures(final boolean skipKnownStructures) {
|
| 149 | this.skipKnownStructures = skipKnownStructures;
|
| 150 | return this;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public LootItemFunction build() {
|
| 155 | return new ExplorationMapFunction(this.getCondition(), this.destination, this.mapDecoration, this.zoom, this.searchRadius, this.skipKnownStructures);
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|