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

Adds ExprPortalCooldown #5356

Merged
merged 16 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
102 changes: 102 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprPortalCooldown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.classes.Changer;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.WeatherType;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Portal Cooldown")
@Description("The amount of time before an entity can use a portal. By default, this is 15 seconds after exiting a nether portal.")
@Examples({
"on portal:",
"\twait 1 tick",
"\tset portal cooldown of event-entity to 5 seconds"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprPortalCooldown extends SimplePropertyExpression<Entity, Timespan> {

static {
register(ExprPortalCooldown.class, Timespan.class, "portal cool[-| ]down", "entities");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
@Nullable
public Timespan convert(Entity entity) {
return Timespan.fromTicks(entity.getPortalCooldown());
}

@Override
@Nullable
public Class<?>[] acceptChange(Changer.ChangeMode mode) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
switch (mode) {
case SET:
case ADD:
case RESET:
case DELETE:
case REMOVE:
return CollectionUtils.array(Timespan.class);
}
return null;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void change(Event event, @Nullable Object[] delta, Changer.ChangeMode mode) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Entity[] entities = getExpr().getArray(event);
int change = delta == null ? 0 : (int) ((Timespan) delta[0]).getTicks();
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
switch (mode) {
case REMOVE:
change = -change;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
case ADD:
for (Entity entity : entities) {
entity.setPortalCooldown(Math.max(entity.getPortalCooldown() + change, 0));
}
break;
case DELETE:
case RESET:
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
case SET:
for (Entity entity : entities) {
entity.setPortalCooldown(Math.max(change, 0));
}
break;
default:
assert false;
}
}

@Override
public Class<? extends Timespan> getReturnType() {
return Timespan.class;
}

@Override
protected String getPropertyName() {
return "portal cooldown";
}

}
14 changes: 14 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprPortalCooldown.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test "portal cooldown":
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
spawn zombie at location(0, 64, 0, world "world")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
set {_m} to last spawned zombie
assert {_m}'s portal cooldown is 0 seconds with "entity spawned with a portal cooldown"
set {_m}'s portal cooldown to 25 ticks
assert {_m}'s portal cooldown is 25 ticks with "portal cooldown set failed"
add 5 seconds to {_m}'s portal cooldown
assert {_m}'s portal cooldown is 125 ticks with "portal cooldown add ##1 failed"
remove 12 ticks from {_m}'s portal cooldown
assert {_m}'s portal cooldown is 113 ticks with "portal cooldown remove ##1 failed"
remove 999 ticks from {_m}'s portal cooldown
assert {_m}'s portal cooldown is 0 ticks with "portal cooldown remove ##2 failed"
delete {_m}'s portal cooldown
assert {_m}'s portal cooldown is 0 ticks with "portal cooldown delete failed"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved