Skip to content

Commit

Permalink
Store whitelist conditions as predicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Jan 2, 2024
1 parent 0dec601 commit d8d4a60
Showing 1 changed file with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,73 @@
package net.xolt.freecam.config;

import net.minecraft.world.level.block.BarrierBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.FenceGateBlock;
import net.minecraft.world.level.block.IronBarsBlock;
import net.minecraft.world.level.block.TransparentBlock;
import net.minecraft.world.level.block.TrapDoorBlock;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CollisionWhitelist {

private static final Collection<Class<? extends Block>> transparentWhitelist = List.of(
TransparentBlock.class,
IronBarsBlock.class,
BarrierBlock.class);
private static final Predicate<Block> transparent = Builder.builder()
.matching(TransparentBlock.class, IronBarsBlock.class)
.matching(BarrierBlock.class)
.build();

private static final Collection<Class<? extends Block>> openableWhitelist = List.of(
FenceGateBlock.class,
DoorBlock.class,
TrapDoorBlock.class);
private static final Predicate<Block> openable = Builder.builder()
.matching(FenceGateBlock.class)
.matching(DoorBlock.class, TrapDoorBlock.class)
.build();

public static boolean isTransparent(Block block) {
return isMatch(block, transparentWhitelist);
return transparent.test(block);
}

public static boolean isOpenable(Block block) {
return isMatch(block, openableWhitelist);
return openable.test(block);
}

private static String getBlockId(Block block) {
return BuiltInRegistries.BLOCK.getKey(block).toString();
}

private static boolean isMatch(Block block, Collection<Class<? extends Block>> whitelist) {
return whitelist.stream().anyMatch(blockClass -> blockClass.isInstance(block));
private static class Builder {
private final Collection<Predicate<Block>> predicates = new ArrayList<>();

private Builder() {}

public static Builder builder() {
return new Builder();
}

public final Builder matching(String... ids) {
return matching(block -> Arrays.asList(ids).contains(getBlockId(block)));
}

public final Builder matching(Pattern... patterns) {
return matching(block -> {
String id = getBlockId(block);
return Arrays.stream(patterns)
.map(pattern -> pattern.matcher(id))
.anyMatch(Matcher::find);
});
}

@SafeVarargs
public final Builder matching(Class<? extends Block>... classes) {
return matching(block -> Arrays.stream(classes).anyMatch(clazz -> clazz.isInstance(block)));
}

public final Builder matching(Predicate<Block> predicate) {
predicates.add(predicate);
return this;
}

public Predicate<Block> build() {
return block -> predicates.stream().anyMatch(predicate -> predicate.test(block));
}
}
}

0 comments on commit d8d4a60

Please sign in to comment.