Skip to content

Commit

Permalink
Add ActionBar Command. fixes #1288
Browse files Browse the repository at this point in the history
  • Loading branch information
Fortifier42 committed Jan 25, 2016
1 parent dcf6422 commit b0f4180
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Expand Up @@ -49,6 +49,36 @@ public void registerCoreMembers() {
"ACTION", "Action [<action name>|...] (<npc>|...) (context:<name>|<object>|...)", 1);


// <--[command]
// @Name ActionBar
// @Syntax ActionBar ["<text>"] (targets:<player>|...) (format:<name>)
// @Required 1
// @Stable stable
// @Short Send a message to a player's action bar.
// @Author Fortifier42
// @group player

// @Description
// Sends a message to the target's action bar area. If no target is specified it will default to the attached
// player. Accepts the 'format:<name>' argument, which will reformat the text according to the specified
// format script.

// @Usage
// Use to send a message to the player's action bar.
// - actionbar "Hey there <player.name>!"

// @Usage
// Use to send a message to a list of players.
// - actionbar "Hey, welcome to the server!" targets:p@Fortifier42|p@mcmonkey4eva|p@Morphan1

// @Usage
// Use to send a message to a list of players, with a formatted message.
// - actionbar "Hey there!" targets:p@Fortifier42|p@mcmonkey4eva format:ServerChat
// -->
registerCoreMember(ActionBarCommand.class,
"ACTIONBAR", "ActionBar [\"<text>\"] (targets:<player>|...)", 1);


// <--[command]
// @Name Adjust
// @Syntax adjust [<dObject>|...] [<mechanism>](:<value>)
Expand Down
@@ -0,0 +1,92 @@
package net.aufdemrand.denizen.scripts.commands.player;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.packets.ActionBar;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.tags.TagManager;

import java.text.Format;
import java.text.Normalizer;
import java.util.Arrays;
import java.util.List;

public class ActionBarCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (arg.matchesPrefix("format", "f")) {
FormatScriptContainer format = null;
String formatStr = arg.getValue();
format = ScriptRegistry.getScriptContainer(formatStr);
if (format == null) {
dB.echoError("Could not find format script matching '" + formatStr + '\'');
}
scriptEntry.addObject("format", format);
}
if (arg.matchesPrefix("targets", "target")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class));
}
else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", new Element(TagManager.cleanOutputFully(arg.raw_value)));
}
}

if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}

if (!scriptEntry.hasObject("targets") && !((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
throw new InvalidArgumentsException("Must specify target(s).");
}

if (!scriptEntry.hasObject("targets")) {
BukkitScriptEntryData data = (BukkitScriptEntryData) scriptEntry.entryData;
if (!data.hasPlayer()) {
throw new InvalidArgumentsException("Must specify valid player Targets!");
}
else {
scriptEntry.addObject("targets",
Arrays.asList(data.getPlayer()));
}
}
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

Element text = scriptEntry.getElement("text");
FormatScriptContainer format = (FormatScriptContainer) scriptEntry.getObject("format");

List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");

dB.report(scriptEntry, getName(), text.debug() + aH.debugList("Targets", targets));
if (format != null) {
text = new Element(format.getFormattedText(scriptEntry));
}

for (dPlayer player : targets) {
if (player.isValid() && player.isOnline()) {
ActionBar.sendActionBarMessage(player.getPlayerEntity(), text.asString());
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Tried to send action bar message to non-existent or offline player!");
}
}

}

}

0 comments on commit b0f4180

Please sign in to comment.