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 19 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
55 changes: 55 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,55 @@
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|infinite) life(time|span) [enabled]",
"(unlimited|infinite) life(time|span) (is|are) enabled for %itementities%",
"[the] %itementities% (has|have) (unlimited|infinite) life(time|span) disabled",
"(unlimited|infinite) life(time|span) (is|are) disabled for %itementities%",
"[the] %itementities% (don't|do not|doesn't|does not) have (unlimited|infinite) life(time|span)");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<Item> entities;
private boolean enabled;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
enabled = matchedPattern <= 1;
//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::isUnlimitedLifetime, !enabled);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the " + entities.toString(event, debug) + " have unlimited lifetime " + (enabled ? "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|infinite) life(time|span) for [the] %itementities%",
"make life(time|span) (unlimited|infinite) for [the] %itementities%",
"make [the] %itementities% life(time|span) (unlimited|infinite)",
"disable (unlimited|infinite) life(time|span) for [the] %itementities%",
"make life(time|span) (limited|finite) for [the] %itementities%",
"make [the] %itementities% life(time|span) (limited|finite)");
}

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);
}

}
133 changes: 133 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,133 @@
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.",
"NOTE: If the owner of a dropped item was an entity and was killed, will return their uuid in string form."
})
@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] %livingentities%'[s] (owner|tamer)",
"[the] [dropped item] owner of %itementities%",
"[the] %itementities%'[s] [dropped item] owner");
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 <= 1;
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;
OfflinePlayer checkPlayer = Bukkit.getOfflinePlayer(uuid);
if (checkPlayer != null)
return checkPlayer;
return uuid.toString();
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
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) {
try {
newId = UUID.fromString(string);
} catch (Exception ignored) {}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

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
public Class<?>[] possibleReturnTypes() {
return CollectionUtils.array(Entity.class, OfflinePlayer.class, String.class);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@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