mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-27 21:27:44 +01:00
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:
parent
2ba0000a8f
commit
cab584d13d
@ -122,4 +122,11 @@ public abstract class GameModeAddon extends Addon {
|
|||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public abstract ChunkGenerator getDefaultWorldGenerator(String worldName, String id);
|
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();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import org.bukkit.Sound;
|
|||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
|
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);
|
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
|
||||||
// Apply change to panel
|
// Apply change to panel
|
||||||
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user).getItem());
|
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user).getItem());
|
||||||
|
// Save world settings
|
||||||
|
plugin.getIWM().getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import org.bukkit.entity.EntityType;
|
|||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
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.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
|
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
|
||||||
@ -66,6 +67,8 @@ public class GeoLimitClickListener implements ClickHandler {
|
|||||||
}
|
}
|
||||||
// Apply change to panel
|
// Apply change to panel
|
||||||
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
|
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
|
||||||
|
// Save settings
|
||||||
|
iwm.getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
|
||||||
} else {
|
} else {
|
||||||
// Open the Sub Settings panel
|
// Open the Sub Settings panel
|
||||||
openPanel(user, panelName);
|
openPanel(user, panelName);
|
||||||
|
@ -15,6 +15,8 @@ import org.bukkit.event.entity.EntityDamageEvent;
|
|||||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
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.flags.FlagListener;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -58,6 +60,8 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
}
|
}
|
||||||
// Apply change to panel
|
// Apply change to panel
|
||||||
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
|
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
|
||||||
|
// Save settings
|
||||||
|
BentoBox.getInstance().getIWM().getAddon(Util.getWorld(user.getWorld())).ifPresent(GameModeAddon::saveWorldSettings);
|
||||||
} else {
|
} else {
|
||||||
// Open the IV Settings panel
|
// Open the IV Settings panel
|
||||||
openPanel(user, ivPanelName);
|
openPanel(user, ivPanelName);
|
||||||
|
@ -15,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
@ -22,6 +23,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||||||
import org.powermock.reflect.Whitebox;
|
import org.powermock.reflect.Whitebox;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
import world.bentobox.bentobox.api.flags.Flag;
|
import world.bentobox.bentobox.api.flags.Flag;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -34,11 +36,16 @@ import world.bentobox.bentobox.util.Util;
|
|||||||
@PrepareForTest({BentoBox.class, Util.class })
|
@PrepareForTest({BentoBox.class, Util.class })
|
||||||
public class WorldToggleClickTest {
|
public class WorldToggleClickTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
private IslandWorldManager iwm;
|
private IslandWorldManager iwm;
|
||||||
private WorldToggleClick listener;
|
private WorldToggleClick listener;
|
||||||
|
@Mock
|
||||||
private Panel panel;
|
private Panel panel;
|
||||||
|
@Mock
|
||||||
private User user;
|
private User user;
|
||||||
private Flag flag;
|
private Flag flag;
|
||||||
|
@Mock
|
||||||
|
private GameModeAddon addon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws java.lang.Exception
|
* @throws java.lang.Exception
|
||||||
@ -50,27 +57,33 @@ public class WorldToggleClickTest {
|
|||||||
BentoBox plugin = mock(BentoBox.class);
|
BentoBox plugin = mock(BentoBox.class);
|
||||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||||
// Island World Manager
|
// Island World Manager
|
||||||
iwm = mock(IslandWorldManager.class);
|
|
||||||
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
||||||
when(iwm.inWorld(any(Location.class))).thenReturn(true);
|
when(iwm.inWorld(any(Location.class))).thenReturn(true);
|
||||||
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
|
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);
|
when(plugin.getIWM()).thenReturn(iwm);
|
||||||
|
|
||||||
listener = new WorldToggleClick("test");
|
listener = new WorldToggleClick("test");
|
||||||
|
|
||||||
panel = mock(Panel.class);
|
// Panel
|
||||||
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
||||||
|
|
||||||
|
// User
|
||||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||||
user = mock(User.class);
|
|
||||||
when(user.getWorld()).thenReturn(mock(World.class));
|
when(user.getWorld()).thenReturn(mock(World.class));
|
||||||
when(user.getLocation()).thenReturn(mock(Location.class));
|
when(user.getLocation()).thenReturn(mock(Location.class));
|
||||||
when(user.getPlayer()).thenReturn(mock(Player.class));
|
when(user.getPlayer()).thenReturn(mock(Player.class));
|
||||||
|
|
||||||
|
// Util
|
||||||
PowerMockito.mockStatic(Util.class);
|
PowerMockito.mockStatic(Util.class);
|
||||||
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
|
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
|
||||||
|
|
||||||
|
// Flags Manager
|
||||||
FlagsManager fm = mock(FlagsManager.class);
|
FlagsManager fm = mock(FlagsManager.class);
|
||||||
flag = mock(Flag.class);
|
flag = mock(Flag.class);
|
||||||
when(flag.isSetForWorld(Mockito.any())).thenReturn(false);
|
when(flag.isSetForWorld(Mockito.any())).thenReturn(false);
|
||||||
|
|
||||||
PanelItem item = mock(PanelItem.class);
|
PanelItem item = mock(PanelItem.class);
|
||||||
when(item.getItem()).thenReturn(mock(ItemStack.class));
|
when(item.getItem()).thenReturn(mock(ItemStack.class));
|
||||||
when(flag.toPanelItem(Mockito.any(), Mockito.eq(user))).thenReturn(item);
|
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);
|
when(iwm.inWorld(any(Location.class))).thenReturn(false);
|
||||||
listener.onClick(panel, user, ClickType.LEFT, 0);
|
listener.onClick(panel, user, ClickType.LEFT, 0);
|
||||||
Mockito.verify(user).sendMessage("general.errors.wrong-world");
|
Mockito.verify(user).sendMessage("general.errors.wrong-world");
|
||||||
|
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -91,6 +105,7 @@ public class WorldToggleClickTest {
|
|||||||
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
|
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
|
||||||
listener.onClick(panel, user, ClickType.LEFT, 0);
|
listener.onClick(panel, user, ClickType.LEFT, 0);
|
||||||
Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test");
|
Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test");
|
||||||
|
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -99,5 +114,6 @@ public class WorldToggleClickTest {
|
|||||||
listener.onClick(panel, user, ClickType.LEFT, 0);
|
listener.onClick(panel, user, ClickType.LEFT, 0);
|
||||||
Mockito.verify(flag).setSetting(Mockito.any(), Mockito.eq(true));
|
Mockito.verify(flag).setSetting(Mockito.any(), Mockito.eq(true));
|
||||||
Mockito.verify(panel).getInventory();
|
Mockito.verify(panel).getInventory();
|
||||||
|
Mockito.verify(addon).saveWorldSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import org.bukkit.inventory.meta.ItemMeta;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
@ -37,6 +38,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||||||
import org.powermock.reflect.Whitebox;
|
import org.powermock.reflect.Whitebox;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
import world.bentobox.bentobox.api.flags.Flag;
|
import world.bentobox.bentobox.api.flags.Flag;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -51,14 +53,21 @@ import world.bentobox.bentobox.util.Util;
|
|||||||
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
|
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
|
||||||
public class InvincibleVisitorsListenerTest {
|
public class InvincibleVisitorsListenerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
private IslandWorldManager iwm;
|
private IslandWorldManager iwm;
|
||||||
private InvincibleVisitorsListener listener;
|
private InvincibleVisitorsListener listener;
|
||||||
|
@Mock
|
||||||
private Panel panel;
|
private Panel panel;
|
||||||
|
@Mock
|
||||||
private User user;
|
private User user;
|
||||||
|
@Mock
|
||||||
private IslandsManager im;
|
private IslandsManager im;
|
||||||
private List<String> ivSettings;
|
private List<String> ivSettings;
|
||||||
|
@Mock
|
||||||
private Player player;
|
private Player player;
|
||||||
private Optional<Island> optionalIsland;
|
private Optional<Island> optionalIsland;
|
||||||
|
@Mock
|
||||||
|
private GameModeAddon addon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws java.lang.Exception
|
* @throws java.lang.Exception
|
||||||
@ -70,23 +79,21 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
BentoBox plugin = mock(BentoBox.class);
|
BentoBox plugin = mock(BentoBox.class);
|
||||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||||
// Island World Manager
|
// Island World Manager
|
||||||
iwm = mock(IslandWorldManager.class);
|
|
||||||
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
when(iwm.inWorld(any(World.class))).thenReturn(true);
|
||||||
when(iwm.inWorld(any(Location.class))).thenReturn(true);
|
when(iwm.inWorld(any(Location.class))).thenReturn(true);
|
||||||
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
|
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);
|
when(plugin.getIWM()).thenReturn(iwm);
|
||||||
|
|
||||||
listener = new InvincibleVisitorsListener();
|
listener = new InvincibleVisitorsListener();
|
||||||
|
|
||||||
panel = mock(Panel.class);
|
|
||||||
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
when(panel.getInventory()).thenReturn(mock(Inventory.class));
|
||||||
when(panel.getName()).thenReturn("panel");
|
when(panel.getName()).thenReturn("panel");
|
||||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||||
user = mock(User.class);
|
|
||||||
when(user.inWorld()).thenReturn(true);
|
when(user.inWorld()).thenReturn(true);
|
||||||
when(user.getWorld()).thenReturn(mock(World.class));
|
when(user.getWorld()).thenReturn(mock(World.class));
|
||||||
when(user.getLocation()).thenReturn(mock(Location.class));
|
when(user.getLocation()).thenReturn(mock(Location.class));
|
||||||
player = mock(Player.class);
|
|
||||||
when(user.getPlayer()).thenReturn(player);
|
when(user.getPlayer()).thenReturn(player);
|
||||||
when(user.hasPermission(Mockito.anyString())).thenReturn(true);
|
when(user.hasPermission(Mockito.anyString())).thenReturn(true);
|
||||||
when(user.getTranslation(Mockito.anyString())).thenReturn("panel");
|
when(user.getTranslation(Mockito.anyString())).thenReturn("panel");
|
||||||
@ -105,7 +112,6 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
when(plugin.getFlagsManager()).thenReturn(fm);
|
when(plugin.getFlagsManager()).thenReturn(fm);
|
||||||
|
|
||||||
// Island Manager
|
// Island Manager
|
||||||
im = mock(IslandsManager.class);
|
|
||||||
Island island = mock(Island.class);
|
Island island = mock(Island.class);
|
||||||
when(island.getOwner()).thenReturn(uuid);
|
when(island.getOwner()).thenReturn(uuid);
|
||||||
when(im.getIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(island);
|
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
|
// IV settings should not have the damage cause in it anymore
|
||||||
assertFalse(ivSettings.contains(dc.name()));
|
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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user