use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class InventoryHolder method registerTags.
public static void registerTags() {
// <--[tag]
// @attribute <InventoryTag.id_holder>
// @returns ObjectTag
// @group properties
// @description
// Returns Denizen's holder ID for this inventory. (player object, location object, etc.)
// -->
PropertyParser.<InventoryHolder, ObjectTag>registerTag(ObjectTag.class, "id_holder", (attribute, object) -> {
return object.inventory.getIdHolder();
});
// <--[tag]
// @attribute <InventoryTag.script>
// @returns ScriptTag
// @group properties
// @description
// Returns the script that this inventory came from (if any).
// -->
PropertyParser.<InventoryHolder, ScriptTag>registerTag(ScriptTag.class, "script", (attribute, object) -> {
ObjectTag holder = object.inventory.getIdHolder();
if (holder instanceof ScriptTag) {
return (ScriptTag) holder;
}
return null;
});
}
use of com.denizenscript.denizencore.objects.ObjectTag 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.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ClickableCommand method runClickable.
public static void runClickable(UUID id, Player player) {
Clickable clickable = clickables.get(id);
if (clickable == null) {
return;
}
if (clickable.until != 0 && System.currentTimeMillis() > clickable.until) {
clickables.remove(id);
return;
}
if (clickable.forPlayers != null && !clickable.forPlayers.contains(player.getUniqueId())) {
return;
}
if (clickable.remainingUsages > 0) {
clickable.remainingUsages--;
if (clickable.remainingUsages <= 0) {
clickables.remove(id);
}
}
Consumer<ScriptQueue> configure = (queue) -> {
if (clickable.defMap != null) {
for (Map.Entry<StringHolder, ObjectTag> val : clickable.defMap.map.entrySet()) {
queue.addDefinition(val.getKey().str, val.getValue());
}
}
};
ScriptUtilities.createAndStartQueue(clickable.script.getContainer(), clickable.path, new BukkitScriptEntryData(new PlayerTag(player), clickable.npc), null, configure, null, null, clickable.definitions, clickable.context);
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class BreakCommand method execute.
// <--[action]
// @Actions
// dig
//
// @Triggers when the NPC breaks a block with the Break Command
//
// @Context
// <context.location> returns the location the NPC Dug
// <context.material> Returns the Block dug
//
// -->
@Override
public void execute(ScriptEntry scriptEntry) {
final LocationTag location = scriptEntry.getObjectTag("location");
final NPCTag npc = scriptEntry.getObjectTag("npc");
ElementTag radius = scriptEntry.getElement("radius");
final HashMap<String, ObjectTag> context = new HashMap<>();
MaterialTag material = new MaterialTag(location.getBlock());
context.put("location", location);
context.put("material", material);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, npc, radius);
}
final ScriptEntry se = scriptEntry;
BlockBreaker.BlockBreakerConfiguration config = new BlockBreaker.BlockBreakerConfiguration();
config.item(npc.getLivingEntity().getEquipment().getItemInMainHand());
config.radius(radius.asDouble());
config.callback(() -> {
npc.action("dig", null, context);
se.setFinished(true);
});
BlockBreaker breaker = npc.getCitizen().getBlockBreaker(location.getBlock(), config);
if (breaker.shouldExecute()) {
TaskRunnable run = new TaskRunnable(breaker);
run.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), run, 0, 1);
} else {
se.setFinished(true);
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class DamageTrigger method damageTrigger.
// <--[language]
// @name Damage Triggers
// @group NPC Interact Scripts
// @description
// Damage Triggers are triggered when when a player left clicks the NPC.
// Despite the name, these do not actually require the NPC take any damage, only that the player left clicks the NPC.
//
// In scripts, use <context.damage> to measure how much damage was done to the NPC
// (though note that invincible NPCs don't necessarily take any damage even when this is non-zero).
//
// These are very basic with no extraneous complexity.
//
// -->
// <--[action]
// @Actions
// no damage trigger
//
// @Triggers when the NPC is damaged by a player but no damage trigger fires.
//
// @Context
// None
//
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// damage
//
// @Triggers when the NPC is damaged by a player.
//
// @Context
// <context.damage> returns how much damage was done.
//
// @Determine
// "cancelled" to cancel the damage event.
//
// -->
// <--[action]
// @Actions
// damaged
//
// @Triggers when the NPC is damaged by an entity.
//
// @Context
// <context.damage> returns how much damage was done.
// <context.damager> returns the entity that did the damage.
//
// @Determine
// "cancelled" to cancel the damage event.
//
// -->
@EventHandler
public void damageTrigger(EntityDamageByEntityEvent event) {
Map<String, ObjectTag> context = new HashMap<>();
context.put("damage", new ElementTag(event.getDamage()));
if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
if (npc == null) {
return;
}
if (npc.getCitizen() == null) {
return;
}
EntityTag damager = new EntityTag(event.getDamager());
if (damager.isProjectile() && damager.hasShooter()) {
damager = damager.getShooter();
}
context.put("damager", damager.getDenizenObject());
ListTag determ = npc.action("damaged", null, context);
if (determ != null && determ.containsCaseInsensitive("cancelled")) {
event.setCancelled(true);
return;
}
if (!damager.isPlayer()) {
return;
}
PlayerTag dplayer = damager.getDenizenPlayer();
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return;
}
if (!npc.getTriggerTrait().isEnabled(name)) {
return;
}
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
if (!trigger.wasTriggered()) {
return;
}
if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
event.setCancelled(true);
return;
}
List<InteractScriptContainer> scripts = InteractScriptHelper.getInteractScripts(npc, dplayer, true, ClickTrigger.class);
boolean any = false;
if (scripts != null) {
for (InteractScriptContainer script : scripts) {
String id = null;
Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, dplayer);
if (!idMap.isEmpty()) {
for (Map.Entry<String, String> entry : idMap.entrySet()) {
String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, null, false, new ScriptTag(script)));
if (ItemTag.valueOf(entry_value, script).comparesTo(dplayer.getPlayerEntity().getEquipment().getItemInMainHand()) >= 0) {
id = entry.getKey();
}
}
}
if (parse(npc, dplayer, script, id, context)) {
any = true;
}
}
}
if (!any) {
npc.action("no damage trigger", dplayer);
}
}
}
Aggregations