Fixed the world-purger using the names from the new EntiyType-enum.

Closes #478.
Should fix #482.
Poor sheep.
This commit is contained in:
main() 2012-02-25 14:48:36 +01:00
parent ccb96ead10
commit 3f09fbd710
2 changed files with 165 additions and 62 deletions

View File

@ -14,11 +14,7 @@ import com.onarandombox.MultiverseCore.api.WorldPurger;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Animals;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Ghast;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Squid;
import java.util.ArrayList;
@ -71,22 +67,57 @@ public class SimpleWorldPurger implements WorldPurger {
if (mvworld == null) {
return;
}
World world = this.plugin.getServer().getWorld(mvworld.getName());
World world = mvworld.getCBWorld();
if (world == null) {
return;
}
int entitiesKilled = 0;
boolean specifiedAll = thingsToKill.contains("ALL");
boolean specifiedAnimals = thingsToKill.contains("ANIMALS") || specifiedAll;
boolean specifiedMonsters = thingsToKill.contains("MONSTERS") || specifiedAll;
for (Entity e : world.getEntities()) {
this.plugin.log(Level.FINEST, "Entity list (aval for purge) from WORLD < " + mvworld.getName() + " >: " + e.toString());
// Check against Monsters
if (killMonster(mvworld, e, thingsToKill, negateMonsters)) {
boolean negate;
boolean specified = false;
if (e instanceof Squid || e instanceof Animals) {
// it's an animal
if (specifiedAnimals && !negateAnimals) {
this.plugin.log(Level.FINEST, "Removing an entity because I was told to remove all animals: " + e);
e.remove();
entitiesKilled++;
continue;
}
// Check against Animals
if (this.killCreature(mvworld, e, thingsToKill, negateAnimals)) {
if (specifiedAnimals)
specified = true;
negate = negateAnimals;
} else {
// it's a monster
if (specifiedMonsters && !negateMonsters) {
this.plugin.log(Level.FINEST, "Removing an entity because I was told to remove all monsters: " + e);
e.remove();
entitiesKilled++;
continue;
}
if (specifiedMonsters)
specified = true;
negate = negateMonsters;
}
for (String s : thingsToKill) {
if (e.getType().getName().equalsIgnoreCase(s)) {
specified = true;
if (!negate) {
this.plugin.log(Level.FINEST, "Removing an entity because it WAS specified and we are NOT negating: " + e);
e.remove();
entitiesKilled++;
continue;
}
break;
}
}
if (!specified && negate) {
this.plugin.log(Level.FINEST, "Removing an entity because it was NOT specified and we ARE negating: " + e);
e.remove();
entitiesKilled++;
continue;
}
}
if (sender != null) {
@ -101,53 +132,4 @@ public class SimpleWorldPurger implements WorldPurger {
public void purgeWorld(MultiverseWorld mvworld, List<String> thingsToKill, boolean negateAnimals, boolean negateMonsters) {
purgeWorld(mvworld, thingsToKill, negateAnimals, negateMonsters, null);
}
private boolean killCreature(MultiverseWorld mvworld, Entity e, List<String> creaturesToKill, boolean negate) {
String entityName = e.toString().replaceAll("Craft", "").toUpperCase();
if (e instanceof Squid || e instanceof Animals) {
if (creaturesToKill.contains(entityName) || creaturesToKill.contains("ALL") || creaturesToKill.contains("ANIMALS")) {
if (!negate) {
e.remove();
return true;
}
} else {
if (negate) {
e.remove();
return true;
}
}
}
return false;
}
/*
* Will kill the monster if it's in the list UNLESS the NEGATE boolean is set, then it will kill it if it's NOT.
*/
private boolean killMonster(MultiverseWorld mvworld, Entity e, List<String> creaturesToKill, boolean negate) {
String entityName = "";
//TODO: Fixme once either Rigby puts his awesome thing in OR Enderdragon gets a toString, OR both.
if (e instanceof EnderDragon) {
entityName = "ENDERDRAGON";
} else {
entityName = e.toString().replaceAll("Craft", "").toUpperCase();
}
if (e instanceof Slime || e instanceof Monster || e instanceof Ghast || e instanceof EnderDragon) {
this.plugin.log(Level.FINEST, "Looking at a monster: " + e);
if (creaturesToKill.contains(entityName) || creaturesToKill.contains("ALL") || creaturesToKill.contains("MONSTERS")) {
if (!negate) {
this.plugin.log(Level.FINEST, "Removing a monster: " + e);
e.remove();
return true;
}
} else {
if (negate) {
this.plugin.log(Level.FINEST, "Removing a monster: " + e);
e.remove();
return true;
}
}
}
return false;
}
}

View File

@ -0,0 +1,121 @@
package com.onarandombox.MultiverseCore.test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Zombie;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.api.WorldPurger;
import com.onarandombox.MultiverseCore.test.utils.TestInstanceCreator;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ MultiverseCore.class })
public class TestWorldPurger {
TestInstanceCreator creator;
MultiverseCore core;
WorldPurger purger;
MultiverseWorld mvWorld;
World cbworld;
Sheep sheep;
Zombie zombie;
@Before
public void setUp() throws Exception {
creator = new TestInstanceCreator();
assertTrue(creator.setUp());
core = creator.getCore();
purger = core.getMVWorldManager().getTheWorldPurger();
core.getMVConfig().setGlobalDebug(3);
mvWorld = mock(MultiverseWorld.class);
cbworld = mock(World.class);
when(mvWorld.getCBWorld()).thenReturn(cbworld);
}
@After
public void tearDown() throws Exception {
creator.tearDown();
}
@Test
public void test() {
// test 1: purge ALL without negations ==> both should be removed
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("ALL"), false, false);
verify(sheep).remove();
verify(zombie).remove();
// test 2: purge ALL with one negation ==> the zombie should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("ALL"), false, true);
verify(sheep).remove();
verify(zombie, never()).remove();
// test 3: purge ALL with both negations ==> everybody should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("ALL"), true, true);
verify(sheep, never()).remove();
verify(zombie, never()).remove();
// test 4: purge ANIMALS without negations ==> the zombie should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("ANIMALS"), false, false);
verify(sheep).remove();
verify(zombie, never()).remove();
// test 5: purge MONSTERS with one negation ==> nobody should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("MONSTERS"), true, false);
verify(sheep).remove();
verify(zombie).remove();
// test 6: purge MONSTERS both negations ==> the zombie should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("MONSTERS"), true, true);
verify(sheep).remove();
verify(zombie, never()).remove();
// test 7: purge SHEEP without negations ==> the zombie should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("SHEEP"), false, false);
verify(sheep).remove();
verify(zombie, never()).remove();
// test 8: purge SHEEP with one negation ==> nobody should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("SHEEP"), false, true);
verify(sheep).remove();
verify(zombie).remove();
// test 9: purge ZOMBIE with both negations ==> the zombie should survive
createAnimals();
purger.purgeWorld(mvWorld, Arrays.asList("ZOMBIE"), true, true);
verify(sheep).remove();
verify(zombie, never()).remove();
// I like sheep.
}
private void createAnimals() {
sheep = mock(Sheep.class);
when(sheep.getType()).thenReturn(EntityType.SHEEP);
zombie = mock(Zombie.class);
when(zombie.getType()).thenReturn(EntityType.ZOMBIE);
when(cbworld.getEntities()).thenReturn(Arrays.asList((Entity) sheep, (Entity) zombie));
}
}