Skip to content

Commit

Permalink
Add kept items expression and effect
Browse files Browse the repository at this point in the history
  • Loading branch information
EduMejorado committed Sep 22, 2023
1 parent a8d6acd commit 2f3794b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
37 changes: 27 additions & 10 deletions src/main/java/ch/njol/skript/effects/EffKeepItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package ch.njol.skript.effects;

import java.util.Iterator;
import java.util.List;

import org.bukkit.event.Event;
Expand All @@ -40,21 +41,29 @@
import ch.njol.util.Kleenean;

@Name("Keep Items")
@Description("Keeps the given items from dropping in a 'death of player' event.")
@Examples({"on death of player:",
"\tkeep the drops"})
@Description({
"Keeps the given items from dropping in a 'death of player' event.",
"Note: this effect will only keep items that have been in the drops.",
"Use the <a href='expressions.html#ExprKeptItems'>kept items</a> expression for items outside of the drops."
})
@Examples({
"on death of player:",
"\tkeep the drops"
})
@Events("death")
@RequiredPlugins("Paper 1.13+ (keep specific items)")
@RequiredPlugins("Paper (keep specific items)")
@Since("INSERT VERSION")
public class EffKeepItems extends Effect {

static {
Skript.registerEffect(EffKeepItems.class, "keep %item types% [from ([the] drops|dropping)]");
Skript.registerEffect(EffKeepItems.class, "keep %itemtypes% [from ([the] drops|dropping)]");
}

private static final boolean SPIGOT = Skript.methodExists(PlayerDeathEvent.class, "getItemsToKeep");
private static final boolean SPIGOT = !Skript.methodExists(PlayerDeathEvent.class, "getItemsToKeep");
private boolean keepAll;

private Expression<ItemType> items;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlayerDeathEvent.class)) {
Expand All @@ -74,6 +83,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
protected void execute(Event event) {
if (!(event instanceof PlayerDeathEvent))
return;

PlayerDeathEvent e = (PlayerDeathEvent) event;
List<ItemStack> drops = e.getDrops();

Expand All @@ -84,14 +96,19 @@ protected void execute(Event event) {
}

List<ItemStack> keptItems = e.getItemsToKeep();
for (ItemType item : items.getArray(e)) {
item.addTo(keptItems);
item.removeFrom(drops);
}
// We make sure the items we're keeping are part of the drops this way
List<ItemStack> itemsToKeep = items.stream(event)
.filter(item -> item.isContainedIn(drops))
.map(ItemType::getRandom)
.toList();

keptItems.addAll(itemsToKeep);
drops.removeAll(itemsToKeep);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "keep " + items.toString(event, debug) + " from the drops";
}

}
35 changes: 21 additions & 14 deletions src/main/java/ch/njol/skript/expressions/ExprKeptItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@
import ch.njol.util.coll.CollectionUtils;

@Name("Kept Items")
@Description({"The kept items in a 'death of player' event.",
"",
"Note: this expression won't remove the items from the drops when changed."})
@Description({
"The kept items in a 'death of player' event.",
"Note: this expression won't remove the items from the drops when changed.",
"Use the <a href='effects.html#EffKeepItems'>keep items</a> effect for that."
})
@Examples({
"on death of player:",
"\tset kept items to the drops where [input isn't victim's tool]"})
"\tset kept items to the drops where [input isn't victim's tool]"
})
@Events("death")
@RequiredPlugins("Paper 1.13+")
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class ExprKeptItems extends SimpleExpression<ItemType> {

Expand All @@ -69,15 +72,20 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
protected @Nullable ItemType[] get(Event event) {
@Nullable
protected ItemType[] get(Event event) {
if (!(event instanceof PlayerDeathEvent))
return null;

return ((PlayerDeathEvent) event).getItemsToKeep()
.stream()
.map(ItemType::new)
.toArray(ItemType[]::new);
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case ADD:
case REMOVE:
Expand All @@ -96,11 +104,14 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
return;

List<ItemStack> keptItems = ((PlayerDeathEvent) event).getItemsToKeep();

boolean canChange = delta != null;
// Clear items before adding the new values
if (mode == ChangeMode.SET)
if ((mode == ChangeMode.SET && canChange) || mode == ChangeMode.DELETE || mode == ChangeMode.RESET)
keptItems.clear();

if (!canChange)
return;

for (Object o : delta) {
ItemType item = (ItemType) o;
switch (mode) {
Expand All @@ -113,9 +124,6 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
break;
case REMOVE_ALL:
item.removeAll(keptItems);
case DELETE:
case RESET:
keptItems.clear();
break;
}
}
Expand All @@ -132,9 +140,8 @@ public Class<? extends ItemType> getReturnType() {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
return "the kept items";
}

}

0 comments on commit 2f3794b

Please sign in to comment.