Skip to content

Commit

Permalink
fishes event, loads crossbow event: add 'with' switch
Browse files Browse the repository at this point in the history
also multiple minor cleanups
  • Loading branch information
mcmonkey4eva committed Oct 28, 2021
1 parent 320ae39 commit cd9d25e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
Expand Up @@ -29,6 +29,8 @@ public class EntityLoadCrossbowScriptEvent extends BukkitScriptEvent implements
//
// @Cancellable true
//
// @Switch crossbow:<item> to only process the event if the crossbow is a specified item.
//
// @Triggers when a living entity loads a crossbow with a projectile.
//
// @Context
Expand Down Expand Up @@ -71,6 +73,9 @@ public boolean matches(ScriptPath path) {
if (!tryEntity(entity, path.eventArgLowerAt(0))) {
return false;
}
if (!runWithCheck(path, new ItemTag(event.getCrossbow()), "crossbow")) {
return false;
}
return super.matches(path);
}

Expand Down
Expand Up @@ -761,8 +761,12 @@ else if (isAdvancedMatchable(lower)) {
}
}

public boolean runWithCheck(ScriptPath path, ItemTag held) {
String with = path.switches.get("with");
public static boolean runWithCheck(ScriptPath path, ItemTag held) {
return runWithCheck(path, held, "with");
}

public static boolean runWithCheck(ScriptPath path, ItemTag held, String key) {
String with = path.switches.get(key);
if (with != null) {
if (CoreUtilities.equalsIgnoreCase(with, "item")) {
return true;
Expand Down
Expand Up @@ -36,6 +36,7 @@ public class PlayerClicksBlockScriptEvent extends BukkitScriptEvent implements L
// @Switch with:<item> to only process the event if a specified item was held.
// @Switch using:hand/off_hand/either_hand to only process the event if the specified hand was used to click.
// @Switch type:<material> to only run if the block clicked matches the material input.
//
// @Location true
//
// @Triggers when a player clicks on a block or in the air.
Expand Down
Expand Up @@ -9,12 +9,14 @@
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;

public class PlayerFishesScriptEvent extends BukkitScriptEvent implements Listener {

Expand All @@ -30,6 +32,8 @@ public class PlayerFishesScriptEvent extends BukkitScriptEvent implements Listen
//
// @Cancellable true
//
// @Switch with:<item> to only process the event if the fishing rod is a specified item.
//
// @Triggers when a player uses a fishing rod.
//
// @Context
Expand All @@ -43,7 +47,8 @@ public class PlayerFishesScriptEvent extends BukkitScriptEvent implements Listen
// "CAUGHT:" + ItemTag to change the item that was caught (only if an item was already being caught).
// "XP:" + ElementTag(Number) to change how much experience will drop.
//
// @Player Always.
// @Player If the fisher or the caught entity is a player (in most cases, the fisher can be assumed to be a real player).
// @NPC If the fisher or the caught entity is an NPC.
//
// -->

Expand All @@ -66,7 +71,6 @@ public boolean couldMatch(ScriptPath path) {
@Override
public boolean matches(ScriptPath path) {
String fish = path.eventArgLowerAt(2);

if (!fish.isEmpty() && !fish.equals("in") && !fish.equals("while")) {
if (entity == null) {
return false;
Expand All @@ -80,18 +84,27 @@ public boolean matches(ScriptPath path) {
}
}
}

String[] data = path.eventArgsLower;
for (int index = 2; index < data.length; index++) {
if (data[index].equals("while") && !data[index + 1].equalsIgnoreCase(state.asString())) {
return false;
}
}

if (!runInCheck(path, hook.getLocation())) {
return false;
}

if (path.switches.containsKey("with")) {
if (!EntityTag.isPlayer(event.getPlayer())) {
return false;
}
ItemStack held = event.getPlayer().getEquipment().getItemInMainHand();
if (held.getType() != Material.FISHING_ROD) {
held = event.getPlayer().getEquipment().getItemInOffHand();
}
if (!runWithCheck(path, new ItemTag(held))) {
return false;
}
}
return super.matches(path);
}

Expand Down
Expand Up @@ -63,9 +63,7 @@ public BlockCrackCommand() {

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

for (Argument arg : scriptEntry) {

if (arg.matchesPrefix("players")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
Expand All @@ -83,17 +81,13 @@ else if (arg.matches("stack")) {
else {
arg.reportUnhandled();
}

}

if (!scriptEntry.hasObject("progress")) {
throw new InvalidArgumentsException("Must specify crack animation progress!");
}

if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a valid location!");
}

scriptEntry.defaultObject("players", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)))
.defaultObject("stack", new ElementTag(false));
}
Expand All @@ -113,25 +107,17 @@ public void execute(ScriptEntry scriptEntry) {
ElementTag progress = scriptEntry.getElement("progress");
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag stack = scriptEntry.getElement("stack");

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("players", players)
+ progress.debug() + location.debug() + stack.debug());

Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("players", players), progress, location, stack);
}

Location loc = location.getBlock().getLocation();
if (!progressTracker.containsKey(loc)) {
progressTracker.put(loc, new HashMap<>());
lastBase += 10;
}
Map<UUID, IntHolder> uuidInt = progressTracker.get(loc);

boolean stackVal = stack.asBoolean();

PacketHelper packetHelper = NMSHandler.getPacketHelper();

for (PlayerTag player : players) {
if (!player.isOnline()) {
Debug.echoError("Players must be online!");
Expand Down

0 comments on commit cd9d25e

Please sign in to comment.