Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow FarmControl to affect non-living entities with 'remove' Action.… #38

Merged
merged 6 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -48,7 +50,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.getEntities()) {
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
@@ -1,7 +1,9 @@
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;
Expand All @@ -11,7 +13,10 @@
import org.bukkit.World;
import org.bukkit.entity.Entity;

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

public class FarmController {
private final FarmControl farmControl;
Expand Down Expand Up @@ -81,7 +86,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,5 +1,6 @@
package com.froobworld.farmcontrol.controller.action;

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

public abstract class Action {
Expand Down Expand Up @@ -34,7 +35,7 @@ public boolean undoOnUnload() {
return undoOnUnload;
}

froobynooby marked this conversation as resolved.
Show resolved Hide resolved
public abstract void doAction(Mob mob);
public abstract void doAction(Entity entity);

public abstract void undoAction(Mob mob);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 {
Expand All @@ -12,8 +13,12 @@ public DisableBreedingAction(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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.froobworld.farmcontrol.controller.action;

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

public class DisableCollisionsAction extends Action {
Expand All @@ -9,8 +10,12 @@ public DisableCollisionsAction() {
}

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

ent.setCollidable(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.froobworld.farmcontrol.controller.action;

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

public class KillAction extends Action {
Expand All @@ -9,8 +10,12 @@ public KillAction() {
}

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

ent.setHealth(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.froobworld.farmcontrol.controller.action;

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

public class RemoveAction extends Action {
Expand All @@ -9,8 +10,8 @@ public RemoveAction() {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.froobworld.farmcontrol.controller.action;

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

Expand All @@ -10,7 +11,10 @@ public RemoveAiAction() {
}

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

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

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

public class RemoveAwarenessAction extends Action {
Expand All @@ -9,7 +10,10 @@ public RemoveAwarenessAction() {
}

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

Expand Down
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 Down Expand Up @@ -33,7 +34,11 @@ public RemoveRandomMovementAction() {
}

@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 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 @@ -19,19 +19,21 @@ public class SnapshotEntity {
private final boolean customName;
private final boolean tamed;
private final boolean isPatrolLeader;
private final boolean isMob;
froobynooby marked this conversation as resolved.
Show resolved Hide resolved
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();
this.isPatrolLeader = entity instanceof Raider && ((Raider) entity).isPatrolLeader();
this.isMob = entity instanceof Mob;
this.ticksLived = entity.getTicksLived();
classifications.add(entity.getType());
if (entity instanceof Colorable) {
Expand All @@ -45,7 +47,7 @@ public SnapshotEntity(Mob entity) {
}
}

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

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

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

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

public boolean isMob() {
return isMob;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.froobworld.farmcontrol.hook.scheduler.ScheduledTask;
import com.froobworld.farmcontrol.hook.scheduler.SchedulerHook;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

import java.util.Map;
Expand All @@ -32,7 +33,7 @@ public ActionPerformTask(World world, SchedulerHook schedulerHook, Map<SnapshotE
public void run() {
CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
for (SnapshotEntity snapshotEntity : triggerActionMap.keySet()) {
Mob entity = snapshotEntity.getEntity();
Entity entity = snapshotEntity.getEntity();
CompletableFuture<Void> entityFuture = new CompletableFuture<>();
ScheduledTask scheduledTask = schedulerHook.runEntityTaskAsap(() -> {
try {
Expand All @@ -57,9 +58,9 @@ public void run() {
}
}
for (SnapshotEntity snapshotEntity : unTriggerActionMap.keySet()) {
Mob entity = snapshotEntity.getEntity();
Entity entity = snapshotEntity.getEntity();
schedulerHook.runEntityTaskAsap(() -> {
if (!entity.isValid()) {
if (!(entity instanceof Mob mob) || !entity.isValid()) {
froobynooby marked this conversation as resolved.
Show resolved Hide resolved
return;
}
FcData fcData = snapshotEntity.getFcData();
Expand All @@ -69,7 +70,7 @@ public void run() {
Set<TriggerActionPair> triggerActionPairs = unTriggerActionMap.get(snapshotEntity);
for (TriggerActionPair triggerActionPair : triggerActionPairs) {
if (fcData.remove(triggerActionPair.trigger, triggerActionPair.action)) {
triggerActionPair.action.undoAction(entity);
triggerActionPair.action.undoAction(mob);
}
}
fcData.save(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.froobworld.farmcontrol.hook.scheduler.ScheduledTask;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Mob;

import java.util.*;
Expand Down Expand Up @@ -62,7 +64,7 @@ public void run() {
List<SnapshotEntity> snapshotEntities = new ArrayList<>();
CompletableFuture<Void> completableFuture = CompletableFuture.completedFuture(null);
if (!profilesToRun.isEmpty() || !untriggerStrategyMap.isEmpty()) {
for (Mob entity : world.getEntitiesByClass(Mob.class)) {
for (Entity entity : world.getEntities()) {
froobynooby marked this conversation as resolved.
Show resolved Hide resolved
CompletableFuture<Void> entityFuture = new CompletableFuture<>();
ScheduledTask scheduledTask = farmControl.getHookManager().getSchedulerHook().runEntityTaskAsap(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity;
import com.froobworld.farmcontrol.data.FcData;
import com.froobworld.farmcontrol.hook.scheduler.SchedulerHook;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;

import java.util.Map;
Expand All @@ -21,9 +22,9 @@ public UntriggerPerformTask(SchedulerHook schedulerHook, Map<SnapshotEntity, Set
@Override
public void run() {
for (SnapshotEntity snapshotEntity : actionsToUndo.keySet()) {
Mob entity = snapshotEntity.getEntity();
Entity entity = snapshotEntity.getEntity();
schedulerHook.runEntityTaskAsap(() -> {
if (!entity.isValid()) {
if (!(entity instanceof Mob mob) || !entity.isValid()) {
return;
}
FcData fcData = FcData.get(entity);
Expand All @@ -32,7 +33,7 @@ public void run() {
}
for (TriggerActionPair triggerActionPair : actionsToUndo.get(snapshotEntity)) {
if (fcData.remove(triggerActionPair.trigger, triggerActionPair.action)) {
triggerActionPair.action.undoAction(entity);
triggerActionPair.action.undoAction(mob);
}
}
fcData.save(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static Predicate<SnapshotEntity> fromString(String string) {
return entity -> Tameable.class.isAssignableFrom(entity.getEntityClass());
} else if (category.equalsIgnoreCase("raider")) {
return entity -> Raider.class.isAssignableFrom(entity.getEntityClass());
} else if (category.equalsIgnoreCase("vehicle")) {
return entity -> Vehicle.class.isAssignableFrom(entity.getEntityClass());
} else if (category.equalsIgnoreCase("item")) {
froobynooby marked this conversation as resolved.
Show resolved Hide resolved
return entity -> Item.class.isAssignableFrom(entity.getEntityClass());
}
}
return entity -> entity.getEntityType().toString().equalsIgnoreCase(string);
Expand Down