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

Fixed unequipping first person cosmetics

This commit is contained in:
Fisher2911
2022-07-08 20:13:43 -04:00
parent b13d44536f
commit 86346583cc
5 changed files with 48 additions and 13 deletions

View File

@@ -26,7 +26,6 @@ import io.github.fisher2911.hmccosmetics.message.Messages;
import io.github.fisher2911.hmccosmetics.message.Translation;
import io.github.fisher2911.hmccosmetics.task.TaskManager;
import io.github.fisher2911.hmccosmetics.user.UserManager;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import me.mattstudios.mf.base.CommandManager;
import me.mattstudios.mf.base.CompletionHandler;
import org.bstats.bukkit.Metrics;
@@ -75,7 +74,7 @@ public class HMCCosmetics extends JavaPlugin {
this.settings = new Settings(this);
this.messageHandler = new MessageHandler(this);
this.userManager = new UserManager(this);
this.cosmeticManager = new CosmeticManager(new HashMap<>(), new HashMap<>());
this.cosmeticManager = new CosmeticManager(new HashMap<>(), new HashMap<>(), new HashMap<>());
this.cosmeticsMenu = new CosmeticsMenu(this);
this.tokenLoader = new TokenLoader(this);

View File

@@ -23,10 +23,12 @@ public class CosmeticManager {
private final Map<String, Token> tokenMap;
private final Map<String, ArmorItem> armorItemMap;
private final Map<String, Integer> backpackParticleCounts;
public CosmeticManager(final Map<String, Token> tokenMap, final Map<String, ArmorItem> armorItemMap) {
public CosmeticManager(final Map<String, Token> tokenMap, final Map<String, ArmorItem> armorItemMap, final Map<String, Integer> backpackParticleCounts) {
this.tokenMap = tokenMap;
this.armorItemMap = armorItemMap;
this.backpackParticleCounts = backpackParticleCounts;
}
@Nullable
@@ -82,6 +84,16 @@ public class CosmeticManager {
this.armorItemMap.clear();
}
public int getBackpackParticleCount(final String id) {
return this.backpackParticleCounts.getOrDefault(id, 0);
}
public int getBackpackParticleCount(final ArmorItem armorItem) {
return this.getBackpackParticleCount(armorItem.getId());
}
private static final String PARTICLE_COUNT = "particle-count";
public void load() {
this.clearItems();
try {
@@ -102,6 +114,10 @@ public class CosmeticManager {
final WrappedGuiItem item = ArmorItemSerializer.INSTANCE.deserialize(WrappedGuiItem.class, node);
if (item instanceof ArmorItem armorItem) {
armorItem.setAction(null);
if (armorItem.getType() == ArmorItem.Type.SELF_BACKPACK) {
final int particleCount = node.node(PARTICLE_COUNT).getInt(0);
this.backpackParticleCounts.put(armorItem.getId(), particleCount);
}
this.armorItemMap.put(armorItem.getId(), armorItem);
}
}

View File

@@ -52,6 +52,9 @@ public class PlayerArmor {
}
public ArmorItem setItem(final ArmorItem armorItem) {
if (armorItem.isEmpty() && armorItem.getType() == ArmorItem.Type.BACKPACK) {
this.armorItems.put(ArmorItem.Type.SELF_BACKPACK, armorItem);
}
return this.armorItems.put(armorItem.getType(), armorItem);
}

View File

@@ -22,13 +22,13 @@ public class Backpack {
private final HMCCosmetics plugin;
private final int armorStandID;
private final int particleCount = HMCCosmetics.getPlugin(HMCCosmetics.class).getSettings().getCosmeticSettings().getParticleCount();
private final List<Integer> IDs = new ArrayList<>();
private final List<Integer> particleIDS = new ArrayList<>();
public Backpack(HMCCosmetics plugin, int armorStandID) {
this.plugin = plugin;
this.armorStandID = armorStandID;
for (int i = 0; i < particleCount; i++) {
IDs.add(SpigotReflectionUtil.generateEntityId());
particleIDS.add(SpigotReflectionUtil.generateEntityId());
}
}
@@ -41,6 +41,21 @@ public class Backpack {
this.spawnForSelf(other, location);
}
private void updateIds(BaseUser<?> owner) {
final ArmorItem selfBackpack = owner.getPlayerArmor().getItem(ArmorItem.Type.SELF_BACKPACK);
if (selfBackpack.isEmpty()) return;
final int particleCount = this.plugin.getCosmeticManager().getBackpackParticleCount(selfBackpack);
final int currentSize = this.particleIDS.size();
if (currentSize == particleCount) return;
if (currentSize < particleCount) {
for (int i = currentSize; i < particleCount; i++) {
this.particleIDS.add(SpigotReflectionUtil.generateEntityId());
}
return;
}
this.particleIDS.subList(particleCount, currentSize).clear();
}
private void spawnForOther(BaseUser<?> owner, Player other, Location location) {
PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityTypes.ARMOR_STAND, other);
PacketManager.sendArmorStandMetaContainer(this.armorStandID, other);
@@ -48,7 +63,7 @@ public class Backpack {
}
private void spawnForSelf(Player other, Location location) {
for (final int id : this.IDs) {
for (final int id : this.particleIDS) {
PacketManager.sendEntityNotLivingSpawnPacket(
location,
id,
@@ -63,14 +78,14 @@ public class Backpack {
public void despawn(Player player) {
PacketManager.sendEntityDestroyPacket(this.armorStandID, player);
for (Integer id : this.IDs) {
for (Integer id : this.particleIDS) {
PacketManager.sendEntityDestroyPacket(id, player);
}
}
public void despawn() {
PacketManager.sendEntityDestroyPacket(this.armorStandID, Bukkit.getOnlinePlayers());
for (Integer id : this.IDs) {
for (Integer id : this.particleIDS) {
PacketManager.sendEntityDestroyPacket(id, Bukkit.getOnlinePlayers());
}
}
@@ -106,7 +121,6 @@ public class Backpack {
itemStack
));
PacketManager.sendEquipmentPacket(equipment, this.armorStandID, other);
PacketManager.sendArmorStandMetaContainer(this.armorStandID, other);
PacketManager.sendRotationPacket(this.armorStandID, location, false, other);
@@ -115,16 +129,16 @@ public class Backpack {
PacketManager.sendRidingPacket(owner.getEntityId(), this.armorStandID, other);
return;
}
for (int i = 0; i < this.IDs.size(); i++) {
final int id = this.IDs.get(i);
for (int i = 0; i < this.particleIDS.size(); i++) {
final int id = this.particleIDS.get(i);
PacketManager.sendCloudMetaData(id, other);
if (i == 0) {
PacketManager.sendRidingPacket(owner.getEntityId(), id, other);
} else {
PacketManager.sendRidingPacket(this.IDs.get(i - 1), id, other);
PacketManager.sendRidingPacket(this.particleIDS.get(i - 1), id, other);
}
}
PacketManager.sendRidingPacket(IDs.get(IDs.size() - 1), this.armorStandID, other);
PacketManager.sendRidingPacket(particleIDS.get(particleIDS.size() - 1), this.armorStandID, other);
}
}

View File

@@ -198,6 +198,9 @@ public class UserManager {
final ArmorItem.Type type = armorItem.getType();
if (type == ArmorItem.Type.BALLOON) user.despawnBalloon();
user.setItem(event.getCosmeticItem().getArmorItem());
if (armorItem.getType() == ArmorItem.Type.BACKPACK && armorItem.isEmpty()) {
user.removeItem(ArmorItem.Type.SELF_BACKPACK);
}
if (!sendPacket) {
user.setArmorUpdated(false);
return;