Files
src
build.gradle
gradle.properties
settings.gradle
Code
23 872
src/main/java/com/example/structurefinder/StructureFinder.java
package com.example.structurefinder;
import com.example.structurefinder.item.ModItemGroups;
import com.example.structurefinder.item.ModItems;
import com.example.structurefinder.network.RequestStructureLocationPayload;
import com.example.structurefinder.network.StructureLocationPayload;
import com.example.structurefinder.screen.ModScreenHandlers;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.gen.structure.Structure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class StructureFinder implements ModInitializer {
public static final String MOD_ID = "structurefinder";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final Map<UUID, Long> COOLDOWNS = new HashMap<>();
private static final long COOLDOWN_MS = 3000;
private static final int[] SEARCH_RADII = {50, 100, 200, 500, 1000};
@Override
public void onInitialize() {
ModItems.initialize();
ModItemGroups.initialize();
ModScreenHandlers.initialize();
PayloadTypeRegistry.playC2S().register(RequestStructureLocationPayload.ID, RequestStructureLocationPayload.CODEC);
PayloadTypeRegistry.playS2C().register(StructureLocationPayload.ID, StructureLocationPayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(RequestStructureLocationPayload.ID, (payload, context) -> {
ServerPlayerEntity player = context.player();
UUID playerUUID = player.getUuid();
long currentTime = System.currentTimeMillis();
if (COOLDOWNS.containsKey(playerUUID)) {
long lastUse = COOLDOWNS.get(playerUUID);
if (currentTime - lastUse < COOLDOWN_MS) {
long remaining = (COOLDOWN_MS - (currentTime - lastUse)) / 1000;
player.sendMessage(Text.translatable("structurefinder.cooldown", remaining), true);
return;
}
}
COOLDOWNS.put(playerUUID, currentTime);
ServerWorld world = player.getServerWorld();
Identifier structureId = Identifier.of(payload.structureId());
world.getRegistryManager().getOrThrow(RegistryKeys.STRUCTURE)
.getOptional(structureId)
.ifPresentOrElse(
structure -> findStructureWithFallback(player, world, structure, structureId, payload.structureId()),
() -> {
player.sendMessage(Text.translatable("structurefinder.invalid", structureId.toString()), false);
ServerPlayNetworking.send(player, new StructureLocationPayload(
payload.structureId(), 0, 0, 0, -1, "structurefinder.invalid"));
}
);
});
LOGGER.info("Structure Finder initialized!");
}
private void findStructureWithFallback(ServerPlayerEntity player, ServerWorld world, Structure structure,
Identifier structureId, String structureIdString) {
BlockPos playerPos = player.getBlockPos();
RegistryEntry.Reference<Structure> structureEntry = world.getRegistryManager()
.getOrThrow(RegistryKeys.STRUCTURE).getEntry(structureId).orElseThrow();
for (int radius : SEARCH_RADII) {
BlockPos structurePos = findStructure(world, structureEntry, playerPos, radius);
if (structurePos != null) {
double distance = Math.sqrt(playerPos.getSquaredDistance(structurePos));
ServerPlayNetworking.send(player, new StructureLocationPayload(
structureIdString,
structurePos.getX(),
structurePos.getY(),
structurePos.getZ(),
distance,
"structurefinder.found"
));
player.sendMessage(Text.translatable("structurefinder.found",
structureId.toString(),
structurePos.getX(), structurePos.getY(), structurePos.getZ(),
(int) distance), false);
world.playSound(null, player.getBlockPos(), SoundEvents.BLOCK_BELL_USE,
SoundCategory.PLAYERS, 1.0f, 1.0f);
spawnParticles(world, structurePos);
return;
}
}
player.sendMessage(Text.translatable("structurefinder.not_found", structureId.toString()), false);
world.playSound(null, player.getBlockPos(), SoundEvents.BLOCK_GLASS_BREAK,
SoundCategory.PLAYERS, 0.5f, 0.5f);
ServerPlayNetworking.send(player, new StructureLocationPayload(
structureIdString, 0, 0, 0, -1, "structurefinder.not_found"));
}
private BlockPos findStructure(ServerWorld world, RegistryEntry<Structure> structureEntry,
BlockPos origin, int radius) {
try {
return world.getChunkManager().getChunkGenerator()
.locateStructure(world, structureEntry, origin, radius, false)
.map(pair -> pair.getFirst())
.orElse(null);
} catch (Exception e) {
LOGGER.warn("Failed to locate structure: {}", e.getMessage());
return null;
}
}
private void spawnParticles(ServerWorld world, BlockPos pos) {
for (int i = 0; i < 30; i++) {
double offsetX = world.getRandom().nextGaussian() * 3;
double offsetY = world.getRandom().nextGaussian() * 3;
double offsetZ = world.getRandom().nextGaussian() * 3;
world.spawnParticles(
ParticleTypes.END_ROD,
pos.getX() + 0.5 + offsetX,
pos.getY() + offsetY,
pos.getZ() + 0.5 + offsetZ,
1, 0, 0, 0, 0.1
);
}
}
}