Skip to content

Commit

Permalink
Improved: EntityEquipmentListener performance by reducing the number …
Browse files Browse the repository at this point in the history
…of foreach loops used.
  • Loading branch information
Bram1903 committed Mar 28, 2024
1 parent fbf87b6 commit 16dc067
Showing 1 changed file with 37 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.deathmotion.antihealthindicator.AntiHealthIndicator;
import com.deathmotion.antihealthindicator.enums.ConfigOption;
import com.deathmotion.antihealthindicator.managers.ConfigManager;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketSendEvent;
Expand All @@ -20,9 +19,11 @@
import java.util.List;

public class EntityEquipmentListener extends PacketListenerAbstract {
private final ConfigManager configManager;

private final boolean useDamageableInterface;
private final boolean bypassPermissionEnabled;
private final boolean spoofStackAmount;
private final boolean spoofDurability;
private final boolean spoofEnchantments;

/**
* The enchantment list to spoof the item with
Expand All @@ -33,8 +34,11 @@ public class EntityEquipmentListener extends PacketListenerAbstract {
.build());

public EntityEquipmentListener(AntiHealthIndicator plugin) {
this.configManager = plugin.getConfigManager();
this.useDamageableInterface = PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_13);
this.bypassPermissionEnabled = plugin.getConfigManager().getConfigurationOption(ConfigOption.ALLOW_BYPASS_ENABLED);
this.spoofStackAmount = plugin.getConfigManager().getConfigurationOption(ConfigOption.STACK_AMOUNT_ENABLED);
this.spoofDurability = plugin.getConfigManager().getConfigurationOption(ConfigOption.DURABILITY_ENABLED);
this.spoofEnchantments = plugin.getConfigManager().getConfigurationOption(ConfigOption.ENCHANTMENTS_ENABLED);
}

/**
Expand All @@ -48,7 +52,7 @@ public void onPacketSend(PacketSendEvent event) {
WrapperPlayServerEntityEquipment packet = new WrapperPlayServerEntityEquipment(event);
Player player = (Player) event.getPlayer();

if (configManager.getConfigurationOption(ConfigOption.ALLOW_BYPASS_ENABLED)) {
if (bypassPermissionEnabled) {
if (player.hasPermission("AntiHealthIndicator.Bypass")) return;
}

Expand All @@ -57,71 +61,46 @@ public void onPacketSend(PacketSendEvent event) {
return;
}

if (configManager.getConfigurationOption(ConfigOption.STACK_AMOUNT_ENABLED))
spoofItemStackAmount(equipmentList);

if (configManager.getConfigurationOption(ConfigOption.DURABILITY_ENABLED))
spoofItemStackDurability(equipmentList);

if (configManager.getConfigurationOption(ConfigOption.ENCHANTMENTS_ENABLED))
spoofEnchantment(packet.getClientVersion(), equipmentList);
equipmentList.forEach(equipment -> handleEquipment(equipment, packet.getClientVersion()));

packet.setEquipment(equipmentList);
event.markForReEncode(true);
}
}

/**
* Spoof the amount of the item to 1
*
* @param equipmentList the list of equipment
*/
private void spoofItemStackAmount(List<Equipment> equipmentList) {
equipmentList.forEach(equipment -> {
ItemStack itemStack = equipment.getItem();

if (itemStack.getAmount() > 1) {
itemStack.setAmount(1);
equipment.setItem(itemStack);
}
});
}

/**
* Spoof the durability items to appear as if they are new
* Handles the modification of an equipment item as per configurations.
* If the spoofStackAmount is enabled and the item amount exceeds 1, the item amount is set to 1.
* If the spoofDurability is enabled and the item is damageable,
* the damage value on the item is set to 0 for non-legacy items
* and the legacy data on the item is set to 0 for legacy items.
* If the spoofEnchantments are enabled and the item is enchanted,
* the enchantments on the item are set to enchantmentList.
*
* @param equipmentList the list of equipment
* @param equipment a single piece of equipment
* @param clientVersion the player's client version
*/
private void spoofItemStackDurability(List<Equipment> equipmentList) {
equipmentList.forEach(equipment -> {
ItemStack itemStack = equipment.getItem();
private void handleEquipment(Equipment equipment, ClientVersion clientVersion) {
ItemStack itemStack = equipment.getItem();
if (itemStack == null) return;

if (itemStack.isDamageableItem()) {
if (useDamageableInterface) {
itemStack.setDamageValue(0);
} else {
itemStack.setLegacyData((short) 0);
}
if (spoofStackAmount && itemStack.getAmount() > 1) {
itemStack.setAmount(1);
equipment.setItem(itemStack);
}

equipment.setItem(itemStack);
if (spoofDurability && itemStack.isDamageableItem()) {
if (useDamageableInterface) {
itemStack.setDamageValue(0);
} else {
itemStack.setLegacyData((short) 0);
}
});
}

/**
* Spoof the enchantment level of the item to Fortune III (Xbox 360 Reference)
*
* @param clientVersion the player client version
* @param equipmentList the list of equipment
*/
private void spoofEnchantment(ClientVersion clientVersion, List<Equipment> equipmentList) {
equipmentList.forEach(equipment -> {
ItemStack itemStack = equipment.getItem();
equipment.setItem(itemStack);
}

if (itemStack.isEnchanted(clientVersion)) {
itemStack.setEnchantments(enchantmentList, clientVersion);
equipment.setItem(itemStack);
}
});
if (spoofEnchantments && itemStack.isEnchanted(clientVersion)) {
itemStack.setEnchantments(enchantmentList, clientVersion);
equipment.setItem(itemStack);
}
}
}

0 comments on commit 16dc067

Please sign in to comment.