Skip to content

Commit

Permalink
Fix resethealth command not working on 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Jun 19, 2024
1 parent fb61fa2 commit 6c72f32
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 85 deletions.
2 changes: 1 addition & 1 deletion api-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
exclude("org.yaml", "snakeyaml")
exclude("org.spongepowered", "configurate-yaml")
}
// api(files("../../Slate/build/libs/Slate-1.0.0.jar"))
// api(files("../../Slate/build/libs/Slate-1.1.7.jar"))
compileOnly("org.jetbrains:annotations:24.1.0")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import co.aikar.commands.CommandHelp;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.*;
import de.tr7zw.changeme.nbtapi.NBTCompoundList;
import de.tr7zw.changeme.nbtapi.NBTFile;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
import dev.aurelium.auraskills.api.registry.NamespacedId;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.util.NumberUtil;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.item.UnclaimedItemsMenu;
import dev.aurelium.auraskills.bukkit.menus.SourcesMenu.SortType;
import dev.aurelium.auraskills.bukkit.storage.Uninstaller;
import dev.aurelium.auraskills.bukkit.util.UpdateChecker;
import dev.aurelium.auraskills.common.config.Option;
import dev.aurelium.auraskills.common.leaderboard.SkillValue;
Expand All @@ -31,10 +28,8 @@
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

@CommandAlias("%skills_alias")
public class SkillsRootCommand extends BaseCommand {
Expand Down Expand Up @@ -297,84 +292,8 @@ public void onMultiplier(CommandSender sender, @Optional @Flags("other") Player
@Description("Removes all attribute modifiers by Aurelium Skills for easy uninstalling. This only works on offline players.")
public void onResetHealth(CommandSender sender) {
if (sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender) {
File playerDataFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "playerdata");
int successful = 0;
int error = 0;
int total = 0;
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (!player.isOnline()) {
total++;
File playerFile = new File(playerDataFolder, player.getUniqueId() + ".dat");
if (playerFile.exists() && playerFile.canWrite()) {
try {
NBTFile nbtFile = new NBTFile(playerFile);
NBTCompoundList compoundList = nbtFile.getCompoundList("Attributes");
if (compoundList != null) {
final AtomicBoolean save = new AtomicBoolean(false);
for (ReadWriteNBT listCompound : compoundList.subList(0, compoundList.size())) {
switch (listCompound.getString("Name")) {
case "generic.maxHealth", "minecraft:generic.max_health" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("skillsHealth")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
case "generic.luck", "minecraft:generic.luck" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("AureliumSkills-Luck")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
case "generic.attackSpeed", "minecraft:generic.attack_speed" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("AureliumSkills-LightningBlade")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
}
}
if (save.get()) {
nbtFile.save();
successful++;
}
}
} catch (Exception e) {
error++;
}
}
}
}
sender.sendMessage("Searched " + total + " offline players. Successfully removed attributes from " + successful + " players. Failed to remove on " + error + " players.");
Uninstaller uninstaller = new Uninstaller();
uninstaller.removeAttributes(sender);
} else {
sender.sendMessage(ChatColor.RED + "Only console may execute this command!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package dev.aurelium.auraskills.bukkit.storage;

import de.tr7zw.changeme.nbtapi.NBTCompoundList;
import de.tr7zw.changeme.nbtapi.NBTFile;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
import dev.aurelium.auraskills.bukkit.util.VersionUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

public class Uninstaller {

public void removeAttributes(CommandSender sender) {
File playerDataFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "playerdata");
int successful = 0;
int error = 0;
int total = 0;
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (!player.isOnline()) {
total++;
File playerFile = new File(playerDataFolder, player.getUniqueId() + ".dat");
if (playerFile.exists() && playerFile.canWrite()) {
try {
NBTFile nbtFile = new NBTFile(playerFile);
if (VersionUtils.isAtLeastVersion(21)) {
if (removePlayer(nbtFile)) {
successful++;
}
} else {
if (removeLegacyPlayer(nbtFile)) {
successful++;
}
}
} catch (Exception e) {
error++;
}
}
}
}
sender.sendMessage("Searched " + total + " offline players. Successfully removed attributes from " + successful + " players. Failed to remove on " + error + " players.");
}

private boolean removePlayer(NBTFile nbtFile) throws IOException {
NBTCompoundList compoundList = nbtFile.getCompoundList("attributes");
if (compoundList == null) {
return false;
}
final AtomicBoolean save = new AtomicBoolean(false);
for (ReadWriteNBT listCompound : compoundList.subList(0, compoundList.size())) {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("modifiers");
if (modifierList == null) continue;
// Check all modifiers of this attribute
modifierList.removeIf(modifier -> {
String id = modifier.getString("id");
// Remove any attribute modifiers with the auraskills namespace
if (id != null && id.startsWith("auraskills:")) {
save.set(true);
return true;
}
return false;
});
if (modifierList.isEmpty()) {
listCompound.removeKey("modifiers");
}
}
if (save.get()) {
nbtFile.save();
return true;
}
return false;
}

private boolean removeLegacyPlayer(NBTFile nbtFile) throws IOException {
NBTCompoundList compoundList = nbtFile.getCompoundList("Attributes");
if (compoundList == null) {
return false;
}
final AtomicBoolean save = new AtomicBoolean(false);
for (ReadWriteNBT listCompound : compoundList.subList(0, compoundList.size())) {
switch (listCompound.getString("Name")) {
case "generic.maxHealth", "minecraft:generic.max_health" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("skillsHealth")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
case "generic.luck", "minecraft:generic.luck" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("AureliumSkills-Luck")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
case "generic.attackSpeed", "minecraft:generic.attack_speed" -> {
ReadWriteNBTCompoundList modifierList = listCompound.getCompoundList("Modifiers");
if (modifierList != null) {
modifierList.removeIf((modifier) -> {
if (modifier.getString("Name").equals("AureliumSkills-LightningBlade")) {
save.set(true);
return true;
} else {
return false;
}
});
if (modifierList.isEmpty()) {
listCompound.removeKey("Modifiers");
}
}
}
}
}
if (save.get()) {
nbtFile.save();
return true;
}
return false;
}

}

0 comments on commit 6c72f32

Please sign in to comment.