Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro-optimization on methods that are called a lot of times #172

Open
wants to merge 9 commits into
base: remapped
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.hibiscusmc.hmccosmetics.config.Settings;
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.user.manager.UserBackpackManager;
import com.hibiscusmc.hmccosmetics.user.manager.UserEntity;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
import com.hibiscusmc.hmccosmetics.util.packets.HMCCPacketManager;
import lombok.Getter;
Expand Down Expand Up @@ -39,50 +41,56 @@ public CosmeticBackpackType(String id, ConfigurationNode config) {

@Override
public void update(@NotNull CosmeticUser user) {
Entity entity = Bukkit.getEntity(user.getUniqueId());
Entity entity = user.getEntity();
if (entity == null) return;

Location loc = entity.getLocation().clone().add(0, 2, 0);
Location entityLocation = entity.getLocation();
Location loc = entityLocation.clone().add(0, 2, 0);

if (user.isInWardrobe() || !user.isBackpackSpawned()) return;
if (user.isHidden()) {
// Sometimes the backpack is not despawned when the player is hidden (weird ass logic happening somewhere)
user.despawnBackpack();
return;
}
List<Player> outsideViewers = user.getUserBackpackManager().getEntityManager().refreshViewers(loc);

user.getUserBackpackManager().getEntityManager().teleport(loc);
user.getUserBackpackManager().getEntityManager().setRotation((int) loc.getYaw(), isFirstPersonCompadible());
UserBackpackManager backpackManager = user.getUserBackpackManager();
UserEntity entityManager = backpackManager.getEntityManager();
int firstArmorStandId = backpackManager.getFirstArmorStandId();

HMCCPacketManager.sendEntitySpawnPacket(user.getEntity().getLocation(), user.getUserBackpackManager().getFirstArmorStandId(), EntityType.ARMOR_STAND, UUID.randomUUID(), outsideViewers);
HMCCPacketManager.sendArmorstandMetadata(user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers);
PacketManager.equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, user.getUserCosmeticItem(this, getItem()), outsideViewers);
List<Player> outsideViewers = entityManager.refreshViewers(loc);

entityManager.teleport(loc);
entityManager.setRotation((int) loc.getYaw(), isFirstPersonCompadible());

HMCCPacketManager.sendEntitySpawnPacket(entityLocation, firstArmorStandId, EntityType.ARMOR_STAND, UUID.randomUUID(), outsideViewers);
HMCCPacketManager.sendArmorstandMetadata(firstArmorStandId, outsideViewers);
PacketManager.equipmentSlotUpdate(firstArmorStandId, EquipmentSlot.HEAD, user.getUserCosmeticItem(this, getItem()), outsideViewers);
// If true, it will send the riding packet to all players. If false, it will send the riding packet only to new players
if (Settings.isBackpackForceRidingEnabled()) HMCCPacketManager.sendRidingPacket(entity.getEntityId(), user.getUserBackpackManager().getFirstArmorStandId(), user.getUserBackpackManager().getEntityManager().getViewers());
else HMCCPacketManager.sendRidingPacket(entity.getEntityId(), user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers);
if (Settings.isBackpackForceRidingEnabled()) HMCCPacketManager.sendRidingPacket(entity.getEntityId(), firstArmorStandId, entityManager.getViewers());
else HMCCPacketManager.sendRidingPacket(entity.getEntityId(), firstArmorStandId, outsideViewers);

if (!user.isInWardrobe() && isFirstPersonCompadible() && user.getPlayer() != null) {
List<Player> owner = List.of(user.getPlayer());

ArrayList<Integer> particleCloud = user.getUserBackpackManager().getAreaEffectEntityId();
ArrayList<Integer> particleCloud = backpackManager.getAreaEffectEntityId();
for (int i = 0; i < particleCloud.size(); i++) {
if (i == 0) {
HMCCPacketManager.sendRidingPacket(entity.getEntityId(), particleCloud.get(i), owner);
} else {
HMCCPacketManager.sendRidingPacket(particleCloud.get(i - 1), particleCloud.get(i) , owner);
}
}
HMCCPacketManager.sendRidingPacket(particleCloud.get(particleCloud.size() - 1), user.getUserBackpackManager().getFirstArmorStandId(), owner);
HMCCPacketManager.sendRidingPacket(particleCloud.get(particleCloud.size() - 1), firstArmorStandId, owner);
if (!user.isHidden()) {
//if (loc.getPitch() < -70) NMSHandlers.getHandler().equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, new ItemStack(Material.AIR), owner);
//else NMSHandlers.getHandler().equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, firstPersonBackpack, owner);
PacketManager.equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, user.getUserCosmeticItem(this, firstPersonBackpack), owner);
PacketManager.equipmentSlotUpdate(firstArmorStandId, EquipmentSlot.HEAD, user.getUserCosmeticItem(this, firstPersonBackpack), owner);
}
//MessagesUtil.sendDebugMessages("First Person Backpack Update[owner=" + user.getUniqueId() + ",player_location=" + loc + "]!", Level.INFO);
}

user.getUserBackpackManager().showBackpack();
backpackManager.showBackpack();
}

public boolean isFirstPersonCompadible() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void update(@NotNull CosmeticUser user) {
}

Vector velocity = newLocation.toVector().subtract(currentLocation.toVector());
userBalloonManager.setVelocity(velocity.multiply(1.1));
userBalloonManager.setLocation(newLocation);
userBalloonManager.setVelocity(velocity.multiply(1.1));

HMCCPacketManager.sendTeleportPacket(userBalloonManager.getPufferfishBalloonId(), newLocation, false, viewer);
HMCCPacketManager.sendLeashPacket(userBalloonManager.getPufferfishBalloonId(), entity.getEntityId(), viewer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ private EquipmentSlot getArmorSlot(final Material material) {
for (final EquipmentSlot slot : EquipmentSlot.values()) {
final Set<Material> armorItems = ARMOR_ITEMS.get(slot);
if (armorItems == null) continue;
if (material == null) continue;
if (armorItems.contains(material)) return slot;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected void tick() {

this.updateCosmetic();

if(isHidden() && !getUserEmoteManager().isPlayingEmote() && !getCosmetics().isEmpty()) {
if(isHidden() && !getUserEmoteManager().isPlayingEmote() && !playerCosmetics.isEmpty()) {
MessagesUtil.sendActionBar(getPlayer(), "hidden-cosmetics");
}
}
Expand Down Expand Up @@ -301,11 +301,10 @@ public Set<CosmeticSlot> getSlotsWithCosmetics() {

@Override
public void updateCosmetic(@NotNull CosmeticSlot slot) {
if (getCosmetic(slot) == null) {
return;
Cosmetic cosmetic = playerCosmetics.get(slot);
if (cosmetic != null) {
cosmetic.update(this);
}
getCosmetic(slot).update(this);
return;
}

public void updateCosmetic(Cosmetic cosmetic) {
Expand All @@ -316,7 +315,7 @@ public void updateCosmetic() {
MessagesUtil.sendDebugMessages("updateCosmetic (All) - start");
HashMap<EquipmentSlot, ItemStack> items = new HashMap<>();

for (Cosmetic cosmetic : getCosmetics()) {
for (Cosmetic cosmetic : playerCosmetics.values()) {
if (cosmetic instanceof CosmeticArmorType armorType) {
if (getUserEmoteManager().isPlayingEmote() || isInWardrobe()) return;
if (!(getEntity() instanceof HumanEntity humanEntity)) return;
Expand All @@ -328,7 +327,7 @@ public void updateCosmetic() {

items.put(HMCCInventoryUtils.getEquipmentSlot(armorType.getSlot()), armorType.getItem(this));
} else {
updateCosmetic(cosmetic.getSlot());
cosmetic.update(this);
}
}
if (items.isEmpty() || getEntity() == null) return;
Expand Down Expand Up @@ -633,7 +632,7 @@ public Color getCosmeticColor(CosmeticSlot slot) {
public List<CosmeticSlot> getDyeableSlots() {
ArrayList<CosmeticSlot> dyableSlots = new ArrayList<>();

for (Cosmetic cosmetic : getCosmetics()) {
for (Cosmetic cosmetic : playerCosmetics.values()) {
if (cosmetic.isDyable()) dyableSlots.add(cosmetic.getSlot());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class UserBackpackManager {
@Getter
private boolean backpackHidden;
@Getter
private int invisibleArmorStand;
private final int invisibleArmorStand;
private ArrayList<Integer> particleCloud = new ArrayList<>();
@Getter
private final CosmeticUser user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public void setLocation(Location location) {
this.getModelEntity().teleport(location);
}

public Vector getVelocity() {
return getModelEntity().getVelocity();
}

public void setVelocity(Vector vector) {
this.getModelEntity().setVelocity(vector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,7 @@ public static void sendMovePacket(
*/
@NotNull
public static List<Player> getViewers(@NotNull Location location) {
ArrayList<Player> viewers = new ArrayList<>();
if (Settings.getViewDistance() <= 0) {
viewers.addAll(location.getWorld().getPlayers());
} else {
viewers.addAll(PacketManager.getViewers(location, Settings.getViewDistance()));
}
return viewers;
return PacketManager.getViewers(location, Settings.getViewDistance());
}

public static void sendPacket(Player player, PacketContainer packet) {
Expand Down