Search in sources :

Example 1 with Advancement

use of com.denizenscript.denizen.nms.util.Advancement in project Denizen-For-Bukkit by DenizenScript.

the class ToastCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag text = scriptEntry.getElement("text");
    ElementTag frame = scriptEntry.getElement("frame");
    ItemTag icon = scriptEntry.getObjectTag("icon");
    final List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, name, text, frame, icon, db("targets", targets));
    }
    final Advancement advancement = new Advancement(true, new NamespacedKey(Denizen.getInstance(), UUID.randomUUID().toString()), null, icon.getItemStack(), text.asString(), "", null, Advancement.Frame.valueOf(frame.asString().toUpperCase()), true, false, true, 0, 0, 1);
    final AdvancementHelper advancementHelper = NMSHandler.getAdvancementHelper();
    for (PlayerTag target : targets) {
        Player player = target.getPlayerEntity();
        if (player != null) {
            advancementHelper.grant(advancement, player);
            advancementHelper.revoke(advancement, player);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) AdvancementHelper(com.denizenscript.denizen.nms.interfaces.AdvancementHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NamespacedKey(org.bukkit.NamespacedKey) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) Advancement(com.denizenscript.denizen.nms.util.Advancement)

Example 2 with Advancement

use of com.denizenscript.denizen.nms.util.Advancement in project Denizen-For-Bukkit by DenizenScript.

the class AdvancementCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("id");
    ElementTag parent = scriptEntry.getElement("parent");
    ElementTag delete = scriptEntry.getElement("delete");
    ListTag grant = scriptEntry.getObjectTag("grant");
    ListTag revoke = scriptEntry.getObjectTag("revoke");
    ItemTag icon = scriptEntry.getObjectTag("icon");
    ElementTag title = scriptEntry.getElement("title");
    ElementTag description = scriptEntry.getElement("description");
    ElementTag background = scriptEntry.getElement("background");
    ElementTag frame = scriptEntry.getElement("frame");
    ElementTag toast = scriptEntry.getElement("toast");
    ElementTag announce = scriptEntry.getElement("announce");
    ElementTag hidden = scriptEntry.getElement("hidden");
    ElementTag x = scriptEntry.getElement("x");
    ElementTag y = scriptEntry.getElement("y");
    ElementTag progressLength = scriptEntry.getElement("progress_length");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, name, id, parent, delete, grant, revoke, icon, title, description, background, progressLength, frame, toast, announce, hidden, x, y);
    }
    final AdvancementHelper advancementHelper = NMSHandler.getAdvancementHelper();
    NamespacedKey key = new NamespacedKey(Denizen.getInstance(), id.asString());
    if (delete == null && grant == null && revoke == null) {
        NamespacedKey parentKey = null;
        NamespacedKey backgroundKey = null;
        if (parent != null) {
            List<String> split = CoreUtilities.split(parent.asString(), ':', 2);
            if (split.size() == 1) {
                parentKey = new NamespacedKey(Denizen.getInstance(), split.get(0));
            } else {
                parentKey = new NamespacedKey(CoreUtilities.toLowerCase(split.get(0)), CoreUtilities.toLowerCase(split.get(1)));
            }
        } else if (background != null) {
            List<String> backgroundSplit = CoreUtilities.split(background.asString(), ':', 2);
            if (backgroundSplit.size() == 1) {
                backgroundKey = NamespacedKey.minecraft(backgroundSplit.get(0));
            } else {
                backgroundKey = new NamespacedKey(CoreUtilities.toLowerCase(backgroundSplit.get(0)), CoreUtilities.toLowerCase(backgroundSplit.get(1)));
            }
        }
        final Advancement advancement = new Advancement(false, key, parentKey, icon.getItemStack(), title.asString(), description.asString(), backgroundKey, Advancement.Frame.valueOf(frame.asString().toUpperCase()), toast.asBoolean(), announce.asBoolean(), hidden.asBoolean(), x.asFloat(), y.asFloat(), progressLength == null ? 1 : progressLength.asInt());
        advancementHelper.register(advancement);
        customRegistered.put(key, advancement);
        return;
    }
    Advancement advancement = customRegistered.get(key);
    if (advancement == null) {
        Debug.echoError("Invalid advancement key '" + key + "': not registered. Are you sure you registered it using the 'advancement' command previously?" + " Note that the 'advancement' command is not for vanilla advancements.");
        return;
    }
    if (delete != null) {
        advancementHelper.unregister(advancement);
        customRegistered.remove(key);
    } else if (grant != null) {
        for (PlayerTag target : grant.filter(PlayerTag.class, scriptEntry)) {
            Player player = target.getPlayerEntity();
            if (player != null) {
                if (progressLength == null) {
                    advancementHelper.grant(advancement, player);
                } else {
                    advancementHelper.grantPartial(advancement, player, progressLength.asInt());
                }
            }
        }
    } else /*if (revoke != null)*/
    {
        for (PlayerTag target : revoke.filter(PlayerTag.class, scriptEntry)) {
            Player player = target.getPlayerEntity();
            if (player != null) {
                advancementHelper.revoke(advancement, player);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) AdvancementHelper(com.denizenscript.denizen.nms.interfaces.AdvancementHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NamespacedKey(org.bukkit.NamespacedKey) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Advancement(com.denizenscript.denizen.nms.util.Advancement)

Aggregations

AdvancementHelper (com.denizenscript.denizen.nms.interfaces.AdvancementHelper)2 Advancement (com.denizenscript.denizen.nms.util.Advancement)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)2 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 List (java.util.List)2 NamespacedKey (org.bukkit.NamespacedKey)2 Player (org.bukkit.entity.Player)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1