Skip to content

Commit

Permalink
Minecraft 1.21 support (#294)
Browse files Browse the repository at this point in the history
- Add 1.21 support
- The option modifier.auto_convert_from_legacy is now false by default
- Fix Lightning Blade attribute modifier not being removed
- Add bogged and breeze to Fighting and Archery sources
  • Loading branch information
Archy-X authored Jun 15, 2024
1 parent 9d70f5d commit f6b0439
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 111 deletions.
2 changes: 1 addition & 1 deletion api-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
}
// api(files("../../Slate/build/libs/Slate-1.0.0.jar"))
compileOnly("org.jetbrains:annotations:24.1.0")
compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
}

tasks.withType<JavaCompile> {
Expand Down
4 changes: 2 additions & 2 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ dependencies {
implementation(project(":common"))
implementation(project(":api-bukkit"))
implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT")
implementation("de.tr7zw:item-nbt-api:2.12.4")
implementation("de.tr7zw:item-nbt-api:2.13.0")
implementation("org.bstats:bstats-bukkit:3.0.2")
implementation("net.kyori:adventure-text-minimessage:4.16.0")
implementation("net.kyori:adventure-platform-bukkit:4.3.2")
compileOnly("org.jetbrains:annotations:24.1.0")
compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.2")
compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.5")
compileOnly("com.gmail.filoghost.holographicdisplays:holographicdisplays-api:2.4.9")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.aurelium.auraskills.bukkit.item;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import dev.aurelium.auraskills.api.bukkit.BukkitTraitHandler;
import dev.aurelium.auraskills.api.item.ModifierType;
import dev.aurelium.auraskills.api.registry.NamespaceIdentified;
Expand Down Expand Up @@ -191,14 +191,13 @@ public void removeIgnore() {
container.remove(key);
}

public void convertFromLegacy() {
public void convertFromLegacy(ReadWriteNBT nbt) {
if (plugin.isNbtApiDisabled()) return;

NBTItem nbtItem = new NBTItem(item);
// Convert stat modifiers
Modifiers modifiers = new Modifiers(plugin);
for (ModifierType type : ModifierType.values()) {
List<StatModifier> legacy = modifiers.getLegacyModifiers(type, nbtItem);
List<StatModifier> legacy = modifiers.getLegacyModifiers(type, nbt);
if (legacy.isEmpty()) continue;

for (StatModifier modifier : legacy) {
Expand All @@ -208,7 +207,7 @@ public void convertFromLegacy() {
// Convert multipliers
Multipliers multipliers = new Multipliers(plugin);
for (ModifierType type : ModifierType.values()) {
List<Multiplier> legacy = multipliers.getLegacyMultipliers(type, nbtItem);
List<Multiplier> legacy = multipliers.getLegacyMultipliers(type, nbt);
if (legacy.isEmpty()) continue;

for (Multiplier multiplier : legacy) {
Expand All @@ -218,7 +217,7 @@ public void convertFromLegacy() {
// Convert requirements
Requirements requirements = new Requirements(plugin);
for (ModifierType type : ModifierType.values()) {
Map<Skill, Integer> legacy = requirements.getLegacyRequirements(type, nbtItem);
Map<Skill, Integer> legacy = requirements.getLegacyRequirements(type, nbt);
if (legacy.isEmpty()) continue;

for (Map.Entry<Skill, Integer> entry : legacy.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.aurelium.auraskills.bukkit.modifier;

import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.NBTType;
import dev.aurelium.auraskills.api.event.user.UserLoadEvent;
import dev.aurelium.auraskills.api.item.ModifierType;
Expand Down Expand Up @@ -105,15 +105,17 @@ public void onJoin(UserLoadEvent event) {
}

private ItemStack convertLegacyItem(ItemStack item) {
if (plugin.isNbtApiDisabled()) return item;

SkillsItem skillsItem = new SkillsItem(item, plugin);
skillsItem.convertFromLegacy();
NBT.modify(item, skillsItem::convertFromLegacy);
item = skillsItem.getItem();

NBTItem nbtItem = new NBTItem(item);
if (nbtItem.hasTag("AureliumSkills", NBTType.NBTTagCompound)) {
nbtItem.removeKey("AureliumSkills");
item = nbtItem.getItem();
}
NBT.modify(item, nbt -> {
if (nbt.hasTag("AureliumSkills", NBTType.NBTTagCompound)) {
nbt.removeKey("AureliumSkills");
}
});

return item;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.aurelium.auraskills.bukkit.modifier;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import dev.aurelium.auraskills.api.item.ModifierType;
import dev.aurelium.auraskills.api.registry.NamespacedId;
import dev.aurelium.auraskills.api.stat.Stat;
Expand All @@ -21,10 +20,10 @@ public Modifiers(AuraSkills plugin) {
this.plugin = plugin;
}

public List<StatModifier> getLegacyModifiers(ModifierType type, NBTItem nbtItem) {
public List<StatModifier> getLegacyModifiers(ModifierType type, ReadWriteNBT nbt) {
if (plugin.isNbtApiDisabled()) return new ArrayList<>();
List<StatModifier> modifiers = new ArrayList<>();
NBTCompound compound = ItemUtils.getLegacyModifiersTypeCompound(nbtItem, type);
ReadWriteNBT compound = ItemUtils.getLegacyModifiersTypeCompound(nbt, type);
for (String key : compound.getKeys()) {
Stat stat = plugin.getStatRegistry().getOrNull(NamespacedId.fromDefault(key.toLowerCase(Locale.ROOT)));
if (stat != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package dev.aurelium.auraskills.bukkit.modifier;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import dev.aurelium.auraskills.api.item.ModifierType;
import dev.aurelium.auraskills.api.registry.NamespacedId;
import dev.aurelium.auraskills.api.skill.Multiplier;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.util.ItemUtils;
import dev.aurelium.auraskills.api.skill.Multiplier;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,8 +20,8 @@ public Multipliers(AuraSkills plugin) {
this.plugin = plugin;
}

public List<Multiplier> getLegacyMultipliers(ModifierType type, NBTItem nbtItem) {
NBTCompound legacyCompound = ItemUtils.getLegacyMultipliersTypeCompound(nbtItem, type);
public List<Multiplier> getLegacyMultipliers(ModifierType type, ReadWriteNBT nbt) {
ReadWriteNBT legacyCompound = ItemUtils.getLegacyMultipliersTypeCompound(nbt, type);
List<Multiplier> multipliers = new ArrayList<>();

for (String key : legacyCompound.getKeys()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.aurelium.auraskills.bukkit.requirement;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import dev.aurelium.auraskills.api.item.ModifierType;
import dev.aurelium.auraskills.api.registry.NamespacedId;
import dev.aurelium.auraskills.api.skill.Skill;
Expand All @@ -20,8 +19,8 @@ public Requirements(AuraSkills plugin) {
this.plugin = plugin;
}

public Map<Skill, Integer> getLegacyRequirements(ModifierType type, NBTItem nbtItem) {
NBTCompound legacyCompound = ItemUtils.getLegacyRequirementsTypeCompound(nbtItem, type);
public Map<Skill, Integer> getLegacyRequirements(ModifierType type, ReadWriteNBT nbtItem) {
ReadWriteNBT legacyCompound = ItemUtils.getLegacyRequirementsTypeCompound(nbtItem, type);
Map<Skill, Integer> requirements = new HashMap<>();

for (String key : legacyCompound.getKeys()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import dev.aurelium.auraskills.common.user.User;
import dev.aurelium.auraskills.common.util.text.TextUtil;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.entity.*;
import org.bukkit.entity.AbstractArrow.PickupStatus;
import org.bukkit.event.EventHandler;
Expand All @@ -26,17 +28,21 @@
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class ArcheryAbilities extends AbilityImpl {

private final String STUN_MODIFIER_NAME = "AureliumSkills-Stun";
private final UUID LEGACY_STUN_ID = UUID.fromString("886ccad1-20f0-48e4-8634-53f3a76cf2ea");
private final String LEGACY_STUN_NAME = "AureliumSkills-Stun";
private final String STUN_KEY = "stun_ability";

public ArcheryAbilities(AuraSkills plugin) {
super(plugin, Abilities.RETRIEVAL, Abilities.ARCHER, Abilities.BOW_MASTER, Abilities.PIERCING, Abilities.STUN);
Expand Down Expand Up @@ -103,37 +109,67 @@ public void stun(Player player, User user, LivingEntity entity) {
AttributeInstance speed = entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
if (speed == null) return;
// Applies stun
double reducedSpeed = speed.getValue() * STUN_SPEED_REDUCTION;
AttributeModifier modifier = new AttributeModifier(STUN_MODIFIER_NAME, -1 * reducedSpeed, AttributeModifier.Operation.ADD_NUMBER);
AttributeModifier modifier = getAttributeModifier(speed, STUN_SPEED_REDUCTION);
speed.addModifier(modifier);
new BukkitRunnable() {
@Override
public void run() {
AttributeInstance newSpeed = entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
if (newSpeed == null) return;
for (AttributeModifier attributeModifier : newSpeed.getModifiers()) {
if (attributeModifier.getName().equals(STUN_MODIFIER_NAME)) {
newSpeed.removeModifier(attributeModifier);
}
}
}
}.runTaskLater(plugin, 40L);

scheduleStunRemoval(entity);
}
}

@SuppressWarnings("deprecation")
private @NotNull AttributeModifier getAttributeModifier(AttributeInstance speed, double STUN_SPEED_REDUCTION) {
double reducedSpeed = speed.getValue() * STUN_SPEED_REDUCTION;
double attributeValue = -1 * reducedSpeed;

AttributeModifier modifier;
if (VersionUtils.isAtLeastVersion(21)) {
NamespacedKey key = new NamespacedKey(plugin, STUN_KEY);
modifier = new AttributeModifier(key, attributeValue, Operation.ADD_NUMBER, EquipmentSlotGroup.ANY);
} else {
modifier = new AttributeModifier(LEGACY_STUN_ID, LEGACY_STUN_NAME, attributeValue, Operation.ADD_NUMBER);
}
return modifier;
}

private void scheduleStunRemoval(LivingEntity entity) {
plugin.getScheduler().scheduleSync(() -> {
AttributeInstance newSpeed = entity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
if (newSpeed == null) return;
for (AttributeModifier attributeModifier : newSpeed.getModifiers()) {
if (isStunModifier(attributeModifier)) {
newSpeed.removeModifier(attributeModifier);
}
}
}, 40L * 50L, TimeUnit.MILLISECONDS);
}

@EventHandler
public void removeStun(PlayerQuitEvent event) {
// Removes stun on logout
AttributeInstance speed = event.getPlayer().getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
if (speed == null) return;

for (AttributeModifier attributeModifier : speed.getModifiers()) {
if (attributeModifier.getName().equals(STUN_MODIFIER_NAME)) {
if (isStunModifier(attributeModifier)) {
speed.removeModifier(attributeModifier);
}
}
}

private boolean isStunModifier(AttributeModifier modifier) {
if (modifier.getName().equals(LEGACY_STUN_NAME)) {
return true;
}
if (VersionUtils.isAtLeastVersion(21)) {
final String pluginNamespace = "auraskills";
String namespace = modifier.getKey().getNamespace();
String key = modifier.getKey().getKey();

return namespace.equals(pluginNamespace) && key.equals(STUN_KEY);
}
return false;
}

public void piercing(Player player, EntityDamageByEntityEvent event, User user, Arrow arrow) {
var ability = Abilities.PIERCING;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import dev.aurelium.auraskills.api.mana.ManaAbilities;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.mana.ReadiedManaAbility;
import dev.aurelium.auraskills.bukkit.util.VersionUtils;
import dev.aurelium.auraskills.common.message.type.ManaAbilityMessage;
import dev.aurelium.auraskills.common.user.User;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -17,39 +20,72 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.EquipmentSlotGroup;

import java.util.UUID;

public class LightningBlade extends ReadiedManaAbility {

private final UUID MODIFIER_ID = UUID.fromString("2fc64528-614b-11ee-8c99-0242ac120002");
private final String LEGACY_MODIFIER_NAME = "auraskills_lightning_blade";
private final String MODIFIER_KEY = "lightning_blade_mana_ability";

public LightningBlade(AuraSkills plugin) {
super(plugin, ManaAbilities.LIGHTNING_BLADE, ManaAbilityMessage.LIGHTNING_BLADE_START, ManaAbilityMessage.LIGHTNING_BLADE_END,
new String[]{"SWORD"}, new Action[]{Action.RIGHT_CLICK_AIR, Action.RIGHT_CLICK_BLOCK});
}

@Override
@SuppressWarnings("deprecation")
public void onActivate(Player player, User user) {
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
if (attribute == null) return;

// Remove existing modifier if exists
for (AttributeModifier modifier : attribute.getModifiers()) {
if (modifier.getName().equals("auraskills_lightning_blade")) {
if (isLightningBladeModifier(modifier)) {
attribute.removeModifier(modifier);
}
}
// Increase attack speed attribute
double addedValue = getValue(user) / 100;
attribute.addModifier(new AttributeModifier(MODIFIER_ID, "auraskills_lightning_blade", addedValue, AttributeModifier.Operation.MULTIPLY_SCALAR_1));
if (VersionUtils.isAtLeastVersion(21)) {
NamespacedKey key = new NamespacedKey(plugin, MODIFIER_KEY);
attribute.addModifier(new AttributeModifier(key, addedValue, Operation.MULTIPLY_SCALAR_1, EquipmentSlotGroup.ANY));
} else {
attribute.addModifier(new AttributeModifier(MODIFIER_ID, LEGACY_MODIFIER_NAME, addedValue, Operation.MULTIPLY_SCALAR_1));
}
// Play sound and send message
player.playSound(player.getLocation(), Sound.ENTITY_ILLUSIONER_PREPARE_MIRROR, 0.5f, 1);
}

@Override
public void onStop(Player player, User user) {
user.removeTraitModifier("lightning_blade");
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
if (attribute == null) return;

for (AttributeModifier modifier : attribute.getModifiers()) {
if (isLightningBladeModifier(modifier)) {
attribute.removeModifier(modifier);
}
}
}

private boolean isLightningBladeModifier(AttributeModifier am) {
if (am.getName().equals(LEGACY_MODIFIER_NAME)) {
return true;
}
if (VersionUtils.isAtLeastVersion(21)) {
String namespace = am.getKey().getNamespace();
String key = am.getKey().getKey();
if (key.equals(MODIFIER_ID.toString())) {
return true;
} else {
final String attributeNamespace = "auraskills";
return namespace.equals(attributeNamespace) && key.equals(MODIFIER_KEY);
}
}
return false;
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down
Loading

0 comments on commit f6b0439

Please sign in to comment.