Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
flag durations
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 13, 2017
1 parent d9f0fc1 commit b052765
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
Expand Up @@ -55,7 +55,7 @@ public void trace(Throwable e) {
Sponge.getServer().getConsole().sendMessage(TextSerializers.formattingCode(Denizen2Sponge.colorChar)
.deserialize((" " + e.getClass().getCanonicalName() + ": " + e.getMessage())));
for (StackTraceElement ste : e.getStackTrace()) {
Sponge.getServer().getConsole().sendMessage(TextSerializers.formattingCode(Denizen2Sponge.colorChar).deserialize((" " + ste.toString())));
Sponge.getServer().getConsole().sendMessage(TextSerializers.formattingCode(Denizen2Sponge.colorChar).deserialize((" at " + ste.toString())));
}
if (e.getCause() != e) {
trace(e.getCause());
Expand Down
Expand Up @@ -4,7 +4,9 @@
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.DurationTag;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.tags.objects.TimeTag;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
Expand All @@ -14,6 +16,9 @@
import com.denizenscript.denizen2sponge.utilities.flags.FlagMapDataImpl;
import org.spongepowered.api.entity.Entity;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.*;
import java.util.Map;
import java.util.Optional;

Expand All @@ -27,12 +32,16 @@ public class FlagCommand extends AbstractCommand {
// @Group Entity
// @Minimum 2
// @Maximum 2
// @Named duration (DurationTag) Sets the duration to apply to the flags being set.
// @Description
// Adds or edits flags on an entity (including players, etc.).
// See also the <@link command unflag>unflag command<@/link>.
// @Example
// # Mark the player as a VIP.
// - flag <player> vip:true
// @Example
// # Increase the player's XP by 5, reverting to 0 (unset) after one minute.
// - flag <player> xp:<player.flag[xp].add[5]||5> --duration 1m
// -->

@Override
Expand Down Expand Up @@ -60,6 +69,12 @@ public void execute(CommandQueue queue, CommandEntry entry) {
AbstractTagObject ato = entry.getArgumentObject(queue, 0);
MapTag basic;
Entity entity = null;
TimeTag tt = null;
if (entry.namedArgs.containsKey("duration")) {
DurationTag duration = DurationTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "duration"));
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("UTC")).plus((long)(duration.getInternal() * 1000), ChronoField.MILLI_OF_SECOND.getBaseUnit());
tt = new TimeTag(ldt);
}
if (CoreUtilities.toLowerCase(ato.toString()).equals("server")) {
basic = Denizen2Sponge.instance.serverFlagMap;
}
Expand All @@ -76,19 +91,26 @@ public void execute(CommandQueue queue, CommandEntry entry) {
}
MapTag propertyMap = MapTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
for (Map.Entry<String, AbstractTagObject> dat : propertyMap.getInternal().entrySet()) {
basic.getInternal().put(CoreUtilities.toLowerCase(dat.getKey()), dat.getValue());
MapTag gen = new MapTag();
gen.getInternal().put("value", dat.getValue());
if (tt != null) {
gen.getInternal().put("duration", tt);
}
basic.getInternal().put(CoreUtilities.toLowerCase(dat.getKey()), gen);
}
if (entity != null) {
entity.offer(new FlagMapDataImpl(new FlagMap(basic)));
if (queue.shouldShowGood()) {
queue.outGood("Flagged the entity "
+ ColorSet.emphasis + new EntityTag(entity).debug() + ColorSet.good
+ " with the specified data... (" + propertyMap.debug() + ")");
+ " with the specified data... (" + propertyMap.debug() + ")"
+ (tt == null ? " For unlimited time. " : " Until time: " + tt.debug()));
}
}
else {
if (queue.shouldShowGood()) {
queue.outGood("Flagged the server with the specified data... (" + propertyMap.debug() + ")");
queue.outGood("Flagged the server with the specified data... (" + propertyMap.debug() + ")"
+ (tt == null ? " For unlimited time. " : " Until time: " + tt.debug()));
}
}
}
Expand Down
Expand Up @@ -8,12 +8,14 @@
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.*;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.world.World;

import java.util.HashMap;
import java.util.Map;

public class ServerTagBase extends AbstractTagBase {

Expand Down Expand Up @@ -163,7 +165,7 @@ public String getName() {
handlers.put("has_flag", (dat, obj) -> {
String flagName = CoreUtilities.toLowerCase(dat.getNextModifier().toString());
MapTag flags = Denizen2Sponge.instance.serverFlagMap;
return new BooleanTag(flags.getInternal().containsKey(flagName));
return new BooleanTag(Utilities.flagIsValidAndNotExpired(dat.error, flags, flagName));
});
// <--[tag]
// @Name ServerBaseTag.flag[<TextTag>]
Expand All @@ -176,17 +178,30 @@ public String getName() {
handlers.put("flag", (dat, obj) -> {
MapTag flags = Denizen2Sponge.instance.serverFlagMap;
if (!dat.hasNextModifier()) {
return flags;
MapTag valid = new MapTag();
for (Map.Entry<String, AbstractTagObject> flag : flags.getInternal().entrySet()) {
if (Utilities.flagIsValidAndNotExpired(dat.error, flags, flag.getKey())) {
MapTag mt = MapTag.getFor(dat.error, flag.getKey());
valid.getInternal().put(flag.getKey(), mt.getInternal().get("value"));
}
}
return valid;
}
String flagName = CoreUtilities.toLowerCase(dat.getNextModifier().toString());
AbstractTagObject ato = flags.getInternal().get(flagName);
if (ato == null) {
if (!Utilities.flagIsValidAndNotExpired(dat.error, flags, flagName)) {
if (!dat.hasFallback()) {
dat.error.run("Invalid flag specified, not present on the server!");
}
return new NullTag();
}
MapTag smap = MapTag.getFor(dat.error, flags.getInternal().get(flagName));
if (smap == null) {
if (!dat.hasFallback()) {
dat.error.run("Invalid flag specified, not present on the server!");
}
return new NullTag();
}
return ato;
return smap.getInternal().get("value");
});
}

Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.Function2;
import com.denizenscript.denizen2sponge.utilities.DataKeys;
import com.denizenscript.denizen2sponge.utilities.Utilities;
import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMap;
import org.spongepowered.api.Sponge;
Expand Down Expand Up @@ -236,7 +237,7 @@ public String friendlyName() {
// @Updated 2016/10/26
// @Group Flag Data
// @ReturnType BooleanTag
// @Returns whether the entity has a flag with the specified key.
// @Returns whether the entity has a flag with the specified key. (And it is not expired).
// -->
handlers.put("has_flag", (dat, obj) -> {
String flagName = CoreUtilities.toLowerCase(dat.getNextModifier().toString());
Expand All @@ -249,15 +250,16 @@ public String friendlyName() {
else {
flags = new MapTag();
}
return new BooleanTag(flags.getInternal().containsKey(flagName));
return new BooleanTag(Utilities.flagIsValidAndNotExpired(dat.error, flags, flagName));
});
// <--[tag]
// @Name EntityTag.flag[<TextTag>]
// @Updated 2016/10/26
// @Group Flag Data
// @ReturnType Dynamic
// @Returns the flag of the specified key from the entity. May become TextTag regardless of input original type.
// @Returns the flag of the specified key from the entity. (And it is not expired).
// Optionally don't specify anything to get the entire flag map.
// Note that flag map uses a s
// -->
handlers.put("flag", (dat, obj) -> {
MapTag flags;
Expand All @@ -270,17 +272,30 @@ public String friendlyName() {
flags = new MapTag();
}
if (!dat.hasNextModifier()) {
return flags;
MapTag valid = new MapTag();
for (Map.Entry<String, AbstractTagObject> flag : flags.getInternal().entrySet()) {
if (Utilities.flagIsValidAndNotExpired(dat.error, flags, flag.getKey())) {
MapTag mt = MapTag.getFor(dat.error, flag.getKey());
valid.getInternal().put(flag.getKey(), mt.getInternal().get("value"));
}
}
return valid;
}
String flagName = CoreUtilities.toLowerCase(dat.getNextModifier().toString());
AbstractTagObject ato = flags.getInternal().get(flagName);
if (ato == null) {
if (!Utilities.flagIsValidAndNotExpired(dat.error, flags, flagName)) {
if (!dat.hasFallback()) {
dat.error.run("Invalid flag specified, not present on this entity!");
}
return new NullTag();
}
MapTag smap = MapTag.getFor(dat.error, flags.getInternal().get(flagName));
if (smap == null) {
if (!dat.hasFallback()) {
dat.error.run("Invalid flag specified, not present on this entity!");
}
return new NullTag();
}
return ato;
return smap.getInternal().get("value");
});
// <--[tag]
// @Name EntityTag.passengers
Expand Down
@@ -1,11 +1,32 @@
package com.denizenscript.denizen2sponge.utilities;

import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.tags.objects.TimeTag;
import com.denizenscript.denizen2core.utilities.Action;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;

import java.time.LocalDateTime;
import java.time.ZoneId;

public class Utilities {

public static double getHandReach(Player player) {
return player.gameMode().equals(GameModes.CREATIVE) ? 5.0 : 4.0;
}

public static boolean flagIsValidAndNotExpired(Action<String> error, MapTag flags, String flagName) {
boolean b = false;
if (flags.getInternal().containsKey(flagName)) {
b = true;
MapTag subMap = MapTag.getFor(error, flags.getInternal().get(flagName));
if (subMap.getInternal().containsKey("duration")) {
TimeTag tt = TimeTag.getFor(error, subMap.getInternal().get("duration"));
if (tt.getInternal().isBefore(LocalDateTime.now(ZoneId.of("UTC")))) {
b = false;
}
}
}
return b;
}
}

0 comments on commit b052765

Please sign in to comment.