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 4 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
108 changes: 108 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,108 @@
/**
* 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.classes.Changer.ChangeMode;
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.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, it is 15 seconds after exiting a nether portal. Resetting will set the cooldown back to the default 15 seconds.")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
@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 cooldown", "entities");
}

// Default cooldown for nether portals is 15 seconds:
// https://minecraft.fandom.com/wiki/Nether_portal#Behavior
private static final int DEFAULT_COOLDOWN = 15 * 20;

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

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case SET:
case ADD:
case RESET:
case DELETE:
case REMOVE:
return CollectionUtils.array(Timespan.class);
default:
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
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 RESET:
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
change = DEFAULT_COOLDOWN;
case DELETE:
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";
}

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprPortalCooldown.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "portal cooldown":
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
spawn zombie at spawn of world "world"
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"
reset {_m}'s portal cooldown
assert {_m}'s portal cooldown is 15 seconds with "portal cooldown reset failed"
delete random entity of {_m}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved