Saves world settings to config.yml when they are changed mid-game. (#615)

Addresses https://github.com/BentoBoxWorld/BentoBox/issues/614
This commit is contained in:
tastybento 2019-03-23 13:54:45 -07:00 committed by Florian CUNY
parent 2ba0000a8f
commit cab584d13d
6 changed files with 49 additions and 9 deletions

View File

@ -122,4 +122,11 @@ public abstract class GameModeAddon extends Addon {
*/
@NonNull
public abstract ChunkGenerator getDefaultWorldGenerator(String worldName, String id);
/**
* Tells the Game Mode Addon to save its settings. Used when world settings are changed
* in-game and need to be saved.
* @since 1.4.0
*/
public abstract void saveWorldSettings();
}

View File

@ -4,6 +4,7 @@ import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
@ -47,6 +48,8 @@ public class WorldToggleClick implements ClickHandler {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
// Apply change to panel
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user).getItem());
// Save world settings
plugin.getIWM().getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
});
return true;
}

View File

@ -14,6 +14,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
@ -66,6 +67,8 @@ public class GeoLimitClickListener implements ClickHandler {
}
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
// Save settings
iwm.getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
} else {
// Open the Sub Settings panel
openPanel(user, panelName);

View File

@ -15,6 +15,8 @@ import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -58,6 +60,8 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
}
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
// Save settings
BentoBox.getInstance().getIWM().getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
} else {
// Open the IV Settings panel
openPanel(user, ivPanelName);

View File

@ -15,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
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;
@ -22,6 +23,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -34,11 +36,16 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest({BentoBox.class, Util.class })
public class WorldToggleClickTest {
@Mock
private IslandWorldManager iwm;
private WorldToggleClick listener;
@Mock
private Panel panel;
@Mock
private User user;
private Flag flag;
@Mock
private GameModeAddon addon;
/**
* @throws java.lang.Exception
@ -50,27 +57,33 @@ public class WorldToggleClickTest {
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Island World Manager
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
Optional<GameModeAddon> optionalAddon = Optional.of(addon);
when(iwm.getAddon(Mockito.any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
listener = new WorldToggleClick("test");
panel = mock(Panel.class);
// Panel
when(panel.getInventory()).thenReturn(mock(Inventory.class));
// User
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.getWorld()).thenReturn(mock(World.class));
when(user.getLocation()).thenReturn(mock(Location.class));
when(user.getPlayer()).thenReturn(mock(Player.class));
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// Flags Manager
FlagsManager fm = mock(FlagsManager.class);
flag = mock(Flag.class);
when(flag.isSetForWorld(Mockito.any())).thenReturn(false);
PanelItem item = mock(PanelItem.class);
when(item.getItem()).thenReturn(mock(ItemStack.class));
when(flag.toPanelItem(Mockito.any(), Mockito.eq(user))).thenReturn(item);
@ -84,6 +97,7 @@ public class WorldToggleClickTest {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.wrong-world");
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
}
@Test
@ -91,6 +105,7 @@ public class WorldToggleClickTest {
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test");
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
}
@Test
@ -99,5 +114,6 @@ public class WorldToggleClickTest {
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(flag).setSetting(Mockito.any(), Mockito.eq(true));
Mockito.verify(panel).getInventory();
Mockito.verify(addon).saveWorldSettings();
}
}

View File

@ -30,6 +30,7 @@ import org.bukkit.inventory.meta.ItemMeta;
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;
@ -37,6 +38,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -51,14 +53,21 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
public class InvincibleVisitorsListenerTest {
@Mock
private IslandWorldManager iwm;
private InvincibleVisitorsListener listener;
@Mock
private Panel panel;
@Mock
private User user;
@Mock
private IslandsManager im;
private List<String> ivSettings;
@Mock
private Player player;
private Optional<Island> optionalIsland;
@Mock
private GameModeAddon addon;
/**
* @throws java.lang.Exception
@ -70,23 +79,21 @@ public class InvincibleVisitorsListenerTest {
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Island World Manager
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
Optional<GameModeAddon> optionalAddon = Optional.of(addon);
when(iwm.getAddon(Mockito.any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
listener = new InvincibleVisitorsListener();
panel = mock(Panel.class);
when(panel.getInventory()).thenReturn(mock(Inventory.class));
when(panel.getName()).thenReturn("panel");
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.inWorld()).thenReturn(true);
when(user.getWorld()).thenReturn(mock(World.class));
when(user.getLocation()).thenReturn(mock(Location.class));
player = mock(Player.class);
when(user.getPlayer()).thenReturn(player);
when(user.hasPermission(Mockito.anyString())).thenReturn(true);
when(user.getTranslation(Mockito.anyString())).thenReturn("panel");
@ -105,7 +112,6 @@ public class InvincibleVisitorsListenerTest {
when(plugin.getFlagsManager()).thenReturn(fm);
// Island Manager
im = mock(IslandsManager.class);
Island island = mock(Island.class);
when(island.getOwner()).thenReturn(uuid);
when(im.getIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(island);
@ -184,7 +190,8 @@ public class InvincibleVisitorsListenerTest {
// IV settings should not have the damage cause in it anymore
assertFalse(ivSettings.contains(dc.name()));
}
// The values should be saved twice because there are two clicks
Mockito.verify(addon, Mockito.times(DamageCause.values().length * 2)).saveWorldSettings();
}
@Test