Skip to content

Commit

Permalink
Merge pull request #73 from syldium/feature/world-time
Browse files Browse the repository at this point in the history
Added time/environment methods to WorldMock
  • Loading branch information
seeseemelk committed Jun 6, 2020
2 parents c79a603 + 5b471f6 commit f89a05b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/main/java/be/seeseemelk/mockbukkit/ServerMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -1084,8 +1084,7 @@ public int getAmbientSpawnLimit()
@Override
public boolean isPrimaryThread()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.isOnMainThread();
}

@Override
Expand Down
34 changes: 24 additions & 10 deletions src/main/java/be/seeseemelk/mockbukkit/WorldMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.entity.LightningStrike;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.world.TimeSkipEvent;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -54,10 +55,12 @@ public class WorldMock implements World
private String name = "World";
private UUID uuid = UUID.randomUUID();
private Location spawnLocation;
private long fullTime = 0;
private int weatherDuration = 0;
private int thunderDuration = 0;
private boolean storming = false;
private Map<GameRule<?>, Object> gameRules = new HashMap<>();
private Environment environment = Environment.NORMAL;

/**
* Creates a new mock world.
Expand Down Expand Up @@ -632,29 +635,31 @@ public Collection<Entity> getNearbyEntities(Location location, double x, double
@Override
public long getTime()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getFullTime() % 24000L;
}

@Override
public void setTime(long time)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
long base = this.getFullTime() - this.getFullTime() % 24000L;
this.setFullTime(base + time % 24000L);
}

@Override
public long getFullTime()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.fullTime;
}

@Override
public void setFullTime(long time)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
TimeSkipEvent event = new TimeSkipEvent(this, TimeSkipEvent.SkipReason.CUSTOM, time - this.getFullTime());
this.server.getPluginManager().callEvent(event);
if (!event.isCancelled())
{
this.fullTime += event.getSkipAmount();
}
}

@Override
Expand Down Expand Up @@ -743,8 +748,17 @@ public boolean createExplosion(Location loc, float power, boolean setFire)
@Override
public Environment getEnvironment()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.environment;
}

/**
* Set a new environment type for this world.
*
* @param environment The world environnement type.
*/
public void setEnvironment(Environment environment)
{
this.environment = environment;
}

@Override
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/be/seeseemelk/mockbukkit/WorldMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.world.TimeSkipEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -241,6 +242,60 @@ public void spawnZombieTest(){
assertTrue(world.getEntities().size()>0);

}

@Test
public void onCreated_TimeSetToBeZero()
{
WorldMock world = new WorldMock();
assertEquals("World time should be zero", 0L, world.getFullTime());
assertEquals("Day time should be zero", 0L, world.getTime());
}

@Test
public void setTime_DayTimeValue()
{
WorldMock world = new WorldMock();
world.setTime(20L);
assertEquals(20L, world.getTime());
assertEquals(20L, world.getFullTime());
}

@Test
public void setTime_FullTimeValue()
{
WorldMock world = new WorldMock();
world.setFullTime(3L * 24000L + 20L);
assertEquals(20L, world.getTime());
assertEquals(3L * 24000L + 20L, world.getFullTime());
}

@Test
public void setTime_FullTimeShouldBeAdjustedWithDayTime()
{
WorldMock world = new WorldMock();
world.setFullTime(3L * 24000L + 20L);
world.setTime(12000L);
assertEquals(12000L, world.getTime());
assertEquals(3L * 24000L + 12000L, world.getFullTime());
}

@Test
public void setTime_Event_Triggered()
{
WorldMock world = new WorldMock();
world.setTime(6000L);
world.setTime(10000L);
server.getPluginManager().assertEventFired(TimeSkipEvent.class, event ->
event.getSkipAmount() == 4000L && event.getSkipReason().equals(TimeSkipEvent.SkipReason.CUSTOM)
);
}

@Test
public void onCreated_EnvironmentSetToBeNormal()
{
WorldMock world = new WorldMock();
assertEquals("World environment type should be normal", World.Environment.NORMAL, world.getEnvironment());
}
}


Expand Down

0 comments on commit f89a05b

Please sign in to comment.