Refactored Generation and class structure

This commit is contained in:
2023-11-11 22:13:58 +01:00
parent d9bbaf9865
commit 6dc4269367
53 changed files with 1049 additions and 72 deletions

View File

@@ -2,14 +2,15 @@ package eu.mhsl.minenet.minigames.util;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.batch.AbsoluteBlockBatch;
import net.minestom.server.instance.batch.Batch;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.utils.chunk.ChunkUtils;
import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
public class BatchUtil {
public static long[] getAffectedChunks(AbsoluteBlockBatch batch) {
public static long[] getAffectedChunks(Batch batch) {
try {
Field field = batch.getClass().getDeclaredField("chunkBatchesMap");
field.setAccessible(true);
@@ -23,10 +24,33 @@ public class BatchUtil {
}
}
public static void loadAndApplyBatch(AbsoluteBlockBatch batch, InstanceContainer instance, Runnable onFinish) {
public static void loadAndApplyBatch(Batch batch, InstanceContainer instance, Runnable onFinish) {
batch.awaitReady();
long[] affectedChunks = BatchUtil.getAffectedChunks(batch);
ChunkUtils.optionalLoadAll(instance, affectedChunks, null).thenRun(() -> batch.apply(instance, onFinish));
}
public static void loadAndApplyBatchBlocking(Batch batch, InstanceContainer instance) {
CompletableFuture<Void> future = new CompletableFuture<>();
batch.awaitReady();
long[] affectedChunks = BatchUtil.getAffectedChunks(batch);
CompletableFuture<Void> loadChunksTask = ChunkUtils.optionalLoadAll(instance, affectedChunks, null);
Runnable completerTask = () -> {
System.out.println("COMPLETE");
future.complete(null);
};
loadChunksTask.thenRun(() -> {
System.out.println("BEGIN");
batch.apply(instance, completerTask);
});
try {
future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,81 @@
package eu.mhsl.minenet.minigames.util;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.generator.GenerationUnit;
import java.util.function.Consumer;
public class GeneratorUtils {
public static void foreachXZ(GenerationUnit unit, Consumer<Point> bottomPoint) {
double startX = unit.absoluteStart().x();
double endX = unit.absoluteEnd().x() - 1;
double startZ = unit.absoluteStart().z();
double endZ = unit.absoluteEnd().z() - 1;
for (double x = startX; x <= endX; x++) {
for (double z = startZ; z <= endZ; z++) {
Point currentPoint = new Pos(x, unit.absoluteStart().y(), z);
bottomPoint.accept(currentPoint);
}
}
}
public static void foreachXZ(Point start, Point end, Consumer<Point> callback) {
double startX = Math.min(start.x(), end.x());
double endX = Math.max(start.x(), end.x());
double startZ = Math.min(start.z(), end.z());
double endZ = Math.max(start.z(), end.z());
for (double x = startX; x <= endX; x++) {
for (double z = startZ; z <= endZ; z++) {
Point currentPoint = new Pos(x, start.y(), z);
callback.accept(currentPoint);
}
}
}
public static void iterateArea(Point pos1, Point pos2, Consumer<Point> callback) {
int minX = (int) Math.min(pos1.x(), pos2.x());
int maxX = (int) Math.max(pos1.x(), pos2.x());
int minY = (int) Math.min(pos1.y(), pos2.y());
int maxY = (int) Math.max(pos1.y(), pos2.y());
int minZ = (int) Math.min(pos1.z(), pos2.z());
int maxZ = (int) Math.max(pos1.z(), pos2.z());
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
Point point = new Pos(x, y, z);
callback.accept(point);
}
}
}
}
public static void outline(Point pos1, Point pos2, Consumer<Point> outline) {
outline(pos1, pos2, outline, point -> {});
}
public static void outline(Point pos1, Point pos2, Consumer<Point> outline, Consumer<Point> innerFill) {
int minX = (int) Math.min(pos1.x(), pos2.x());
int minY = (int) Math.min(pos1.y(), pos2.y());
int minZ = (int) Math.min(pos1.z(), pos2.z());
int maxX = (int) Math.max(pos1.x(), pos2.x());
int maxY = (int) Math.max(pos1.y(), pos2.y());
int maxZ = (int) Math.max(pos1.z(), pos2.z());
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
Point blockPos = new Pos(x, y, z);
if (x == minX || x == maxX || y == minY || y == maxY || z == minZ || z == maxZ) {
outline.accept(blockPos);
}
}
}
}
}
}

View File

@@ -1,6 +1,6 @@
package eu.mhsl.minenet.minigames.util;
import eu.mhsl.minenet.minigames.world.generator.BlockPallet;
import eu.mhsl.minenet.minigames.world.BlockPallet;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;

View File

@@ -0,0 +1,24 @@
package eu.mhsl.minenet.minigames.util;
import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
public class InventoryUtil {
public static void removeItemFromPlayer(Player player, Material material, int amount) {
ItemStack[] items = player.getInventory().getItemStacks();
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
if (item.material() != material) continue;
int stackSize = item.amount();
if (stackSize < amount) {
amount -= stackSize;
player.getInventory().setItemStack(i, ItemStack.AIR);
} else {
player.getInventory().setItemStack(i, item.withAmount(stackSize - amount));
break;
}
}
}
}

View File

@@ -0,0 +1,17 @@
package eu.mhsl.minenet.minigames.util;
public class NumberUtil {
public static double map(double oldValue, double oldMin, double oldMax, double newMin, double newMax) {
double out = (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
if(out > newMax) out = newMax;
if(out < newMin) out = newMin;
return out;
}
public static <T extends Comparable<T>> T clamp(T val, T min, T max) {
if (val.compareTo(min) < 0) return min;
else if (val.compareTo(max) > 0) return max;
else return val;
}
}

View File

@@ -1,8 +1,14 @@
package eu.mhsl.minenet.minigames.util;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
public class Position {
public static final double PIXEL = 0.0625;
@@ -23,4 +29,13 @@ public class Position {
return new Pos(x, y, z);
}
public static List<Block> blocksBelowPlayer(Instance instance, Player p) {
Point playerPos = p.getPosition();
List<Block> blocks = new ArrayList<>();
GeneratorUtils.foreachXZ(playerPos.sub(0.5, 1, 0.5), playerPos.add(0.5, -1, 0.5), point -> {
blocks.add(instance.getBlock(point));
});
return blocks.stream().distinct().toList();
}
}

View File

@@ -1,11 +0,0 @@
package eu.mhsl.minenet.minigames.util;
public class RangeMap {
public static long map(double oldValue, double oldMin, double oldMax, double newMin, double newMax) {
double out = (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
if(out > newMax) out = newMax;
if(out < newMin) out = newMin;
return (long) out;
}
}

View File

@@ -0,0 +1,19 @@
package eu.mhsl.minenet.minigames.util;
public class SingleExecution {
public static SingleExecution make() {
return new SingleExecution();
}
private boolean wasExecuted = false;
public void reset() {
this.wasExecuted = false;
}
public void singleRun(Runnable task) {
if(wasExecuted) return;
task.run();
this.wasExecuted = true;
}
}