API: Require getWorldSettings to be a game mode world

This commit is contained in:
tastybento 2021-10-03 20:33:50 -07:00
parent cfad9879a5
commit b5d9d2e52e
8 changed files with 18 additions and 22 deletions

View File

@ -1,7 +1,6 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.Objects;
import org.eclipse.jdt.annotation.Nullable;
@ -53,7 +52,7 @@ public class IslandSethomeCommand extends ConfirmableCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
String number = String.join(" ", args);
WorldSettings ws = Objects.requireNonNull(getIWM().getWorldSettings(user.getWorld()));
WorldSettings ws = getIWM().getWorldSettings(user.getWorld());
// Check if the player is in the Nether
if (getIWM().isNether(user.getWorld())) {
// Check if he is (not) allowed to set his home here

View File

@ -175,8 +175,10 @@ public class Flag implements Comparable<Flag> {
* If world is not a game world, then the result will always be false!
*/
public boolean isSetForWorld(World world) {
if (!BentoBox.getInstance().getIWM().inWorld(world)) {
return false;
}
WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world);
if (ws == null) return false;
if (type.equals(Type.WORLD_SETTING) || type.equals(Type.PROTECTION)) {
if (!ws.getWorldFlags().containsKey(getID())) {
ws.getWorldFlags().put(getID(), setting);
@ -234,12 +236,11 @@ public class Flag implements Comparable<Flag> {
* @param defaultSetting - true means it is allowed. false means it is not allowed
*/
public void setDefaultSetting(World world, boolean defaultSetting) {
WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world);
if (ws == null ) {
if (!BentoBox.getInstance().getIWM().inWorld(world)) {
BentoBox.getInstance().logError("Attempt to set default world setting for unregistered world. Register flags in onEnable.");
return;
}
ws.getWorldFlags().put(getID(), defaultSetting);
BentoBox.getInstance().getIWM().getWorldSettings(world).getWorldFlags().put(getID(), defaultSetting);
// Save config file
BentoBox.getInstance().getIWM().getAddon(world).ifPresent(GameModeAddon::saveWorldSettings);
}

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@ -199,11 +200,11 @@ public class IslandWorldManager {
* Get the settings for this world or sub-worlds (nether, end)
*
* @param world world
* @return world settings, or null if world is unknown
* @return world settings
*/
@Nullable
@NonNull
public WorldSettings getWorldSettings(@NonNull World world) {
return gameModes.containsKey(world) ? gameModes.get(world).getWorldSettings() : null;
return Objects.requireNonNull(gameModes.get(world), "Attempt to get WorldSettings for non-game world " + world.getName()).getWorldSettings();
}
/**

View File

@ -86,7 +86,7 @@ public class FlagTest {
GameModeAddon gma = mock(GameModeAddon.class);
Optional<GameModeAddon> opGma = Optional.of(gma );
when(iwm.getAddon(any())).thenReturn(opGma);
when(iwm.inWorld(any(World.class))).thenReturn(true);
worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
@ -223,7 +223,7 @@ public class FlagTest {
*/
@Test
public void testSetDefaultSettingWorldBooleanNullWorldSettings() {
when(iwm.getWorldSettings(any())).thenReturn(null);
when(iwm.inWorld(any(World.class))).thenReturn(false);
f.setDefaultSetting(world, true);
verify(plugin).logError("Attempt to set default world setting for unregistered world. Register flags in onEnable.");
}

View File

@ -104,10 +104,14 @@ public class MobSpawnListenerTest {
when(zombie.getLocation()).thenReturn(location);
when(slime.getLocation()).thenReturn(location);
when(cow.getLocation()).thenReturn(location);
when(zombie.getWorld()).thenReturn(world);
when(slime.getWorld()).thenReturn(world);
when(cow.getWorld()).thenReturn(world);
// Worlds
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Util class

View File

@ -211,14 +211,6 @@ public class IslandWorldManagerTest {
assertEquals(ws, iwm.getWorldSettings(world));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandWorldManager#getWorldSettings(org.bukkit.World)}.
*/
@Test
public void testGetWorldSettingsNull() {
assertNull(iwm.getWorldSettings(null));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandWorldManager#getOverWorld(java.lang.String)}.
*/

View File

@ -191,7 +191,6 @@ public class IslandsManagerTest {
// Player's manager
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getHomeLocations(any(), any())).thenReturn(Collections.emptyMap());
// Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);