-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TNTPrimed Mock and Tests * Removed public modifier for Test class Co-authored-by: Nirbose <62440475+Nirbose@users.noreply.github.com> * Added explosion code and tests * Added LivingEntity check for source and tests * fix(codestyle): Rename variables and code style --------- Co-authored-by: Nirbose <62440475+Nirbose@users.noreply.github.com> Co-authored-by: thelooter <evekolb2204@gmail.com>
- Loading branch information
1 parent
d67e60d
commit 0ce3833
Showing
2 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/main/java/be/seeseemelk/mockbukkit/entity/TNTPrimedMock.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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package be.seeseemelk.mockbukkit.entity; | ||
|
||
import be.seeseemelk.mockbukkit.ServerMock; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.entity.TNTPrimed; | ||
import org.bukkit.event.entity.ExplosionPrimeEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Mock implementation of a {@link TNTPrimed}. | ||
* | ||
* @see EntityMock | ||
*/ | ||
public class TNTPrimedMock extends EntityMock implements TNTPrimed | ||
{ | ||
|
||
private int fuseTicks = 80; | ||
private Entity source; | ||
private float explosionYield = 4; | ||
private boolean incendiary = false; | ||
|
||
/** | ||
* Constructs a new {@link TNTPrimedMock} on the provided {@link ServerMock} with a specified {@link UUID}. | ||
* | ||
* @param server The server to create the entity on. | ||
* @param uuid The UUID of the entity. | ||
*/ | ||
protected TNTPrimedMock(@NotNull ServerMock server, @NotNull UUID uuid) | ||
{ | ||
super(server, uuid); | ||
} | ||
|
||
@Override | ||
public void setFuseTicks(int fuseTicks) | ||
{ | ||
this.fuseTicks = fuseTicks; | ||
} | ||
|
||
@Override | ||
public int getFuseTicks() | ||
{ | ||
return this.fuseTicks; | ||
} | ||
|
||
@Override | ||
public @Nullable Entity getSource() | ||
{ | ||
return this.source; | ||
} | ||
|
||
@Override | ||
public void setSource(@Nullable Entity source) | ||
{ | ||
if(source instanceof LivingEntity) { | ||
this.source = source; | ||
} else { | ||
this.source = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void setYield(float explosionYield) | ||
{ | ||
this.explosionYield = explosionYield; | ||
} | ||
|
||
@Override | ||
public float getYield() | ||
{ | ||
return this.explosionYield; | ||
} | ||
|
||
@Override | ||
public void setIsIncendiary(boolean incendiary) | ||
{ | ||
this.incendiary = incendiary; | ||
} | ||
|
||
@Override | ||
public boolean isIncendiary() | ||
{ | ||
return this.incendiary; | ||
} | ||
|
||
@Override | ||
public @NotNull EntityType getType() | ||
{ | ||
return EntityType.PRIMED_TNT; | ||
} | ||
|
||
/** | ||
* Simulate server tick. | ||
* @param ticks The number of ticks to simulate. | ||
*/ | ||
public void tick(int ticks) | ||
{ | ||
setFuseTicks(getFuseTicks() - ticks); | ||
if(getFuseTicks() <= 0) { | ||
explode(); | ||
this.remove(); | ||
} | ||
} | ||
|
||
/** | ||
* Simulate one server tick. | ||
*/ | ||
public void tick() | ||
{ | ||
tick(1); | ||
} | ||
|
||
private void explode() | ||
{ | ||
ExplosionPrimeEvent event = new ExplosionPrimeEvent(this); | ||
server.getPluginManager().callEvent(event); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/test/java/be/seeseemelk/mockbukkit/entity/TNTPrimedMockTest.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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package be.seeseemelk.mockbukkit.entity; | ||
|
||
import be.seeseemelk.mockbukkit.MockBukkit; | ||
import be.seeseemelk.mockbukkit.MockBukkitExtension; | ||
import be.seeseemelk.mockbukkit.MockBukkitInject; | ||
import be.seeseemelk.mockbukkit.ServerMock; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.event.entity.ExplosionPrimeEvent; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@ExtendWith(MockBukkitExtension.class) | ||
class TNTPrimedMockTest | ||
{ | ||
|
||
@MockBukkitInject | ||
private ServerMock server; | ||
private TNTPrimedMock tntPrimed; | ||
|
||
@BeforeEach | ||
void setUp() | ||
{ | ||
tntPrimed = new TNTPrimedMock(server, UUID.randomUUID()); | ||
} | ||
|
||
@Test | ||
void testGetType() | ||
{ | ||
assertEquals(EntityType.PRIMED_TNT, tntPrimed.getType()); | ||
} | ||
|
||
@Test | ||
void testGetFuseTicksDefault() | ||
{ | ||
assertEquals(80, tntPrimed.getFuseTicks()); | ||
} | ||
|
||
@Test | ||
void testSetFuseTicks() | ||
{ | ||
tntPrimed.setFuseTicks(10); | ||
assertEquals(10, tntPrimed.getFuseTicks()); | ||
} | ||
|
||
@Test | ||
void testGetSourceDefault() | ||
{ | ||
assertNull(tntPrimed.getSource()); | ||
} | ||
|
||
@Test | ||
void testSetSource() { | ||
PlayerMock playerMock = server.addPlayer(); | ||
tntPrimed.setSource(playerMock); | ||
assertEquals(playerMock, tntPrimed.getSource()); | ||
} | ||
|
||
@Test | ||
void testSetSourceNotLivingEntity() | ||
{ | ||
tntPrimed.setSource(new ExperienceOrbMock(server, UUID.randomUUID())); | ||
assertNull(tntPrimed.getSource()); | ||
} | ||
|
||
@Test | ||
void testGetYieldDefault() | ||
{ | ||
assertEquals(4, tntPrimed.getYield()); | ||
} | ||
|
||
@Test | ||
void testSetYield() | ||
{ | ||
tntPrimed.setYield(2); | ||
assertEquals(2, tntPrimed.getYield()); | ||
} | ||
|
||
@Test | ||
void testIsIncendiaryDefault() | ||
{ | ||
assertFalse(tntPrimed.isIncendiary()); | ||
} | ||
|
||
@Test | ||
void testSetIsIncendiary() | ||
{ | ||
tntPrimed.setIsIncendiary(true); | ||
assertTrue(tntPrimed.isIncendiary()); | ||
} | ||
|
||
@Test | ||
void testExplosion() | ||
{ | ||
tntPrimed.tick(tntPrimed.getFuseTicks()); | ||
assertTrue(tntPrimed.isDead()); | ||
server.getPluginManager().assertEventFired(ExplosionPrimeEvent.class); | ||
} | ||
|
||
@Test | ||
void testOneTickNoExplosion() | ||
{ | ||
tntPrimed.tick(); | ||
assertFalse(tntPrimed.isDead()); | ||
server.getPluginManager().assertEventNotFired(ExplosionPrimeEvent.class); | ||
} | ||
|
||
@Test | ||
void testOneTickExplosion() | ||
{ | ||
tntPrimed.setFuseTicks(1); | ||
tntPrimed.tick(); | ||
assertTrue(tntPrimed.isDead()); | ||
server.getPluginManager().assertEventFired(ExplosionPrimeEvent.class); | ||
} | ||
} |