Fix ItemCase only working in standard minecraft original worlds.

A long standing bug with ItemCase not supporting custom multiverse worlds.
This commit is contained in:
Jesse Prescott 2018-05-31 00:34:32 +01:00
parent 668d02e307
commit 9482eac6a4
3 changed files with 64 additions and 43 deletions

View File

@ -47,7 +47,7 @@ public final class ItemCaseCore extends JavaPlugin {
/**
* ItemcaseManager.
*/
private ItemcaseManager itemcaseManager;
private final ItemcaseManager itemcaseManager = new ItemcaseManager();
@Override
public void onEnable() {
@ -77,7 +77,7 @@ public final class ItemCaseCore extends JavaPlugin {
this.consoleLogger.info("Successfully loaded main configuration file.");
// Initialize ItemcaseManager.
this.itemcaseManager = new ItemcaseManager();
this.itemcaseManager.registerListener();
// Register ItemcaseListener.
this.getServer().getPluginManager().registerEvents(

View File

@ -323,7 +323,7 @@ public final class Itemcase {
* is particularly useful when servers use anti-lag plugins that forcibly
* kill entities or a player has somehow caused an item to move.
*/
public static final class ItemcaseTask extends BukkitRunnable {
private final class ItemcaseTask extends BukkitRunnable {
/**
* The itemcase that this task is for.

View File

@ -24,6 +24,10 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.inventory.ItemStack;
/**
@ -44,46 +48,6 @@ public final class ItemcaseManager {
*/
private final ArrayList<Itemcase> itemcases = new ArrayList<>();
/**
* Constructor.
*/
public ItemcaseManager() {
// For every world.
for(World world : Bukkit.getWorlds()) {
// Create WorldFile object.
WorldFile file = new WorldFile(world);
// Add to hashmap.
this.worldFiles.put(world, file);
// Attempt to load itemcases and add them to list.
try {
// Load itemcaes.
ArrayList<Itemcase> loadedItemcases = file.loadItemcases();
// Add to list.
this.itemcases.addAll(loadedItemcases);
} catch (IOException e) {
// Log error.
ItemCaseCore.instance.getConsoleLogger().severe(
"Failed to load itemcases for world: " +
world.getName(), e);
// Exit.
return;
}
// Log.
ItemCaseCore.instance.getConsoleLogger().info(
"Loaded itemcases for world: " + world.getName());
}
}
/**
* Create a new Itemcase.
*
@ -133,10 +97,67 @@ public final class ItemcaseManager {
this.itemcases.clear();
}
/**
* Register the event listener for this class.
*/
public void registerListener() {
// Register listener with bukkit.
Bukkit.getPluginManager().registerEvents(
new ItemcaseManagerListener(), ItemCaseCore.instance);
}
/**
* @return A list of all active Itemcase instances.
*/
public ArrayList<Itemcase> getItemcases() {
// List of Itemcases.
return this.itemcases;
}
/**
* A bukkit listener for the ItemcaseManager. Used to load Itemcases upon
* world loading.
*/
private final class ItemcaseManagerListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onWorldLoadEvent(WorldLoadEvent event) {
// Get world.
World world = event.getWorld();
// Create WorldFile object.
WorldFile file = new WorldFile(world);
// Add to hashmap.
ItemcaseManager.this.worldFiles.put(world, file);
// Attempt to load itemcases and add them to list.
try {
// Load itemcaes.
ArrayList<Itemcase> loadedItemcases = file.loadItemcases();
// Add to list.
ItemcaseManager.this.itemcases.addAll(loadedItemcases);
} catch (IOException e) {
// Log error.
ItemCaseCore.instance.getConsoleLogger().severe(
"Failed to load itemcases for world: " +
world.getName(), e);
// Exit.
return;
}
// Log.
ItemCaseCore.instance.getConsoleLogger().info(
"Loaded itemcases for world: " + world.getName());
}
}
}