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 11 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
36 changes: 36 additions & 0 deletions src/main/java/ch/njol/skript/bukkitutil/UUIDUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.njol.skript.bukkitutil;

import ch.njol.util.StringUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

/**
* Utility class for quick {@link UUID} methods.
*/
public class UUIDUtils {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

/**
* Converts {@code object} into a UUID if possible
* @param object
* @return The resulting {@link UUID}
*/
public static @Nullable UUID asUUID(@NotNull Object object) {
if (object instanceof OfflinePlayer offlinePlayer) {
return offlinePlayer.getUniqueId();
} else if (object instanceof Entity entity) {
return entity.getUniqueId();
} else if (object instanceof String string && StringUtils.containsAny(string, "-")) {
try {
return UUID.fromString(string);
} catch (Exception ignored) {}
} else if (object instanceof UUID uuid) {
return uuid;
}
return null;
}

}
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("Will Despawn")
@Description("Checks if the dropped item will be despawned naturally through Minecraft's timer.")
@Examples({
"if all dropped items can despawn naturally:",
"\tprevent all dropped items from naturally despawning"
})
@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,
"%itementities% can despawn naturally",
"%itementities% can naturally despawn",
"%itementities% (can not|can't) despawn naturally",
"%itementities% (can not|can't) naturally despawn"
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
);
}

private Expression<Item> entities;
private boolean canDespawn;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
canDespawn = matchedPattern <= 1;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
//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, canDespawn);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the " + entities.toString(event, debug) + (canDespawn ? " can " : " can not ") + "despawn naturally";
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}
62 changes: 62 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,62 @@
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("Item Despawn")
@Description("Prevent a dropped item from naturally despawning through Minecraft's timer.")
@Examples({
"prevent all dropped items from naturally despawning",
"allow all dropped items to naturally despawn"
})
@Since("INSERT VERSION")
public class EffItemLifetime extends Effect {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

static {
Skript.registerEffect(EffItemLifetime.class,
"prevent %itementities% from naturally despawning",
"prevent %itementities% from despawning naturally",
"allow %itementities% to naturally despawn",
"allow %itementities% to despawn naturally");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<Item> entities;
private boolean prevent;

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

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

@Override
public String toString(@Nullable Event event, boolean debug) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
String start = "prevent ";
String designation = "from ";
if (!prevent) {
start = "allow ";
designation = "to ";
}
return start + entities.toString(event, debug) + designation + "naturally despawning";
}

}
115 changes: 115 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,115 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.UUIDUtils;
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.ExpressionType;
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.",
"NOTES:",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"Getting the owner of a dropped item will only return a loaded entity or a player that has played before. "
+ "If the entity was killed, or the player has never played before, will return null.",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"Setting the owner of a dropped item means only that entity or player can pick it up. "
+ "This is UUID based, so it can also be set to a specific UUID.",
"Dropping an item does not automatically make the entity or player the owner."
})
@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%",
"%livingentities%'[s] (owner|tamer)",
"[the] [dropped item] owner of %itementities%",
"%itementities%'[s] [dropped item] owner");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@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.hasPlayedBefore())
return checkPlayer;
return null;
}
return null;
}

@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
return switch (mode) {
case SET, DELETE, RESET -> 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 {
newId = UUIDUtils.asUUID(delta[0]);
}
}

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

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

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

This file was deleted.

Loading
Loading