diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Config.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Config.kt index 48fdb67..5098783 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Config.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Config.kt @@ -47,6 +47,8 @@ class Config(file: File, stream: InputStream) { @JvmStatic fun PLACE_PERMISSION() = AxMinionsAPI.INSTANCE.getConfig().get("place-permissions", false) @JvmStatic + fun WORK_WHEN_OWNER_OFFLINE() = AxMinionsAPI.INSTANCE.getConfig().get("work-when-owner-offline", true) + @JvmStatic fun DEBUG(): Boolean { if (debug === null) { debug = AxMinionsAPI.INSTANCE.getConfig().get("debug", false) diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/Minion.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/Minion.kt index 76a39d8..dfb20ec 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/Minion.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/Minion.kt @@ -109,4 +109,8 @@ interface Minion : InventoryHolder { fun damageTool(amount: Int = 1) fun canUseTool(): Boolean + + fun isOwnerOnline(): Boolean + + fun setOwnerOnline(online: Boolean) } \ No newline at end of file diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/miniontype/MinionType.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/miniontype/MinionType.kt index 1199a0f..0c2d58a 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/miniontype/MinionType.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/minions/miniontype/MinionType.kt @@ -33,6 +33,7 @@ abstract class MinionType(private val name: String, private val defaults: InputS } fun tick(minion: Minion) { + if (!com.artillexstudios.axminions.api.config.Config.WORK_WHEN_OWNER_OFFLINE() && !minion.isOwnerOnline()) return if (!shouldRun(minion)) return minion.resetAnimation() diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/AxMinionsPlugin.kt b/common/src/main/kotlin/com/artillexstudios/axminions/AxMinionsPlugin.kt index 01495d9..c68c577 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/AxMinionsPlugin.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/AxMinionsPlugin.kt @@ -20,6 +20,7 @@ import com.artillexstudios.axminions.listeners.MinionDamageListener import com.artillexstudios.axminions.listeners.MinionDropListener import com.artillexstudios.axminions.listeners.MinionInventoryListener import com.artillexstudios.axminions.listeners.MinionPlaceListener +import com.artillexstudios.axminions.listeners.PlayerListener import com.artillexstudios.axminions.listeners.WorldListener import com.artillexstudios.axminions.minions.Minion import com.artillexstudios.axminions.minions.MinionTicker @@ -114,6 +115,7 @@ class AxMinionsPlugin : AxPlugin() { it.registerEvents(MinionDamageListener(), this) it.registerEvents(WorldListener(), this) it.registerEvents(MinionDropListener(), this) + it.registerEvents(PlayerListener(), this) } // Retroactively load minions for the already loaded worlds diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/PlayerListener.kt b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/PlayerListener.kt new file mode 100644 index 0000000..825b979 --- /dev/null +++ b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/PlayerListener.kt @@ -0,0 +1,28 @@ +package com.artillexstudios.axminions.listeners + +import com.artillexstudios.axminions.minions.Minions +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent +import org.bukkit.event.player.PlayerQuitEvent + +class PlayerListener : Listener { + + @EventHandler + fun onPlayerJoinEvent(event: PlayerJoinEvent) { + Minions.getMinions().forEach { + if (it.getOwnerUUID() == event.player.uniqueId) { + it.setOwnerOnline(true) + } + } + } + + @EventHandler + fun onPlayerJoinEvent(event: PlayerQuitEvent) { + Minions.getMinions().forEach { + if (it.getOwnerUUID() == event.player.uniqueId) { + it.setOwnerOnline(false) + } + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt index bb64a2c..1b70ea9 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt @@ -67,10 +67,12 @@ class Minion( private val extraData = hashMapOf() private var linkedInventory: Inventory? = null internal val openInventories = mutableListOf() + @Volatile private var ticking = false private var debugHologram: Hologram? = null private var broken = false + private var ownerOnline = false init { spawn() @@ -671,8 +673,8 @@ class Minion( setTool(it) linkedInventory?.remove(it) return canUseTool() - } } + } return false } else { return true @@ -682,6 +684,14 @@ class Minion( return false } + override fun isOwnerOnline(): Boolean { + return ownerOnline + } + + override fun setOwnerOnline(online: Boolean) { + ownerOnline = online + } + override fun getInventory(): Inventory { return Bukkit.createInventory(this, 9) } diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FarmerMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FarmerMinionType.kt index d6a74ef..9a0885d 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FarmerMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FarmerMinionType.kt @@ -45,6 +45,11 @@ class FarmerMinionType : MinionType("farmer", AxMinionsPlugin.INSTANCE.getResour minion.setLinkedChest(null) } + if (minion.getLinkedInventory()?.firstEmpty() == -1) { + Warnings.CONTAINER_FULL.display(minion) + return + } + if (!minion.canUseTool()) { Warnings.NO_TOOL.display(minion) return diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FisherMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FisherMinionType.kt index c3339fd..6e91ed0 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FisherMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/FisherMinionType.kt @@ -44,6 +44,11 @@ class FisherMinionType : MinionType("fisher", AxMinionsPlugin.INSTANCE.getResour minion.setLinkedChest(null) } + if (minion.getLinkedInventory()?.firstEmpty() == -1) { + Warnings.CONTAINER_FULL.display(minion) + return + } + var waterLocation: Location? = null run breaking@{ LocationUtils.getAllBlocksInRadius(minion.getLocation(), 2.0, false).fastFor { diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt index efd4512..4852a81 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/LumberMinionType.kt @@ -49,6 +49,11 @@ class LumberMinionType : MinionType("lumber", AxMinionsPlugin.INSTANCE.getResour return } + if (minion.getLinkedInventory()?.firstEmpty() == -1) { + Warnings.CONTAINER_FULL.display(minion) + return + } + Warnings.remove(minion, Warnings.NO_TOOL) val loot = ArrayList() diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt index 303350e..f6aea67 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/MinerMinionType.kt @@ -57,6 +57,11 @@ class MinerMinionType : MinionType("miner", AxMinionsPlugin.INSTANCE.getResource return } + if (minion.getLinkedInventory()?.firstEmpty() == -1) { + Warnings.CONTAINER_FULL.display(minion) + return + } + Warnings.remove(minion, Warnings.NO_TOOL) var amount = 0 diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/SlayerMinionType.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/SlayerMinionType.kt index 446ec86..ce8c6e6 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/SlayerMinionType.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/miniontype/SlayerMinionType.kt @@ -49,6 +49,11 @@ class SlayerMinionType : MinionType("slayer", AxMinionsPlugin.INSTANCE.getResour return } + if (minion.getLinkedInventory()?.firstEmpty() == -1) { + Warnings.CONTAINER_FULL.display(minion) + return + } + Warnings.remove(minion, Warnings.NO_TOOL) minion.getLocation().world!!.getNearbyEntities( diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index 3d18d0d..05080cf 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -39,6 +39,9 @@ use-durability: true # If the minion should pull new tools from the chest it's connected to pull-tools-from-chest: false +# If the minion should work when the owner is offline (only if the chunk is loaded) +work-when-owner-offline: true + # What type of message we should send when the upgrade fails # due to insufficient funds # Possible options: chat, title, subtitle, actionbar @@ -95,4 +98,4 @@ gui: debug: false # Do not change! -config-version: 3 \ No newline at end of file +config-version: 4 \ No newline at end of file