Skip to content

Commit

Permalink
Expand entity shoots bow event for paper and fix arrow color property
Browse files Browse the repository at this point in the history
  • Loading branch information
mergu committed May 11, 2020
1 parent 08b140c commit 79af6cf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
Expand Up @@ -18,6 +18,7 @@ public static void init() {
// Events
ScriptEvent.registerScriptEvent(new EntityKnocksbackEntityScriptEvent());
ScriptEvent.registerScriptEvent(new EntityPathfindScriptEvent());
ScriptEvent.registerScriptEvent(new EntityShootsBowPaperScriptEventImpl());
ScriptEvent.registerScriptEvent(new PlayerBeaconEffectScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerEquipsArmorScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerJumpsPaperScriptEventImpl());
Expand Down
@@ -0,0 +1,48 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.entity.EntityShootsBowEvent;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class EntityShootsBowPaperScriptEventImpl extends EntityShootsBowEvent {

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag && !isDefaultDetermination(determinationObj)) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.equals("keep_item")) {
event.setConsumeArrow(false);
return true;
}
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("item")) {
return new ItemTag(event.getArrowItem());
}
return super.getContext(name);
}

@Override
public void fire() {
super.fire();
if (!event.getConsumeArrow() && entity.isPlayer()) {
final Player p = entity.getPlayer();
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
p.updateInventory();
}
}, 1);
}
}
}
Expand Up @@ -71,7 +71,9 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new EntityPotionEffectScriptEvent());
}
ScriptEvent.registerScriptEvent(new EntityResurrectScriptEvent());
ScriptEvent.registerScriptEvent(new EntityShootsBowEvent());
if (!Denizen.supportsPaper) {
ScriptEvent.registerScriptEvent(new EntityShootsBowEvent());
}
ScriptEvent.registerScriptEvent(new EntitySpawnerSpawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntitySpawnScriptEvent());
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13)) {
Expand Down
Expand Up @@ -3,14 +3,17 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.Conversion;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.entity.Position;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
Expand Down Expand Up @@ -39,9 +42,11 @@ public class EntityShootsBowEvent extends BukkitScriptEvent implements Listener
// <context.projectile> returns a EntityTag of the projectile.
// <context.bow> returns the ItemTag of the bow used to shoot.
// <context.force> returns the force of the shot.
// <context.item> returns an ItemTag of the shot projectile, if any (on Paper servers only).
//
// @Determine
// ListTag(EntityTag) to change the projectile(s) being shot. (Note that in certain cases, determining an arrow may not be valid).
// "KEEP_ITEM" to keep the projectile item on shooting it (on Paper servers only).
//
// @Player when the entity that shot the bow is a player.
//
Expand Down Expand Up @@ -145,6 +150,20 @@ else if (name.equals("projectile")) {
return super.getContext(name);
}

@Override
public void cancellationChanged() {
if (cancelled && entity.isPlayer()) {
final Player p = entity.getPlayer();
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
p.updateInventory();
}
}, 1);
}
super.cancellationChanged();
}

@EventHandler
public void onEntityShootsBow(EntityShootBowEvent event) {
entity = new EntityTag(event.getEntity());
Expand Down
Expand Up @@ -109,7 +109,12 @@ else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityTy
return PandaHelper.getColor(colored);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.ARROW) {
return new ColorTag(((Arrow) colored).getColor()).identify();
try {
return new ColorTag(((Arrow) colored.getBukkitEntity()).getColor()).identify();
}
catch (Exception e) {
return null;
}
}
else { // Should never happen
return null;
Expand Down Expand Up @@ -247,7 +252,7 @@ else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityTy
PandaHelper.setColor(colored, mechanism.getValue().asString());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.ARROW) {
((Arrow) colored).setColor(mechanism.valueAsType(ColorTag.class).getColor());
((Arrow) colored.getBukkitEntity()).setColor(mechanism.valueAsType(ColorTag.class).getColor());
}
else { // Should never happen
Debug.echoError("Could not apply color '" + mechanism.getValue().toString() + "' to entity of type " + type.name() + ".");
Expand Down

0 comments on commit 79af6cf

Please sign in to comment.