Skip to content

Commit

Permalink
ExprAffectedEntities (#7083)
Browse files Browse the repository at this point in the history
* Starter Commit

* Removed Test

* Requested Change

* Requested Changes

* Requested Changes

* Requested Changes

Plus Test

* Add private

---------

Co-authored-by: Efnilite <[email protected]>
Co-authored-by: sovdee <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2024
1 parent 6f0b993 commit a22e34e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/main/java/ch/njol/skript/expressions/ExprAffectedEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package ch.njol.skript.expressions;

import java.util.Iterator;
import java.util.Objects;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
Expand Down Expand Up @@ -58,20 +61,52 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
return true;
}

@Nullable
@Override
protected LivingEntity[] get(Event e) {
if (e instanceof AreaEffectCloudApplyEvent)
return ((AreaEffectCloudApplyEvent) e).getAffectedEntities().toArray(new LivingEntity[0]);
protected LivingEntity @Nullable [] get(Event event) {
if (event instanceof AreaEffectCloudApplyEvent areaEvent)
return areaEvent.getAffectedEntities().toArray(new LivingEntity[0]);
return null;
}

@Nullable
@Override
public Iterator<? extends LivingEntity> iterator(Event e) {
if (e instanceof AreaEffectCloudApplyEvent)
return ((AreaEffectCloudApplyEvent) e).getAffectedEntities().iterator();
return super.iterator(e);
public @Nullable Iterator<? extends LivingEntity> iterator(Event event) {
if (event instanceof AreaEffectCloudApplyEvent areaEvent)
return areaEvent.getAffectedEntities().iterator();
return super.iterator(event);
}

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

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (!(event instanceof AreaEffectCloudApplyEvent areaEvent))
return;

LivingEntity[] entities = (LivingEntity[]) delta;
switch (mode) {
case REMOVE:
for (LivingEntity entity : entities) {
areaEvent.getAffectedEntities().remove(entity);
}
break;
case SET:
areaEvent.getAffectedEntities().clear();
// FALLTHROUGH
case ADD:
for (LivingEntity entity : entities) {
areaEvent.getAffectedEntities().add(entity);
}
break;
case RESET, DELETE:
areaEvent.getAffectedEntities().clear();
break;
}
}

@Override
Expand All @@ -90,7 +125,7 @@ public Class<? extends LivingEntity> getReturnType() {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.skriptlang.skript.test.tests.syntaxes.expressions;

import ch.njol.skript.test.runner.SkriptJUnitTest;
import org.bukkit.Bukkit;
import org.bukkit.entity.AreaEffectCloud;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Pig;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ExprAffectedEntitiesTest extends SkriptJUnitTest {

private AreaEffectCloud cloud;
private Pig piggy;
private final List<LivingEntity> entityList = new ArrayList<>();

@Before
public void setUp() {
piggy = spawnTestPig();
entityList.add(piggy);
cloud = (AreaEffectCloud) getTestWorld().spawnEntity(getTestLocation(), EntityType.AREA_EFFECT_CLOUD);
}

@Test
public void callEvent() {
AreaEffectCloudApplyEvent event = new AreaEffectCloudApplyEvent(cloud, entityList);
Bukkit.getPluginManager().callEvent(event);
}

@After
public void cleanUp() {
if (piggy != null)
piggy.remove();
if (cloud != null)
cloud.remove();
}

}
27 changes: 27 additions & 0 deletions src/test/skript/junit/ExprAffectedEntities.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on load:
set {_tests::1} to "cleared piggies"
set {_tests::2} to "add 5 pigs"
set {_tests::3} to "remove 2 pigs"
set {_tests::4} to "set 4 pigs"

ensure junit test "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprAffectedEntitiesTest" completes {_tests::*}

on area effect:
set {_test} to "org.skriptlang.skript.test.tests.syntaxes.expressions.ExprAffectedEntitiesTest"
junit test is {_test}
clear affected entities
spawn 5 pigs at spawn of world "world":
add entity to {_pigs::*}
add {_pigs::*} to affected entities
if size of affected entities = 5:
complete objective "add 5 pigs" for junit test {_test}
remove (elements from 1 to 2 of {_pigs::*}) from affected entities
if size of affected entities = 3:
complete objective "remove 2 pigs" for junit test {_test}
set affected entities to (elements from 1 to 4 of {_pigs::*})
if size of affected entities = 4:
complete objective "set 4 pigs" for junit test {_test}
clear affected entities
if size of affected entities = 0:
complete objective "cleared piggies" for junit test {_test}
clear entities within {_pigs::*}

0 comments on commit a22e34e

Please sign in to comment.