Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 348
src/main/java/de/kodari/grasdeckel/Grasdeckel.java
package de.kodari.grasdeckel;
import com.mojang.brigadier.context.CommandContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
public class Grasdeckel implements ModInitializer {
private static final Map<UUID, Selection> SELECTIONS = new HashMap<>();
private static final Map<UUID, Job> JOBS = new HashMap<>();
private static final Map<UUID, Job> HISTORY = new HashMap<>();
private static final Map<UUID, UndoJob> UNDO_JOBS = new HashMap<>();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(CommandManager.literal("grasdeckel")
.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
.then(CommandManager.literal("pos1").executes(ctx -> select(ctx, true)))
.then(CommandManager.literal("pos2").executes(ctx -> select(ctx, false)))
.then(CommandManager.literal("fuellen").executes(Grasdeckel::start))
.then(CommandManager.literal("rueckgaengig").executes(Grasdeckel::undo))
.then(CommandManager.literal("abbrechen").executes(Grasdeckel::cancel))));
ServerTickEvents.END_SERVER_TICK.register(server -> {
Iterator<Job> iterator = JOBS.values().iterator();
while (iterator.hasNext()) {
Job job = iterator.next();
if (job.tick()) {
iterator.remove();
job.source.sendFeedback(() -> Text.literal("Grasdeckel fertig: " + job.placed + " Grasblöcke gesetzt, " + job.skipped + " belegte Positionen übersprungen."), false);
}
}
Iterator<UndoJob> undoIterator = UNDO_JOBS.values().iterator();
while (undoIterator.hasNext()) {
UndoJob undo = undoIterator.next();
if (undo.tick()) {
undoIterator.remove();
undo.source.sendFeedback(() -> Text.literal("Rückgängig abgeschlossen: " + undo.restored + " Blöcke wiederhergestellt, " + undo.skipped + " inzwischen veränderte Blöcke ausgelassen."), false);
}
}
});
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
JOBS.clear();
SELECTIONS.clear();
HISTORY.clear();
UNDO_JOBS.clear();
});
}
private static int select(CommandContext<ServerCommandSource> ctx, boolean first) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerCommandSource source = ctx.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
Selection selection = SELECTIONS.computeIfAbsent(player.getUuid(), id -> new Selection());
if (selection.world != source.getWorld()) {
selection.first = null;
selection.second = null;
selection.world = source.getWorld();
}
BlockPos pos = player.getBlockPos().down();
if (first) selection.first = pos; else selection.second = pos;
source.sendFeedback(() -> Text.literal((first ? "Position 1" : "Position 2") + " gesetzt: " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ()), false);
return 1;
}
private static int start(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerCommandSource source = ctx.getSource();
UUID id = source.getPlayerOrThrow().getUuid();
Selection selection = SELECTIONS.get(id);
if (selection == null || selection.first == null || selection.second == null || selection.world != source.getWorld()) {
source.sendError(Text.literal("Markiere zuerst beide Ecken in dieser Welt mit /grasdeckel pos1 und /grasdeckel pos2."));
return 0;
}
if (JOBS.containsKey(id)) {
source.sendError(Text.literal("Dein Grasdeckel wird bereits gebaut. Mit /grasdeckel abbrechen stoppen."));
return 0;
}
if (UNDO_JOBS.containsKey(id)) {
source.sendError(Text.literal("Das Rückgängigmachen läuft noch."));
return 0;
}
long width = Math.abs((long) selection.first.getX() - selection.second.getX()) + 1;
long depth = Math.abs((long) selection.first.getZ() - selection.second.getZ()) + 1;
if (width > 4096 || depth > 4096 || width * depth > 4_000_000L) {
source.sendError(Text.literal("Bereich zu groß: maximal 4096 Blöcke pro Seite und 4.000.000 Blöcke insgesamt."));
return 0;
}
Job job = new Job(source, selection);
JOBS.put(id, job);
HISTORY.put(id, job);
source.sendFeedback(() -> Text.literal("Grasdeckel gestartet. Der Rand wird abgetastet und die Fläche anschließend schrittweise gefüllt."), false);
return 1;
}
private static int cancel(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerCommandSource source = ctx.getSource();
if (JOBS.remove(source.getPlayerOrThrow().getUuid()) == null) {
source.sendError(Text.literal("Du hast keinen laufenden Grasdeckel."));
return 0;
}
source.sendFeedback(() -> Text.literal("Grasdeckel gestoppt. Mit /grasdeckel rueckgaengig kannst du die gesetzten Blöcke entfernen."), false);
return 1;
}
private static int undo(CommandContext<ServerCommandSource> ctx) throws com.mojang.brigadier.exceptions.CommandSyntaxException {
ServerCommandSource source = ctx.getSource();
UUID id = source.getPlayerOrThrow().getUuid();
if (UNDO_JOBS.containsKey(id)) {
source.sendError(Text.literal("Das Rückgängigmachen läuft bereits."));
return 0;
}
Job job = HISTORY.remove(id);
if (job == null) {
source.sendError(Text.literal("Kein Grasdeckel zum Rückgängigmachen vorhanden. Der Verlauf gilt nur bis zum Serverneustart und nicht für ältere Versionen."));
return 0;
}
JOBS.remove(id);
UNDO_JOBS.put(id, new UndoJob(source, job));
source.sendFeedback(() -> Text.literal("Rückgängigmachen gestartet; ein laufender Bau wurde gestoppt."), false);
return 1;
}
private record Change(BlockPos pos, BlockState before, BlockState placed) {}
private static final class UndoJob {
final ServerCommandSource source;
final Job job;
int index, restored, skipped;
UndoJob(ServerCommandSource source, Job job) {
this.source = source;
this.job = job;
this.index = job.changes.size() - 1;
}
boolean tick() {
int budget = 256;
while (budget-- > 0 && index >= 0) {
Change change = job.changes.get(index--);
if (job.world.getBlockState(change.pos()).equals(change.placed())) {
if (job.world.setBlockState(change.pos(), change.before())) restored++;
} else skipped++;
}
return index < 0;
}
}
private static final class Selection {
ServerWorld world;
BlockPos first;
BlockPos second;
}
private static final class Job {
final ServerCommandSource source;
final ServerWorld world;
final int minX, minZ, width, depth, referenceY;
final int[] left, right, north, south;
final List<Change> changes = new ArrayList<>();
int edgeIndex, cellIndex, placed, skipped;
int columnY, columnBottom;
boolean edgesReady, columnActive;
Job(ServerCommandSource source, Selection selection) {
this.source = source;
this.world = selection.world;
this.minX = Math.min(selection.first.getX(), selection.second.getX());
this.minZ = Math.min(selection.first.getZ(), selection.second.getZ());
this.width = Math.abs(selection.first.getX() - selection.second.getX()) + 1;
this.depth = Math.abs(selection.first.getZ() - selection.second.getZ()) + 1;
this.referenceY = (selection.first.getY() + selection.second.getY()) / 2;
this.left = new int[depth];
this.right = new int[depth];
this.north = new int[width];
this.south = new int[width];
}
boolean tick() {
int edgeCount = 2 * depth + 2 * width;
int budget = 256;
while (budget > 0 && edgeIndex < edgeCount) {
int i = edgeIndex++;
if (i < depth) left[i] = sample(minX - 1, minZ + i);
else if (i < 2 * depth) right[i - depth] = sample(minX + width, minZ + i - depth);
else if (i < 2 * depth + width) north[i - 2 * depth] = sample(minX + i - 2 * depth, minZ - 1);
else south[i - 2 * depth - width] = sample(minX + i - 2 * depth - width, minZ + depth);
