use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class EngageCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
if (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsRuntimeException("This command requires a linked NPC!");
}
DurationTag duration = scriptEntry.getObjectTag("duration");
boolean linkedPlayer = scriptEntry.argAsBoolean("player");
NPCTag npc = Utilities.getEntryNPC(scriptEntry);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), npc, duration, db("player", linkedPlayer));
}
if (duration.getSecondsAsInt() > 0) {
setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, duration.getSecondsAsInt());
} else {
setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, true);
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class FishCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag catchtype = scriptEntry.getElement("catch");
ElementTag stop = scriptEntry.getElement("stop");
ElementTag percent = scriptEntry.getElement("percent");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, catchtype, percent, stop);
}
NPCTag npc = Utilities.getEntryNPC(scriptEntry);
FishingTrait trait = npc.getFishingTrait();
if (stop.asBoolean()) {
trait.stopFishing();
return;
}
npc.getEquipmentTrait().set(0, new ItemStack(Material.FISHING_ROD));
trait.setCatchPercent(percent.asInt());
trait.setCatchType(FishingHelper.CatchType.valueOf(catchtype.asString().toUpperCase()));
trait.startFishing(location);
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class PushableCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
NPCTag denizenNPC = Utilities.getEntryNPC(scriptEntry);
if (denizenNPC == null) {
Debug.echoError("No valid NPC attached to this queue!");
return;
}
PushableTrait trait = denizenNPC.getPushableTrait();
ElementTag state = scriptEntry.getElement("state");
DurationTag delay = scriptEntry.getObjectTag("delay");
ElementTag returnable = scriptEntry.getElement("return");
if (state == null && delay == null && returnable == null) {
state = new ElementTag("TOGGLE");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), denizenNPC, state, delay, returnable);
}
if (delay != null) {
trait.setDelay(delay.getSecondsAsInt());
}
if (returnable != null) {
trait.setReturnable(returnable.asBoolean());
}
if (state != null) {
switch(Toggle.valueOf(state.asString().toUpperCase())) {
case TRUE:
case ON:
trait.setPushable(true);
break;
case FALSE:
case OFF:
trait.setPushable(false);
break;
case TOGGLE:
trait.setPushable(!trait.isPushable());
break;
}
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class LegacySavesUpdater method updateLegacySaves.
public static void updateLegacySaves() {
Debug.log("==== UPDATING LEGACY SAVES TO NEW FLAG ENGINE ====");
File savesFile = new File(Denizen.getInstance().getDataFolder(), "saves.yml");
if (!savesFile.exists()) {
Debug.echoError("Legacy update went weird: file doesn't exist?");
return;
}
YamlConfiguration saveSection;
try {
FileInputStream fis = new FileInputStream(savesFile);
String saveData = ScriptHelper.convertStreamToString(fis, false);
fis.close();
if (saveData.trim().length() == 0) {
Debug.log("Nothing to update.");
savesFile.delete();
return;
}
saveSection = YamlConfiguration.load(saveData);
if (saveSection == null) {
Debug.echoError("Something went very wrong: legacy saves file failed to load!");
return;
}
} catch (Throwable ex) {
Debug.echoError(ex);
return;
}
if (!savesFile.renameTo(new File(Denizen.getInstance().getDataFolder(), "saves.yml.bak"))) {
Debug.echoError("Legacy saves file failed to rename!");
}
if (saveSection.contains("Global")) {
Debug.log("==== Update global data ====");
YamlConfiguration globalSection = saveSection.getConfigurationSection("Global");
if (globalSection.contains("Flags")) {
applyFlags("Server", DenizenCore.serverFlagMap, globalSection.getConfigurationSection("Flags"));
}
if (globalSection.contains("Scripts")) {
YamlConfiguration scriptsSection = globalSection.getConfigurationSection("Scripts");
for (StringHolder script : scriptsSection.getKeys(false)) {
YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
if (scriptSection.contains("Cooldown Time")) {
long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
TimeTag cooldown = new TimeTag(time);
DenizenCore.serverFlagMap.setFlag("__interact_cooldown." + script.low, cooldown, cooldown);
}
}
}
}
if (saveSection.contains("Players")) {
Debug.log("==== Update player data ====");
YamlConfiguration playerSection = saveSection.getConfigurationSection("Players");
for (StringHolder plPrefix : playerSection.getKeys(false)) {
YamlConfiguration subSection = playerSection.getConfigurationSection(plPrefix.str);
for (StringHolder uuidString : subSection.getKeys(false)) {
if (uuidString.str.length() != 32) {
Debug.echoError("Cannot update data for player with non-ID entry listed: " + uuidString);
continue;
}
try {
UUID id = UUID.fromString(uuidString.str.substring(0, 8) + "-" + uuidString.str.substring(8, 12) + "-" + uuidString.str.substring(12, 16) + "-" + uuidString.str.substring(16, 20) + "-" + uuidString.str.substring(20, 32));
PlayerTag player = PlayerTag.valueOf(id.toString(), CoreUtilities.errorButNoDebugContext);
if (player == null) {
Debug.echoError("Cannot update data for player with id: " + uuidString);
continue;
}
YamlConfiguration actual = subSection.getConfigurationSection(uuidString.str);
AbstractFlagTracker tracker = player.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(player.identify(), tracker, actual.getConfigurationSection("Flags"));
}
if (actual.contains("Scripts")) {
YamlConfiguration scriptsSection = actual.getConfigurationSection("Scripts");
for (StringHolder script : scriptsSection.getKeys(false)) {
YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
if (scriptSection.contains("Current Step")) {
tracker.setFlag("__interact_step." + script, new ElementTag(scriptSection.getString("Current Step")), null);
}
if (scriptSection.contains("Cooldown Time")) {
long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
TimeTag cooldown = new TimeTag(time);
tracker.setFlag("__interact_cooldown." + script, cooldown, cooldown);
}
}
}
player.reapplyTracker(tracker);
} catch (Throwable ex) {
Debug.echoError("Error updating flags for player with ID " + uuidString.str);
Debug.echoError(ex);
}
}
}
}
if (saveSection.contains("NPCs")) {
final YamlConfiguration npcsSection = saveSection.getConfigurationSection("NPCs");
new BukkitRunnable() {
@Override
public void run() {
Debug.log("==== Late update NPC data ====");
for (StringHolder npcId : npcsSection.getKeys(false)) {
YamlConfiguration actual = npcsSection.getConfigurationSection(npcId.str);
NPCTag npc = NPCTag.valueOf(npcId.str, CoreUtilities.errorButNoDebugContext);
if (npc == null) {
Debug.echoError("Cannot update data for NPC with id: " + npcId.str);
continue;
}
AbstractFlagTracker tracker = npc.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(npc.identify(), tracker, actual.getConfigurationSection("Flags"));
}
npc.reapplyTracker(tracker);
Debug.log("==== Done late-updating NPC data ====");
}
}
}.runTaskLater(Denizen.getInstance(), 3);
}
Denizen.getInstance().saveSaves(true);
Debug.log("==== Done updating legacy saves (except NPCs) ====");
}
use of com.denizenscript.denizen.objects.NPCTag 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