Search in sources :

Example 6 with PacketHelper

use of com.denizenscript.denizen.nms.interfaces.PacketHelper in project Denizen 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)6 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)6 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)6 List (java.util.List)6 Player (org.bukkit.entity.Player)6 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 ItemTag (com.denizenscript.denizen.objects.ItemTag)2 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)2 OneTimeSchedulable (com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable)2 UUID (java.util.UUID)2 Location (org.bukkit.Location)2 ItemStack (org.bukkit.inventory.ItemStack)2