use of com.denizenscript.denizencore.objects.core.ListTag in project Denizen-For-Bukkit by DenizenScript.
the class ExecuteCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag cmd = scriptEntry.getElement("command");
ElementTag type = scriptEntry.getElement("type");
boolean silent = scriptEntry.argAsBoolean("silent");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), type, cmd, db("silent", silent));
}
String command = cmd.asString();
switch(Type.valueOf(type.asString())) {
case AS_PLAYER:
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(Utilities.getEntryPlayer(scriptEntry).getPlayerEntity(), "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
if (silent) {
NetworkInterceptHelper.enable();
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silent) {
silencedPlayers.remove(player.getUniqueId());
}
}
} catch (Throwable e) {
Debug.echoError(scriptEntry, "Exception while executing command as player.");
Debug.echoError(scriptEntry, e);
}
break;
case AS_OP:
if (CoreUtilities.equalsIgnoreCase(command, "stop")) {
Debug.echoError("Please use as_server to execute 'stop'.");
return;
}
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
PlayerHelper playerHelper = NMSHandler.getPlayerHelper();
boolean isOp = player.isOp();
if (!isOp) {
playerHelper.setTemporaryOp(player, true);
}
try {
PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
Bukkit.getPluginManager().callEvent(pcpe);
if (!pcpe.isCancelled()) {
if (silent) {
NetworkInterceptHelper.enable();
silencedPlayers.add(player.getUniqueId());
}
player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
if (silent) {
silencedPlayers.remove(player.getUniqueId());
}
}
} catch (Throwable e) {
Debug.echoError(scriptEntry, "Exception while executing command as OP.");
Debug.echoError(scriptEntry, e);
}
if (!isOp) {
playerHelper.setTemporaryOp(player, false);
}
break;
case AS_NPC:
if (!Utilities.getEntryNPC(scriptEntry).isSpawned()) {
Debug.echoError(scriptEntry, "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
return;
}
if (Utilities.getEntryNPC(scriptEntry).getEntity().getType() != EntityType.PLAYER) {
Debug.echoError(scriptEntry, "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
return;
}
Utilities.getEntryNPC(scriptEntry).getEntity().setOp(true);
try {
((Player) Utilities.getEntryNPC(scriptEntry).getEntity()).performCommand(command);
} catch (Throwable e) {
Debug.echoError(scriptEntry, "Exception while executing command as NPC-OP.");
Debug.echoError(scriptEntry, e);
}
Utilities.getEntryNPC(scriptEntry).getEntity().setOp(false);
break;
case AS_SERVER:
dcs.clearOutput();
dcs.silent = silent;
ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
Bukkit.getPluginManager().callEvent(sce);
Denizen.getInstance().getServer().dispatchCommand(dcs, sce.getCommand());
scriptEntry.addObject("output", new ListTag(dcs.getOutput()));
break;
}
}
use of com.denizenscript.denizencore.objects.core.ListTag in project Denizen-For-Bukkit by DenizenScript.
the class ChunkLoadCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
ListTag chunklocs = scriptEntry.getObjectTag("location");
DurationTag length = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, chunklocs, length);
}
for (String chunkText : chunklocs) {
Chunk chunk;
if (ChunkTag.matches(chunkText)) {
chunk = ChunkTag.valueOf(chunkText, scriptEntry.context).getChunk();
} else if (LocationTag.matches(chunkText)) {
chunk = LocationTag.valueOf(chunkText, scriptEntry.context).getChunk();
} else {
Debug.echoError("Chunk input '" + chunkText + "' is invalid.");
return;
}
ChunkCoordinate coord = new ChunkCoordinate(chunk);
switch(Action.valueOf(action.asString())) {
case ADD:
if (length.getSeconds() != 0) {
chunkDelays.put(coord, System.currentTimeMillis() + length.getMillis());
} else {
chunkDelays.put(coord, (long) 0);
}
Debug.echoDebug(scriptEntry, "...added chunk " + chunk.getX() + ", " + chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
if (!chunk.isLoaded()) {
chunk.load();
}
chunk.addPluginChunkTicket(Denizen.getInstance());
if (length.getSeconds() > 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= System.currentTimeMillis()) {
chunk.removePluginChunkTicket(Denizen.getInstance());
chunkDelays.remove(coord);
}
}, length.getTicks() + 20);
}
break;
case REMOVE:
if (chunkDelays.containsKey(coord)) {
chunkDelays.remove(coord);
chunk.removePluginChunkTicket(Denizen.getInstance());
Debug.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
} else {
Debug.echoDebug(scriptEntry, "Chunk '" + coord + "' was not on the load list, ignoring.");
}
break;
case REMOVEALL:
Debug.echoDebug(scriptEntry, "...allowing unloading of all stored chunks");
for (ChunkCoordinate loopCoord : chunkDelays.keySet()) {
loopCoord.getChunk().getChunk().removePluginChunkTicket(Denizen.getInstance());
}
chunkDelays.clear();
break;
}
}
}
use of com.denizenscript.denizencore.objects.core.ListTag in project Denizen-For-Bukkit by DenizenScript.
the class TeamCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag name = scriptEntry.getElement("name");
ListTag add = scriptEntry.getObjectTag("add");
ListTag remove = scriptEntry.getObjectTag("remove");
ElementTag prefix = scriptEntry.getElement("prefix");
ElementTag suffix = scriptEntry.getElement("suffix");
ElementTag option = scriptEntry.getElement("option");
ElementTag status = scriptEntry.getElement("status");
ElementTag color = scriptEntry.getElement("color");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, name, add, remove, prefix, suffix, color, option, status);
}
Scoreboard board;
if (id.asString().equalsIgnoreCase("main")) {
board = ScoreboardHelper.getMain();
} else {
if (ScoreboardHelper.hasScoreboard(id.asString())) {
board = ScoreboardHelper.getScoreboard(id.asString());
} else {
board = ScoreboardHelper.createScoreboard(id.asString());
}
}
Team team = board.getTeam(name.asString());
if (team == null) {
String low = CoreUtilities.toLowerCase(name.asString());
team = board.getTeams().stream().filter(t -> CoreUtilities.toLowerCase(t.getName()).equals(low)).findFirst().orElse(null);
if (team == null) {
team = board.registerNewTeam(name.asString());
}
}
if (add != null) {
for (String string : add) {
string = translateEntry(string, scriptEntry.context);
if (!team.hasEntry(string)) {
team.addEntry(string);
}
}
}
if (remove != null) {
for (String string : remove) {
string = translateEntry(string, scriptEntry.context);
if (team.hasEntry(string)) {
team.removeEntry(string);
}
}
}
if (option != null) {
String optName = CoreUtilities.toLowerCase(option.asString());
String statusName = CoreUtilities.toLowerCase(status.asString());
if (optName.equals("friendly_fire")) {
team.setAllowFriendlyFire(statusName.equals("always"));
} else if (optName.equals("see_invisible")) {
team.setCanSeeFriendlyInvisibles(statusName.equals("always"));
} else {
team.setOption(Team.Option.valueOf(optName.toUpperCase()), Team.OptionStatus.valueOf(statusName.toUpperCase()));
}
}
if (prefix != null) {
team.setPrefix(prefix.asString());
}
if (suffix != null) {
team.setSuffix(suffix.asString());
}
if (color != null) {
team.setColor(ChatColor.valueOf(color.asString().toUpperCase()));
}
if (team.getEntries().isEmpty()) {
team.unregister();
}
}
use of com.denizenscript.denizencore.objects.core.ListTag in project Denizen-For-Bukkit by DenizenScript.
the class ModifyBlockCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final ListTag materials = scriptEntry.getObjectTag("materials");
final List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
final ListTag location_list = scriptEntry.getObjectTag("location_list");
final ElementTag physics = scriptEntry.getElement("physics");
final ItemTag natural = scriptEntry.getObjectTag("natural");
final ElementTag delayed = scriptEntry.getElement("delayed");
final ElementTag maxDelayMs = scriptEntry.getElement("max_delay_ms");
final ElementTag radiusElement = scriptEntry.getElement("radius");
final ElementTag heightElement = scriptEntry.getElement("height");
final ElementTag depthElement = scriptEntry.getElement("depth");
final ScriptTag script = scriptEntry.getObjectTag("script");
final PlayerTag source = scriptEntry.getObjectTag("source");
ListTag percents = scriptEntry.getObjectTag("percents");
if (percents != null && percents.size() != materials.size()) {
Debug.echoError(scriptEntry, "Percents length != materials length");
percents = null;
}
final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);
for (MaterialTag mat : materialList) {
if (!mat.getMaterial().isBlock()) {
Debug.echoError("Material '" + mat.getMaterial().name() + "' is not a block material");
return;
}
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), materials, physics, radiusElement, heightElement, depthElement, natural, delayed, maxDelayMs, script, percents, source, (locations == null ? location_list : db("locations", locations)));
}
Player sourcePlayer = source == null ? null : source.getPlayerEntity();
final boolean doPhysics = physics.asBoolean();
final int radius = radiusElement.asInt();
final int height = heightElement.asInt();
final int depth = depthElement.asInt();
List<Float> percentages = null;
if (percents != null) {
percentages = new ArrayList<>();
for (String str : percents) {
percentages.add(new ElementTag(str).asFloat());
}
}
final List<Float> percs = percentages;
if (locations == null && location_list == null) {
Debug.echoError("Must specify a valid location!");
return;
}
if ((location_list != null && location_list.isEmpty()) || (locations != null && locations.isEmpty())) {
return;
}
if (materialList.isEmpty()) {
Debug.echoError("Must specify a valid material!");
return;
}
no_physics = !doPhysics;
if (delayed.asBoolean()) {
final long maxDelay = maxDelayMs.asLong();
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
try {
long start = System.currentTimeMillis();
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
} else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
boolean was_static = preSetup(loc);
while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
LocationTag nLoc;
if (locations != null) {
nLoc = locations.get(index);
} else {
nLoc = getLocAt(location_list, index, scriptEntry);
}
if (isLocationBad(scriptEntry, nLoc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
handleLocation(nLoc, index, materialList, doPhysics, natural, radius, height, depth, percs, sourcePlayer, scriptEntry);
index++;
if (System.currentTimeMillis() - start > maxDelay) {
break;
}
}
postComplete(loc, was_static);
if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
if (script != null) {
ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, null, null, null, null, scriptEntry);
}
scriptEntry.setFinished(true);
cancel();
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
} else {
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
} else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
return;
}
boolean was_static = preSetup(loc);
int index = 0;
if (locations != null) {
for (LocationTag obj : locations) {
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
} else {
for (int i = 0; i < location_list.size(); i++) {
LocationTag obj = getLocAt(location_list, i, scriptEntry);
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
use of com.denizenscript.denizencore.objects.core.ListTag in project Denizen-For-Bukkit by DenizenScript.
the class BossBarCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag action = scriptEntry.getElement("action");
ListTag players = scriptEntry.getObjectTag("players");
ElementTag title = scriptEntry.getElement("title");
ElementTag progress = scriptEntry.getElement("progress");
ElementTag color = scriptEntry.getElement("color");
ElementTag style = scriptEntry.getElement("style");
ListTag options = scriptEntry.getObjectTag("options");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, action, players, title, progress, color, style, options);
}
String idString = CoreUtilities.toLowerCase(id.asString());
switch(Action.valueOf(action.asString().toUpperCase())) {
case CREATE:
{
if (bossBarMap.containsKey(idString)) {
Debug.echoError("BossBar '" + idString + "' already exists!");
return;
}
String barTitle = title != null ? title.asString() : "";
List<PlayerTag> barPlayers = players.filter(PlayerTag.class, scriptEntry);
double barProgress = progress != null ? progress.asDouble() : 1D;
BarColor barColor = color != null ? BarColor.valueOf(color.asString().toUpperCase()) : BarColor.WHITE;
BarStyle barStyle = style != null ? BarStyle.valueOf(style.asString().toUpperCase()) : BarStyle.SOLID;
BarFlag[] barFlags = new BarFlag[options != null ? options.size() : 0];
if (options != null) {
for (int i = 0; i < options.size(); i++) {
barFlags[i] = (BarFlag.valueOf(options.get(i).toUpperCase()));
}
}
BossBar bossBar = Bukkit.createBossBar(barTitle, barColor, barStyle, barFlags);
NMSHandler.getPlayerHelper().setBossBarTitle(bossBar, barTitle);
bossBar.setProgress(barProgress);
for (PlayerTag player : barPlayers) {
if (!player.isOnline()) {
Debug.echoError("Player must be online to show a BossBar to them!");
continue;
}
bossBar.addPlayer(player.getPlayerEntity());
}
bossBar.setVisible(true);
bossBarMap.put(idString, bossBar);
break;
}
case UPDATE:
{
if (!bossBarMap.containsKey(idString)) {
Debug.echoError("BossBar '" + idString + "' does not exist!");
return;
}
BossBar bossBar1 = bossBarMap.get(idString);
if (title != null) {
NMSHandler.getPlayerHelper().setBossBarTitle(bossBar1, title.asString());
}
if (progress != null) {
bossBar1.setProgress(progress.asDouble());
}
if (color != null) {
bossBar1.setColor(BarColor.valueOf(color.asString().toUpperCase()));
}
if (style != null) {
bossBar1.setStyle(BarStyle.valueOf(style.asString().toUpperCase()));
}
if (options != null) {
HashSet<BarFlag> oldFlags = new HashSet<>(Arrays.asList(BarFlag.values()));
HashSet<BarFlag> newFlags = new HashSet<>(options.size());
for (String flagName : options) {
BarFlag flag = BarFlag.valueOf(flagName.toUpperCase());
newFlags.add(flag);
oldFlags.remove(flag);
}
for (BarFlag flag : oldFlags) {
bossBar1.removeFlag(flag);
}
for (BarFlag flag : newFlags) {
bossBar1.addFlag(flag);
}
}
if (players != null) {
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
bossBar1.addPlayer(player.getPlayerEntity());
}
}
break;
}
case REMOVE:
{
if (!bossBarMap.containsKey(idString)) {
Debug.echoError("BossBar '" + idString + "' does not exist!");
return;
}
if (players != null) {
BossBar bar = bossBarMap.get(idString);
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
bar.removePlayer(player.getPlayerEntity());
}
break;
}
bossBarMap.get(idString).setVisible(false);
bossBarMap.remove(idString);
break;
}
}
}
Aggregations