Initial commit

This commit is contained in:
2022-09-17 10:49:36 +02:00
parent 1e8420a83e
commit 59a6e1c423
368 changed files with 26176 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
package eu.mhsl.minenet.minigames.shared.entity;
import eu.mhsl.minenet.minigames.instance.Spawnable;
import net.minestom.server.entity.EntityCreature;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.ai.target.ClosestEntityTarget;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
import net.minestom.server.event.player.PlayerEntityInteractEvent;
import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.timer.ExecutionType;
import net.minestom.server.timer.TaskSchedule;
import org.jetbrains.annotations.NotNull;
public class InteractableEntity extends EntityCreature {
/**
* Declares an Entity with direct callbacks on interaction
* @param entityType type of entity
*/
public InteractableEntity(@NotNull EntityType entityType) {
super(entityType);
eventNode()
.addListener(AddEntityToInstanceEvent.class, this::setInstanceEvent)
.addListener(AddEntityToInstanceEvent.class, this::onSpawn)
.addListener(RemoveEntityFromInstanceEvent.class, this::onDespawn)
.addListener(PlayerEntityInteractEvent.class, this::onInteract)
.addListener(EntityAttackEvent.class, this::onAttack);
}
private void setInstanceEvent(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {
if(addEntityToInstanceEvent.getInstance() instanceof Spawnable instance) {
scheduleNextTick((unused) -> {
this.lookAt(instance.getSpawn()); //TODO only works someitmes?
});
}
addEntityToInstanceEvent.getInstance().eventNode()
.addListener(PlayerEntityInteractEvent.class, playerEntityInteractEvent -> {
if(playerEntityInteractEvent.getTarget() == this) onInteract(playerEntityInteractEvent);
})
.addListener(EntityAttackEvent.class, entityAttackEvent -> {
if(entityAttackEvent.getTarget() == this) onAttack(entityAttackEvent);
})
.addListener(PlayerMoveEvent.class, playerMoveEvent -> {
//TODO this is heavy in production
//maybe store the player and update to the closest Entity only periodic
scheduler().submitTask(() -> {
setTarget(new ClosestEntityTarget(this, 5, entity -> entity instanceof Player).findTarget());
if(getTarget() != null) lookAt(getTarget());
return TaskSchedule.stop();
}, ExecutionType.ASYNC);
});
}
/**
* Called when instance of entity is set
* @param addEntityToInstanceEvent
*/
protected void onSpawn(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {
}
/**
* Called when instance of entity is unset
* @param removeEntityFromInstanceEvent
*/
protected void onDespawn(@NotNull RemoveEntityFromInstanceEvent removeEntityFromInstanceEvent) {
}
/**
* Called when a Player interacts with the entity
* @param playerEntityInteractEvent
*/
protected void onInteract(@NotNull PlayerEntityInteractEvent playerEntityInteractEvent) {
}
/**
* Called when a Player attacks the entity
* @param entityAttackEvent
*/
protected void onAttack(@NotNull EntityAttackEvent entityAttackEvent) {
}
}