use of com.denizenscript.denizen.objects.PlayerTag 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()));
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag 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);
}
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class OxygenCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag type = scriptEntry.getElement("type");
ElementTag mode = scriptEntry.getElement("mode");
ElementTag amount = scriptEntry.getElement("amount");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), type, mode, amount);
}
PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
switch(Type.valueOf(type.asString().toUpperCase())) {
case MAXIMUM:
switch(Mode.valueOf(mode.asString().toUpperCase())) {
case SET:
player.setMaximumAir(amount.asInt());
break;
case ADD:
player.setMaximumAir(player.getMaximumAir() + amount.asInt());
break;
case REMOVE:
player.setMaximumAir(player.getMaximumAir() - amount.asInt());
break;
}
break;
case REMAINING:
switch(Mode.valueOf(mode.asString().toUpperCase())) {
case SET:
player.setRemainingAir(amount.asInt());
break;
case ADD:
player.setRemainingAir(player.getRemainingAir() + amount.asInt());
break;
case REMOVE:
player.setRemainingAir(player.getRemainingAir() - amount.asInt());
break;
}
break;
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class ResourcePackCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
List<PlayerTag> targets = scriptEntry.getObjectTag("targets");
ElementTag url = scriptEntry.getElement("url");
ElementTag hash = scriptEntry.getElement("hash");
ElementTag prompt = scriptEntry.getElement("prompt");
ElementTag forced = scriptEntry.getElement("forced");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("Targets", targets), url, hash, prompt, forced);
}
if (hash.asString().length() != 40) {
Debug.echoError("Invalid resource_pack hash. Should be 40 characters of hexadecimal data.");
return;
}
for (PlayerTag player : targets) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
continue;
}
AdvancedTextImpl.instance.sendResourcePack(player.getPlayerEntity(), url.asString(), hash.asString(), forced != null && forced.asBoolean(), prompt == null ? null : prompt.asString());
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class SidebarCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
ElementTag elTitle = scriptEntry.getElement("title");
ElementTag elScores = scriptEntry.getElement("scores");
ElementTag elValue = scriptEntry.getElement("value");
ElementTag elIncrement = scriptEntry.getElement("increment");
ElementTag elStart = scriptEntry.getElement("start");
ElementTag elPlayers = scriptEntry.getElement("players");
ElementTag elPerPlayer = scriptEntry.getElement("per_player");
ListTag players = ListTag.valueOf(TagManager.tag(elPlayers.asString(), scriptEntry.getContext()), scriptEntry.getContext());
boolean per_player = elPerPlayer.asBoolean();
String perTitle = null;
String perScores = null;
String perValue = null;
String perIncrement = null;
String perStart = null;
ElementTag title = null;
ListTag scores = null;
ListTag value = null;
ElementTag increment = null;
ElementTag start = null;
if (per_player) {
if (elTitle != null) {
perTitle = elTitle.asString();
}
if (elScores != null) {
perScores = elScores.asString();
}
if (elValue != null) {
perValue = elValue.asString();
}
if (elIncrement != null) {
perIncrement = elIncrement.asString();
}
if (elStart != null) {
perStart = elStart.asString();
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, elTitle, elScores, elValue, elIncrement, elStart, db("players", players));
}
} else {
BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
if (elTitle != null) {
title = new ElementTag(TagManager.tag(elTitle.asString(), context));
}
if (elScores != null) {
scores = ListTag.getListFor(TagManager.tagObject(elScores.asString(), context), context);
}
if (elValue != null) {
value = ListTag.getListFor(TagManager.tagObject(elValue.asString(), context), context);
}
if (elIncrement != null) {
increment = new ElementTag(TagManager.tag(elIncrement.asString(), context));
}
if (elStart != null) {
start = new ElementTag(TagManager.tag(elStart.asString(), context));
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, title, scores, value, increment, start, db("players", players));
}
}
switch(Action.valueOf(action.asString())) {
case ADD:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
continue;
}
Sidebar sidebar = createSidebar(player);
if (sidebar == null) {
continue;
}
List<Sidebar.SidebarLine> current = sidebar.getLines();
if (per_player) {
TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
if (perScores != null) {
scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
}
}
try {
int index = start != null ? start.asInt() : (current.size() > 0 ? current.get(current.size() - 1).score : value.size());
int incr = increment != null ? increment.asInt() : -1;
for (int i = 0; i < value.size(); i++, index += incr) {
int score = (scores != null && i < scores.size()) ? Integer.valueOf(scores.get(i)) : index;
while (hasScoreAlready(current, score)) {
score += (incr == 0 ? 1 : incr);
}
current.add(new Sidebar.SidebarLine(value.get(i), score));
}
} catch (Exception e) {
Debug.echoError(e);
continue;
}
sidebar.setLines(current);
sidebar.sendUpdate();
}
break;
case REMOVE:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
continue;
}
Sidebar sidebar = createSidebar(player);
if (sidebar == null) {
continue;
}
List<Sidebar.SidebarLine> current = sidebar.getLines();
if (per_player) {
TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
if (perValue != null) {
value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
}
if (perScores != null) {
scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
}
}
boolean removedAny = false;
if (scores != null) {
try {
for (String scoreString : scores) {
int score = Integer.valueOf(scoreString);
for (int i = 0; i < current.size(); i++) {
if (current.get(i).score == score) {
current.remove(i--);
}
}
}
} catch (Exception e) {
Debug.echoError(e);
continue;
}
sidebar.setLines(current);
sidebar.sendUpdate();
removedAny = true;
}
if (value != null) {
for (String line : value) {
for (int i = 0; i < current.size(); i++) {
if (current.get(i).text.equalsIgnoreCase(line)) {
current.remove(i--);
}
}
}
sidebar.setLines(current);
sidebar.sendUpdate();
removedAny = true;
}
if (!removedAny) {
sidebar.remove();
sidebars.remove(player.getPlayerEntity().getUniqueId());
}
}
break;
case SET_LINE:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
continue;
}
if ((scores == null || scores.isEmpty()) && perScores == null) {
Debug.echoError("Missing or invalid 'scores' parameter.");
return;
}
if ((value == null || value.size() != scores.size()) && perValue == null) {
Debug.echoError("Missing or invalid 'values' parameter.");
return;
}
Sidebar sidebar = createSidebar(player);
if (sidebar == null) {
continue;
}
List<Sidebar.SidebarLine> current = sidebar.getLines();
if (per_player) {
TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
if (perValue != null) {
value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
}
if (perScores != null) {
scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
}
}
try {
for (int i = 0; i < value.size(); i++) {
if (!ArgumentHelper.matchesInteger(scores.get(i))) {
Debug.echoError("Sidebar command scores input contains not-a-valid-number: " + scores.get(i));
return;
}
int score = Integer.parseInt(scores.get(i));
if (hasScoreAlready(current, score)) {
for (Sidebar.SidebarLine line : current) {
if (line.score == score) {
line.text = value.get(i);
break;
}
}
} else {
current.add(new Sidebar.SidebarLine(value.get(i), score));
}
}
} catch (Exception e) {
Debug.echoError(e);
continue;
}
sidebar.setLines(current);
sidebar.sendUpdate();
}
break;
case SET:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
continue;
}
Sidebar sidebar = createSidebar(player);
if (sidebar == null) {
continue;
}
List<Sidebar.SidebarLine> current = new ArrayList<>();
if (per_player) {
TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
if (perValue != null) {
value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
}
if (perScores != null) {
scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
}
if (perStart != null) {
start = new ElementTag(TagManager.tag(perStart, context));
}
if (perIncrement != null) {
increment = new ElementTag(TagManager.tag(perIncrement, context));
}
if (perTitle != null) {
title = new ElementTag(TagManager.tag(perTitle, context));
}
}
if (value != null) {
try {
int index = start != null ? start.asInt() : value.size();
int incr = increment != null ? increment.asInt() : -1;
for (int i = 0; i < value.size(); i++, index += incr) {
int score = (scores != null && i < scores.size()) ? Integer.valueOf(scores.get(i)) : index;
current.add(new Sidebar.SidebarLine(value.get(i), score));
}
} catch (Exception e) {
Debug.echoError(e);
continue;
}
sidebar.setLines(current);
}
if (title != null) {
sidebar.setTitle(title.asString());
}
sidebar.sendUpdate();
}
break;
}
}
Aggregations