hollow-cube/generic-projectile-generator-for-ranged-attack (#9)

* general ProjectileGenerator for RangedAttackGoal

Let the users create any Entity as projectile (not just EntityProjectile)

* disable server address tests in github actions

---------

Co-authored-by: mworzala <mattheworzala@gmail.com>
This commit is contained in:
Huynh Tien 2023-05-07 17:10:50 +07:00 committed by GitHub
parent 381d89497a
commit d411c271cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.entity.ai.goal;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.EntityProjectile;
@ -26,7 +27,7 @@ public class RangedAttackGoal extends GoalSelector {
private final double power;
private final double spread;
private Function<Entity, EntityProjectile> projectileGenerator;
private ProjectileGenerator projectileGenerator;
private boolean stop;
private Entity cachedTarget;
@ -69,10 +70,25 @@ public class RangedAttackGoal extends GoalSelector {
return this.cooldown;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
public void setProjectileGenerator(ProjectileGenerator projectileGenerator) {
this.projectileGenerator = projectileGenerator;
}
public void setProjectileGenerator(Function<Entity, EntityProjectile> projectileGenerator) {
this.projectileGenerator = (shooter, target, pow, spr) -> {
EntityProjectile projectile = projectileGenerator.apply(shooter);
projectile.setInstance(shooter.getInstance(), shooter.getPosition().add(0D, shooter.getEyeHeight(), 0D));
projectile.shoot(target, pow, spr);
};
}
private ProjectileGenerator getProjectileGeneratorOrDefault() {
if (projectileGenerator == null) {
setProjectileGenerator(shooter -> new EntityProjectile(shooter, EntityType.ARROW));
}
return projectileGenerator;
}
@Override
public boolean shouldStart() {
this.cachedTarget = findTarget();
@ -103,15 +119,8 @@ public class RangedAttackGoal extends GoalSelector {
if (!Cooldown.hasCooldown(time, this.lastShot, this.delay)) {
if (this.entityCreature.hasLineOfSight(target)) {
final var to = target.getPosition().add(0D, target.getEyeHeight(), 0D);
this.getProjectileGeneratorOrDefault().shootProjectile(this.entityCreature, to, this.power, this.spread);
Function<Entity, EntityProjectile> projectileGenerator = this.projectileGenerator;
if (projectileGenerator == null) {
projectileGenerator = shooter -> new EntityProjectile(shooter, EntityType.ARROW);
}
EntityProjectile projectile = projectileGenerator.apply(this.entityCreature);
projectile.setInstance(this.entityCreature.getInstance(), this.entityCreature.getPosition().add(0D, this.entityCreature.getEyeHeight(), 0D));
projectile.shoot(to, this.power, this.spread);
this.lastShot = time;
} else {
comeClose = this.comeClose;
@ -146,4 +155,19 @@ public class RangedAttackGoal extends GoalSelector {
// Stop following the target
this.entityCreature.getNavigator().setPathTo(null);
}
/**
* The function used to generate a projectile.
*/
public interface ProjectileGenerator {
/**
* Shoots a projectile.
*
* @param shooter the shooter.
* @param target the target position.
* @param power the shot power.
* @param spread the shot spread.
*/
void shootProjectile(EntityCreature shooter, Pos target, double power, double spread);
}
}

View File

@ -9,11 +9,15 @@ import java.net.UnixDomainSocketAddress;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class ServerAddressTest {
@Test
public void inetAddressTest() throws IOException {
// These like to fail on github actions
assumeTrue(System.getenv("GITHUB_ACTIONS") == null);
InetSocketAddress address = new InetSocketAddress("localhost", 25565);
var server = new Server(new PacketProcessor());
server.init(address);
@ -27,6 +31,9 @@ public class ServerAddressTest {
@Test
public void inetAddressDynamicTest() throws IOException {
// These like to fail on github actions
assumeTrue(System.getenv("GITHUB_ACTIONS") == null);
InetSocketAddress address = new InetSocketAddress("localhost", 0);
var server = new Server(new PacketProcessor());
server.init(address);
@ -40,6 +47,9 @@ public class ServerAddressTest {
@Test
public void unixAddressTest() throws IOException {
// These like to fail on github actions
assumeTrue(System.getenv("GITHUB_ACTIONS") == null);
UnixDomainSocketAddress address = UnixDomainSocketAddress.of("minestom.sock");
var server = new Server(new PacketProcessor());
server.init(address);