diff --git a/gradle.properties b/gradle.properties index 6c25881..a97ccf8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,12 +1,9 @@ -# Done to increase the memory available to gradle. -org.gradle.jvmargs = -Xmx4G - # Fabric Properties minecraft_version = 1.16.5 yarn_mappings = 1.16.5+build.4 loader_version = 0.11.1 # Mod Properties -mod_version = 0.1.7 +mod_version = 0.2.0 maven_group = dev.hephaestus archives_base_name = FibLib \ No newline at end of file diff --git a/src/main/java/dev/hephaestus/fiblib/blocks/BlockFib.java b/src/main/java/dev/hephaestus/fiblib/blocks/BlockFib.java index 9ce2b01..5752735 100644 --- a/src/main/java/dev/hephaestus/fiblib/blocks/BlockFib.java +++ b/src/main/java/dev/hephaestus/fiblib/blocks/BlockFib.java @@ -3,31 +3,57 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.server.network.ServerPlayerEntity; +import org.jetbrains.annotations.Nullable; -public class BlockFib { +import java.util.function.Predicate; + +public class BlockFib implements Predicate { private final BlockState input; private final BlockState output; + private final boolean soft; + private final Predicate predicate; - public BlockFib(BlockState input, BlockState output) { + public BlockFib(BlockState input, BlockState output, @Nullable Predicate predicate, boolean soft) { this.input = input; this.output = output; + this.predicate = predicate == null ? this::condition : predicate; + this.soft = soft; + } + + public BlockFib(Block input, Block output, @Nullable Predicate predicate, boolean soft) { + this(input.getDefaultState(), output.getDefaultState(), predicate, soft); + } + + public BlockFib(BlockState input, BlockState output, boolean soft) { + this(input, output, null, soft); + } + + public BlockFib(Block input, Block output, boolean soft) { + this(input, output, null, soft); + } + + public BlockFib(BlockState input, BlockState output, @Nullable Predicate predicate) { + this(input, output, predicate, false); + } + + public BlockFib(Block input, Block output, @Nullable Predicate predicate) { + this(input.getDefaultState(), output.getDefaultState(), predicate, false); + } + + public BlockFib(BlockState input, BlockState output) { + this(input, output, null, false); } public BlockFib(Block input, Block output) { - this.input = input.getDefaultState(); - this.output = output.getDefaultState(); + this(input, output, null, false); } public BlockState getOutput(BlockState inputState) { return inputState == this.input ? this.output : inputState; } - public BlockState getOutput(BlockState inputState, ServerPlayerEntity player) { - boolean bl1 = player != null; - boolean bl2 = inputState == this.input; - boolean bl3 = condition(player); - - return bl1 && bl2 && bl3 ? getOutput(inputState) : inputState; + public BlockState getOutput(BlockState inputState, @Nullable ServerPlayerEntity player) { + return player != null && inputState == this.input && test(player) ? getOutput(inputState) : inputState; } public BlockState getInput() { @@ -40,6 +66,11 @@ public BlockState getInput() { * @return does not fib on block update (i.e. player right click) */ public boolean isSoft() { - return false; + return this.soft; + } + + @Override + public final boolean test(ServerPlayerEntity playerEntity) { + return this.predicate.test(playerEntity); } } diff --git a/src/main/java/dev/hephaestus/fiblib/blocks/LookupTable.java b/src/main/java/dev/hephaestus/fiblib/blocks/LookupTable.java index 9f77b62..d5c92b8 100644 --- a/src/main/java/dev/hephaestus/fiblib/blocks/LookupTable.java +++ b/src/main/java/dev/hephaestus/fiblib/blocks/LookupTable.java @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; public class LookupTable { private final Collection updated = new HashSet<>(); @@ -39,17 +40,24 @@ public BlockState get(BlockFib fib, BlockState input, ServerPlayerEntity player) public int getVersion() { return version; } public void update() { - playerLookupTable.entrySet().removeIf(entry -> entry.getKey().right.isDisconnected()); updated.clear(); - for (ImmutablePair key : playerLookupTable.keySet()) { - BlockState newState = key.left.getOutput(key.left.getInput(), key.right); - BlockState oldState = playerLookupTable.get(key); + Iterator> it = playerLookupTable.keySet().iterator(); - if (newState != oldState) { - playerLookupTable.put(key, newState); - updated.add(key.right); - ++updates; + while (it.hasNext()) { + ImmutablePair key = it.next(); + + if (key.right.isDisconnected() || key.right.removed) { + it.remove(); + } else { + BlockState newState = key.left.getOutput(key.left.getInput(), key.right); + BlockState oldState = playerLookupTable.get(key); + + if (newState != oldState) { + playerLookupTable.put(key, newState); + updated.add(key.right); + ++updates; + } } }