Search in sources :

Example 46 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNPCHelper method getInventory.

/**
 * Returns a InventoryTag object from the Inventory trait of a valid NPC.
 *
 * @param npc the Citizens NPC
 * @return the NPC's InventoryTag
 */
public static Inventory getInventory(NPC npc) {
    if (npc == null) {
        return null;
    }
    /*if (!npcInventories.containsKey(npc.getId())) {
            _registerNPC(npc);
        }
        return npcInventories.get(npc.getId());*/
    if (npc.isSpawned() && npc.getEntity() instanceof InventoryHolder) {
        return ((InventoryHolder) npc.getEntity()).getInventory();
    } else {
        try {
            NPCTag npcTag = new NPCTag(npc);
            Inventory inv = (Inventory) INVENTORY_TRAIT_VIEW.get(npcTag.getInventoryTrait());
            if (inv != null) {
                return inv;
            } else {
                // TODO: ???
                Inventory npcInventory = Bukkit.getServer().createInventory(npcTag, InventoryType.PLAYER);
                npcInventory.setContents(Arrays.copyOf(npcTag.getInventoryTrait().getContents(), npcInventory.getSize()));
                return npcInventory;
            }
        } catch (Exception e) {
            Debug.echoError(e);
            return null;
        }
    }
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 47 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class FishingTrait method cast.

// <--[action]
// @Actions
// cast fishing rod
// 
// @Triggers when the NPC casts a fishing rod. See also <@link command fish>.
// 
// @Context
// None
// 
// -->
private void cast() {
    new NPCTag(npc).action("cast fishing rod", null);
    if (fishingLocation == null) {
        Debug.echoError("Fishing location not found!");
        return;
    }
    isCast = true;
    double v = 34;
    double g = 20;
    Location from = npc.getStoredLocation();
    from = from.add(0, 0.33, 0);
    Location to = fishingLocation;
    Vector test = to.clone().subtract(from).toVector();
    double elev = test.getY();
    Double testAngle = launchAngle(from, to, v, elev, g);
    if (testAngle == null) {
        return;
    }
    double hangtime = hangtime(testAngle, v, elev, g);
    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;
    }
    victor.setY(Math.tan(launchAngle) * dist);
    victor = normalizeVector(victor);
    v += 0.5 * Math.pow(hangtime, 2);
    v += (CoreUtilities.getRandom().nextDouble() - 0.8) / 2;
    victor = victor.multiply(v / 20.0);
    if (npc.getEntity() instanceof Player) {
        fishHook = NMSHandler.getFishingHelper().spawnHook(from, (Player) npc.getEntity());
        fishHook.setShooter((ProjectileSource) npc.getEntity());
        fishHook.setVelocity(victor);
        PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
    }
}
Also used : Player(org.bukkit.entity.Player) NPCTag(com.denizenscript.denizen.objects.NPCTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 48 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class FishingTrait method startFishing.

// <--[action]
// @Actions
// start fishing
// 
// @Triggers when the NPC starts fishing. See also <@link command fish>.
// 
// @Context
// None
// 
// -->
/**
 * Makes the NPC fish at the specified location
 * <p/>
 * TODO Reimplement variance, so each cast doesn't land in the exact same spot.
 *
 * @param location the location to fish at
 */
public void startFishing(Location location) {
    new NPCTag(npc).action("start fishing", null);
    fishingLocation = location.clone();
    cast();
    fishing = true;
}
Also used : NPCTag(com.denizenscript.denizen.objects.NPCTag)

Example 49 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class FishingTrait method reel.

// <--[action]
// @Actions
// reel in fishing rod
// 
// @Triggers when the NPC reels in its fishing rod. See also <@link command fish>.
// 
// @Context
// None
// 
// -->
// <--[action]
// @Actions
// catch fish
// 
// @Triggers when the NPC catches a fish. See also <@link command fish>.
// 
// @Context
// None
// 
// -->
private void reel() {
    isCast = false;
    new NPCTag(npc).action("reel in fishing rod", null);
    int chance = (int) (Math.random() * 100);
    if (catchPercent > chance && fishHook != null && catchType != FishingHelper.CatchType.NONE) {
        try {
            fish.remove();
        } catch (Exception e) {
        }
        Location location = fishHook.getLocation();
        ItemStack result = NMSHandler.getFishingHelper().getResult(fishHook, catchType);
        if (result != null) {
            fish = location.getWorld().dropItem(location, result);
            Location npcLocation = npc.getStoredLocation();
            double d5 = npcLocation.getX() - location.getX();
            double d6 = npcLocation.getY() - location.getY();
            double d7 = npcLocation.getZ() - location.getZ();
            double d8 = Math.sqrt(d5 * d5 + d6 * d6 + d7 * d7);
            double d9 = 0.1D;
            fish.setVelocity(new Vector(d5 * d9, d6 * d9 + Math.sqrt(d8) * 0.08D, d7 * d9));
        }
        new NPCTag(npc).action("catch fish", null);
    }
    if (fishHook != null && fishHook.isValid()) {
        fishHook.remove();
        fishHook = null;
    }
    if (npc.getEntity() instanceof Player) {
        PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
    }
}
Also used : Player(org.bukkit.entity.Player) NPCTag(com.denizenscript.denizen.objects.NPCTag) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 50 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.

the class HungerTrait method onMove.

// <--[action]
// @Actions
// exhausted
// 
// @Triggers when the NPC is exhausted (Requires the Hunger trait)
// 
// @Context
// None
// 
// -->
/**
 * Listens for the NPC to move so hunger-loss can be calculated.
 * Cuts down on processing since loss is only calculated when moving.
 * Also checks for exhaustion, if enabled. If a NPC is exhausted, that is,
 * if currenthunger >= maxhunger, the NPC cannot move and a
 * NPCExhaustedEvent and 'On Exhausted:' action will fire.
 */
@EventHandler
public void onMove(NavigationBeginEvent event) {
    // TODO: Check if NPC == this NPC?
    if (allowexhaustion) {
        if (isStarving()) {
            // Create NPCExhaustedEvent, give chance for outside plugins to cancel.
            ExhaustedNPCEvent e = new ExhaustedNPCEvent(npc);
            Bukkit.getServer().getPluginManager().callEvent(e);
            // If still exhausted, cancel navigation and fire 'On Exhausted:' action
            if (!e.isCancelled()) {
                npc.getNavigator().cancelNavigation();
                new NPCTag(npc).action("exhausted", null);
                // No need to progress any further.
                return;
            }
        }
    }
    location = npc.getStoredLocation().clone();
    listening = true;
}
Also used : ExhaustedNPCEvent(com.denizenscript.denizen.events.bukkit.ExhaustedNPCEvent) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NPCTag (com.denizenscript.denizen.objects.NPCTag)72 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)24 EventHandler (org.bukkit.event.EventHandler)22 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)21 Player (org.bukkit.entity.Player)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)12 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 ListTag (com.denizenscript.denizencore.objects.core.ListTag)9 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 HashMap (java.util.HashMap)8 NPC (net.citizensnpcs.api.npc.NPC)8 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)6 Location (org.bukkit.Location)6 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)5 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Entity (org.bukkit.entity.Entity)5