Skip to content

Commit

Permalink
Adds some handy new constructors for BlockFib and some minor optimiza…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
Haven-King committed Feb 18, 2021
1 parent b0daa36 commit 5bbe1c9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
5 changes: 1 addition & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
53 changes: 42 additions & 11 deletions src/main/java/dev/hephaestus/fiblib/blocks/BlockFib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerPlayerEntity> {
private final BlockState input;
private final BlockState output;
private final boolean soft;
private final Predicate<ServerPlayerEntity> predicate;

public BlockFib(BlockState input, BlockState output) {
public BlockFib(BlockState input, BlockState output, @Nullable Predicate<ServerPlayerEntity> 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<ServerPlayerEntity> 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<ServerPlayerEntity> predicate) {
this(input, output, predicate, false);
}

public BlockFib(Block input, Block output, @Nullable Predicate<ServerPlayerEntity> 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() {
Expand All @@ -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);
}
}
24 changes: 16 additions & 8 deletions src/main/java/dev/hephaestus/fiblib/blocks/LookupTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerPlayerEntity> updated = new HashSet<>();
Expand Down Expand Up @@ -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<BlockFib, ServerPlayerEntity> key : playerLookupTable.keySet()) {
BlockState newState = key.left.getOutput(key.left.getInput(), key.right);
BlockState oldState = playerLookupTable.get(key);
Iterator<ImmutablePair<BlockFib, ServerPlayerEntity>> it = playerLookupTable.keySet().iterator();

if (newState != oldState) {
playerLookupTable.put(key, newState);
updated.add(key.right);
++updates;
while (it.hasNext()) {
ImmutablePair<BlockFib, ServerPlayerEntity> 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;
}
}
}

Expand Down

0 comments on commit 5bbe1c9

Please sign in to comment.