Skip to content

Commit

Permalink
Port the BetterAI mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Mar 21, 2021
1 parent df13f63 commit bf56972
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void setup() {
// registerMechanic("CommandItems", org.enginehub.craftbook.mechanics.items.CommandItems.class, MechanicCategory.CUSTOMISATION);
// registerMechanic("CustomCrafting", org.enginehub.craftbook.mechanics.crafting.CustomCrafting.class, MechanicCategory.CUSTOMISATION);
// registerMechanic("CustomDrops", org.enginehub.craftbook.mechanics.drops.CustomDrops.class, MechanicCategory.CUSTOMISATION);
// registerMechanic("BetterAi", org.enginehub.craftbook.mechanics.AIMechanic.class, MechanicCategory.GENERAL);
// registerMechanic("TreeLopper", org.enginehub.craftbook.mechanics.TreeLopper.class, MechanicCategory.GENERAL);
// registerMechanic("XpStorer", org.enginehub.craftbook.mechanics.XPStorer.class, MechanicCategory.GENERAL);
// registerMechanic("CommandSigns", org.enginehub.craftbook.mechanics.CommandSigns.class, MechanicCategory.GENERAL);
Expand Down Expand Up @@ -381,6 +380,15 @@ public void setup() {
.category(MechanicCategory.GENERAL)
.buildAndRegister();

MechanicType.Builder
.create()
.id("better_ai")
.name("BetterAI")
.description(TranslatableComponent.of("craftbook.betterai.description"))
.className("org.enginehub.craftbook.mechanics.betterai.BetterAI")
.category(MechanicCategory.GENERAL)
.buildAndRegister();

// TODO CommandItems needs to load early (after variables).
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.enginehub.craftbook.mechanic;

import org.enginehub.craftbook.mechanics.AIMechanic;
import org.enginehub.craftbook.mechanics.betterai.BetterAI;
import org.enginehub.craftbook.mechanics.Ammeter;
import org.enginehub.craftbook.mechanics.BetterLeads;
import org.enginehub.craftbook.mechanics.BetterPhysics;
Expand Down Expand Up @@ -91,7 +91,7 @@ public class MechanicTypes {
@Nullable
public static final MechanicType<Ammeter> AMMETER = get("ammeter");
@Nullable
public static final MechanicType<AIMechanic> BETTER_AI = get("better_ai");
public static final MechanicType<BetterAI> BETTER_AI = get("better_ai");
@Nullable
public static final MechanicType<BetterLeads> BETTER_LEADS = get("better_leads");
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.enginehub.craftbook.mechanics;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.entity.EntityTypes;
Expand All @@ -42,8 +44,7 @@
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.util.EventUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class BetterLeads extends AbstractCraftBookMechanic {

Expand All @@ -69,15 +70,9 @@ public void onPlayerClick(final PlayerInteractEntityEvent event) {

CraftBookPlugin.logDebugMessage("It is of type: " + typeName, "betterleads.allowed-mobs");

boolean found = false;
for (String type : allowedMobs) {
if (type.equalsIgnoreCase(typeName)) {
found = true;
break;
}
}

if (!found) {
if (!allowedMobs.contains(typeName)
|| !typeName.startsWith("minecraft:")
|| !allowedMobs.contains(typeName.substring("minecraft:".length()))) {
return;
}

Expand Down Expand Up @@ -226,7 +221,7 @@ public void onUnleash(PlayerUnleashEntityEvent event) {
private boolean ownerBreakOnly;
private boolean persistentHitch;
private boolean mobRepellant;
private List<String> allowedMobs;
private Set<String> allowedMobs;

@Override
public void loadFromConfiguration(YAMLProcessor config) {
Expand All @@ -243,6 +238,6 @@ public void loadFromConfiguration(YAMLProcessor config) {
mobRepellant = config.getBoolean("mob-repel", false);

config.setComment("allowed-mobs", "The list of mobs that can be tethered with a lead.");
allowedMobs = config.getStringList("allowed-mobs", Arrays.asList(EntityTypes.ZOMBIE.getId(), EntityTypes.SPIDER.getId()));
allowedMobs = ImmutableSet.copyOf(config.getStringList("allowed-mobs", Lists.newArrayList(EntityTypes.ZOMBIE.getId(), EntityTypes.SPIDER.getId())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.bukkit.Material;
Expand Down Expand Up @@ -305,7 +305,7 @@ public void onThink(SelfTriggerThinkEvent event) {

private boolean requireBottle;
private int xpPerBottle;
private BlockStateHolder block;
private BaseBlock block;
private TernaryState sneakingState;
private boolean autonomousMode;
private int radius;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* CraftBook Copyright (C) me4502 <https://matthewmiller.dev/>
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/

package org.enginehub.craftbook.mechanics.betterai;

import com.destroystokyo.paper.entity.ai.Goal;
import com.destroystokyo.paper.entity.ai.GoalKey;
import com.destroystokyo.paper.entity.ai.GoalType;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.EnumSet;

class AttackPassiveGoal implements Goal<Monster> {
private static final GoalKey<Monster> key = GoalKey.of(
Monster.class,
new NamespacedKey("craftbook", "attack_passive")
);

private final Monster monster;
private LivingEntity target;

public AttackPassiveGoal(Monster monster) {
this.monster = monster;
}

@Override
public boolean shouldActivate() {
return !monster.isDead();
}

@Override
public void start() {
tick();
}

@Override
public void stop() {
this.target = null;
monster.setTarget(null);
}

@Override
public void tick() {
if (target != null && !target.isDead()) {
if (target.getWorld().equals(monster.getWorld())
&& target.getLocation().distanceSquared(monster.getLocation()) < 15*15) {
return;
}
}

Animals closest = null;
double closestDist = Double.MAX_VALUE;

for (Entity ent : monster.getNearbyEntities(10D, 10D, 10D)) {
if (ent instanceof Animals && monster.hasLineOfSight(ent)) {
double dist = ent.getLocation().distanceSquared(monster.getLocation());
if (dist < closestDist) {
closest = (Animals) ent;
closestDist = dist;
}
}
}

if (closest != null) {
target = closest;
monster.setTarget(this.target);
CraftBookPlugin.logDebugMessage("Setting target to entity: " + closest.getType().name(), "ai-mechanics.entity-target.attack-passive");
}
}

@Override
public @NotNull GoalKey<Monster> getKey() {
return key;
}

@Override
public @NotNull EnumSet<GoalType> getTypes() {
return EnumSet.of(GoalType.TARGET);
}
}
Loading

0 comments on commit bf56972

Please sign in to comment.