Attempt to work around a vague error with possibly equipment enchants

This commit is contained in:
libraryaddict 2024-06-30 23:05:34 +12:00
parent 410c53c18d
commit 728917e31a

View File

@ -5,6 +5,9 @@ import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent; import com.github.retrooper.packetevents.event.simple.PacketPlaySendEvent;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentType;
import com.github.retrooper.packetevents.protocol.item.enchantment.type.EnchantmentTypes;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.packettype.PacketType; import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.particle.Particle; import com.github.retrooper.packetevents.protocol.particle.Particle;
import com.github.retrooper.packetevents.protocol.player.Equipment; import com.github.retrooper.packetevents.protocol.player.Equipment;
@ -102,6 +105,9 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.boss.KeyedBossBar; import org.bukkit.boss.KeyedBossBar;
@ -293,7 +299,7 @@ public class DisguiseUtilities {
private static boolean fancyHiddenTabs; private static boolean fancyHiddenTabs;
@Getter @Getter
private static NamespacedKey savedDisguisesKey; private static NamespacedKey savedDisguisesKey;
private static final List<Enchantment> whitelistedEnchantments = new ArrayList<>(); private static final Map<Enchantment, EnchantmentType> whitelistedEnchantments = new HashMap<Enchantment, EnchantmentType>();
@Getter @Getter
private static Enchantment durabilityEnchantment, waterbreathingEnchantment; private static Enchantment durabilityEnchantment, waterbreathingEnchantment;
@Getter @Getter
@ -303,6 +309,8 @@ public class DisguiseUtilities {
private static final GsonComponentSerializer internalComponentSerializer = GsonComponentSerializer.gson(); private static final GsonComponentSerializer internalComponentSerializer = GsonComponentSerializer.gson();
private static final io.github.retrooper.packetevents.adventure.serializer.gson.GsonComponentSerializer externalComponentSerializer = private static final io.github.retrooper.packetevents.adventure.serializer.gson.GsonComponentSerializer externalComponentSerializer =
io.github.retrooper.packetevents.adventure.serializer.gson.GsonComponentSerializer.gson(); io.github.retrooper.packetevents.adventure.serializer.gson.GsonComponentSerializer.gson();
@Getter
private static NamespacedKey selfDisguiseScaleNamespace;
static { static {
try { try {
@ -327,22 +335,37 @@ public class DisguiseUtilities {
durabilityEnchantment = Enchantment.getByName("unbreaking"); durabilityEnchantment = Enchantment.getByName("unbreaking");
waterbreathingEnchantment = Enchantment.getByName("respiration"); waterbreathingEnchantment = Enchantment.getByName("respiration");
whitelistedEnchantments.add(Enchantment.DEPTH_STRIDER); whitelistedEnchantments.put(Enchantment.DEPTH_STRIDER, EnchantmentTypes.DEPTH_STRIDER);
whitelistedEnchantments.add(getWaterbreathingEnchantment()); whitelistedEnchantments.put(getWaterbreathingEnchantment(), EnchantmentTypes.RESPIRATION);
if (Bukkit.getServer() != null && NmsVersion.v1_13.isSupported()) { if (Bukkit.getServer() != null && NmsVersion.v1_13.isSupported()) {
whitelistedEnchantments.add(Enchantment.RIPTIDE); whitelistedEnchantments.put(Enchantment.RIPTIDE, EnchantmentTypes.RIPTIDE);
if (NmsVersion.v1_19_R1.isSupported()) { if (NmsVersion.v1_19_R1.isSupported()) {
whitelistedEnchantments.add(Enchantment.SOUL_SPEED); whitelistedEnchantments.put(Enchantment.SOUL_SPEED, EnchantmentTypes.SOUL_SPEED);
whitelistedEnchantments.add(Enchantment.SWIFT_SNEAK); whitelistedEnchantments.put(Enchantment.SWIFT_SNEAK, EnchantmentTypes.SWIFT_SNEAK);
} }
} }
} }
} }
public static boolean shouldBeHiddenSelfDisguise(com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) { public static boolean shouldBeHiddenSelfDisguise(com.github.retrooper.packetevents.protocol.item.ItemStack itemStack) {
return shouldBeHiddenSelfDisguise(SpigotConversionUtil.toBukkitItemStack(itemStack)); if (itemStack == null || itemStack.isEmpty() || itemStack.getType() == ItemTypes.AIR) {
return false;
}
List<com.github.retrooper.packetevents.protocol.item.enchantment.Enchantment> enchants =
itemStack.getEnchantments(PacketEvents.getAPI().getServerManager().getVersion().toClientVersion());
for (com.github.retrooper.packetevents.protocol.item.enchantment.Enchantment enchantment : enchants) {
if (enchantment == null || !whitelistedEnchantments.containsValue(enchantment.getType())) {
continue;
}
return false;
}
return true;
} }
public static boolean shouldBeHiddenSelfDisguise(ItemStack itemStack) { public static boolean shouldBeHiddenSelfDisguise(ItemStack itemStack) {
@ -353,7 +376,7 @@ public class DisguiseUtilities {
Map<Enchantment, Integer> enchants = itemStack.getEnchantments(); Map<Enchantment, Integer> enchants = itemStack.getEnchantments();
for (Enchantment enchantment : enchants.keySet()) { for (Enchantment enchantment : enchants.keySet()) {
if (!whitelistedEnchantments.contains(enchantment)) { if (!whitelistedEnchantments.containsKey(enchantment)) {
continue; continue;
} }
@ -363,6 +386,16 @@ public class DisguiseUtilities {
return true; return true;
} }
public static void removeSelfDisguiseScale(Entity entity) {
if (!NmsVersion.v1_21_R1.isSupported() || isInvalidFile() || !(entity instanceof LivingEntity)) {
return;
}
AttributeInstance attribute = ((LivingEntity) entity).getAttribute(Attribute.GENERIC_SCALE);
attribute.getModifiers().stream().filter(a -> a.getKey().equals(DisguiseUtilities.getSelfDisguiseScaleNamespace()))
.forEach(attribute::removeModifier);
}
public static double getNameSpacing() { public static double getNameSpacing() {
return 0.28; return 0.28;
} }
@ -617,6 +650,7 @@ public class DisguiseUtilities {
Plugin plugin = Bukkit.getPluginManager().getPlugin("packetevents"); Plugin plugin = Bukkit.getPluginManager().getPlugin("packetevents");
if (plugin == null) { if (plugin == null) {
DisguiseUtilities.getLogger().severe("PacketEvents not installed on server (as a plugin), must be missing!");
return true; return true;
} }
@ -625,6 +659,7 @@ public class DisguiseUtilities {
try { try {
packetEventsVersion = plugin.getDescription().getVersion(); packetEventsVersion = plugin.getDescription().getVersion();
} catch (Throwable throwable) { } catch (Throwable throwable) {
throwable.printStackTrace();
return true; return true;
} }
@ -1606,6 +1641,7 @@ public class DisguiseUtilities {
public static void init() { public static void init() {
fancyHiddenTabs = NmsVersion.v1_19_R2.isSupported() && Bukkit.getPluginManager().getPlugin("ViaBackwards") == null; fancyHiddenTabs = NmsVersion.v1_19_R2.isSupported() && Bukkit.getPluginManager().getPlugin("ViaBackwards") == null;
savedDisguisesKey = new NamespacedKey(LibsDisguises.getInstance(), "SavedDisguises"); savedDisguisesKey = new NamespacedKey(LibsDisguises.getInstance(), "SavedDisguises");
selfDisguiseScaleNamespace = new NamespacedKey(LibsDisguises.getInstance(), "Self_Disguise_Scaling");
recreateGsonSerializer(); recreateGsonSerializer();
@ -2752,17 +2788,7 @@ public class DisguiseUtilities {
} }
public static int[] getNumericVersion(String version) { public static int[] getNumericVersion(String version) {
int[] v = new int[0]; return PacketEventsUpdater.getNumericVersion(version);
for (String split : version.split("[.\\-]")) {
if (!split.matches("\\d+")) {
return v;
}
v = Arrays.copyOf(v, v.length + 1);
v[v.length - 1] = Integer.parseInt(split);
}
return v;
} }
public static String getSimpleString(Component component) { public static String getSimpleString(Component component) {
@ -2822,18 +2848,7 @@ public class DisguiseUtilities {
* "1.5" and "1.0" will return true * "1.5" and "1.0" will return true
*/ */
public static boolean isOlderThan(String requiredVersion, String theirVersion) { public static boolean isOlderThan(String requiredVersion, String theirVersion) {
int[] required = getNumericVersion(requiredVersion); return PacketEventsUpdater.isOlderThan(requiredVersion, theirVersion);
int[] has = getNumericVersion(theirVersion);
for (int i = 0; i < Math.min(required.length, has.length); i++) {
if (required[i] == has[i]) {
continue;
}
return required[i] >= has[i];
}
return false;
} }
public static Logger getLogger() { public static Logger getLogger() {
@ -3407,6 +3422,42 @@ public class DisguiseUtilities {
return packets; return packets;
} }
/**
* Grabs the scale of the entity as if the LibsDisguises: attributes did not exist
*/
public static double getActualEntityScale(Entity entity) {
if (!(entity instanceof LivingEntity)) {
return 1;
}
AttributeInstance attribute = ((LivingEntity) entity).getAttribute(Attribute.GENERIC_SCALE);
double scale = attribute.getBaseValue();
double modifiedScale = 0;
for (int operation = 0; operation < 3; operation++) {
if (operation == 1) {
modifiedScale = scale;
}
for (AttributeModifier modifier : attribute.getModifiers()) {
if (modifier.getKey().equals(getSelfDisguiseScaleNamespace())) {
continue;
}
if (modifier.getOperation() == AttributeModifier.Operation.ADD_NUMBER && operation == 0) {
scale += modifier.getAmount();
} else if (modifier.getOperation() == AttributeModifier.Operation.ADD_SCALAR && operation == 1) {
modifiedScale += scale * modifier.getAmount();
} else if (modifier.getOperation() == AttributeModifier.Operation.MULTIPLY_SCALAR_1 && operation == 2) {
modifiedScale *= 1 + modifier.getAmount();
}
}
}
return modifiedScale;
}
public static Disguise getDisguise(Player observer, int entityId) { public static Disguise getDisguise(Player observer, int entityId) {
// If the entity ID is the same as self disguises' id, then it needs to be set to the observers id // If the entity ID is the same as self disguises' id, then it needs to be set to the observers id
if (entityId == DisguiseAPI.getSelfDisguiseId()) { if (entityId == DisguiseAPI.getSelfDisguiseId()) {