forked from MinecraftFreecam/Freecam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store whitelist conditions as predicate functions
- Loading branch information
1 parent
0dec601
commit d8d4a60
Showing
1 changed file
with
57 additions
and
20 deletions.
There are no files selected for viewing
77 changes: 57 additions & 20 deletions
77
common/src/main/java/net/xolt/freecam/config/CollisionWhitelist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |