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

Additional Dropped Item Elements #7270

Open
wants to merge 21 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
58 changes: 58 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondItemLifetime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ch.njol.skript.conditions;

import ch.njol.skript.Skript;
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.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.entity.Item;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Item Has Unlimited Lifetime")
@Description("Checks if the dropped item has unlimited lifetime enabled or disabled.")
@Examples({
"if all dropped items have unlimited lifetime disabled:",
"\tenable unlimited lifetime for all dropped items"
})
@Since("INSERT VERSION")
public class CondItemLifetime extends Condition {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

static {
Skript.registerCondition(CondItemLifetime.class, ConditionType.PROPERTY,
"[the] %itementities% (has|have) unlimited lifetime enabled",
"unlimited lifetime (is|are) enabled for %itementities%",
"[the] %itementities% (has|have) unlimited lifetime",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"[the] %itementities% (has|have) unlimited lifetime disabled",
"unlimited lifetime (is|are) disabled for %itementities%",
"[the] %itementities% (don't|do not|doesn't|does not) have unlimited lifetime");
}

private Expression<Item> entities;
private boolean checkEnabled;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
checkEnabled = matchedPattern <= 2;
//noinspection unchecked
entities = (Expression<Item>) exprs[0];
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

@Override
public boolean check(Event event) {
return entities.check(event, item -> {
return item.isUnlimitedLifetime() == checkEnabled;
});
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the " + entities.toString(event, debug) + " have unlimited lifetime " + (checkEnabled ? "enabled" : "disabled");
}

}
57 changes: 57 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffItemLifetime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
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.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.entity.Item;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Unlimited Item Lifetime")
@Description("Makes a dropped item have unlimited lifetime, meaning it won't despawn from vanilla Minecraft timer.")
@Examples({
"enabled unlimited lifetime of all dropped items"
})
@Since("INSERT VERSION")
public class EffItemLifetime extends Effect {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

static {
Skript.registerEffect(EffItemLifetime.class,
"enable unlimited lifetime for %itementities%",
"make [the] lifetime unlimited for %itementities%",
"make [the] %itementities% lifetime unlimited",
"disable unlimited lifetime for %itementities%",
"make [the] lifetime limited for %itementities%",
"make [the] %itementities% lifetime limited");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<Item> entities;
private boolean enable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
enable = matchedPattern <= 2;
//noinspection unchecked
entities = (Expression<Item>) exprs[0];
return true;
}

@Override
protected void execute(Event event) {
for (Item item : entities.getArray(event)) {
item.setUnlimitedLifetime(enable);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return (enable ? "enable" : "disable") + " unlimited lifetime of " + entities.toString(event, debug);
}

}
136 changes: 136 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprEntityOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* 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
*/
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
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.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Tameable;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

@Name("Entity Owner")
@Description("The owner of a tameable entity (i.e. horse or wolf) or a dropped item.")
@Examples({
"set owner of target entity to player",
"delete owner of target entity",
"set {_t} to uuid of tamer of target entity",
"",
"set the owner of all dropped items to player"
})
@Since("2.5, INSERT VERSION (dropped items)")
public class ExprEntityOwner extends SimplePropertyExpression<Entity, Object> {

static {
Skript.registerExpression(ExprEntityOwner.class, Object.class, ExpressionType.PROPERTY,
"[the] (owner|tamer) of %livingentities%",
"[the] [[dropped] item] owner of %itementities%");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean useTameable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
useTameable = matchedPattern == 0;
return super.init(exprs, matchedPattern, isDelayed, parseResult);
}

@Override
public @Nullable Object convert(Entity entity) {
if (entity instanceof Tameable tameable && tameable.isTamed()) {
return tameable.getOwner();
} else if (entity instanceof Item item) {
UUID uuid = item.getOwner();
if (uuid == null)
return null;
Entity checkEntity = Bukkit.getEntity(uuid);
if (checkEntity != null)
return checkEntity;
return Bukkit.getOfflinePlayer(uuid);
}
return null;
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
return switch (mode) {
case SET, DELETE, RESET -> {
if (useTameable)
yield CollectionUtils.array(OfflinePlayer.class);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
yield CollectionUtils.array(OfflinePlayer.class, Entity.class, String.class);
}
default -> null;
};
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
UUID newId = null;
OfflinePlayer newPlayer = null;
if (delta != null) {
if (delta[0] instanceof OfflinePlayer offlinePlayer) {
newPlayer = offlinePlayer;
newId = offlinePlayer.getUniqueId();
} else if (delta[0] instanceof Entity entity) {
newId = entity.getUniqueId();
} else if (delta[0] instanceof String string) {
newId = UUID.fromString(string);
}
}

for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof Tameable tameable) {
tameable.setOwner(newPlayer);
} else if (entity instanceof Item item) {
item.setOwner(newId);
}
}
}

@Override
public Class<Object> getReturnType() {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return Object.class;
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return "owner of " + getExpr().toString(event, debug);
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

}
103 changes: 0 additions & 103 deletions src/main/java/ch/njol/skript/expressions/ExprEntityTamer.java

This file was deleted.

Loading
Loading