Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregisters worlds with MV on shutdown. #2150

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

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;
Expand Down Expand Up @@ -52,7 +55,7 @@
* Main BentoBox class
* @author tastybento, Poslovitch
*/
public class BentoBox extends JavaPlugin {
public class BentoBox extends JavaPlugin implements Listener {

private static BentoBox instance;

Expand Down Expand Up @@ -227,7 +230,7 @@ private void completeSetup(long loadTime) {
// 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());
Expand Down Expand Up @@ -300,6 +303,8 @@ private void registerListeners() {
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);
Expand All @@ -321,6 +326,15 @@ public void onDisable() {
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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public void registerWorld(World world, boolean islandWorld) {
}
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,48 @@ public IslandWorldManager(BentoBox plugin) {
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);
}
}

/**
Expand Down Expand Up @@ -169,27 +181,27 @@ public void addGameMode(@NonNull GameModeAddon gameMode) {
// 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
Expand Down Expand Up @@ -494,8 +506,8 @@ public int getMaxHomes(@NonNull World world) {
*/
public String getFriendlyName(@NonNull World world) {
return gameModes.containsKey(world) ?
gameModes.get(world).getWorldSettings().getFriendlyName() :
world.getName();
gameModes.get(world).getWorldSettings().getFriendlyName() :
world.getName();
}

/**
Expand Down Expand Up @@ -720,8 +732,8 @@ public Optional<GameModeAddon> getAddon(@Nullable World world) {
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();
}

/**
Expand All @@ -742,8 +754,8 @@ public List<String> getHiddenFlags(@NonNull World world) {
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) {
Expand Down Expand Up @@ -955,7 +967,7 @@ private Map<Flag, Integer> convertToFlags(Map<String, Integer> flagNamesMap)
{
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;
}
}