From 99de824b049eb1c3388d25c3dd201f338ebe515e Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 13 Aug 2021 14:12:47 +0100 Subject: [PATCH] Added death time ticker --- .../com/willfp/ecobosses/bosses/EcoBoss.java | 7 +++++ .../ecobosses/bosses/LivingEcoBoss.java | 10 +++++-- .../bosses/tick/tickers/DeathTimeTicker.java | 25 ++++++++++++++++ .../tick/tickers/HealthPlaceholderTicker.java | 16 ---------- .../tick/tickers/NamePlaceholderTicker.java | 30 +++++++++++++++++++ .../src/main/resources/bosses/illusioner.yml | 1 + 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/DeathTimeTicker.java delete mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/HealthPlaceholderTicker.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/NamePlaceholderTicker.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java index 14e84e8..ddb1b95 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/EcoBoss.java @@ -239,6 +239,12 @@ public class EcoBoss extends PluginDependent { @Getter private final int autoSpawnInterval; + /** + * The time to live. + */ + @Getter + private final int timeToLive; + /** * Locations that the boss can auto spawn at. */ @@ -277,6 +283,7 @@ public class EcoBoss extends PluginDependent { this.maxHealth = this.getConfig().getInt("max-health"); this.followRange = this.getConfig().getInt("follow-range"); this.movementSpeedMultiplier = this.getConfig().getInt("movement-speed"); + this.timeToLive = this.getConfig().getInt("time-to-live", -1); // Spawn Totem this.spawnTotemEnabled = this.getConfig().getBool("spawn-totem.enabled"); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java index f628fa4..cff355d 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/LivingEcoBoss.java @@ -8,7 +8,8 @@ import com.willfp.eco.util.StringUtils; import com.willfp.ecobosses.bosses.effects.Effect; import com.willfp.ecobosses.bosses.tick.BossTicker; import com.willfp.ecobosses.bosses.tick.tickers.BossBarTicker; -import com.willfp.ecobosses.bosses.tick.tickers.HealthPlaceholderTicker; +import com.willfp.ecobosses.bosses.tick.tickers.DeathTimeTicker; +import com.willfp.ecobosses.bosses.tick.tickers.NamePlaceholderTicker; import com.willfp.ecobosses.bosses.tick.tickers.TargetTicker; import com.willfp.ecobosses.bosses.util.obj.OptionedSound; import lombok.Getter; @@ -66,7 +67,8 @@ public class LivingEcoBoss extends PluginDependent { // Tickers this.tickers = new ArrayList<>(); - this.tickers.add(new HealthPlaceholderTicker()); + this.tickers.add(new NamePlaceholderTicker()); + this.tickers.add(new DeathTimeTicker()); this.tickers.add(new TargetTicker(boss.getTargetMode(), boss.getTargetDistance())); if (boss.isBossbarEnabled()) { this.tickers.add( @@ -96,6 +98,10 @@ public class LivingEcoBoss extends PluginDependent { entity.setPersistent(true); entity.setRemoveWhenFarAway(false); + if (boss.getTimeToLive() > 0) { + entity.setMetadata("death-time", this.getPlugin().getMetadataValueFactory().create(System.currentTimeMillis() + (boss.getTimeToLive() * 1000L))); + } + entity.setCustomName(boss.getDisplayName()); entity.setCustomNameVisible(true); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/DeathTimeTicker.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/DeathTimeTicker.java new file mode 100644 index 0000000..9d33479 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/DeathTimeTicker.java @@ -0,0 +1,25 @@ +package com.willfp.ecobosses.bosses.tick.tickers; + +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.tick.BossTicker; +import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.NotNull; + +public class DeathTimeTicker implements BossTicker { + @Override + public void tick(@NotNull final EcoBoss boss, + @NotNull final LivingEntity entity, + final long tick) { + if (boss.getTimeToLive() < 0) { + return; + } + + int timeLeft; + timeLeft = (int) (entity.getMetadata("death-time").get(0).asLong() - System.currentTimeMillis()); + timeLeft = (int) Math.ceil(timeLeft / 1000D); + if (timeLeft <= 0) { + entity.remove(); + boss.removeLivingBoss(entity); + } + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/HealthPlaceholderTicker.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/HealthPlaceholderTicker.java deleted file mode 100644 index e8458d0..0000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/HealthPlaceholderTicker.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.willfp.ecobosses.bosses.tick.tickers; - -import com.willfp.eco.util.StringUtils; -import com.willfp.ecobosses.bosses.EcoBoss; -import com.willfp.ecobosses.bosses.tick.BossTicker; -import org.bukkit.entity.LivingEntity; -import org.jetbrains.annotations.NotNull; - -public class HealthPlaceholderTicker implements BossTicker { - @Override - public void tick(@NotNull final EcoBoss boss, - @NotNull final LivingEntity entity, - final long tick) { - entity.setCustomName(boss.getDisplayName().replace("%health%", StringUtils.internalToString(entity.getHealth()))); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/NamePlaceholderTicker.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/NamePlaceholderTicker.java new file mode 100644 index 0000000..f12c94d --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/tick/tickers/NamePlaceholderTicker.java @@ -0,0 +1,30 @@ +package com.willfp.ecobosses.bosses.tick.tickers; + +import com.willfp.eco.util.StringUtils; +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.tick.BossTicker; +import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.NotNull; + +public class NamePlaceholderTicker implements BossTicker { + @Override + public void tick(@NotNull final EcoBoss boss, + @NotNull final LivingEntity entity, + final long tick) { + int timeLeft = -1; + if (boss.getTimeToLive() > 0) { + timeLeft = (int) (entity.getMetadata("death-time").get(0).asLong() - System.currentTimeMillis()); + timeLeft = (int) Math.ceil(timeLeft / 1000D); + } + + String time = ""; + if (timeLeft > 0) { + time = String.format("%d:%02d", timeLeft/60, timeLeft%60); + } + entity.setCustomName( + boss.getDisplayName() + .replace("%health%", StringUtils.internalToString(entity.getHealth())) + .replace("%time%", time) + ); + } +} diff --git a/eco-core/core-plugin/src/main/resources/bosses/illusioner.yml b/eco-core/core-plugin/src/main/resources/bosses/illusioner.yml index ced3373..557c4b5 100644 --- a/eco-core/core-plugin/src/main/resources/bosses/illusioner.yml +++ b/eco-core/core-plugin/src/main/resources/bosses/illusioner.yml @@ -12,6 +12,7 @@ max-health: 300 attack-damage: 30 movement-speed: 1.2 follow-range: 20 +time-to-live: 120 # Time to live before auto despawn, in seconds. Set to -1 to disable. auto-spawn-interval: -1 # Time between auto spawns in ticks (20 ticks in a second) - Set to -1 to disable. auto-spawn-locations: [ ] # Formatted as world:x:y:z - for example world_nether:100:10:100