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

Knockback #2094

Merged
merged 9 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(new PlayerEquipsArmorScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerJumpsPaperScriptEventImpl());
ScriptEvent.registerScriptEvent(new PlayerSpectatesEntityScriptEvent());
ScriptEvent.registerScriptEvent(new EntityKnocksbackEntityScriptEvent());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list should be alphabetized (just look at the order of events in your IntelliJ class listing)

ScriptEvent.registerScriptEvent(new PlayerStopsSpectatingScriptEvent());
ScriptEvent.registerScriptEvent(new ProjectileCollideScriptEvent());
ScriptEvent.registerScriptEvent(new TNTPrimesScriptEvent());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class EntityKnocksbackEntityScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// entity knocks back entity
// entity knocks back <entity>
// <entity> knocks back entity
// <entity> knocks back <entity>
//
// @Regex ^on [^\s]+ knocks back [^\s]+$
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
// @Switch with:<item> to only process the event when the item used to cause damage (in the damager's hand) is a specified item.
//
// @Plugin Paper
//
// @Cancellable true
//
// @Triggers when an entity is knocked back from the hit of another entity.
//
// @Context
// <context.entity> returns the EntityTag that was knocked back.
// <context.damager> returns the EntityTag of the one who knocked.
// <context.acceleration> returns the knockback applied as a vector.
//
// @Player when the damager or damaged entity is a player. Cannot be both.
//
// @NPC when the damager or damaged entity is an NPC. Cannot be both.
//
// -->

public EntityKnocksbackEntityScriptEvent() {
instance = this;
}

public static EntityKnocksbackEntityScriptEvent instance;

public EntityTag entity;
public EntityTag hitBy;
public ItemTag held;
public EntityKnockbackByEntityEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original issue here still not corrected

String lower = CoreUtilities.toLowerCase(s);
return CoreUtilities.getXthArg(1, lower).equals("knocks") &&
CoreUtilities.getXthArg(2, lower).equals("back");
}

@Override
public boolean matches(ScriptPath path) {
String attacker = path.eventArgLowerAt(0);
String target = path.eventArgLowerAt(3);
if (!tryEntity(hitBy, attacker) || (!tryEntity(entity, target))) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (!runWithCheck(path, held)) {
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original issue here still not resolved

return super.matches(path);
}

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

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof LocationTag) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a reliable check, the object is not guaranteed to be of the expected type when fed in, LocationTag.matches + valueOf are required.
Also, !isDefaultDetermination

event.getAcceleration().copy(((LocationTag) determinationObj).toVector());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(
hitBy.isPlayer() ? hitBy.getDenizenPlayer() :
(entity.isPlayer() ? entity.getDenizenPlayer() : null),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray doublenewline ^
Also the newlines on the individual parts

I'm confused why this isn't formatted exactly the same as the EntityDamaged event, which I think is what you were referencing anyway?

        return new BukkitScriptEntryData(damager != null && damager.isPlayer() ? damager.getDenizenPlayer() : entity.isPlayer() ? entity.getDenizenPlayer() : null,
                damager != null && damager.isCitizensNPC() ? damager.getDenizenNPC() : entity.isCitizensNPC() ? entity.getDenizenNPC() : null);

hitBy.isCitizensNPC() ? hitBy.getDenizenNPC() :
(entity.isCitizensNPC() ? entity.getDenizenNPC() : null)
);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
} else if (name.equals("damager")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline between } and else, not on the same line.

return hitBy.getDenizenObject();
} else if (name.equals("acceleration")) {
return new LocationTag(event.getAcceleration());
}
return super.getContext(name);
}

@EventHandler
public void onEntityKnockbackEntity(EntityKnockbackByEntityEvent event) {
entity = new EntityTag(event.getEntity());
hitBy = new EntityTag(event.getHitBy());
held = hitBy.getItemInHand();
this.event = event;
fire(event);
}
}