Skip to content

Commit

Permalink
Entity limits are now counted to include nether and end.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 8, 2020
1 parent 25be7a3 commit f907784
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.9.0</build.version>
<build.version>1.9.1</build.version>
</properties>

<!-- Profiles will allow to automatically change build version. -->
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/world/bentobox/limits/commands/LimitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void showLimits(World world, User user, UUID target) {
user.sendMessage("island.limits.no-limits");
return;
}
// Material limits
for (Entry<Material, Integer> en : matLimits.entrySet()) {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(en.getKey().toString()));
Expand All @@ -103,6 +104,7 @@ public void showLimits(World world, User user, UUID target) {
"[limit]", String.valueOf(en.getValue())));
pb.item(pib.build());
}
// Entity limits
addon.getSettings().getLimits().forEach((k,v) -> {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(k.toString()));
Expand Down Expand Up @@ -131,9 +133,22 @@ public void showLimits(World world, User user, UUID target) {
pb.build();
}

private long getCount(Island island, EntityType ent) {
return island.getWorld().getEntities().stream()
long getCount(Island island, EntityType ent) {
long count = island.getWorld().getEntities().stream()
.filter(e -> e.getType().equals(ent))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
// Nether
if (addon.getPlugin().getIWM().isNetherIslands(island.getWorld())) {
count += addon.getPlugin().getIWM().getNetherWorld(island.getWorld()).getEntities().stream()
.filter(e -> e.getType().equals(ent))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
// End
if (addon.getPlugin().getIWM().isEndIslands(island.getWorld())) {
count += addon.getPlugin().getIWM().getEndWorld(island.getWorld()).getEntities().stream()
.filter(e -> e.getType().equals(ent))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
return count;
}
}
105 changes: 105 additions & 0 deletions src/test/java/world/bentobox/limits/commands/LimitPanelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package world.bentobox.limits.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collections;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.limits.Limits;

@RunWith(PowerMockRunner.class)
@PrepareForTest( Bukkit.class )
public class LimitPanelTest {

@Mock
private Limits addon;

private LimitPanel lp;

@Mock
private Island island;
@Mock
private World world;
@Mock
private World nether;
@Mock
private World end;
@Mock
private BentoBox plugin;
@Mock
private IslandWorldManager iwm;

@Before
public void setUp() throws Exception {
// Island
when(island.getWorld()).thenReturn(world);
// Addon
when(addon.getPlugin()).thenReturn(plugin);
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isEndIslands(any())).thenReturn(true);
when(iwm.getNetherWorld(eq(world))).thenReturn(nether);
when(iwm.getEndWorld(eq(world))).thenReturn(end);
// Worlds
Entity entity = mock(Entity.class);
when(entity.getType()).thenReturn(EntityType.BAT);
when(entity.getLocation()).thenReturn(mock(Location.class));
when(world.getEntities()).thenReturn(Collections.singletonList(entity));
when(nether.getEntities()).thenReturn(Collections.singletonList(entity));
when(end.getEntities()).thenReturn(Collections.singletonList(entity));
lp = new LimitPanel(addon);
}

@After
public void tearDown() throws Exception {
}

@Test
@Ignore
public void testShowLimits() {
fail("Not yet implemented");
}

@Test
public void testGetCountInIslandSpace() {
when(island.inIslandSpace(any(Location.class))).thenReturn(true);
EntityType ent = EntityType.BAT;
assertEquals(3L, lp.getCount(island, ent));
ent = EntityType.GHAST;
assertEquals(0L, lp.getCount(island, ent));
when(iwm.isEndIslands(any())).thenReturn(false);
ent = EntityType.BAT;
assertEquals(2L, lp.getCount(island, ent));
when(iwm.isNetherIslands(any())).thenReturn(false);
ent = EntityType.BAT;
assertEquals(1L, lp.getCount(island, ent));
}

@Test
public void testGetCountNotInIslandSpace() {
EntityType ent = EntityType.BAT;
assertEquals(0L, lp.getCount(island, ent));
}

}

0 comments on commit f907784

Please sign in to comment.