9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-29 20:09:13 +00:00

feat: reduced packets for armor cosmetics

This commit is contained in:
LoJoSho
2023-12-16 16:13:52 -06:00
parent 9dc6eb1dfc
commit c4591b030b
10 changed files with 193 additions and 7 deletions

View File

@@ -27,20 +27,26 @@ public class CosmeticArmorType extends Cosmetic {
@Override
public void update(@NotNull CosmeticUser user) {
if (user.getUserEmoteManager().isPlayingEmote()) return;
Entity entity = Bukkit.getEntity(user.getUniqueId());
if (entity == null) return;
if (user.getUserEmoteManager().isPlayingEmote()) return; // There has to be a better way of doing this...
if (!Settings.isCosmeticForceOffhandCosmeticShow()
&& equipSlot.equals(EquipmentSlot.OFF_HAND)
&& ((user.getEntity() instanceof Player) && !user.getPlayer().getInventory().getItemInOffHand().getType().isAir())) return;
ItemStack item = getItem(user);
if (item == null) return;
NMSHandlers.getHandler().equipmentSlotUpdate(entity.getEntityId(), equipSlot, item, PacketManager.getViewers(entity.getLocation()));
}
public ItemStack getItem(@NotNull CosmeticUser user) {
ItemStack cosmeticItem = user.getUserCosmeticItem(this);
if (!(entity instanceof HumanEntity humanEntity)) return;
ItemStack equippedItem = humanEntity.getInventory().getItem(equipSlot);
if (!(user.getEntity() instanceof HumanEntity humanEntity)) return null;
if (Settings.getShouldAddEnchants(equipSlot)) {
ItemStack equippedItem = humanEntity.getInventory().getItem(equipSlot);
cosmeticItem.addUnsafeEnchantments(equippedItem.getEnchantments());
}
// Basically, if force offhand is off AND there is no item in an offhand slot, then the equipment packet to add the cosmetic
if (!Settings.isCosmeticForceOffhandCosmeticShow()
&& equipSlot.equals(EquipmentSlot.OFF_HAND)
&& ((entity instanceof Player) && !user.getPlayer().getInventory().getItemInOffHand().getType().isAir())) return;
NMSHandlers.getHandler().equipmentSlotUpdate(entity.getEntityId(), equipSlot, cosmeticItem, PacketManager.getViewers(entity.getLocation()));
return cosmeticItem;
}
public EquipmentSlot getEquipSlot() {

View File

@@ -7,8 +7,10 @@ import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.List;
public interface NMSHandler {
@@ -35,6 +37,12 @@ public interface NMSHandler {
List<Player> sendTo
);
void equipmentSlotUpdate(
int entityId,
HashMap<EquipmentSlot, ItemStack> equipment,
List<Player> sendTo
);
void hideNPCName(
Player player,
String NPCName);

View File

@@ -193,9 +193,27 @@ public class CosmeticUser {
}
public void updateCosmetic() {
bulkUpdateCosmetic();
}
private void bulkUpdateCosmetic() {
MessagesUtil.sendDebugMessages("bulkUpdateCosmetic - start");
HashMap<EquipmentSlot, ItemStack> items = new HashMap<>();
for (Cosmetic cosmetic : getCosmetics()) {
if (cosmetic instanceof CosmeticArmorType armorType) {
if (getUserEmoteManager().isPlayingEmote()) return;
if (!Settings.isCosmeticForceOffhandCosmeticShow()
&& armorType.getEquipSlot().equals(EquipmentSlot.OFF_HAND)
&& !getPlayer().getInventory().getItemInOffHand().getType().isAir()) continue;
items.put(InventoryUtils.getEquipmentSlot(armorType.getSlot()), armorType.getItem(this));
continue;
}
updateCosmetic(cosmetic.getSlot());
}
if (items.isEmpty()) return;
NMSHandlers.getHandler().equipmentSlotUpdate(getEntity().getEntityId(), items, PlayerUtils.getNearbyPlayers(getEntity().getLocation()));
MessagesUtil.sendDebugMessages("bulkUpdateCosmetic - end - " + items.size());
}
public ItemStack getUserCosmeticItem(CosmeticSlot slot) {