Added ability to reset one island flag to default for all islands

https://github.com/BentoBoxWorld/BentoBox/issues/958
This commit is contained in:
tastybento 2019-09-29 14:35:15 -07:00
parent f8c4ea568f
commit 8a1d969103
5 changed files with 154 additions and 53 deletions

View File

@ -1,10 +1,17 @@
package world.bentobox.bentobox.api.commands.admin;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
* Admin command to reset all islands in a world to the default flag setting in the game mode config.yml
@ -13,25 +20,47 @@ import world.bentobox.bentobox.api.user.User;
*/
public class AdminResetFlagsCommand extends ConfirmableCommand {
private List<String> options;
public AdminResetFlagsCommand(CompositeCommand parent) {
super(parent, "resetflags");
options = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.PROTECTION) || f.getType().equals(Type.SETTING))
.map(Flag::getID).collect(Collectors.toList());
}
@Override
public void setup() {
setPermission("admin.resetflags");
setOnlyPlayer(false);
setParametersHelp("commands.admin.resetflags.parameters");
setDescription("commands.admin.resetflags.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Everything's fine, we can set the island as spawn :)
askConfirmation(user, () -> {
getIslands().resetAllFlags(getWorld());
user.sendMessage("commands.admin.resetflags.success");
});
return true;
if (args.isEmpty()) {
askConfirmation(user, user.getTranslation("commands.admin.resetflags.confirm"), () -> {
getIslands().resetAllFlags(getWorld());
user.sendMessage("commands.admin.resetflags.success");
});
return true;
} else if (args.size() == 1 && options.contains(args.get(0).toUpperCase(Locale.ENGLISH))) {
getPlugin().getFlagsManager().getFlag(args.get(0).toUpperCase(Locale.ENGLISH)).ifPresent(flag ->
askConfirmation(user, user.getTranslation("commands.admin.resetflags.confirm"), () -> {
getIslands().resetFlag(getWorld(), flag);
user.sendMessage("commands.admin.resetflags.success-one", TextVariables.NAME, flag.getID());
}));
return true;
}
// Show help
showHelp(this, user);
return false;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -38,6 +38,7 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.user.User;
@ -1211,6 +1212,17 @@ public class IslandsManager {
this.saveAll();
}
/**
* Resets a flag to gamemode config.yml default
* @param world - world
* @param flag - flag to reset
* @since 1.8.0
*/
public void resetFlag(World world, Flag flag) {
islandCache.resetFlag(world, flag);
this.saveAll();
}
/**
* Returns whether the specified island custom name exists in this world.
* @param world World of the gamemode
@ -1222,4 +1234,5 @@ public class IslandsManager {
return getIslands(world).stream().filter(island -> island.getName() != null).map(Island::getName)
.anyMatch(n -> ChatColor.stripColor(n).equals(ChatColor.stripColor(name)));
}
}

View File

@ -16,6 +16,8 @@ import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
@ -312,4 +314,16 @@ public class IslandCache {
World w = Util.getWorld(world);
islandsById.values().stream().filter(i -> i.getWorld().equals(w)).forEach(Island::setFlagsDefaults);
}
/**
* Resets a specific flag on all game mode islands in world to default setting
* @param world - world
* @param flag - flag to reset
* @since 1.8.0
*/
public void resetFlag(World world, Flag flag) {
World w = Util.getWorld(world);
int setting = BentoBox.getInstance().getIWM().getDefaultIslandFlags(w).getOrDefault(flag, flag.getDefaultRank());
islandsById.values().stream().filter(i -> i.getWorld().equals(w)).forEach(i -> i.setFlag(flag, setting));
}
}

View File

@ -310,8 +310,11 @@ commands:
&aLeft click to increment
&aRight click to decrement
resetflags:
parameters: "[flag]"
description: "Reset all islands to default flag settings in config.yml"
confirm: "&4This will reset the flag(s) to default for all islands!"
success: "&aSuccessfully reset all islands' flags to the default settings."
success-one: "&a[name] flag set to default for all islands."
world:
description: "Manage world settings"
delete:

View File

@ -2,14 +2,16 @@ package world.bentobox.bentobox.managers.island;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
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 static org.mockito.Mockito.verify;
import java.util.Collections;
import java.util.UUID;
import org.bukkit.Location;
@ -17,15 +19,18 @@ import org.bukkit.World;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
@ -33,25 +38,37 @@ import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Util.class)
@PrepareForTest({BentoBox.class, Util.class})
public class IslandCacheTest {
@Mock
private BentoBox plugin;
private static World world;
@Mock
private World world;
@Mock
private Island island;
// UUID
private UUID owner = UUID.randomUUID();
@Mock
private Location location;
// Test class
private IslandCache ic;
@Mock
private IslandWorldManager iwm;
@Mock
private Flag flag;
@Mock
private IslandsManager im;
@Before
public void setUp() throws Exception {
plugin = mock(BentoBox.class);
world = mock(World.class);
// Plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// IWM
when(iwm.getDefaultIslandFlags(any())).thenReturn(Collections.singletonMap(flag, 400));
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
@ -59,14 +76,11 @@ public class IslandCacheTest {
when(Util.getWorld(Mockito.any())).thenReturn(world);
// Mock up IslandsManager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
// Island
when(island.getWorld()).thenReturn(world);
location = mock(Location.class);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
@ -82,25 +96,26 @@ public class IslandCacheTest {
when(island.getMinX()).thenReturn(-200);
when(island.getMinZ()).thenReturn(-200);
// New cache
ic = new IslandCache();
}
@Test
public void testIslandCache() {
assertNotNull(new IslandCache());
}
/**
* Test for {@link IslandCache#addIsland(Island)}
*/
@Test
public void testAddIsland() {
IslandCache ic = new IslandCache();
assertTrue(ic.addIsland(island));
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
}
/**
* Test for {@link IslandCache#addPlayer(UUID, Island)}
*/
@Test
public void testAddPlayer() {
IslandCache ic = new IslandCache();
UUID playerUUID = UUID.randomUUID();
ic.addPlayer(playerUUID, island);
// Check if they are added
@ -109,9 +124,11 @@ public class IslandCacheTest {
}
/**
* Test for {@link IslandCache#clear()}
*/
@Test
public void testClear() {
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
@ -121,10 +138,11 @@ public class IslandCacheTest {
assertNull(ic.get(location));
}
/**
* Test for {@link IslandCache#deleteIslandFromCache(Island)}
*/
@Test
public void testDeleteIslandFromCache() {
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
@ -156,31 +174,35 @@ public class IslandCacheTest {
}
/**
* Test for {@link IslandCache#get(Location)}
*/
@Test
public void testGetLocation() {
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(location));
}
/**
* Test for {@link IslandCache#get(World, UUID)}
*/
@Test
public void testGetUUID() {
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
}
/**
* Test for {@link IslandCache#getIslandAt(Location)}
*/
@Test
public void testGetIslandAtLocation() {
// Set coords to be in island space
when(island.inIslandSpace(Mockito.any(Integer.class), Mockito.any(Integer.class))).thenReturn(true);
// Set plugin
Util.setPlugin(plugin);
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check exact match for location
@ -199,16 +221,11 @@ public class IslandCacheTest {
assertNull(ic.getIslandAt(location2));
}
/*
@Test
public void testGetIslands() {
fail("Not yet implemented"); // TODO
}
/**
* Test for {@link IslandCache#getMembers(World, UUID, int)}
*/
@Test
public void testGetMembers() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.getMembers(world, null, RanksManager.MEMBER_RANK).isEmpty());
@ -217,10 +234,12 @@ public class IslandCacheTest {
assertEquals(3, ic.getMembers(world, island.getOwner(), RanksManager.MEMBER_RANK).size());
}
/**
* Test for {@link IslandCache#getOwner(World, UUID)}
*/
@Test
public void testGetOwner() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertEquals(owner, ic.getOwner(world, owner));
@ -228,10 +247,11 @@ public class IslandCacheTest {
assertNull(ic.getOwner(world, UUID.randomUUID()));
}
/**
* Test for {@link IslandCache#hasIsland(World, UUID)}
*/
@Test
public void testHasIsland() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.hasIsland(world, owner));
@ -239,12 +259,12 @@ public class IslandCacheTest {
assertFalse(ic.hasIsland(world, null));
}
/**
* Test for {@link IslandCache#removePlayer(World, UUID)}
*/
@Test
public void testRemovePlayer() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.hasIsland(world, owner));
ic.removePlayer(world, null);
assertTrue(ic.hasIsland(world, owner));
@ -254,18 +274,20 @@ public class IslandCacheTest {
assertFalse(ic.hasIsland(world, owner));
}
/**
* Test for {@link IslandCache#size()}
*/
@Test
public void testSize() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertEquals(1, ic.size());
}
/**
* Test for {@link IslandCache#setOwner(Island, UUID)}
*/
@Test
public void testSetOwner() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
UUID newOwnerUUID = UUID.randomUUID();
ic.setOwner(island, newOwnerUUID);
@ -274,4 +296,24 @@ public class IslandCacheTest {
assertEquals(island, ic.get(world, newOwnerUUID));
assertEquals(island, ic.get(island.getCenter()));
}
/**
* Test for {@link IslandCache#resetFlag(World, world.bentobox.bentobox.api.flags.Flag)}
*/
@Test
public void testResetFlag() {
ic.addIsland(island);
ic.resetFlag(world, flag);
verify(island).setFlag(eq(flag), eq(400));
}
/**
* Test for {@link IslandCache#resetAllFlags(World)}
*/
@Test
public void testResetAllFlags() {
ic.addIsland(island);
ic.resetAllFlags(world);
verify(island).setFlagsDefaults();
}
}