Search in sources :

Example 1 with InventoryManager

use of combatlogx.expansion.compatibility.citizens.manager.InventoryManager in project CombatLogX by SirBlobman.

the class ListenerDeath method onDespawnNPC.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDespawnNPC(NPCDespawnEvent e) {
    printDebug("Detected NPCDespawnEvent...");
    DespawnReason despawnReason = e.getReason();
    if (despawnReason == DespawnReason.PENDING_RESPAWN) {
        printDebug("Despawn reason is PENDING_RESPAWN, ignoring event.");
        return;
    }
    NPC npc = e.getNPC();
    CombatNpcManager combatNpcManager = this.expansion.getCombatNpcManager();
    CombatNPC combatNPC = combatNpcManager.getCombatNPC(npc);
    if (combatNPC == null) {
        printDebug("NPC was not combat NPC, ignoring event.");
        return;
    }
    OfflinePlayer offlinePlayer = combatNPC.getOfflineOwner();
    if (despawnReason == DespawnReason.DEATH) {
        Location location = combatNpcManager.getLocation(npc);
        if (location != null) {
            printDebug("Despawn reason was death, drop NPC inventory.");
            InventoryManager inventoryManager = this.expansion.getInventoryManager();
            inventoryManager.dropInventory(offlinePlayer, location);
        }
    }
    if (despawnReason != DespawnReason.REMOVAL) {
        printDebug("Despawn reason was not removal, destroying NPC.");
        combatNpcManager.remove(combatNPC);
        printDebug("Destroy NPC later.");
        JavaPlugin plugin = this.expansion.getPlugin().getPlugin();
        BukkitScheduler scheduler = Bukkit.getScheduler();
        scheduler.runTaskLater(plugin, npc::destroy, 1L);
    }
    printDebug("Setting player to be punished when they next join.");
    YamlConfiguration data = combatNpcManager.getData(offlinePlayer);
    data.set("citizens-compatibility.punish-next-join", true);
    combatNpcManager.saveData(offlinePlayer);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) CombatNPC(combatlogx.expansion.compatibility.citizens.object.CombatNPC) InventoryManager(combatlogx.expansion.compatibility.citizens.manager.InventoryManager) CombatNPC(combatlogx.expansion.compatibility.citizens.object.CombatNPC) OfflinePlayer(org.bukkit.OfflinePlayer) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) DespawnReason(net.citizensnpcs.api.event.DespawnReason) CombatNpcManager(combatlogx.expansion.compatibility.citizens.manager.CombatNpcManager) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with InventoryManager

use of combatlogx.expansion.compatibility.citizens.manager.InventoryManager in project CombatLogX by SirBlobman.

the class ListenerJoin method punish.

private void punish(Player player) {
    if (player == null || player.hasMetadata("NPC")) {
        printDebug("Could not punish player, they are null or an NPC.");
        return;
    }
    CombatNpcManager combatNpcManager = this.expansion.getCombatNpcManager();
    YamlConfiguration playerData = combatNpcManager.getData(player);
    if (!playerData.getBoolean("citizens-compatibility.punish")) {
        printDebug("Punishment set to false in player data.");
        return;
    }
    playerData.set("citizens-compatibility.punish", false);
    combatNpcManager.saveData(player);
    printDebug("Setting punish to false in player data to prevent double punishment.");
    YamlConfiguration configuration = getConfiguration();
    if (configuration.getBoolean("store-location")) {
        Location location = combatNpcManager.loadLocation(player);
        if (location != null) {
            printDebug("Teleporting player to last known location for NPC.");
            player.teleport(location, TeleportCause.PLUGIN);
        }
        playerData.set("citizens-compatibility.location", null);
    }
    if (configuration.getBoolean("store-inventory")) {
        printDebug("Clearing player inventory.");
        PlayerInventory playerInventory = player.getInventory();
        playerInventory.clear();
    }
    double health = combatNpcManager.loadHealth(player);
    setHealth(player, health);
    printDebug("Set player health to " + health);
    if (health <= 0.0D) {
        printDebug("Player died or health was invalid, removing stored inventory to prevent duplication.");
        playerData.set("citizens-compatibility.inventory", null);
        playerData.set("citizens-compatibility.armor", null);
    }
    if (configuration.getBoolean("store-inventory")) {
        printDebug("Restoring player inventory if possible.");
        InventoryManager inventoryManager = expansion.getInventoryManager();
        inventoryManager.restoreInventory(player);
    }
}
Also used : InventoryManager(combatlogx.expansion.compatibility.citizens.manager.InventoryManager) PlayerInventory(org.bukkit.inventory.PlayerInventory) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) CombatNpcManager(combatlogx.expansion.compatibility.citizens.manager.CombatNpcManager) Location(org.bukkit.Location)

Aggregations

CombatNpcManager (combatlogx.expansion.compatibility.citizens.manager.CombatNpcManager)2 InventoryManager (combatlogx.expansion.compatibility.citizens.manager.InventoryManager)2 Location (org.bukkit.Location)2 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)2 CombatNPC (combatlogx.expansion.compatibility.citizens.object.CombatNPC)1 DespawnReason (net.citizensnpcs.api.event.DespawnReason)1 NPC (net.citizensnpcs.api.npc.NPC)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 EventHandler (org.bukkit.event.EventHandler)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1 BukkitScheduler (org.bukkit.scheduler.BukkitScheduler)1