Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for infinite duration effects #6531

Open
wants to merge 1 commit into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/command/defaults/EffectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
}

$amplification = 0;
$infinite = false;

if(count($args) >= 3){
if(($d = $this->getBoundedInt($sender, $args[2], 0, (int) (Limits::INT32_MAX / 20))) === null){
if($args[2] === "infinite") {
$infinite = true;
$duration = null;
}elseif(($d = $this->getBoundedInt($sender, $args[2], 0, (int) (Limits::INT32_MAX / 20))) === null){
return false;
}else{
$duration = $d * 20; //ticks
}
$duration = $d * 20; //ticks
}else{
$duration = null;
}
Expand Down Expand Up @@ -112,8 +117,14 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
$sender->sendMessage(KnownTranslationFactory::commands_effect_success_removed($effect->getName(), $player->getDisplayName()));
}else{
$instance = new EffectInstance($effect, $duration, $amplification, $visible);
if($infinite) {
$instance->setInfinite();
}
$effectManager->add($instance);
self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_effect_success($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName(), (string) ($instance->getDuration() / 20)));
self::broadcastCommandMessage($sender, $infinite
? KnownTranslationFactory::commands_effect_success_infinite($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName())
: KnownTranslationFactory::commands_effect_success($effect->getName(), (string) $instance->getAmplifier(), $player->getDisplayName(), (string) ($instance->getDuration() / 20))
);
}

return true;
Expand Down
26 changes: 22 additions & 4 deletions src/entity/effect/EffectInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function getDuration() : int{
* @return $this
*/
public function setDuration(int $duration) : EffectInstance{
if($duration < 0 || $duration > Limits::INT32_MAX){
throw new \InvalidArgumentException("Effect duration must be in range 0 - " . Limits::INT32_MAX . ", got $duration");
if($duration < -1 || $duration > Limits::INT32_MAX){
throw new \InvalidArgumentException("Effect duration must be in range -1 (infinite) - " . Limits::INT32_MAX . ", got $duration");
}
$this->duration = $duration;

Expand All @@ -80,7 +80,9 @@ public function setDuration(int $duration) : EffectInstance{
* @return $this
*/
public function decreaseDuration(int $ticks) : EffectInstance{
$this->duration = max(0, $this->duration - $ticks);
if(!$this->isInfinite()) {
$this->duration = max(0, $this->duration - $ticks);
}

return $this;
}
Expand All @@ -89,7 +91,7 @@ public function decreaseDuration(int $ticks) : EffectInstance{
* Returns whether the duration has run out.
*/
public function hasExpired() : bool{
return $this->duration <= 0;
return !$this->isInfinite() && $this->duration <= 0;
}

public function getAmplifier() : int{
Expand Down Expand Up @@ -174,4 +176,20 @@ public function resetColor() : EffectInstance{

return $this;
}

/**
* Set if effect duration is infinite, does not expire.
*/
public function setInfinite() : EffectInstance{
$this->duration = -1;

return $this;
}

/**
* Returns if effect duration is infinite.
*/
public function isInfinite() : bool{
return $this->duration === -1;
}
}
8 changes: 8 additions & 0 deletions src/lang/KnownTranslationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ public static function commands_effect_success(Translatable|string $param0, Tran
]);
}

public static function commands_effect_success_infinite(Translatable|string $param0, Translatable|string $param1, Translatable|string $param2) : Translatable{
return new Translatable(KnownTranslationKeys::COMMANDS_EFFECT_SUCCESS_INFINITE, [
0 => $param0,
1 => $param1,
2 => $param2,
]);
}

public static function commands_effect_success_removed(Translatable|string $param0, Translatable|string $param1) : Translatable{
return new Translatable(KnownTranslationKeys::COMMANDS_EFFECT_SUCCESS_REMOVED, [
0 => $param0,
Expand Down
1 change: 1 addition & 0 deletions src/lang/KnownTranslationKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class KnownTranslationKeys{
public const COMMANDS_EFFECT_FAILURE_NOTACTIVE_ALL = "commands.effect.failure.notActive.all";
public const COMMANDS_EFFECT_NOTFOUND = "commands.effect.notFound";
public const COMMANDS_EFFECT_SUCCESS = "commands.effect.success";
public const COMMANDS_EFFECT_SUCCESS_INFINITE = "commands.effect.success.infinite";
public const COMMANDS_EFFECT_SUCCESS_REMOVED = "commands.effect.success.removed";
public const COMMANDS_EFFECT_SUCCESS_REMOVED_ALL = "commands.effect.success.removed.all";
public const COMMANDS_EFFECT_USAGE = "commands.effect.usage";
Expand Down
Loading