Skip to content

Commit

Permalink
BlockMock | Light level, LightFromSky and LightFromBlocks (#970)
Browse files Browse the repository at this point in the history
* Implemented block light, light from sky and light from blocks at BlockMock

* Updated code to enforce the values

* Replaced if statement with Preconditions
  • Loading branch information
4everTheOne committed Feb 17, 2024
1 parent 97402da commit 261bfb3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
36 changes: 30 additions & 6 deletions src/main/java/be/seeseemelk/mockbukkit/block/BlockMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class BlockMock implements Block
private byte data;
private BlockData blockData;

private byte lightFromSky = 15;
private byte lightFromBlocks = 0;

/**
* Creates a basic block made of air.
*/
Expand Down Expand Up @@ -171,22 +174,43 @@ public void assertType(@NotNull Material material)
@Override
public byte getLightLevel()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return (byte) Math.max(getLightFromSky(), getLightFromBlocks());
}

@Override
public byte getLightFromSky()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return lightFromSky;
}

/**
* Sets the light level received from sky.
*
* @param lightFromSky Value between 0 and 15.
*/
public void setLightFromSky(byte lightFromSky)
{
Preconditions.checkArgument(lightFromSky >= 0 && lightFromSky <= 15, "Light level should be between 0 and 15.");

this.lightFromSky = lightFromSky;
}

@Override
public byte getLightFromBlocks()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return lightFromBlocks;
}

/**
* Sets the light level received from other blocks.
*
* @param lightFromBlocks Value between 0 and 15.
*/
public void setLightFromBlocks(byte lightFromBlocks)
{
Preconditions.checkArgument(lightFromBlocks >= 0 && lightFromBlocks <= 15, "Light level should be between 0 and 15.");

this.lightFromBlocks = lightFromBlocks;
}

@Override
Expand Down
56 changes: 55 additions & 1 deletion src/test/java/be/seeseemelk/mockbukkit/block/BlockMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import be.seeseemelk.mockbukkit.ChunkMock;
import be.seeseemelk.mockbukkit.Coordinate;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.UnimplementedOperationException;
import be.seeseemelk.mockbukkit.WorldMock;
import be.seeseemelk.mockbukkit.block.data.BlockDataMock;
import org.bukkit.Location;
Expand All @@ -19,7 +18,10 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
Expand Down Expand Up @@ -102,6 +104,58 @@ void setType_SetToMaterialWithBlockDataMockSubclass()
assertInstanceOf(TrapDoor.class, block.getBlockData());
}

@Test
void getLightLevel() {
block.setLightFromSky((byte) 15);
assertEquals(15, block.getLightLevel());
block.setLightFromSky((byte) 5);
assertEquals(5, block.getLightLevel());
block.setLightFromBlocks((byte) 15);
assertEquals(15, block.getLightLevel());
}

@Test
void getLightFromSky() {
assertEquals(15, block.getLightFromSky());
block.setLightFromSky((byte) 0);
assertEquals(0, block.getLightFromSky());
}

@ParameterizedTest
@ValueSource(bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
void setLightFromSky_GivenValidValues(byte lightLevel) {
assertDoesNotThrow(() -> block.setLightFromSky(lightLevel));
}

@ParameterizedTest
@ValueSource(bytes = { -1, 16 })
void setLightFromSky_GivenInvalidValues(byte invalidLightLevel) {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> block.setLightFromSky(invalidLightLevel));

assertEquals("Light level should be between 0 and 15.", e.getMessage());
}

@Test
void getLightFromBlocks() {
assertEquals(0, block.getLightFromBlocks());
block.setLightFromBlocks((byte) 15);
assertEquals(15, block.getLightFromBlocks());
}

@ParameterizedTest
@ValueSource(bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
void setLightFromBlocks_GivenValidValues(byte lightLevel) {
assertDoesNotThrow(() -> block.setLightFromBlocks(lightLevel));
}

@ParameterizedTest
@ValueSource(bytes = { -1, 16 })
void setLightFromBlocks_GivenInvalidValues(byte invalidLightLevel) {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> block.setLightFromBlocks(invalidLightLevel));

assertEquals("Light level should be between 0 and 15.", e.getMessage());
}

@Test
void getLocation_Default_Null()
{
Expand Down

0 comments on commit 261bfb3

Please sign in to comment.