Skip to content

Commit

Permalink
Make all BlockProperty properties use lazily evaluated Suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Oct 22, 2017
1 parent 5afe5e6 commit bd579d9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public BWBlock(net.minecraft.block.Block block) {
public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
this.mcBlock = block;
components.add(new BWBlockTransform(this, world, pos));
components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().isOpaque() ? 1 : 0));
components.add(new BlockProperty.Opacity()).setOpacity(() -> mcBlock.getMaterial().isOpaque() ? 1 : 0);
Optional.of(components.add(new BlockProperty.Replaceable()))
.filter(r -> block != Blocks.air)
.ifPresent(r -> r.setReplaceable(() -> mcBlock.isReplaceable(blockAccess(), xi(), yi(), zi())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public ProxyMaterial(MapColor color, Optional<BlockProperty.Opacity> opacity, Op
this.replaceable = replaceable;
}

@Override
public boolean getCanBlockGrass() {
return opacity.isPresent() ? opacity.get().isOpaque() : super.isOpaque();
}

@Override
public boolean isOpaque() {
return opacity.isPresent() ? opacity.get().isOpaque() : super.isOpaque();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BWBlock(net.minecraft.block.Block block) {
public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
this.mcBlock = block;
components.add(new BWBlockTransform(this, world, pos));
components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().blocksLight() ? 1 : 0));
components.add(new BlockProperty.Opacity()).setOpacity(() -> mcBlock.getMaterial().isOpaque() ? 1 : 0);
Optional.of(components.add(new BlockProperty.Replaceable()))
.filter(r -> block != Blocks.air)
.ifPresent(r -> r.setReplaceable(() -> mcBlock.isReplaceable((net.minecraft.world.World) blockAccess(), blockPos())));
Expand Down
62 changes: 48 additions & 14 deletions src/main/java/nova/core/block/component/BlockProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;

/**
* Block properties.
Expand All @@ -46,26 +47,36 @@ public interface BlockProperty {
*/
@UnsidedComponent
public static class Hardness extends Component implements BlockProperty {
private double hardness = 1.0;
private DoubleSupplier hardness = () -> 1;

/**
* Sets the breaking difficulty.
*
* @param hardness The breaking difficulty.
* @return This instance for chaining if desired.
*/
public Hardness setHardness(double hardness) {
public Hardness setHardness(DoubleSupplier hardness) {
this.hardness = hardness;
return this;
}

/**
* Sets the breaking difficulty.
*
* @param hardness The breaking difficulty.
* @return This instance for chaining if desired.
*/
public Hardness setHardness(double hardness) {
return this.setHardness(() -> hardness);
}

/**
* Gets the breaking difficulty.
*
* @return The breaking difficulty.
*/
public double getHardness() {
return hardness;
return hardness.getAsDouble();
}
}

Expand All @@ -77,26 +88,36 @@ public double getHardness() {
*/
@UnsidedComponent
public static class Resistance extends Component implements BlockProperty {
private double resistance = 1.0;
private DoubleSupplier resistance = () -> 1;

/**
* Sets the blast resistance
*
* @param resistance The blast resistance.
* @return This instance for chaining if desired.
*/
public Resistance setResistance(double resistance) {
public Resistance setResistance(DoubleSupplier resistance) {
this.resistance = resistance;
return this;
}

/**
* Sets the blast resistance
*
* @param resistance The blast resistance.
* @return This instance for chaining if desired.
*/
public Resistance setResistance(double resistance) {
return this.setResistance(() -> resistance);
}

/**
* Gets the blast resistance.
*
* @return The blast resistance.
*/
public double getResistance() {
return resistance;
return resistance.getAsDouble();
}
}

Expand Down Expand Up @@ -176,18 +197,21 @@ public enum BlockSoundTrigger {
@SidedComponent
@SuppressWarnings("deprecation")
public static class Opacity extends Component implements BlockProperty {
private static final DoubleSupplier TRANSPARENT = () -> 0;
private static final DoubleSupplier OPAQUE = () -> 1;

/**
* This value determines if the block should allow light through itself or not.
*/
private double opacity = 1;
private DoubleSupplier opacity = OPAQUE;

/**
* Sets that the block should allow light through
*
* @return This instance for chaining if desired.
*/
public Opacity setTransparent() {
opacity = 0;
opacity = TRANSPARENT;
return this;
}

Expand All @@ -197,7 +221,7 @@ public Opacity setTransparent() {
* @return This instance for chaining if desired.
*/
public Opacity setOpaque() {
opacity = 1;
opacity = OPAQUE;
return this;
}

Expand All @@ -207,18 +231,28 @@ public Opacity setOpaque() {
* @param opacity The block's opacity
* @return This instance for chaining if desired.
*/
public Opacity setOpacity(double opacity) {
this.opacity = MathUtil.clamp(opacity, 0, 1);
public Opacity setOpacity(DoubleSupplier opacity) {
this.opacity = opacity;
return this;
}

/**
* Sets if light should be transmitted through this block
*
* @param opacity The block's opacity
* @return This instance for chaining if desired.
*/
public Opacity setOpacity(double opacity) {
return this.setOpacity(opacity <= 0 ? TRANSPARENT : (opacity >= 1 ? OPAQUE : () -> opacity));
}

/**
* This value determines if the block should allow light through itself or not.
*
* @return The block's opacity
*/
public double getOpacity() {
return opacity;
return MathUtil.clamp(opacity.getAsDouble(), 0, 1);
}

/**
Expand All @@ -227,7 +261,7 @@ public double getOpacity() {
* @return If the block should allow light through
*/
public boolean isTransparent() {
return opacity < 1;
return getOpacity() < 1;
}

/**
Expand All @@ -236,7 +270,7 @@ public boolean isTransparent() {
* @return If the block should disallow light through
*/
public boolean isOpaque() {
return opacity == 1;
return getOpacity() == 1;
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/test/java/nova/core/block/component/BlockPropertyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ public class BlockPropertyTest {

@Test
public void testHardness() {
BlockProperty.Hardness hardness = new BlockProperty.Hardness().setHardness(1.0);
BlockProperty.Hardness hardness = new BlockProperty.Hardness();
assertThat(hardness.getHardness()).isEqualTo(1.0);

hardness.setHardness(10.0);
assertThat(hardness.getHardness()).isEqualTo(10.0);
}

@Test
public void testResistance() {
BlockProperty.Resistance resistance = new BlockProperty.Resistance().setResistance(1.0);
BlockProperty.Resistance resistance = new BlockProperty.Resistance();
assertThat(resistance.getResistance()).isEqualTo(1.0);

resistance.setResistance(10.0);
assertThat(resistance.getResistance()).isEqualTo(10.0);
}

@Test
Expand Down Expand Up @@ -74,6 +80,16 @@ public void testOpacity() {
assertThat(opacity.getOpacity()).isEqualTo(0.5);
assertThat(opacity.isOpaque()).isFalse();
assertThat(opacity.isTransparent()).isTrue();

opacity.setOpacity(-0.5);
assertThat(opacity.getOpacity()).isEqualTo(0.0);
assertThat(opacity.isOpaque()).isFalse();
assertThat(opacity.isTransparent()).isTrue();

opacity.setOpacity(1.5);
assertThat(opacity.getOpacity()).isEqualTo(1.0);
assertThat(opacity.isOpaque()).isTrue();
assertThat(opacity.isTransparent()).isFalse();
}

@Test
Expand Down

0 comments on commit bd579d9

Please sign in to comment.