Skip to content

Commit

Permalink
feat(world & server): Implement Spawn Limit API (#909)
Browse files Browse the repository at this point in the history
* feat(world & server): Implement Spawn Limit API

* Fix merge issues

---------

Co-authored-by: Thorinwasher <ragnarlol96@gmail.com>
  • Loading branch information
thelooter and Thorinwasher committed Feb 7, 2024
1 parent 4f0e712 commit ca614fc
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 47 deletions.
26 changes: 11 additions & 15 deletions src/main/java/be/seeseemelk/mockbukkit/ServerMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -1772,32 +1772,28 @@ public void setMaxChainedNeighborUpdates(int maxChainedNeighborUpdates)
@Deprecated
public int getMonsterSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.MONSTER);
}

@Override
@Deprecated
public int getAnimalSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.ANIMAL);
}

@Override
@Deprecated
public int getWaterAnimalSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.WATER_ANIMAL);
}

@Override
@Deprecated
public int getAmbientSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.AMBIENT);
}

@Override
Expand Down Expand Up @@ -2251,17 +2247,14 @@ public int getTicksPerWaterUndergroundCreatureSpawns()
@Deprecated
public int getWaterAmbientSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.WATER_AMBIENT);
}

@Override
@Deprecated
public int getWaterUndergroundCreatureSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();

return this.getSpawnLimit(SpawnCategory.WATER_UNDERGROUND_CREATURE);
}

@Override
Expand Down Expand Up @@ -2474,8 +2467,11 @@ public int getTicksPerSpawns(@NotNull SpawnCategory spawnCategory)
@Override
public int getSpawnLimit(@NotNull SpawnCategory spawnCategory)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Preconditions.checkArgument(spawnCategory != null, "SpawnCategory cannot be null");
Preconditions.checkArgument(spawnCategory != SpawnCategory.MISC,
"SpawnCategory.%s are not supported", spawnCategory);

return this.serverConfiguration.getSpawnLimits().getOrDefault(spawnCategory, -1);
}

@Override
Expand Down
72 changes: 42 additions & 30 deletions src/main/java/be/seeseemelk/mockbukkit/WorldMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import io.papermc.paper.event.world.WorldGameRuleChangeEvent;
import io.papermc.paper.math.Position;
import io.papermc.paper.world.MoonPhase;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import org.bukkit.BlockChangeDelegate;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -308,6 +309,7 @@ public class WorldMock implements World
private boolean getKeepSpawnInMemory = true;

private final Object2LongOpenHashMap<SpawnCategory> ticksPerSpawn = new Object2LongOpenHashMap<>();
private final Object2IntOpenHashMap<SpawnCategory> spawnLimits = new Object2IntOpenHashMap<>();

/**
* Creates a new mock world.
Expand Down Expand Up @@ -344,6 +346,7 @@ public WorldMock(Material defaultBlock, Biome defaultBiome, int minHeight, int m
{
this.pvp = this.server.getServerConfiguration().isPvpEnabled();
this.ticksPerSpawn.putAll(this.server.getServerConfiguration().getTicksPerSpawn());
this.spawnLimits.putAll(this.server.getServerConfiguration().getSpawnLimits());
}
else
{
Expand All @@ -356,6 +359,13 @@ public WorldMock(Material defaultBlock, Biome defaultBiome, int minHeight, int m
ticksPerSpawn.put(SpawnCategory.WATER_UNDERGROUND_CREATURE, 1);
ticksPerSpawn.put(SpawnCategory.WATER_ANIMAL, 1);
ticksPerSpawn.put(SpawnCategory.AMBIENT, 1);

spawnLimits.put(SpawnCategory.MONSTER, 70);
spawnLimits.put(SpawnCategory.ANIMAL, 10);
spawnLimits.put(SpawnCategory.WATER_ANIMAL, 5);
spawnLimits.put(SpawnCategory.WATER_AMBIENT, 20);
spawnLimits.put(SpawnCategory.WATER_UNDERGROUND_CREATURE, 5);
spawnLimits.put(SpawnCategory.AMBIENT, 15);
}

// Set the default gamerule values.
Expand Down Expand Up @@ -2041,82 +2051,70 @@ public void setTicksPerMonsterSpawns(int ticksPerMonsterSpawns)
@Deprecated(since = "1.18")
public int getMonsterSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.MONSTER);
}

@Override
@Deprecated(since = "1.18")
public void setMonsterSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
this.setSpawnLimit(SpawnCategory.MONSTER,limit);
}

@Override
@Deprecated(since = "1.18")
public int getAnimalSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.ANIMAL);
}

@Override
@Deprecated(since = "1.18")
public void setAnimalSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
this.setSpawnLimit(SpawnCategory.ANIMAL, limit);
}

@Override
@Deprecated(since = "1.18")
public int getWaterAnimalSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.WATER_ANIMAL);
}

@Override
@Deprecated(since = "1.18")
public void setWaterAnimalSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
this.setSpawnLimit(SpawnCategory.WATER_ANIMAL, limit);
}

@Override
@Deprecated(since = "1.18")
public int getWaterUndergroundCreatureSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();

return this.getSpawnLimit(SpawnCategory.WATER_UNDERGROUND_CREATURE);
}

@Override
@Deprecated(since = "1.18")
public void setWaterUndergroundCreatureSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();

this.setSpawnLimit(SpawnCategory.WATER_UNDERGROUND_CREATURE, limit);
}

@Override
@Deprecated(since = "1.18")
public int getAmbientSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.AMBIENT);
}

@Override
@Deprecated(since = "1.18")
public void setAmbientSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
this.setSpawnLimit(SpawnCategory.AMBIENT, limit);
}

@Override
Expand Down Expand Up @@ -3127,17 +3125,17 @@ public void setTicksPerWaterUndergroundCreatureSpawns(int ticksPerWaterUndergrou
}

@Override
@Deprecated(since = "1.18")
public int getWaterAmbientSpawnLimit()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return this.getSpawnLimit(SpawnCategory.WATER_AMBIENT);
}

@Override
@Deprecated(since = "1.18")
public void setWaterAmbientSpawnLimit(int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
this.setSpawnLimit(SpawnCategory.WATER_AMBIENT, limit);
}

@Override
Expand Down Expand Up @@ -3242,15 +3240,29 @@ public void setTicksPerSpawns(@NotNull SpawnCategory spawnCategory, int ticksPer
@Override
public int getSpawnLimit(@NotNull SpawnCategory spawnCategory)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Preconditions.checkArgument(spawnCategory != null, "SpawnCategory cannot be null");
Preconditions.checkArgument(spawnCategory != SpawnCategory.MISC,
"SpawnCategory.%s are not supported", spawnCategory);

return this.getSpawnLimitUnsafe(spawnCategory);
}

public final int getSpawnLimitUnsafe(final SpawnCategory spawnCategory) {
int limit = this.spawnLimits.getOrDefault(spawnCategory, -1);
if (limit < 0) {
limit = this.server.getSpawnLimit(spawnCategory);
}
return limit;
}

@Override
public void setSpawnLimit(@NotNull SpawnCategory spawnCategory, int limit)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Preconditions.checkArgument(spawnCategory != null, "SpawnCategory cannot be null");
Preconditions.checkArgument(spawnCategory != SpawnCategory.MISC,
"SpawnCategory.%s are not supported", spawnCategory);

this.spawnLimits.put(spawnCategory, limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package be.seeseemelk.mockbukkit.configuration;

import be.seeseemelk.mockbukkit.ServerMock;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.kyori.adventure.text.Component;
import org.bukkit.Server;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class ServerConfiguration
private int serverPort = 25565;
private boolean pvpEnabled = true;
private Object2LongOpenHashMap<SpawnCategory> ticksPerSpawn = new Object2LongOpenHashMap<>();
private Object2IntOpenHashMap<SpawnCategory> spawnLimits = new Object2IntOpenHashMap<>();

public ServerConfiguration()
{
Expand All @@ -49,6 +51,13 @@ public ServerConfiguration()
ticksPerSpawn.put(SpawnCategory.WATER_UNDERGROUND_CREATURE, 1);
ticksPerSpawn.put(SpawnCategory.WATER_ANIMAL, 1);
ticksPerSpawn.put(SpawnCategory.AMBIENT, 1);

spawnLimits.put(SpawnCategory.MONSTER, 70);
spawnLimits.put(SpawnCategory.ANIMAL, 10);
spawnLimits.put(SpawnCategory.WATER_ANIMAL, 5);
spawnLimits.put(SpawnCategory.WATER_AMBIENT, 20);
spawnLimits.put(SpawnCategory.WATER_UNDERGROUND_CREATURE, 5);
spawnLimits.put(SpawnCategory.AMBIENT, 15);
}

/**
Expand Down Expand Up @@ -398,6 +407,14 @@ public Object2LongOpenHashMap<SpawnCategory> getTicksPerSpawn()
return ticksPerSpawn;
}

/**
* @return The spawn limits for each spawn category.
*/
public Object2IntOpenHashMap<SpawnCategory> getSpawnLimits()
{
return spawnLimits;
}

/**
* Enum representing all different world types.
* <a href="https://minecraft.wiki/w/World_preset">Wiki</a>
Expand Down
72 changes: 70 additions & 2 deletions src/test/java/be/seeseemelk/mockbukkit/ServerMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1506,9 +1506,9 @@ void testGetServerConfiguration()

@ParameterizedTest
@MethodSource("testGetTicksPerSpawnsArguments")
void testGetTicksPerSpawns()
void testGetTicksPerSpawns(SpawnCategory category, int expected)
{
assertEquals(400, server.getTicksPerAnimalSpawns());
assertEquals(expected, server.getTicksPerSpawns(category));
}

public static Stream<Arguments> testGetTicksPerSpawnsArguments()
Expand Down Expand Up @@ -1571,6 +1571,74 @@ void testGetTicksPerAnimalSpawns()
assertEquals(400, server.getTicksPerAnimalSpawns());
}

@ParameterizedTest
@MethodSource("getSpawnLimitArguments")
void testGetSpawnLimit(SpawnCategory category, int expected)
{
assertEquals(expected, server.getSpawnLimit(category));
}

public static Stream<Arguments> getSpawnLimitArguments()
{
return Stream.of(
Arguments.of(SpawnCategory.MONSTER, 70),
Arguments.of(SpawnCategory.ANIMAL, 10),
Arguments.of(SpawnCategory.WATER_AMBIENT, 20),
Arguments.of(SpawnCategory.WATER_ANIMAL, 5),
Arguments.of(SpawnCategory.AMBIENT, 15),
Arguments.of(SpawnCategory.WATER_UNDERGROUND_CREATURE,5)
);
}

@Test
void testGetSpawnLimit_NullCategory()
{
assertThrows(IllegalArgumentException.class, () -> server.getSpawnLimit(null));
}

@Test
void testGetSpawnLimit_InvalidCategory()
{
assertThrows(IllegalArgumentException.class, () -> server.getSpawnLimit(SpawnCategory.MISC));
}

@Test
void testGetMonsterSpawnLimit()
{
assertEquals(70, server.getMonsterSpawnLimit());
}

@Test
void testGetWaterAmbientSpawnLimit()
{
assertEquals(20, server.getWaterAmbientSpawnLimit());
}

@Test
void testGetWaterAnimalSpawnLimit()
{
assertEquals(5, server.getWaterAnimalSpawnLimit());
}

@Test
void testGetAmbientSpawnLimit()
{
assertEquals(15, server.getAmbientSpawnLimit());
}

@Test
void testGetWaterUndergroundCreatureSpawnLimit()
{
assertEquals(5, server.getWaterUndergroundCreatureSpawnLimit());
}

@Test
void testGetAnimalSpawnLimit()
{
assertEquals(10, server.getAnimalSpawnLimit());
}


@Test
void testBanIP()
{
Expand Down

0 comments on commit ca614fc

Please sign in to comment.