Skip to content

Commit

Permalink
enchantment script: after attack/hurt
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 24, 2021
1 parent 3d2e62a commit 180da25
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
@@ -1,14 +1,18 @@
package com.denizenscript.denizen.scripts.containers.core;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ContextSource;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
Expand All @@ -18,6 +22,7 @@
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
Expand All @@ -31,9 +36,11 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// @description
// Enchantment script containers allow you to register custom item enchantments.
// For the most part, they work similarly to vanilla enchantments, albeit with some limitations.
// These can be attached to enchanted books and used in anvils, and can be generated by the enchanting table (requires discoverable: true and treasure_only: false).
// These can be attached to enchanted books and used in anvils,
// and can be generated by the enchanting table (requires discoverable: true and treasure_only: false).
//
// In current implementation, custom enchantments do not appear in lore on their own, and will need fake lore added in their place. This may be fixed in the future.
// In current implementation, custom enchantments do not appear in lore on their own, and will need fake lore added in their place.
// This might be fixed in the future.
//
// It may be beneficial in some cases to restart your server after making changes to enchantments, rather than just reloading scripts.
//
Expand Down Expand Up @@ -61,7 +68,8 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts should have this key!
// rarity: common
//
// # The category/type of this enchantment. Can be any of: ARMOR, ARMOR_FEET, ARMOR_LEGS, ARMOR_CHEST, ARMOR_HEAD, WEAPON, DIGGER, FISHING_ROD, TRIDENT, BREAKABLE, BOW, WEARABLE, CROSSBOW, VANISHABLE
// # The category/type of this enchantment. Can be any of: ARMOR, ARMOR_FEET, ARMOR_LEGS, ARMOR_CHEST, ARMOR_HEAD,
// # WEAPON, DIGGER, FISHING_ROD, TRIDENT, BREAKABLE, BOW, WEARABLE, CROSSBOW, VANISHABLE
// # If unspecified, will use WEAPON.
// # | Most enchantment scripts should have this key!
// category: weapon
Expand Down Expand Up @@ -106,7 +114,8 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts can exclude this key.
// is_tradable: true
//
// # Whether this enchantment is only considered to be discoverable. If true, this will spawn from vanilla sources like the enchanting table. If false, it can only be given directly by script.
// # Whether this enchantment is only considered to be discoverable.
// # If true, this will spawn from vanilla sources like the enchanting table. If false, it can only be given directly by script.
// # If unspecified, will be true.
// # | Most enchantment scripts can exclude this key.
// is_discoverable: true
Expand All @@ -123,19 +132,33 @@ public class EnchantmentScriptContainer extends ScriptContainer {
// # | Most enchantment scripts can exclude this key.
// can_enchant: <context.item.advanced_matches[*_sword|*_axe]>
//
// # A tag that returns an integer number indicating how much extra damage this item should deal.
// # Can make use of "<context.level>" for the enchantment level, and "<context.type>" for the type of monster being fought: ARTHROPOD, ILLAGER, WATER, UNDEAD, or UNDEFINED
// # If unspecified, will default to 0.
// # A tag that returns a decimal number indicating how much extra damage this item should deal.
// # Can make use of "<context.level>" for the enchantment level,
// # and "<context.type>" for the type of monster being fought: ARTHROPOD, ILLAGER, WATER, UNDEAD, or UNDEFINED
// # If unspecified, will default to 0.0.
// # | Most enchantment scripts can exclude this key.
// damage_bonus: 0
// damage_bonus: 0.0
//
// # A tag that returns a decimal number indicating how much this item should protection against damage.
// # A tag that returns an integer number indicating how much this item should protection against damage.
// # Can make use of "<context.level>" for the enchantment level, and "<context.cause>" for the applicable damage cause, using internal cause names.
// # Internal cause names: inFire, lightningBolt, onFire, lava, hotFloor, inWall, cramming, drown, starve, cactus, fall, flyIntoWall, outOfWorld, generic, magic, wither, anvil, fallingBlock, dragonBreath, dryout, sweetBerryBush, freeze, fallingStalactite, stalagmite
// # Internal cause names: inFire, lightningBolt, onFire, lava, hotFloor, inWall, cramming, drown, starve, cactus, fall, flyIntoWall,
// # outOfWorld, generic, magic, wither, anvil, fallingBlock, dragonBreath, dryout, sweetBerryBush, freeze, fallingStalactite, stalagmite
// # If unspecified, will default to 0.
// # | Most enchantment scripts can exclude this key.
// damage_protection: 0.0
// damage_protection: 0
//
// # Triggered after an enchanted weapon is used to attack an entity.
// # Has <context.attacker> (EntityTag), <context.victim> (EntityTag), and <context.level>.
// # May fire rapidly or multiple times per hit. Use a ratelimit to prevent this.
// # | Some enchantment scripts should have this key.
// after attack:
// - narrate "You attacked <context.victim.name> with a <context.level> enchantment!"
//
// # Triggered after an enchanted armor is used to defend against an attack.
// # Has <context.attacker> (EntityTag), <context.victim> (EntityTag), and <context.level>.
// # | Some enchantment scripts should have this key.
// after hurt:
// - narrate "You got hurt by <context.attacker.name> and protected by a level <context.level> enchantment!"
// </code>
//
// -->
Expand Down Expand Up @@ -261,4 +284,30 @@ public float getDamageBonus(int level, String type) {
src.contexts.put("type", new ElementTag(type));
return Float.parseFloat(autoTag(damageBonusTaggable, src));
}

public void runSubScript(String pathName, Entity attacker, Entity victim, Entity primary, int level) {
validateThread();
List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(new EntityTag(primary)), pathName);
InstantQueue queue = new InstantQueue(getName());
queue.addEntries(entries);
ContextSource.SimpleMap src = new ContextSource.SimpleMap();
src.contexts = new HashMap<>();
if (attacker != null) {
src.contexts.put("attacker", new EntityTag(attacker));
}
if (victim != null) {
src.contexts.put("victim", new EntityTag(victim));
}
src.contexts.put("level", new ElementTag(level));
queue.contextSource = src;
queue.start();
}

public void doPostAttack(Entity attacker, Entity victim, int level) {
runSubScript("after attack", attacker, victim, attacker, level);
}

public void doPostHurt(Entity victim, Entity attacker, int level) {
runSubScript("after hurt", attacker, victim, victim, level);
}
}
Expand Up @@ -435,14 +435,12 @@ public boolean canEnchant(net.minecraft.world.item.ItemStack var0) {
return script.script.canEnchant(CraftItemStack.asBukkitCopy(var0));
}
@Override
public void doPostAttack(LivingEntity var0, Entity var1, int level) {
// TODO
super.doPostAttack(var0, var1, level);
public void doPostAttack(LivingEntity attacker, Entity victim, int level) {
script.script.doPostAttack(attacker.getBukkitEntity(), victim.getBukkitEntity(), level);
}
@Override
public void doPostHurt(LivingEntity var0, Entity var1, int level) {
// TODO
super.doPostHurt(var0, var1, level);
public void doPostHurt(LivingEntity victim, Entity attacker, int level) {
script.script.doPostHurt(victim.getBukkitEntity(), attacker.getBukkitEntity(), level);
}
@Override
public boolean isTreasureOnly() {
Expand Down

0 comments on commit 180da25

Please sign in to comment.