Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 261
src/main/java/ru/kodari/dungeonmarkers/DungeonMarkers.java
package ru.kodari.dungeonmarkers;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.WeakHashMap;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.block.entity.MobSpawnerBlockEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.decoration.DisplayEntity;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.structure.StructureStart;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Heightmap;
import net.minecraft.world.chunk.WorldChunk;
import ru.kodari.dungeonmarkers.mixin.DisplayEntityAccessor;
import ru.kodari.dungeonmarkers.mixin.TextDisplayEntityAccessor;
public final class DungeonMarkers implements ModInitializer {
private static final Map<ServerWorld, Deque<WorldChunk>> PENDING = new WeakHashMap<>();
@Override
public void onInitialize() {
ServerChunkEvents.CHUNK_LOAD.register((world, chunk) ->
PENDING.computeIfAbsent(world, ignored -> new ArrayDeque<>()).add(chunk));
ServerTickEvents.END_WORLD_TICK.register(world -> {
Deque<WorldChunk> queue = PENDING.get(world);
if (queue == null) return;
for (int i = 0; i < 4 && !queue.isEmpty(); i++) {
WorldChunk chunk = queue.removeFirst();
if (world.isChunkLoaded(chunk.getPos().toLong())) scan(world, chunk);
}
});
}
private static void scan(ServerWorld world, WorldChunk chunk) {
MarkerState state = MarkerState.get(world);
for (StructureStart start : chunk.getStructureStarts().values()) {
if (!start.hasChildren()) continue;
Identifier id = world.getRegistryManager().getOrThrow(RegistryKeys.STRUCTURE).getId(start.getStructure());
if (id == null) continue;
BlockBox box = start.getBoundingBox();
int x = box.getMinX() + (box.getMaxX() - box.getMinX()) / 2;
int y = box.getMinY() + (box.getMaxY() - box.getMinY()) / 2;
int z = box.getMinZ() + (box.getMaxZ() - box.getMinZ()) / 2;
String key = "structure:" + id + ":" + chunk.getPos().getStartX() + ":" + chunk.getPos().getStartZ();
createMarker(world, state, key, name(id), x, y, z, box.getMaxY());
}
for (BlockPos pos : chunk.getBlockEntityPositions()) {
if (chunk.getBlockEntity(pos) instanceof MobSpawnerBlockEntity) {
String key = "spawner:" + pos.getX() + ":" + pos.getY() + ":" + pos.getZ();
createMarker(world, state, key, "Спавнер", pos.getX(), pos.getY(), pos.getZ(), pos.getY());
}
}
}
private static void createMarker(ServerWorld world, MarkerState state, String key, String name, int x, int y, int z, int top) {
if (state.contains(key)) return;
int surface = world.getTopY(Heightmap.Type.WORLD_SURFACE, x, z);
int markerY = Math.max(surface, top + 2) + 2;
DisplayEntity.TextDisplayEntity display = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY, world);
display.setPosition(x + 0.5, markerY, z + 0.5);
display.setInvulnerable(true);
display.setNoGravity(true);
display.addCommandTag("dungeon_markers");
((DisplayEntityAccessor) display).dungeonMarkers$setBillboardMode(DisplayEntity.BillboardMode.CENTER);
((DisplayEntityAccessor) display).dungeonMarkers$setViewRange(4.0f);
((TextDisplayEntityAccessor) display).dungeonMarkers$setDisplayFlags((byte) 3);
((TextDisplayEntityAccessor) display).dungeonMarkers$setText(Text.literal(name + "\n" + x + ", " + y + ", " + z));
if (world.spawnEntity(display)) state.add(key);
}
private static String name(Identifier id) {
if (!id.getNamespace().equals("minecraft")) return id.toString();
return switch (id.getPath()) {
case "mineshaft", "mineshaft_mesa" -> "Заброшенная шахта";
case "stronghold" -> "Крепость Края";
case "fortress" -> "Незерская крепость";
case "bastion_remnant" -> "Бастион";
case "ancient_city" -> "Древний город";
case "trial_chambers" -> "Камеры испытаний";
case "woodland_mansion" -> "Лесной особняк";
case "monument" -> "Подводная крепость";
case "desert_pyramid" -> "Храм в пустыне";
case "jungle_pyramid" -> "Храм в джунглях";
case "igloo" -> "Иглу";
case "swamp_hut" -> "Хижина ведьмы";
case "pillager_outpost" -> "Аванпост разбойников";
case "ocean_ruin_cold", "ocean_ruin_warm" -> "Подводные руины";
case "shipwreck", "shipwreck_beached" -> "Кораблекрушение";
case "buried_treasure" -> "Зарытый клад";
case "end_city" -> "Город Края";
case "ruined_portal", "ruined_portal_desert", "ruined_portal_jungle", "ruined_portal_swamp", "ruined_portal_mountain", "ruined_portal_ocean", "ruined_portal_nether" -> "Разрушенный портал";
case "village_plains", "village_desert", "village_savanna", "village_snowy", "village_taiga" -> "Деревня";
default -> id.getPath().replace('_', ' ');
};
}
}