Merge pull request #2150 from BentoBoxWorld/mv_remove_on_disable

Unregisters worlds with MV on shutdown.
This commit is contained in:
tastybento 2023-06-28 21:03:09 -07:00 committed by GitHub
commit cbed9ac831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 36 deletions

View File

@ -5,6 +5,9 @@ import java.util.Optional;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@ -52,7 +55,7 @@ import world.bentobox.bentobox.versions.ServerCompatibility;
* Main BentoBox class
* @author tastybento, Poslovitch
*/
public class BentoBox extends JavaPlugin {
public class BentoBox extends JavaPlugin implements Listener {
private static BentoBox instance;
@ -227,7 +230,7 @@ public class BentoBox extends JavaPlugin {
// Make sure all worlds are already registered to Multiverse.
hooksManager.registerHook(new MultiverseCoreHook());
hooksManager.registerHook(new MyWorldsHook());
islandWorldManager.registerWorldsToMultiverse();
islandWorldManager.registerWorldsToMultiverse(true);
// TODO: re-enable after implementation
//hooksManager.registerHook(new DynmapHook());
@ -300,6 +303,8 @@ public class BentoBox extends JavaPlugin {
manager.registerEvents(new BannedCommands(this), this);
// Death counter
manager.registerEvents(new DeathListener(this), this);
// MV unregister
manager.registerEvents(this, this);
// Island Delete Manager
islandChunkDeletionManager = new IslandChunkDeletionManager(this);
islandDeletionManager = new IslandDeletionManager(this);
@ -321,6 +326,15 @@ public class BentoBox extends JavaPlugin {
if (islandsManager != null) {
islandsManager.shutdown();
}
}
@EventHandler
public void onServerStop(ServerCommandEvent e) {
if (islandWorldManager != null && (e.getCommand().equalsIgnoreCase("stop") || e.getCommand().equalsIgnoreCase("restart"))) {
// Unregister any MV worlds if () {
islandWorldManager.registerWorldsToMultiverse(false);
}
}
/**

View File

@ -49,6 +49,12 @@ public class MultiverseCoreHook extends Hook implements WorldManagementHook {
}
}
@Override
public void unregisterWorld(World world) {
String cmd = "mv remove " + world.getName();
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd);
}
@Override
public boolean hook() {
return true; // The hook process shouldn't fail

View File

@ -13,8 +13,17 @@ public interface WorldManagementHook {
/**
* Register the world with the World Management hook
*
*
* @param world - world to register
* @param islandWorld - if true, then this is an island world
*/
void registerWorld(World world, boolean islandWorld);
/**
* Unregisters a world.
* @param world - world to unregister
*/
default void unregisterWorld(World world) {
// Do nothing
}
}

View File

@ -50,36 +50,48 @@ public class IslandWorldManager {
gameModes = new HashMap<>();
}
public void registerWorldsToMultiverse() {
/**
* Registers or unregisters worlds with world management plugins
*
* @param reg true to register, false to remove registration
*
* Updated 1.24.0
*/
public void registerWorldsToMultiverse(boolean reg) {
gameModes.values().stream().distinct().forEach(gm -> {
registerToWorldManagementPlugins(gm.getOverWorld(), true);
registerToWorldManagementPlugins(gm.getOverWorld(), true, reg);
if (gm.getWorldSettings().isNetherGenerate()) {
registerToWorldManagementPlugins(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands());
registerToWorldManagementPlugins(gm.getNetherWorld(), gm.getWorldSettings().isNetherIslands(), reg);
}
if (gm.getWorldSettings().isEndGenerate()) {
registerToWorldManagementPlugins(gm.getEndWorld(), gm.getWorldSettings().isEndIslands());
registerToWorldManagementPlugins(gm.getEndWorld(), gm.getWorldSettings().isEndIslands(), reg);
}
});
}
/**
* Registers a world with world management plugins
*
* @param world the World to register
* @param islandWorld true if this is an island world
*/
private void registerToWorldManagementPlugins(@NonNull World world, boolean islandWorld) {
if (plugin.getHooks() != null) {
for (Hook hook : plugin.getHooks().getHooks()) {
if (hook instanceof final WorldManagementHook worldManagementHook) {
if (Bukkit.isPrimaryThread()) {
worldManagementHook.registerWorld(world, islandWorld);
} else {
Bukkit.getScheduler().runTask(plugin, () -> worldManagementHook.registerWorld(world, islandWorld));
}
private void registerToWorldManagementPlugins(@NonNull World world, boolean islandWorld, boolean reg) {
if (plugin.getHooks() == null) {
return;
}
for (Hook hook : plugin.getHooks().getHooks()) {
if (hook instanceof final WorldManagementHook worldManagementHook) {
if (Bukkit.isPrimaryThread()) {
runTask(worldManagementHook, world, islandWorld, reg);
} else {
Bukkit.getScheduler().runTask(plugin, () -> runTask(worldManagementHook, world, islandWorld, reg));
}
}
}
}
private void runTask(WorldManagementHook worldManagementHook, @NonNull World world, boolean islandWorld, boolean reg) {
if (reg) {
worldManagementHook.registerWorld(world, islandWorld);
} else {
worldManagementHook.unregisterWorld(world);
}
}
/**
@ -169,27 +181,27 @@ public class IslandWorldManager {
// Add worlds to map
gameModes.put(world, gameMode);
// Call Multiverse
registerToWorldManagementPlugins(world, true);
registerToWorldManagementPlugins(world, true, true);
if (settings.isNetherGenerate()) {
gameModes.put(gameMode.getNetherWorld(), gameMode);
if (settings.isNetherIslands()) {
registerToWorldManagementPlugins(gameMode.getNetherWorld(), true);
registerToWorldManagementPlugins(gameMode.getNetherWorld(), true, true);
}
}
if (settings.isEndGenerate()) {
gameModes.put(gameMode.getEndWorld(), gameMode);
if (settings.isEndIslands()) {
registerToWorldManagementPlugins(gameMode.getEndWorld(), true);
registerToWorldManagementPlugins(gameMode.getEndWorld(), true, true);
}
}
// Set default island settings
plugin.getFlagsManager().getFlags().stream().
filter(f -> f.getType().equals(Flag.Type.PROTECTION)).
forEach(f -> settings.getDefaultIslandFlagNames().putIfAbsent(f.getID(), f.getDefaultRank()));
filter(f -> f.getType().equals(Flag.Type.PROTECTION)).
forEach(f -> settings.getDefaultIslandFlagNames().putIfAbsent(f.getID(), f.getDefaultRank()));
plugin.getFlagsManager().getFlags().stream().
filter(f -> f.getType().equals(Flag.Type.SETTING)).
forEach(f -> settings.getDefaultIslandSettingNames().putIfAbsent(f.getID(), f.getDefaultRank()));
filter(f -> f.getType().equals(Flag.Type.SETTING)).
forEach(f -> settings.getDefaultIslandSettingNames().putIfAbsent(f.getID(), f.getDefaultRank()));
Bukkit.getScheduler().runTask(plugin, () -> {
// Set world difficulty
@ -494,8 +506,8 @@ public class IslandWorldManager {
*/
public String getFriendlyName(@NonNull World world) {
return gameModes.containsKey(world) ?
gameModes.get(world).getWorldSettings().getFriendlyName() :
world.getName();
gameModes.get(world).getWorldSettings().getFriendlyName() :
world.getName();
}
/**
@ -720,8 +732,8 @@ public class IslandWorldManager {
public Map<Flag, Integer> getDefaultIslandFlags(@NonNull World world)
{
return this.gameModes.containsKey(world) ?
this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandFlagNames()) :
Collections.emptyMap();
this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandFlagNames()) :
Collections.emptyMap();
}
/**
@ -742,8 +754,8 @@ public class IslandWorldManager {
public Map<Flag, Integer> getDefaultIslandSettings(@NonNull World world)
{
return this.gameModes.containsKey(world) ?
this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandSettingNames()) :
Collections.emptyMap();
this.convertToFlags(this.gameModes.get(world).getWorldSettings().getDefaultIslandSettingNames()) :
Collections.emptyMap();
}
public boolean isUseOwnGenerator(@NonNull World world) {
@ -955,7 +967,7 @@ public class IslandWorldManager {
{
Map<Flag, Integer> flagMap = new HashMap<>();
flagNamesMap.forEach((key, value) ->
this.plugin.getFlagsManager().getFlag(key).ifPresent(flag -> flagMap.put(flag, value)));
this.plugin.getFlagsManager().getFlag(key).ifPresent(flag -> flagMap.put(flag, value)));
return flagMap;
}
}

View File

@ -114,7 +114,7 @@ public class IslandWorldManagerTest {
*/
@Test
public void testRegisterWorldsToMultiverse() {
iwm.registerWorldsToMultiverse();
iwm.registerWorldsToMultiverse(true);
}
/**