Skip to content

Commit

Permalink
minor fixes (rename meta, excess newlines, attach unspawned error)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 15, 2020
1 parent 197a66b commit 5e69a1f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 75 deletions.
Expand Up @@ -165,6 +165,10 @@ public void execute(final ScriptEntry scriptEntry) {
}
};
for (EntityTag entity : entities) {
if (!entity.isSpawned() && !shouldCancel) {
Debug.echoError("Cannot attach entity '" + entity + "': entity is not spawned.");
continue;
}
if (forPlayers == null) {
procPlayer.accept(entity, null);
}
Expand Down
Expand Up @@ -33,15 +33,15 @@ public class RenameCommand extends AbstractCommand {

public RenameCommand() {
setName("rename");
setSyntax("rename [<name>] (t:<entity>|...) (per_player) (for:<player>|...)");
setSyntax("rename [<name>/cancel] (t:<entity>|...) (per_player) (for:<player>|...)");
setRequiredArguments(1, 4);
setParseArgs(false);
isProcedural = false;
}

// <--[command]
// @Name Rename
// @Syntax rename [<name>] (t:<entity>|...) (per_player) (for:<player>|...)
// @Syntax rename [<name>/cancel] (t:<entity>|...) (per_player) (for:<player>|...)
// @Required 1
// @Maximum 4
// @Short Renames the linked NPC or list of entities.
Expand All @@ -68,11 +68,11 @@ public RenameCommand() {
// <NPCTag.nickname>
//
// @Usage
// Use to rename the linked NPC.
// Use to rename the linked NPC to 'Bob'.
// - rename Bob
//
// @Usage
// Use to rename a different NPC.
// Use to rename a different NPC to 'Bob'.
// - rename Bob t:<[some_npc]>
//
// @Usage
Expand Down
Expand Up @@ -95,9 +95,7 @@ public ShootCommand() {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("origin")
&& arg.matchesPrefix("origin", "o", "source", "s")) {

Expand All @@ -114,25 +112,21 @@ else if (arg.matchesArgumentType(LocationTag.class)) {
else if (!scriptEntry.hasObject("destination")
&& arg.matchesArgumentType(LocationTag.class)
&& arg.matchesPrefix("destination", "d")) {

scriptEntry.addObject("destination", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("lead")
&& arg.matchesArgumentType(LocationTag.class)
&& arg.matchesPrefix("lead")) {

scriptEntry.addObject("lead", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesFloat()
&& arg.matchesPrefix("height", "h")) {

scriptEntry.addObject("height", arg.asElement());
}
else if (!scriptEntry.hasObject("speed")
&& arg.matchesFloat()
&& arg.matchesPrefix("speed")) {

scriptEntry.addObject("speed", arg.asElement());
}
else if (!scriptEntry.hasObject("script")
Expand All @@ -147,15 +141,12 @@ else if (!scriptEntry.hasObject("shooter")
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {

scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}

// Don't document this argument; it is for debug purposes only
else if (!scriptEntry.hasObject("gravity")
&& arg.matchesFloat()
&& arg.matchesPrefix("gravity", "g")) {

scriptEntry.addObject("gravity", arg.asElement());
}
else if (!scriptEntry.hasObject("spread")
Expand All @@ -174,24 +165,16 @@ else if (arg.matchesPrefix("def", "define", "context")) {
arg.reportUnhandled();
}
}

// Use the NPC or player's locations as the origin if one is not specified

if (!scriptEntry.hasObject("origin_location")) {

scriptEntry.defaultObject("origin_entity",
Utilities.entryHasNPC(scriptEntry) ? Utilities.getEntryNPC(scriptEntry).getDenizenEntity() : null,
Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getDenizenEntity() : null);
}

scriptEntry.defaultObject("height", new ElementTag(3));

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}

if (!scriptEntry.hasObject("origin_entity") && !scriptEntry.hasObject("origin_location")) {
throw new InvalidArgumentsException("Must specify an origin location!");
}
Expand All @@ -200,14 +183,12 @@ else if (arg.matchesPrefix("def", "define", "context")) {
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {

EntityTag originEntity = scriptEntry.getObjectTag("origin_entity");
LocationTag originLocation = scriptEntry.hasObject("origin_location") ?
(LocationTag) scriptEntry.getObject("origin_location") :
new LocationTag(originEntity.getEyeLocation()
.add(originEntity.getEyeLocation().getDirection()));
boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();

// If there is no destination set, but there is a shooter, get a point
// in front of the shooter and set it as the destination
final LocationTag destination = scriptEntry.hasObject("destination") ?
Expand All @@ -216,27 +197,22 @@ public void execute(final ScriptEntry scriptEntry) {
.add(originEntity.getEyeLocation().clone().getDirection().multiply(30)))
: (originLocation != null ? new LocationTag(originLocation.clone().add(
originLocation.getDirection().multiply(30))) : null));

// TODO: Same as PUSH -- is this the place to do this?
if (destination == null) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), "No destination specified!");
}
return;
}

final List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final ScriptTag script = scriptEntry.getObjectTag("script");
final ListTag definitions = scriptEntry.getObjectTag("definitions");
EntityTag shooter = scriptEntry.getObjectTag("shooter");

ElementTag height = scriptEntry.getElement("height");
ElementTag gravity = scriptEntry.getElement("gravity");
ElementTag speed = scriptEntry.getElement("speed");
ElementTag spread = scriptEntry.getElement("spread");

LocationTag lead = scriptEntry.getObjectTag("lead");

if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("origin", originEntity != null ? originEntity : originLocation) +
ArgumentHelper.debugObj("entities", entities.toString()) +
Expand All @@ -251,31 +227,22 @@ public void execute(final ScriptEntry scriptEntry) {
(no_rotate ? ArgumentHelper.debugObj("no_rotate", "true") : "") +
(definitions != null ? definitions.debug() : ""));
}

// Keep a ListTag of entities that can be called using <entry[name].shot_entities>
// later in the script queue

final ListTag entityList = new ListTag();

if (!no_rotate) {
originLocation = new LocationTag(NMSHandler.getEntityHelper().faceLocation(originLocation, destination));
}

// Go through all the entities, spawning/teleporting and rotating them
for (EntityTag entity : entities) {
if (!entity.isSpawned() || !no_rotate) {
entity.spawnAt(originLocation);
}

// Only add to entityList after the entities have been
// spawned, otherwise you'll get something like "e@skeleton"
// instead of "e@57" on it
entityList.addObject(entity);

if (!no_rotate) {
NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
}

// If the current entity is a projectile, set its shooter
// when applicable
if (entity.isProjectile() && (shooter != null || originEntity != null)) {
Expand All @@ -284,27 +251,22 @@ public void execute(final ScriptEntry scriptEntry) {
arrows.put(entity.getUUID(), null);
}
}

// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("shot_entities", entityList);
if (entityList.size() == 1) {
scriptEntry.addObject("shot_entity", entityList.getObject(0));
}

if (spread == null) {
Position.mount(Conversion.convertEntities(entities));
}

// Get the entity at the bottom of the entity list, because
// only its gravity should be affected and tracked considering
// that the other entities will be mounted on it
final EntityTag lastEntity = entities.get(entities.size() - 1);

if (gravity == null) {
gravity = new ElementTag(lastEntity.getEntityType().getGravity());
}

if (speed == null) {
Vector v1 = lastEntity.getLocation().toVector();
Vector v2 = destination.toVector();
Expand Down Expand Up @@ -334,7 +296,6 @@ else if (lead == null) {
relative = relative.multiply(v / 20.0d);
lastEntity.setVelocity(relative);
}

if (spread != null) {
Vector base = lastEntity.getVelocity().clone();
float sf = spread.asFloat();
Expand All @@ -344,25 +305,19 @@ else if (lead == null) {
entity.setVelocity(newvel);
}
}

final LocationTag start = new LocationTag(lastEntity.getLocation());
final Vector start_vel = lastEntity.getVelocity();

// A task used to trigger a script if the entity is no longer
// being shot, when the script argument is used
BukkitRunnable task = new BukkitRunnable() {

boolean flying = true;
LocationTag lastLocation = null;
Vector lastVelocity = null;

public void run() {

// If the entity is no longer spawned, stop the task
if (!lastEntity.isSpawned()) {
flying = false;
}

// Otherwise, if the entity is no longer traveling through
// the air, stop the task
else if (lastLocation != null && lastVelocity != null) {
Expand All @@ -371,13 +326,10 @@ else if (lastLocation != null && lastVelocity != null) {
flying = false;
}
}

// Stop the task and run the script if conditions
// are met
if (!flying) {

this.cancel();

if (script != null) {
if (lastLocation == null) {
lastLocation = start;
Expand All @@ -403,7 +355,6 @@ else if (lastLocation != null && lastVelocity != null) {
};
ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
}

scriptEntry.setFinished(true);
}
else {
Expand All @@ -413,25 +364,21 @@ else if (lastLocation != null && lastVelocity != null) {
}
}
};

task.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 2);
}

@EventHandler
public void arrowDamage(EntityDamageByEntityEvent event) {
// Get the damager
Entity arrow = event.getDamager();

// First, quickly confirm it's a projectile (relevant at all)
if (!(arrow instanceof Projectile)) {
return;
}

// Second, more slowly check if we shot it
if (!arrows.containsKey(arrow.getUniqueId())) {
return;
}

// Replace its entry with the hit entity.
arrows.remove(arrow.getUniqueId());
arrows.put(arrow.getUniqueId(), new EntityTag(event.getEntity()));
Expand Down
Expand Up @@ -59,9 +59,7 @@ public CreateWorldCommand() {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("generator")
&& arg.matchesPrefix("generator", "g")) {
scriptEntry.addObject("generator", arg.asElement());
Expand Down Expand Up @@ -96,41 +94,33 @@ else if (!scriptEntry.hasObject("world_name")) {
arg.reportUnhandled();
}
}

if (!scriptEntry.hasObject("world_name")) {
throw new InvalidArgumentsException("Must specify a world name.");
}

if (!scriptEntry.hasObject("worldtype")) {
scriptEntry.addObject("worldtype", new ElementTag("NORMAL"));
}

scriptEntry.defaultObject("environment", new ElementTag("NORMAL"));
}

@Override
public void execute(ScriptEntry scriptEntry) {

ElementTag worldName = scriptEntry.getElement("world_name");
ElementTag generator = scriptEntry.getElement("generator");
ElementTag worldType = scriptEntry.getElement("worldtype");
ElementTag environment = scriptEntry.getElement("environment");
ElementTag copy_from = scriptEntry.getElement("copy_from");
ElementTag settings = scriptEntry.getElement("settings");
ElementTag seed = scriptEntry.getElement("seed");

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(), worldName.debug() +
(generator != null ? generator.debug() : "") +
environment.debug() +
(copy_from != null ? copy_from.debug() : "") +
(settings != null ? settings.debug() : "") +
worldType.debug() +
(seed != null ? seed.debug() : ""));

}

if (copy_from != null) {
try {
if (copy_from.asString().contains("..")) {
Expand Down Expand Up @@ -158,30 +148,22 @@ public void execute(ScriptEntry scriptEntry) {
return;
}
}

World world;

WorldCreator worldCreator = WorldCreator.name(worldName.asString())
.environment(World.Environment.valueOf(environment.asString().toUpperCase()))
.type(WorldType.valueOf(worldType.asString().toUpperCase()));

if (generator != null) {
worldCreator.generator(generator.asString());
}

if (seed != null) {
worldCreator.seed(seed.asLong());
}

if (settings != null) {
worldCreator.generatorSettings(settings.asString());
}

world = Bukkit.getServer().createWorld(worldCreator);

if (world == null) {
Debug.echoDebug(scriptEntry, "World is null, something went wrong in creation!");
}

}
}

0 comments on commit 5e69a1f

Please sign in to comment.