Search in sources :

Example 1 with OneTimeSchedulable

use of com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable in project Denizen-For-Bukkit by DenizenScript.

the class FakeItemCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<ItemTag> items = (List<ItemTag>) scriptEntry.getObject("item");
    final ElementTag elSlot = scriptEntry.getElement("slot");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    final List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    final ElementTag player_only = scriptEntry.getElement("player_only");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("items", items), elSlot, duration, db("players", players), player_only);
    }
    if (players.size() == 0) {
        return;
    }
    int slot = SlotHelper.nameToIndex(elSlot.asString(), players.get(0).getPlayerEntity());
    if (slot == -1) {
        Debug.echoError(scriptEntry, "The input '" + elSlot.asString() + "' is not a valid slot!");
        return;
    }
    final boolean playerOnly = player_only.asBoolean();
    final PacketHelper packetHelper = NMSHandler.getPacketHelper();
    for (ItemTag item : items) {
        if (item == null) {
            slot++;
            continue;
        }
        for (PlayerTag player : players) {
            Player ent = player.getPlayerEntity();
            packetHelper.setSlot(ent, translateSlot(ent, slot, playerOnly), item.getItemStack(), playerOnly);
        }
        final int slotSnapshot = slot;
        slot++;
        if (duration.getSeconds() > 0) {
            DenizenCore.schedule(new OneTimeSchedulable(() -> {
                for (PlayerTag player : players) {
                    Player ent = player.getPlayerEntity();
                    int translated = translateSlot(ent, slotSnapshot, playerOnly);
                    ItemStack original = ent.getOpenInventory().getItem(translated);
                    packetHelper.setSlot(ent, translated, original, playerOnly);
                }
            }, (float) duration.getSeconds()));
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PacketHelper(com.denizenscript.denizen.nms.interfaces.PacketHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ItemStack(org.bukkit.inventory.ItemStack) OneTimeSchedulable(com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable)

Aggregations

PacketHelper (com.denizenscript.denizen.nms.interfaces.PacketHelper)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 OneTimeSchedulable (com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable)1 List (java.util.List)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1