Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Target command and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Jan 16, 2018
1 parent 8375084 commit 9f771ec
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
Expand Up @@ -137,6 +137,7 @@ public void onServerStart(GamePreInitializationEvent event) {
Denizen2Core.register(new PrimeCommand());
Denizen2Core.register(new RemoveCommand());
Denizen2Core.register(new SpawnCommand());
Denizen2Core.register(new TargetCommand());
Denizen2Core.register(new TeleportCommand());
Denizen2Core.register(new UnflagCommand());
// Commands: Item
Expand Down
Expand Up @@ -20,6 +20,7 @@ public class DefuseCommand extends AbstractCommand {
// @Maximum 1
// @Description
// Defuses an explosive entity. Inverse of <@link command prime>prime<@/link>.
// @Warning This command does not work in Sponge during last testing.
// @Example
// # This example defuses the creeper the player is looking at.
// - defuse <player.target_entities[type:creeper].get[1]>
Expand Down
Expand Up @@ -20,6 +20,7 @@ public class PrimeCommand extends AbstractCommand {
// @Maximum 1
// @Description
// Primes an explosive entity. Inverse of <@link command defuse>defuse<@/link>.
// @Warning This command does not work in Sponge during last testing.
// @Example
// # This example primes the creeper the player is looking at.
// - prime <player.target_entities[type:creeper].get[1]>
Expand Down
@@ -0,0 +1,58 @@
package com.denizenscript.denizen2sponge.commands.entity;

import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.EntityTag;
import org.spongepowered.api.entity.living.Agent;

public class TargetCommand extends AbstractCommand {

// <--[command]
// @Since 0.4.0
// @Name target
// @Arguments <entity> <target entity>
// @Short sets the target entity of an entity's AI.
// @Updated 2018/01/16
// @Group Entity
// @Minimum 2
// @Maximum 2
// @Description
// Sets the target entity of an entity's AI. This overrides the AI's target selector.
// @Example
// # This example makes an iron golem in front of the player attack him.
// - target <player.target_entities[type:iron_golem].get[1]> <player>
// -->

@Override
public String getName() {
return "target";
}

@Override
public String getArguments() {
return "<entity> <target entity>";
}

@Override
public int getMinimumArguments() {
return 2;
}

@Override
public int getMaximumArguments() {
return 2;
}

@Override
public void execute(CommandQueue queue, CommandEntry entry) {
EntityTag ent = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
EntityTag target = EntityTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
((Agent) ent.getInternal()).setTarget(target.getInternal());
if (queue.shouldShowGood()) {
queue.outGood("Entity '" + ColorSet.emphasis + ent.debug() + ColorSet.good
+ "' is now targeting entity '" + ColorSet.emphasis + target.debug() + ColorSet.good + "'!");
}
}
}
Expand Up @@ -24,13 +24,13 @@
import org.spongepowered.api.entity.hanging.LeashHitch;
import org.spongepowered.api.entity.hanging.Painting;
import org.spongepowered.api.entity.living.Ageable;
import org.spongepowered.api.entity.living.Agent;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.projectile.Projectile;
import org.spongepowered.api.entity.projectile.source.BlockProjectileSource;
import org.spongepowered.api.entity.projectile.source.ProjectileSource;
import org.spongepowered.api.entity.vehicle.Boat;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Carrier;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.Color;
Expand Down Expand Up @@ -810,7 +810,7 @@ else if (ent instanceof Ageable) {
// If no distance is specified, the default hand-reach distance is used.
// -->
handlers.put("target_block", (dat, obj) -> {
Entity ent = ((EntityTag) obj).getInternal();
Entity ent = ((EntityTag) obj).internal;
return new LocationTag(BlockRay.from(ent)
.stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1))
.distanceLimit(dat.hasNextModifier() ? NumberTag.getFor(dat.error, dat.getNextModifier()).getInternal() :
Expand All @@ -826,7 +826,7 @@ else if (ent instanceof Ageable) {
// If no distance is specified, the default hand-reach distance is used.
// -->
handlers.put("precise_target_location", (dat, obj) -> {
Entity ent = ((EntityTag) obj).getInternal();
Entity ent = ((EntityTag) obj).internal;
BlockRayHit hit = BlockRay.from(ent).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1))
.distanceLimit(dat.hasNextModifier() ? NumberTag.getFor(dat.error, dat.getNextModifier()).getInternal() :
(Utilities.getHandReach(ent))).build().end().get();
Expand All @@ -842,7 +842,7 @@ else if (ent instanceof Ageable) {
// If no distance is specified, the default hand-reach distance is used.
// -->
handlers.put("precise_target_normal", (dat, obj) -> {
Entity ent = ((EntityTag) obj).getInternal();
Entity ent = ((EntityTag) obj).internal;
return new LocationTag(BlockRay.from(ent)
.stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1))
.distanceLimit(dat.hasNextModifier() ? NumberTag.getFor(dat.error, dat.getNextModifier()).getInternal() :
Expand All @@ -859,7 +859,7 @@ else if (ent instanceof Ageable) {
// Input is type:<EntityTypeTag>|range:<NumberTag>
// -->
handlers.put("target_entities", (dat, obj) -> {
Entity ent = ((EntityTag) obj).getInternal();
Entity ent = ((EntityTag) obj).internal;
ListTag list = new ListTag();
MapTag map = MapTag.getFor(dat.error, dat.getNextModifier());
EntityTypeTag requiredTypeTag = null;
Expand Down Expand Up @@ -903,8 +903,38 @@ else if (ent instanceof Ageable) {
// @Group Explosive Entities
// @ReturnType BooleanTag
// @Returns whether an explosive entity is primed.
// @Warning This tag always returns false in Sponge during last testing.
// -->
handlers.put("is_primed", (dat, obj) -> new BooleanTag(((FusedExplosive) ((EntityTag) obj).internal).isPrimed()));
// <--[tag]
// @Since 0.4.0
// @Name EntityTag.has_ai
// @Updated 2018/01/16
// @Group Entity Behavior
// @ReturnType BooleanTag
// @Returns whether an entity has ai enabled.
// -->
handlers.put("has_ai", (dat, obj) -> new BooleanTag(((Agent) ((EntityTag) obj).internal).aiEnabled().get()));
// <--[tag]
// @Since 0.4.0
// @Name EntityTag.ai_target
// @Updated 2018/01/16
// @Group Entity Behavior
// @ReturnType EntityTag
// @Returns the entity that is being targeted by this entity's AI, if any.
// -->
handlers.put("ai_target", (dat, obj) -> {
Optional<Entity> opt = ((Agent) ((EntityTag) obj).internal).getTarget();
if (opt.isPresent()) {
return new EntityTag(opt.get());
}
else {
if (!dat.hasFallback()) {
dat.error.run("This entity doesn't have target!");
}
return new NullTag();
}
});
}

public static EntityTag getFor(Action<String> error, String text) {
Expand Down

0 comments on commit 9f771ec

Please sign in to comment.