Support worlds being deleted mid-game.

This commit is contained in:
Jesse Prescott 2018-06-03 22:38:44 +01:00
parent 699b551562
commit 899bb08a80
4 changed files with 79 additions and 0 deletions

View File

@ -50,6 +50,11 @@
<artifactId>jansi</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.onarandombox.multiversecore</groupId>
<artifactId>Multiverse-Core</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>

View File

@ -123,6 +123,23 @@ public class ConfigurationFile {
this.file = YamlConfiguration.loadConfiguration(inputStreamReader);
}
/**
* Delete this configuration file, non-jar only.
*
* @param plugin JavaPlugin.
*/
public void delete(JavaPlugin plugin) {
// Create file reference.
File fileReference = new File(plugin.getDataFolder(), this.name);
// Nullify reference.
this.file = null;
// Delete file.
fileReference.delete();
}
/**
* Attempt to copy the default configuration file from the given plugin's
* jar to the data folder.

View File

@ -17,16 +17,21 @@ package com.gmail.bleedobsidian.itemcase.configurations;
import com.gmail.bleedobsidian.itemcase.ConfigurationFile;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import com.gmail.bleedobsidian.itemcase.Itemcase;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.multiverse.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
/**
* A configuration file that holds all Itemcase saves for a specific world.
@ -161,4 +166,22 @@ public class WorldFile extends ConfigurationFile {
// Return list of loaded itemcases.
return itemcases;
}
/**
* Delete directory and config.
*
* @throws IOException
*/
public void deleteDirectory() throws IOException {
// Create file reference.
File fileReference = new File(ItemCaseCore.instance.getDataFolder(),
this.world.getName());
// Nullify reference.
this.file = null;
// Delete directory.
FileUtils.deleteDirectory(fileReference);
}
}

View File

@ -17,9 +17,13 @@ package com.gmail.bleedobsidian.itemcase.managers;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import com.gmail.bleedobsidian.itemcase.Itemcase;
import com.gmail.bleedobsidian.itemcase.configurations.WorldFile;
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -260,6 +264,36 @@ public final class ItemcaseManager {
// Request itemcases to be loaded.
ItemcaseManager.this.loadItemcases(world);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onWorldDeleteEvent(MVWorldDeleteEvent event) {
// Get world name.
String worldName = event.getWorld().getName();
// For every entry.
for(Entry<World, WorldFile> entry :
ItemcaseManager.this.worldFiles.entrySet()) {
// Check if world name matches.
if(entry.getKey().getName() == worldName) {
// Attempt to delete config.
try {
// Delete config.
entry.getValue().deleteDirectory();
} catch (IOException e) {
// Log error.
ItemCaseCore.instance.getConsoleLogger().severe(
"Failed to delete itemcase config for world:"
+ worldName, e);
}
}
}
}
}
}