mirror of https://github.com/Minestom/Minestom.git
Stop builder in favor of wither
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
3155c99e2b
commit
dae6da3824
|
@ -2,7 +2,6 @@ package net.minestom.server.instance.batch;
|
||||||
|
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ApiStatus.Experimental
|
@ApiStatus.Experimental
|
||||||
|
@ -14,13 +13,13 @@ public sealed interface BatchQuery
|
||||||
return BatchQueryImpl.fallback(getter, x, y, z, condition, query);
|
return BatchQueryImpl.fallback(getter, x, y, z, condition, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
static @NotNull Builder builder(int radius) {
|
static @NotNull BatchQuery radius(int radius) {
|
||||||
return new BatchQueryImpl.Builder(radius);
|
return BatchQueryImpl.radius(radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
static @NotNull BatchQuery radius(int radius) {
|
@NotNull BatchQuery withType(@NotNull Block @NotNull ... blocks);
|
||||||
return builder(radius).build();
|
|
||||||
}
|
@NotNull BatchQuery withExact(@NotNull Block @NotNull ... blocks);
|
||||||
|
|
||||||
interface Result extends Block.Getter {
|
interface Result extends Block.Getter {
|
||||||
/**
|
/**
|
||||||
|
@ -30,15 +29,4 @@ public sealed interface BatchQuery
|
||||||
*/
|
*/
|
||||||
int count();
|
int count();
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed interface Builder
|
|
||||||
permits BatchQueryImpl.Builder {
|
|
||||||
@Contract("_ -> this")
|
|
||||||
@NotNull Builder type(@NotNull Block @NotNull ... blocks);
|
|
||||||
|
|
||||||
@Contract("_ -> this")
|
|
||||||
@NotNull Builder exact(@NotNull Block @NotNull ... blocks);
|
|
||||||
|
|
||||||
@NotNull BatchQuery build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,43 @@ import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.coordinate.Vec;
|
import net.minestom.server.coordinate.Vec;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.annotations.UnknownNullability;
|
import org.jetbrains.annotations.UnknownNullability;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
record BatchQueryImpl(int radius,
|
record BatchQueryImpl(int radius,
|
||||||
@NotNull Predicate<Block> predicate) implements BatchQuery {
|
@Nullable Set<Integer> type,
|
||||||
|
@Nullable Set<Block> exact) implements BatchQuery {
|
||||||
|
public BatchQueryImpl {
|
||||||
|
type = type != null ? Set.copyOf(type) : null;
|
||||||
|
exact = exact != null ? Set.copyOf(exact) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static BatchQuery radius(int radius) {
|
||||||
|
return new BatchQueryImpl(radius, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull BatchQuery withType(@NotNull Block @NotNull ... blocks) {
|
||||||
|
var updatedType = Arrays.stream(blocks).map(Block::id).collect(Collectors.toUnmodifiableSet());
|
||||||
|
return new BatchQueryImpl(radius, updatedType, exact);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull BatchQuery withExact(@NotNull Block @NotNull ... blocks) {
|
||||||
|
return new BatchQueryImpl(radius, type, Set.of(blocks));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean valid(Block block) {
|
||||||
|
if (type != null && !type.contains(block.id()))
|
||||||
|
return false;
|
||||||
|
return exact == null || exact.contains(block);
|
||||||
|
}
|
||||||
|
|
||||||
static Result fallback(Block.Getter getter, int x, int y, int z,
|
static Result fallback(Block.Getter getter, int x, int y, int z,
|
||||||
Block.Getter.Condition condition, BatchQuery query) {
|
Block.Getter.Condition condition, BatchQuery query) {
|
||||||
|
@ -29,7 +55,7 @@ record BatchQueryImpl(int radius,
|
||||||
final int blockY = y + j;
|
final int blockY = y + j;
|
||||||
final int blockZ = z + k;
|
final int blockZ = z + k;
|
||||||
final Block block = getter.getBlock(blockX, blockY, blockZ);
|
final Block block = getter.getBlock(blockX, blockY, blockZ);
|
||||||
if (!queryImpl.predicate.test(block)) {
|
if (!queryImpl.valid(block)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
blocks.put(new Vec(blockX, blockY, blockZ), block);
|
blocks.put(new Vec(blockX, blockY, blockZ), block);
|
||||||
|
@ -55,40 +81,4 @@ record BatchQueryImpl(int radius,
|
||||||
return blocks.size();
|
return blocks.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class Builder implements BatchQuery.Builder {
|
|
||||||
private final int radius;
|
|
||||||
private Set<Integer> type;
|
|
||||||
private Set<Block> exact;
|
|
||||||
|
|
||||||
Builder(Integer radius) {
|
|
||||||
this.radius = radius;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BatchQuery.@NotNull Builder type(@NotNull Block @NotNull ... blocks) {
|
|
||||||
this.type = Arrays.stream(blocks).map(Block::id).collect(Collectors.toUnmodifiableSet());
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BatchQuery.@NotNull Builder exact(@NotNull Block @NotNull ... blocks) {
|
|
||||||
this.exact = Set.of(blocks);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull BatchQuery build() {
|
|
||||||
var type = this.type != null ? Set.copyOf(this.type) : null;
|
|
||||||
var exact = this.exact != null ? Set.copyOf(this.exact) : null;
|
|
||||||
return new BatchQueryImpl(radius,
|
|
||||||
block -> {
|
|
||||||
if (type != null && !type.contains(block.id()))
|
|
||||||
return false;
|
|
||||||
if (exact != null && !exact.contains(block))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class BatchQueryIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void type(Env env) {
|
public void type(Env env) {
|
||||||
var instance = env.process().instance().createInstanceContainer();
|
var instance = env.process().instance().createInstanceContainer();
|
||||||
var batch = BatchQuery.builder(3).type(Block.STONE).build();
|
var batch = BatchQuery.radius(3).withType(Block.STONE);
|
||||||
|
|
||||||
instance.loadChunk(0, 0).join();
|
instance.loadChunk(0, 0).join();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue