Skip to content

Commit

Permalink
Allow FarmControl to affect non-living entities with 'remove' Action.… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco authored Feb 17, 2024
1 parent 07d009c commit 4727b23
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 69 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ repositories {
maven {
url = 'https://repo.codemc.org/repository/maven-public/'
}
maven {
url = 'https://jitpack.io'
}
}

dependencies {
Expand All @@ -35,7 +38,7 @@ dependencies {
compileOnly 'dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT'
compileOnly 'me.clip:placeholderapi:2.10.9'
implementation 'org.jooq:joor-java-8:0.9.14'
implementation 'com.froobworld:nab-configuration:1.0.2'
implementation 'com.github.froobynooby:nab-configuration:master-SNAPSHOT' //'com.froobworld:nab-configuration:1.0.2'
implementation 'org.bstats:bstats-bukkit:2.2.1'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.froobworld.farmcontrol.command;

import com.froobworld.farmcontrol.FarmControl;
import com.froobworld.farmcontrol.controller.FarmController;
import com.froobworld.farmcontrol.data.FcData;
import com.froobworld.farmcontrol.hook.scheduler.ScheduledTask;
import org.bukkit.Bukkit;
Expand All @@ -9,8 +10,7 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
Expand Down Expand Up @@ -48,7 +48,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
Map<String, AtomicInteger> actionCount = new HashMap<>();

CompletableFuture<Void> completableFuture = CompletableFuture.completedFuture(null);
for (LivingEntity entity : world.getLivingEntities()) {
for (Entity entity : world.getEntitiesByClasses(FarmController.ENTITY_CLASSES)) {
entityCount.incrementAndGet();
CompletableFuture<Void> entityFuture = new CompletableFuture();
ScheduledTask scheduledTask = farmControl.getHookManager().getSchedulerHook().runEntityTaskAsap(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void onEntityDamage(EntityDamageEvent event) {

@EventHandler
public void onEntityTarget(EntityTargetLivingEntityEvent event) {
if (event.getTarget() == null) {
return;
}
Actioner.undoActions(event.getTarget(), action -> {
return farmControl.getFcConfig().worldSettings.of(event.getEntity().getWorld()).actionSettings.undoOn.of(action).target.get();
}, farmControl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.froobworld.farmcontrol.controller;

import com.froobworld.farmcontrol.FarmControl;
import com.froobworld.farmcontrol.controller.task.*;
import com.froobworld.farmcontrol.controller.task.ActionPerformTask;
import com.froobworld.farmcontrol.controller.task.TriggerCheckTask;
import com.froobworld.farmcontrol.controller.task.UntriggerPerformTask;
import com.froobworld.farmcontrol.controller.tracker.CycleHistoryManager;
import com.froobworld.farmcontrol.controller.trigger.Trigger;
import com.froobworld.farmcontrol.hook.scheduler.RegionisedSchedulerHook;
import com.froobworld.farmcontrol.hook.scheduler.ScheduledTask;
import com.froobworld.farmcontrol.utils.Actioner;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.*;

import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.List;

public class FarmController {
public static final Class<?>[] ENTITY_CLASSES = List.of(Mob.class, Vehicle.class, Projectile.class, Item.class).toArray(new Class[0]);
private final FarmControl farmControl;
private final CycleHistoryManager cycleHistoryManager;
private final Map<World, Map<Trigger, Set<ActionProfile>>> worldTriggerProfilesMap = new HashMap<>();
Expand Down Expand Up @@ -81,7 +88,7 @@ public void addWorld(World world) {

public void removeWorld(World world) {
worldTriggerProfilesMap.remove(world);
for (Entity entity : world.getLivingEntities()) {
for (Entity entity : world.getEntities()) {
farmControl.getHookManager().getSchedulerHook().runEntityTaskAsap(
() -> Actioner.undoAllActions(entity, farmControl),
null, entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Mob;
import org.bukkit.entity.Entity;

public abstract class Action {
private final String name;
private final Class<?> entityClass;
private final boolean removes;
private final boolean persistent;
private final boolean undoOnUnload;

public Action(String name, boolean removes, boolean persistent, boolean undoOnUnload) {
public Action(String name, Class<?> entityClass, boolean removes, boolean persistent, boolean undoOnUnload) {
if (!name.matches("[a-z-]+")) {
throw new IllegalArgumentException("Name must match [a-z-]+");
}
this.entityClass = entityClass;
this.name = name;
this.removes = removes;
this.persistent = persistent;
Expand All @@ -34,7 +36,11 @@ public boolean undoOnUnload() {
return undoOnUnload;
}

public abstract void doAction(Mob mob);
public abstract void doAction(Entity entity);

public abstract void undoAction(Mob mob);
public abstract void undoAction(Entity entity);

public Class<?> getEntityClass() {
return entityClass;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.froobworld.farmcontrol.controller.action;

import com.froobworld.farmcontrol.controller.breeding.BreedingBlocker;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

public class DisableBreedingAction extends Action {
private final BreedingBlocker breedingBlocker;

public DisableBreedingAction(BreedingBlocker breedingBlocker) {
super("disable-breeding", false, true, false);
super("disable-breeding", Mob.class, false, true, false);
this.breedingBlocker = breedingBlocker;
}

@Override
public void doAction(Mob mob) {
breedingBlocker.setBreedingDisabled(mob, true);
public void doAction(Entity entity) {
if (!(entity instanceof Mob)) {
return;
}
breedingBlocker.setBreedingDisabled(entity, true);
}

@Override
public void undoAction(Mob mob) {
public void undoAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
breedingBlocker.setBreedingDisabled(mob, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

public class DisableCollisionsAction extends Action {

public DisableCollisionsAction() {
super("disable-collisions", false, false, true);
super("disable-collisions", Mob.class, false, false, true);
}

@Override
public void doAction(Mob mob) {
public void doAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}

mob.setCollidable(false);
}

@Override
public void undoAction(Mob mob) {
public void undoAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setCollidable(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

public class KillAction extends Action {

public KillAction() {
super("kill", true, false, false);
super("kill", Mob.class, true, false, false);
}

@Override
public void doAction(Mob mob) {
public void doAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setHealth(0);
}

@Override
public void undoAction(Mob mob) {}
public void undoAction(Entity entity) {}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Mob;
import org.bukkit.entity.Entity;

public class RemoveAction extends Action {

public RemoveAction() {
super("remove", true, false, false);
super("remove", Entity.class, true, false, false);
}

@Override
public void doAction(Mob mob) {
mob.remove();
public void doAction(Entity entity) {
entity.remove();
}

@Override
public void undoAction(Mob mob) {}
public void undoAction(Entity entity) {}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.bukkit.util.Vector;

public class RemoveAiAction extends Action {

public RemoveAiAction() {
super("remove-ai", false, true, true);
super("remove-ai", Mob.class, false, true, true);
}

@Override
public void doAction(Mob mob) {
public void doAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setAI(false);
}

@Override
public void undoAction(Mob mob) {
public void undoAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setAI(true);
mob.setVelocity(new Vector(0, 0, 0));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.froobworld.farmcontrol.controller.action;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

public class RemoveAwarenessAction extends Action {

public RemoveAwarenessAction() {
super("remove-awareness", false, true, true);
super("remove-awareness", Mob.class, false, true, true);
}

@Override
public void doAction(Mob mob) {
public void doAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setAware(false);
}

@Override
public void undoAction(Mob mob) {
public void undoAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
mob.setAware(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.froobworld.farmcontrol.utils.NmsUtils;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Sets;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

import java.util.*;
Expand All @@ -29,11 +30,15 @@ public static void cleanUp() {
}

public RemoveRandomMovementAction() {
super("remove-random-movement", false, false, true);
super("remove-random-movement", Mob.class, false, false, true);
}

@Override
public void doAction(Mob mob) {
public void doAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}

Object entityObject = on(mob).call("getHandle").get();
Set<?> wrappedGoals = on(entityObject)
.field(NmsUtils.GoalSelectorHelper.getGoalSelectorFieldName())
Expand All @@ -55,7 +60,10 @@ public void doAction(Mob mob) {
}

@Override
public void undoAction(Mob mob) {
public void undoAction(Entity entity) {
if (!(entity instanceof Mob mob)) {
return;
}
Object entityObject = on(mob).call("getHandle").get();
Set<Object> wrappedGoals = on(entityObject)
.field(NmsUtils.GoalSelectorHelper.getGoalSelectorFieldName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

public class SnapshotEntity {
private final Mob entity;
private final Entity entity;
private final int entityId;
private final Vector location;
private final FcData fcData;
Expand All @@ -22,12 +22,12 @@ public class SnapshotEntity {
private final int ticksLived;
private final List<Object> classifications = new ArrayList<>();

public SnapshotEntity(Mob entity) {
public SnapshotEntity(Entity entity) {
this.entity = entity;
this.entityId = entity.getEntityId();
this.location = entity.getLocation().toVector();
this.fcData = FcData.get(entity);
this.leashed = entity.isLeashed();
this.leashed = entity instanceof Mob && ((Mob) entity).isLeashed();
this.loveMode = entity instanceof Animals && ((Animals) entity).isLoveMode();
this.customName = entity.getCustomName() != null;
this.tamed = entity instanceof Tameable && ((Tameable) entity).isTamed();
Expand All @@ -45,7 +45,7 @@ public SnapshotEntity(Mob entity) {
}
}

public Class<? extends Mob> getEntityClass() {
public Class<? extends Entity> getEntityClass() {
return entity.getClass();
}

Expand All @@ -65,7 +65,7 @@ public FcData getFcData() {
return fcData;
}

public Mob getEntity() {
public Entity getEntity() {
return entity;
}

Expand Down Expand Up @@ -100,4 +100,5 @@ public int getTicksLived() {
public List<Object> getClassifications() {
return classifications;
}

}
Loading

0 comments on commit 4727b23

Please sign in to comment.