Unload itemcases on plugin disable.

Unload and despawn itemcases and their items on plugin disable.
This commit is contained in:
Jesse Prescott 2018-05-29 14:39:01 +01:00
parent 1f1a675e43
commit 51d0057fdb
3 changed files with 33 additions and 6 deletions

View File

@ -81,6 +81,13 @@ public final class ItemCaseCore extends JavaPlugin {
new ItemcaseListener(), this); new ItemcaseListener(), this);
} }
@Override
public void onDisable() {
// Unload all itemcases.
this.itemcaseManager.unloadItemcases();
}
/** /**
* @return Main ItemCase configuration file. * @return Main ItemCase configuration file.

View File

@ -25,7 +25,6 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -69,7 +68,7 @@ public final class Itemcase {
/** /**
* This itemcase's task. * This itemcase's task.
*/ */
private final ItemcaseTask task; private ItemcaseTask task;
/** /**
* The active item that is currently on display. * The active item that is currently on display.
@ -108,10 +107,6 @@ public final class Itemcase {
// Set owner. // Set owner.
this.owner = owner; this.owner = owner;
// Schedule itemcase task to execute every 200 server ticks (10 secs).
this.task = new ItemcaseTask(this);
this.task.runTaskTimer(ItemCaseCore.instance, 0, 200);
// Spawn display item for the first time. // Spawn display item for the first time.
this.spawnItem(); this.spawnItem();
} }
@ -121,6 +116,10 @@ public final class Itemcase {
*/ */
public void spawnItem() { public void spawnItem() {
// Schedule itemcase task to execute every 200 server ticks (10 secs).
this.task = new ItemcaseTask(this);
this.task.runTaskTimer(ItemCaseCore.instance, 0, 200);
// Get the world that this itemcase is in. // Get the world that this itemcase is in.
World world = this.location.getWorld(); World world = this.location.getWorld();
@ -159,6 +158,11 @@ public final class Itemcase {
* Despawn the display item for this itemcase. * Despawn the display item for this itemcase.
*/ */
public void despawnItem() { public void despawnItem() {
// Cancel running task.
this.task.cancel();
// Remove current display item from world.
this.displayItem.remove(); this.displayItem.remove();
} }

View File

@ -113,6 +113,22 @@ public final class ItemcaseManager {
} }
} }
/**
* Unload all currently loaded Itemcases.
*/
public void unloadItemcases() {
// For every loaded itemcase.
for(Itemcase itemcase : this.itemcases) {
// Despawn the item.
itemcase.despawnItem();
}
// Clear list.
this.itemcases.clear();
}
/** /**
* @return A list of all active Itemcase instances. * @return A list of all active Itemcase instances.
*/ */