|
1 | 1 | package io.github.samarium150.minecraft.mod.structures_compass.item; |
2 | 2 |
|
| 3 | +import com.mojang.datafixers.util.Pair; |
3 | 4 | import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig; |
4 | 5 | import io.github.samarium150.minecraft.mod.structures_compass.network.StructuresCompassNetwork; |
5 | 6 | import io.github.samarium150.minecraft.mod.structures_compass.network.packet.CompassSearchPacket; |
|
9 | 10 | import io.github.samarium150.minecraft.mod.structures_compass.util.StructureUtils; |
10 | 11 | import net.minecraft.client.resources.language.I18n; |
11 | 12 | import net.minecraft.core.BlockPos; |
| 13 | +import net.minecraft.core.Holder; |
| 14 | +import net.minecraft.core.HolderSet; |
| 15 | +import net.minecraft.core.Registry; |
12 | 16 | import net.minecraft.nbt.*; |
13 | 17 | import net.minecraft.network.chat.TextComponent; |
14 | 18 | import net.minecraft.network.chat.TranslatableComponent; |
| 19 | +import net.minecraft.resources.ResourceKey; |
15 | 20 | import net.minecraft.resources.ResourceLocation; |
16 | 21 | import net.minecraft.server.level.ServerLevel; |
17 | 22 | import net.minecraft.world.InteractionHand; |
|
22 | 27 | import net.minecraft.world.item.ItemStack; |
23 | 28 | import net.minecraft.world.item.Rarity; |
24 | 29 | import net.minecraft.world.level.Level; |
| 30 | +import net.minecraft.world.level.chunk.ChunkGenerator; |
| 31 | +import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature; |
25 | 32 | import net.minecraft.world.level.levelgen.feature.StructureFeature; |
26 | 33 | import net.minecraft.world.phys.Vec3; |
27 | 34 | import net.minecraftforge.api.distmarker.Dist; |
@@ -160,18 +167,26 @@ public static boolean isSkip(@Nonnull ItemStack stack) { |
160 | 167 | * @see ServerLevel#findNearestMapFeature |
161 | 168 | */ |
162 | 169 | public static void search(@Nonnull ServerLevel world, Player player, @Nonnull StructureFeature<?> structure, ItemStack stack) { |
163 | | - ResourceLocation registry = structure.getRegistryName(); |
164 | | - assert registry != null; |
165 | | - setStructureName(registry.toString(), stack); |
| 170 | + ResourceLocation structureRegistryName = structure.getRegistryName(); |
| 171 | + assert structureRegistryName != null; |
| 172 | + setStructureName(structureRegistryName.toString(), stack); |
166 | 173 | sendTranslatedMessage(GeneralUtils.prefix + "msg_searching", player); |
167 | | - BlockPos pos = world.findNearestMapFeature( |
168 | | - structure, player.blockPosition(), StructuresCompassConfig.radius.get(), isSkip(stack) |
| 174 | + ChunkGenerator generator = world.getChunkSource().getGenerator(); |
| 175 | + HolderSet<ConfiguredStructureFeature<?, ?>> holderSet = world |
| 176 | + .registryAccess() |
| 177 | + .registryOrThrow(Registry.CONFIGURED_STRUCTURE_FEATURE_REGISTRY) |
| 178 | + .getHolder(ResourceKey.create(Registry.CONFIGURED_STRUCTURE_FEATURE_REGISTRY, structureRegistryName)) |
| 179 | + .map(HolderSet::direct) |
| 180 | + .orElseThrow(); |
| 181 | + Pair<BlockPos, Holder<ConfiguredStructureFeature<?, ?>>> result = generator.findNearestMapFeature( |
| 182 | + world, holderSet, player.blockPosition(), StructuresCompassConfig.radius.get(), isSkip(stack) |
169 | 183 | ); |
170 | 184 | sendTranslatedMessage(GeneralUtils.prefix + "msg_done", player); |
171 | | - if (pos == null) { |
| 185 | + if (result == null) { |
172 | 186 | ItemUtils.removeTag(stack, DIM_TAG); |
173 | 187 | ItemUtils.removeTag(stack, POS_TAG); |
174 | 188 | } else { |
| 189 | + BlockPos pos = result.getFirst(); |
175 | 190 | Vec3 dis = StructureUtils.getDistance(pos, player); |
176 | 191 | double distance = (double) Math.round(dis.length() * 100) / 100; |
177 | 192 | if (distance > StructuresCompassConfig.maxDistance.get()) { |
@@ -222,7 +237,8 @@ public InteractionResultHolder<ItemStack> use( |
222 | 237 | sendMessage(I18n.get(GeneralUtils.prefix + "msg_no_target"), player); |
223 | 238 | return super.use(world, player, hand); |
224 | 239 | } |
225 | | - StructureFeature<?> structure = StructureFeature.STRUCTURES_REGISTRY.get(name.replace("minecraft:", "")); |
| 240 | + @SuppressWarnings("deprecation") |
| 241 | + StructureFeature<?> structure = Registry.STRUCTURE_FEATURE.get(ResourceLocation.tryParse(name)); |
226 | 242 | if (structure == null) { |
227 | 243 | sendMessage(I18n.get(GeneralUtils.prefix + "msg_error_name") + name, player); |
228 | 244 | return super.use(world, player, hand); |
|
0 commit comments