use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class PlayerPreLoginScriptEvent method onPlayerLogin.
@EventHandler
public void onPlayerLogin(AsyncPlayerPreLoginEvent event) {
if (!Bukkit.isPrimaryThread()) {
PlayerPreLoginScriptEvent altEvent = (PlayerPreLoginScriptEvent) clone();
Future future = Bukkit.getScheduler().callSyncMethod(Denizen.getInstance(), () -> {
altEvent.onPlayerLogin(event);
return null;
});
try {
future.get(30, TimeUnit.SECONDS);
while (altEvent.needsToWait()) {
Thread.sleep(50);
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
return;
}
waitForQueues = new ArrayList<>();
OfflinePlayer bukkitPlayer = Bukkit.getOfflinePlayer(event.getUniqueId());
if (bukkitPlayer != null && bukkitPlayer.getName() != null) {
player = new PlayerTag(bukkitPlayer);
} else {
player = null;
}
this.event = event;
fire(event);
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class BukkitScriptProperties method registerTags.
public static void registerTags() {
// <--[tag]
// @attribute <ScriptTag.cooled_down[<player>]>
// @returns ElementTag(Boolean)
// @description
// Returns whether the script is currently cooled down for the player. Any global
// cooldown present on the script will also be taken into account. Not specifying a player will result in
// using the attached player available in the script entry. Not having a valid player will result in 'null'.
// -->
PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "cooled_down", (attribute, script) -> {
PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
if (player != null && player.isValid()) {
return new ElementTag(CooldownCommand.checkCooldown(player, script.script.getContainer().getName()));
} else {
return null;
}
});
// <--[tag]
// @attribute <ScriptTag.cooldown[<player>]>
// @returns DurationTag
// @description
// Returns the time left for the player to cooldown for the script.
// -->
PropertyParser.<BukkitScriptProperties, DurationTag>registerTag(DurationTag.class, "cooldown", (attribute, script) -> {
PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
return CooldownCommand.getCooldownDuration(player, script.script.getName());
});
// <--[tag]
// @attribute <ScriptTag.step[(<player>)]>
// @returns ElementTag
// @description
// Returns the name of a script step that the player is currently on.
// Must be an INTERACT script.
// -->
PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "step", (attribute, script) -> {
PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
if (player != null && player.isValid()) {
return new ElementTag(InteractScriptHelper.getCurrentStep(player, script.script.getContainer().getName()));
} else {
return null;
}
});
// <--[tag]
// @attribute <ScriptTag.step_expiration[(<player>)]>
// @returns TimeTag
// @description
// Returns the time that an interact script step expires at, if applied by <@link command zap> with a duration limit.
// -->
PropertyParser.<BukkitScriptProperties, TimeTag>registerTag(TimeTag.class, "step_expiration", (attribute, script) -> {
PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
if (player != null && player.isValid()) {
return InteractScriptHelper.getStepExpiration(player, script.script.getContainer().getName());
} else {
return null;
}
});
// <--[tag]
// @attribute <ScriptTag.default_step>
// @returns ElementTag
// @description
// Returns the name of the default step of an interact script.
// -->
PropertyParser.<BukkitScriptProperties, ElementTag>registerStaticTag(ElementTag.class, "default_step", (attribute, script) -> {
String step = ((InteractScriptContainer) script.script.getContainer()).getDefaultStepName();
return new ElementTag(step);
});
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class RenameCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final ElementTag name = scriptEntry.getElement("name");
ElementTag perPlayer = scriptEntry.getElement("per_player");
ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
ListTag targets = scriptEntry.getObjectTag("targets");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (perPlayer != null && perPlayer.asBoolean()) {
NetworkInterceptHelper.enable();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
}
for (ObjectTag target : targets.objectForms) {
EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity != null) {
Entity bukkitEntity = entity.getBukkitEntity();
if (bukkitEntity == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (name.asString().equals("cancel")) {
customNames.remove(bukkitEntity.getUniqueId());
if (bukkitEntity.isCustomNameVisible()) {
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
}
}
} else {
bukkitEntity.setCustomNameVisible(true);
// Force a metadata update
bukkitEntity.setCustomNameVisible(false);
}
} else {
final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
Function<Player, String> nameGetter = p -> {
originalContext.player = new PlayerTag(p);
return TagManager.tag(name.asString(), originalContext);
};
RenameData renamer = new RenameData();
renamer.nameFunction = nameGetter;
renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
if (players == null) {
playerToFuncMap.put(null, renamer);
} else {
for (PlayerTag player : players) {
playerToFuncMap.put(player.getUUID(), renamer);
}
}
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
}
}
}
}
}
return;
}
String nameString = TagManager.tag(name.asString(), scriptEntry.context);
if (nameString.length() > 256) {
nameString = nameString.substring(0, 256);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("name", nameString), listNameOnly, targets);
}
for (ObjectTag target : targets.objectForms) {
EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity == null) {
entity = target.asType(NPCTag.class, scriptEntry.context);
} else {
entity = ((EntityTag) entity).getDenizenObject();
}
if (entity == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (entity instanceof NPCTag) {
NPC npc = ((NPCTag) entity).getCitizen();
if (npc.isSpawned()) {
Location prev = npc.getStoredLocation().clone();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(nameString);
npc.spawn(prev);
} else {
npc.setName(nameString);
}
} else if (entity instanceof PlayerTag) {
if (listNameOnly != null && listNameOnly.asBoolean()) {
AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
} else {
String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
}
} else {
Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
customNames.remove(bukkitEntity.getUniqueId());
bukkitEntity.setCustomName(nameString);
bukkitEntity.setCustomNameVisible(true);
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class AnimateCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for");
PlayerAnimation animation = scriptEntry.hasObject("animation") ? (PlayerAnimation) scriptEntry.getObject("animation") : null;
EntityEffect effect = scriptEntry.hasObject("effect") ? (EntityEffect) scriptEntry.getObject("effect") : null;
String nmsAnimation = scriptEntry.hasObject("nms_animation") ? (String) scriptEntry.getObject("nms_animation") : null;
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (animation != null ? db("animation", animation.name()) : effect != null ? db("effect", effect.name()) : db("animation", nmsAnimation)), db("entities", entities), db("for", forPlayers));
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
try {
if (animation != null && entity.getBukkitEntity() instanceof Player) {
Player player = (Player) entity.getBukkitEntity();
animation.play(player);
} else if (effect != null) {
if (forPlayers != null) {
for (PlayerTag player : forPlayers) {
NMSHandler.getPacketHelper().sendEntityEffect(player.getPlayerEntity(), entity.getBukkitEntity(), effect.getData());
}
} else {
entity.getBukkitEntity().playEffect(effect);
}
} else if (nmsAnimation != null) {
EntityAnimation entityAnimation = NMSHandler.getAnimationHelper().getEntityAnimation(nmsAnimation);
entityAnimation.play(entity.getBukkitEntity());
} else {
Debug.echoError("No way to play the given animation on entity '" + entity + "'");
}
} catch (Exception e) {
Debug.echoError(scriptEntry, "Error playing that animation!");
Debug.echoError(e);
}
}
}
}
use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.
the class FakeEquipCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
NetworkInterceptHelper.enable();
EquipmentOverride equipment = (EquipmentOverride) scriptEntry.getObject("equipment");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> playersFor = (List<PlayerTag>) scriptEntry.getObject("for");
ElementTag reset = scriptEntry.getElement("reset");
DurationTag duration = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment), reset, duration, db("for", playersFor));
}
boolean isReset = reset != null && reset.asBoolean();
for (PlayerTag player : playersFor) {
HashMap<UUID, EquipmentOverride> playersMap = overrides.computeIfAbsent(player.getUUID(), (k) -> new HashMap<>());
for (EntityTag entity : entities) {
if (entity.isGeneric()) {
Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
continue;
}
LivingEntity livingEntity = entity.getLivingEntity();
if (livingEntity == null) {
Debug.echoError(scriptEntry, "Cannot equip invalid/non-living entity " + entity.identify() + "!");
continue;
}
EquipmentOverride entityData;
if (isReset) {
entityData = playersMap.remove(entity.getUUID());
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
} else {
entityData = playersMap.computeIfAbsent(entity.getUUID(), (k) -> new EquipmentOverride());
entityData.copyFrom(equipment);
}
if (entityData != null) {
if (entityData.cancelTask != null && !entityData.cancelTask.isCancelled()) {
entityData.cancelTask.cancel();
entityData.cancelTask = null;
}
if (duration != null && duration.getTicks() > 0) {
entityData.cancelTask = new BukkitRunnable() {
@Override
public void run() {
entityData.cancelTask = null;
HashMap<UUID, EquipmentOverride> playersMap = overrides.get(player.getUUID());
if (playersMap != null) {
if (playersMap.remove(entity.getUUID()) != null) {
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
}.runTaskLater(Denizen.getInstance(), duration.getTicks());
}
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
Aggregations