Search in sources :

Example 11 with Item

use of org.bukkit.entity.Item in project Prism-Bukkit by prism.

the class ItemStackAction method placeItems.

/**
     * 
     * @return
     */
protected ChangeResult placeItems(Player player, QueryParameters parameters, boolean is_preview) {
    ChangeResultType result = null;
    if (is_preview) {
        return new ChangeResult(ChangeResultType.PLANNED, null);
    }
    if (plugin.getConfig().getBoolean("prism.appliers.allow-rollback-items-removed-from-container")) {
        final Block block = getWorld().getBlockAt(getLoc());
        Inventory inventory = null;
        // Item drop/pickup from player inventories
        if (getType().getName().equals("item-drop") || getType().getName().equals("item-pickup")) {
            // Is player online?
            final String playerName = getPlayerName();
            final Player onlinePlayer = Bukkit.getServer().getPlayer(playerName);
            if (onlinePlayer != null) {
                inventory = onlinePlayer.getInventory();
            } else {
                // Skip if the player isn't online
                Prism.debug("Skipping inventory process because player is offline");
                return new ChangeResult(ChangeResultType.SKIPPED, null);
            }
        } else {
            if (block.getType().equals(Material.JUKEBOX)) {
                final Jukebox jukebox = (Jukebox) block.getState();
                jukebox.setPlaying(item.getType());
                jukebox.update();
            } else if (block.getState() instanceof InventoryHolder) {
                final InventoryHolder ih = (InventoryHolder) block.getState();
                inventory = ih.getInventory();
            } else {
                Entity[] foundEntities = block.getChunk().getEntities();
                if (foundEntities.length > 0) {
                    for (Entity e : foundEntities) {
                        if (!e.getType().equals(EntityType.ITEM_FRAME))
                            continue;
                        // https://snowy-evening.com/botsko/prism/318/
                        if (!block.getWorld().equals(e.getWorld()))
                            continue;
                        // Let's limit this to only entities within 1 block of the current.
                        Prism.debug(block.getLocation());
                        Prism.debug(e.getLocation());
                        if (block.getLocation().distance(e.getLocation()) < 2) {
                            final ItemFrame frame = (ItemFrame) e;
                            if ((getType().getName().equals("item-remove") && parameters.getProcessType().equals(PrismProcessType.ROLLBACK)) || (getType().getName().equals("item-insert") && parameters.getProcessType().equals(PrismProcessType.RESTORE))) {
                                frame.setItem(item);
                            } else {
                                frame.setItem(null);
                            }
                            result = ChangeResultType.APPLIED;
                        }
                    }
                }
            }
        }
        if (inventory != null) {
            final PrismProcessType pt = parameters.getProcessType();
            final String n = getType().getName();
            // inventory
            if ((pt.equals(PrismProcessType.ROLLBACK) && (n.equals("item-remove") || n.equals("item-drop"))) || (pt.equals(PrismProcessType.RESTORE) && (n.equals("item-insert") || n.equals("item-pickup")))) {
                boolean added = false;
                // We'll attempt to put it back in the same slot
                if (getActionData().slot >= 0) {
                    // https://snowy-evening.com/botsko/prism/450/
                    if (getActionData().slot < inventory.getSize()) {
                        final ItemStack currentSlotItem = inventory.getItem(getActionData().slot);
                        // Make sure nothing's there.
                        if (currentSlotItem == null) {
                            result = ChangeResultType.APPLIED;
                            added = true;
                            inventory.setItem(getActionData().slot, getItem());
                        }
                    }
                }
                // If that failed we'll attempt to put it anywhere
                if (!added) {
                    final HashMap<Integer, ItemStack> leftovers = InventoryUtils.addItemToInventory(inventory, getItem());
                    if (leftovers.size() > 0) {
                        Prism.debug("Skipping adding items because there are leftovers");
                        result = ChangeResultType.SKIPPED;
                    } else {
                        result = ChangeResultType.APPLIED;
                        added = true;
                    }
                }
                // Item was added to the inv, we need to remove the entity
                if (added && (n.equals("item-drop") || n.equals("item-pickup"))) {
                    final Entity[] entities = getLoc().getChunk().getEntities();
                    for (final Entity entity : entities) {
                        if (entity instanceof Item) {
                            final ItemStack stack = ((Item) entity).getItemStack();
                            if (stack.isSimilar(getItem())) {
                                // Remove the event's number of items from
                                // the stack
                                stack.setAmount(stack.getAmount() - getItem().getAmount());
                                if (stack.getAmount() == 0) {
                                    entity.remove();
                                }
                                break;
                            }
                        }
                    }
                }
            }
            // inventory
            if ((pt.equals(PrismProcessType.ROLLBACK) && (n.equals("item-insert") || n.equals("item-pickup"))) || (pt.equals(PrismProcessType.RESTORE) && (n.equals("item-remove") || n.equals("item-drop")))) {
                // does inventory have item?
                boolean removed = false;
                // We'll attempt to take it from the same slot
                if (getActionData().slot >= 0) {
                    if (getActionData().slot > inventory.getContents().length) {
                        inventory.addItem(getItem());
                    } else {
                        final ItemStack currentSlotItem = inventory.getItem(getActionData().slot);
                        // Make sure something's there.
                        if (currentSlotItem != null) {
                            currentSlotItem.setAmount(currentSlotItem.getAmount() - getItem().getAmount());
                            result = ChangeResultType.APPLIED;
                            removed = true;
                            inventory.setItem(getActionData().slot, currentSlotItem);
                        }
                    }
                }
                // If that failed we'll attempt to take it from anywhere
                if (!removed) {
                    final int slot = InventoryUtils.inventoryHasItem(inventory, getItem().getTypeId(), getItem().getDurability());
                    if (slot > -1) {
                        inventory.removeItem(getItem());
                        result = ChangeResultType.APPLIED;
                        removed = true;
                    } else {
                        Prism.debug("Item removal from container skipped because it's not currently inside.");
                        result = ChangeResultType.SKIPPED;
                    }
                }
                // If the item was removed and it's a drop type, re-drop it
                if (removed && (n.equals("item-drop") || n.equals("item-pickup"))) {
                    me.botsko.elixr.ItemUtils.dropItem(getLoc(), getItem());
                }
            }
        }
    }
    return new ChangeResult(result, null);
}
Also used : Jukebox(org.bukkit.block.Jukebox) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ItemFrame(org.bukkit.entity.ItemFrame) ChangeResult(me.botsko.prism.appliers.ChangeResult) ChangeResultType(me.botsko.prism.appliers.ChangeResultType) Item(org.bukkit.entity.Item) PrismProcessType(me.botsko.prism.appliers.PrismProcessType) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 12 with Item

use of org.bukkit.entity.Item in project Jobs by GamingMesh.

the class JobsPaymentListener method onPlayerFish.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerFish(PlayerFishEvent event) {
    // make sure plugin is enabled
    if (!plugin.isEnabled())
        return;
    Player player = event.getPlayer();
    // check if in creative
    if (event.getPlayer().getGameMode().equals(GameMode.CREATIVE) && !ConfigManager.getJobsConfiguration().payInCreative())
        return;
    if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName()))
        return;
    // restricted area multiplier
    double multiplier = ConfigManager.getJobsConfiguration().getRestrictedMultiplier(player);
    if (event.getState().equals(PlayerFishEvent.State.CAUGHT_FISH) && event.getCaught() instanceof Item) {
        JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
        ItemStack items = ((Item) event.getCaught()).getItemStack();
        Jobs.action(jPlayer, new ItemActionInfo(items, ActionType.FISH), multiplier);
    }
}
Also used : Item(org.bukkit.entity.Item) Player(org.bukkit.entity.Player) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) ItemActionInfo(com.gamingmesh.jobs.actions.ItemActionInfo) ItemStack(org.bukkit.inventory.ItemStack) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) EventHandler(org.bukkit.event.EventHandler)

Example 13 with Item

use of org.bukkit.entity.Item in project Denizen-For-Bukkit by DenizenScript.

the class ItemSpawnsScriptEvent method onItemSpawns.

@EventHandler
public void onItemSpawns(ItemSpawnEvent event) {
    Item entity = event.getEntity();
    location = new dLocation(event.getLocation());
    item = new dItem(entity.getItemStack());
    this.entity = new dEntity(entity);
    cancelled = event.isCancelled();
    this.event = event;
    dEntity.rememberEntity(entity);
    fire();
    dEntity.forgetEntity(entity);
    event.setCancelled(cancelled);
}
Also used : net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) Item(org.bukkit.entity.Item) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) EventHandler(org.bukkit.event.EventHandler)

Example 14 with Item

use of org.bukkit.entity.Item in project Denizen-For-Bukkit by DenizenScript.

the class PlayerFishesScriptEvent method onPlayerFishes.

@EventHandler
public void onPlayerFishes(PlayerFishEvent event) {
    if (dEntity.isNPC(event.getPlayer())) {
        return;
    }
    Entity hookEntity = event.getHook();
    dEntity.rememberEntity(hookEntity);
    hook = new dEntity(hookEntity);
    state = new Element(event.getState().toString());
    item = null;
    entity = null;
    Entity caughtEntity = event.getCaught();
    if (caughtEntity != null) {
        dEntity.rememberEntity(caughtEntity);
        entity = new dEntity(caughtEntity);
        if (caughtEntity instanceof Item) {
            item = new dItem(((Item) caughtEntity).getItemStack());
        }
    }
    cancelled = event.isCancelled();
    this.event = event;
    fire();
    dEntity.forgetEntity(hookEntity);
    dEntity.forgetEntity(caughtEntity);
    event.setCancelled(cancelled);
}
Also used : Entity(org.bukkit.entity.Entity) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) Item(org.bukkit.entity.Item) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) Element(net.aufdemrand.denizencore.objects.Element) EventHandler(org.bukkit.event.EventHandler)

Example 15 with Item

use of org.bukkit.entity.Item in project Denizen-For-Bukkit by DenizenScript.

the class DropCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    dLocation location = (dLocation) scriptEntry.getObject("location");
    Element qty = scriptEntry.getElement("qty");
    Element action = scriptEntry.getElement("action");
    Element speed = scriptEntry.getElement("speed");
    List<dItem> items = (List<dItem>) scriptEntry.getObject("item");
    dEntity entity = (dEntity) scriptEntry.getObject("entity");
    Duration delay = (Duration) scriptEntry.getObject("delay");
    // Report to dB
    dB.report(scriptEntry, getName(), action.debug() + location.debug() + qty.debug() + (items != null ? aH.debugList("items", items) : "") + (entity != null ? entity.debug() : "") + (speed != null ? speed.debug() : "") + (delay != null ? delay.debug() : ""));
    dList entityList = new dList();
    // Do the drop
    switch(Action.valueOf(action.asString())) {
        case DROP_EXP:
            dEntity orb = new dEntity(location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB));
            ((ExperienceOrb) orb.getBukkitEntity()).setExperience(qty.asInt());
            entityList.add(orb.toString());
            break;
        case DROP_ITEM:
            for (dItem item : items) {
                if (qty.asInt() > 1 && item.isUnique()) {
                    dB.echoDebug(scriptEntry, "Cannot drop multiples of this item because it is Unique!");
                }
                for (int x = 0; x < qty.asInt(); x++) {
                    dEntity e = new dEntity(location.getWorld().dropItem(location, item.getItemStack()));
                    if (e.isValid()) {
                        e.setVelocity(e.getVelocity().multiply(speed != null ? speed.asDouble() : 1d));
                        if (delay != null) {
                            ((Item) e.getBukkitEntity()).setPickupDelay(delay.getTicksAsInt());
                        }
                    }
                    entityList.add(e.toString());
                }
            }
            break;
        case DROP_ENTITY:
            if (qty.asInt() > 1 && entity.isUnique()) {
                dB.echoDebug(scriptEntry, "Cannot drop multiples of this entity because it is Unique!");
                entity.spawnAt(location);
                entityList.add(entity.toString());
                break;
            }
            for (int x = 0; x < qty.asInt(); x++) {
                ArrayList<Mechanism> mechanisms = new ArrayList<Mechanism>();
                for (Mechanism mechanism : entity.getWaitingMechanisms()) {
                    mechanisms.add(new Mechanism(new Element(mechanism.getName()), mechanism.getValue()));
                }
                dEntity ent = new dEntity(entity.getEntityType(), mechanisms);
                ent.spawnAt(location);
                entityList.add(ent.toString());
            }
            break;
    }
    // Add entities to context so that the specific entities dropped can be fetched.
    scriptEntry.addObject("dropped_entities", entityList);
}
Also used : ArrayList(java.util.ArrayList) ExperienceOrb(org.bukkit.entity.ExperienceOrb) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) Item(org.bukkit.entity.Item) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) ArrayList(java.util.ArrayList) List(java.util.List) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Aggregations

Item (org.bukkit.entity.Item)19 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)7 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)7 EventHandler (org.bukkit.event.EventHandler)7 ItemStack (org.bukkit.inventory.ItemStack)7 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Location (org.bukkit.Location)4 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)4 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)4 Entity (org.bukkit.entity.Entity)3 Player (org.bukkit.entity.Player)3 Vector (org.bukkit.util.Vector)3 UUID (java.util.UUID)2 Block (org.bukkit.block.Block)2 ExperienceOrb (org.bukkit.entity.ExperienceOrb)2 Inventory (org.bukkit.inventory.Inventory)2 ItemActionInfo (com.gamingmesh.jobs.actions.ItemActionInfo)1