Skip to content

Commit

Permalink
improvements to fish commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 19, 2020
1 parent 0209872 commit 7471429
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
Expand Up @@ -9,6 +9,9 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -56,14 +59,12 @@ public void run() {
if (!fishing) {
return;
}

if (reelCount == 400) {
if (reelCount >= 400) {
reel();
reelCount = 0;
castCount = 325;
}

if (castCount == 400) {
if (castCount >= 400) {
cast();
castCount = 0;
}
Expand Down Expand Up @@ -117,14 +118,42 @@ public void stopFishing() {
fishing = false;
}

/**
* Makes the NPC fish in the nearest water
* <p/>
* TODO Needs logic for handling that.
*/
public boolean scanForFishSpot(Location near, boolean horizontal) {
Block block = near.getBlock();
if (block.getType() == Material.WATER) {
fishingLocation = near;
return true;
}
else if (block.getRelative(BlockFace.DOWN).getType() == Material.WATER) {
fishingLocation = near.add(0, -1, 0);
return true;
}
else if (block.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN).getType() == Material.WATER) {
fishingLocation = near.add(0, -2, 0);
return true;
}
if (horizontal) {
return scanForFishSpot(near.clone().add(1, 0, 0), false)
|| scanForFishSpot(near.clone().add(-1, 0, 0), false)
|| scanForFishSpot(near.clone().add(0, 0, 1), false)
|| scanForFishSpot(near.clone().add(0, 0, -1), false);
}
return false;
}

public void startFishing() {
fishing = true;
fishingLocation = npc.getEntity().getLocation();
Location search = npc.getEntity().getLocation().clone();
Vector direction = npc.getEntity().getLocation().getDirection().clone();
if (direction.getY() > -0.1) {
direction.setY(-0.1);
}
for (int i = 0; i < 10; i++) {
search.add(direction.clone());
if (scanForFishSpot(search, true)) {
break;
}
}
}

// <--[action]
Expand All @@ -139,15 +168,12 @@ public void startFishing() {
// -->
private void cast() {
DenizenAPI.getDenizenNPC(npc).action("cast fishing rod", null);

if (fishingLocation == null) {
Debug.echoError("Fishing location not found!");
return;
}

double v = 34;
double g = 20;

Location from = npc.getEntity().getLocation();
from = from.add(0, .33, 0);
Location to = fishingLocation;
Expand All @@ -162,11 +188,9 @@ private void cast() {
Vector victor = to.clone().subtract(from).toVector();
double dist = Math.sqrt(Math.pow(victor.getX(), 2) + Math.pow(victor.getZ(), 2));
elev = victor.getY();

if (dist == 0) {
return;
}

Double launchAngle = launchAngle(from, to, v, elev, g);
if (launchAngle == null) {
return;
Expand All @@ -182,7 +206,6 @@ private void cast() {
fishHook = NMSHandler.getFishingHelper().spawnHook(from, (Player) npc.getEntity());
fishHook.setShooter((ProjectileSource) npc.getEntity());
fishHook.setVelocity(victor);

PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
}
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public FishCommand() {

// <--[command]
// @Name Fish
// @Syntax fish [<location>] (catch:{none}/default/junk/treasure/fish) (stop) (chance:<#>)
// @Syntax fish [<location>/stop] (catch:{none}/default/junk/treasure/fish) (chance:<#>)
// @Required 1
// @Maximum 4
// @Plugin Citizens
Expand All @@ -34,29 +34,23 @@ public FishCommand() {
//
// @Description
// Causes an NPC to begin fishing at the specified location.
// Setting catch determines what items the NPC may fish up, and
// the chance is the odds of the NPC fishing up an item.
//
// Also note that it seems you must specify the same location initially chosen for the NPC to fish at
// when stopping it.
// Setting catch determines what items the NPC may fish up, and the chance is the odds of the NPC fishing up an item.
//
// @Tags
// None
//
// @Usage
// Makes the NPC throw their fishing line out to where the player is looking, with a 50% chance of catching fish
// Makes the NPC throw their fishing line out to where the player is looking, with a 50% chance of catching fish.
// - fish <player.cursor_on> catch:fish chance:50
//
// @Usage
// Makes the NPC stop fishing
// - fish <player.cursor_on> stop
// Makes the NPC stop fishing.
// - fish stop
// -->

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

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

if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
Expand All @@ -75,50 +69,36 @@ else if (!scriptEntry.hasObject("percent")
&& arg.matchesInteger()) {
scriptEntry.addObject("percent", arg.asElement());
}

}

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

scriptEntry.defaultObject("catch", new ElementTag("NONE"))
.defaultObject("stop", new ElementTag(false))
.defaultObject("percent", new ElementTag(65));

if (!Utilities.entryHasNPC(scriptEntry) || !Utilities.getEntryNPC(scriptEntry).isSpawned()) {
throw new InvalidArgumentsException("This command requires a linked and spawned NPC!");
}

}

@Override
public void execute(ScriptEntry scriptEntry) {

LocationTag location = scriptEntry.getObjectTag("location");
ElementTag catchtype = scriptEntry.getElement("catch");
ElementTag stop = scriptEntry.getElement("stop");
ElementTag percent = scriptEntry.getElement("percent");

NPCTag npc = Utilities.getEntryNPC(scriptEntry);
FishingTrait trait = npc.getFishingTrait();

if (scriptEntry.dbCallShouldDebug()) {

Debug.report(scriptEntry, getName(), location.debug() + catchtype.debug() + percent.debug() + stop.debug());

Debug.report(scriptEntry, getName(), (location == null ? "" : location.debug()) + catchtype.debug() + percent.debug() + stop.debug());
}

NPCTag npc = Utilities.getEntryNPC(scriptEntry);
FishingTrait trait = npc.getFishingTrait();
if (stop.asBoolean()) {
trait.stopFishing();
return;
}

npc.getEquipmentTrait().set(0, new ItemStack(Material.FISHING_ROD));

trait.setCatchPercent(percent.asInt());
trait.setCatchType(FishingHelper.CatchType.valueOf(catchtype.asString().toUpperCase()));
trait.startFishing(location);

}
}

0 comments on commit 7471429

Please sign in to comment.