mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-30 04:19:28 +00:00
Reattach backpacks after a player is mounted or dismounted
This commit is contained in:
@@ -45,6 +45,8 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spigotmc.event.entity.EntityDismountEvent;
|
||||
import org.spigotmc.event.entity.EntityMountEvent;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -358,6 +360,24 @@ public class PlayerGameListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerMounted(EntityMountEvent event) {
|
||||
if (!event.isCancelled() && event.getMount() instanceof Player player) {
|
||||
CosmeticUser user = CosmeticUsers.getUser(player);
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(HMCCosmeticsPlugin.getInstance(), ()->{
|
||||
user.getUserBackpackManager().reattach();
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDismounted(EntityDismountEvent event) {
|
||||
if (!event.isCancelled() && event.getDismounted() instanceof Player player) {
|
||||
CosmeticUser user = CosmeticUsers.getUser(player);
|
||||
user.getUserBackpackManager().reattach();
|
||||
}
|
||||
}
|
||||
|
||||
private void registerInventoryClickListener() {
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(HMCCosmeticsPlugin.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Client.WINDOW_CLICK) {
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.hibiscusmc.hmccosmetics.user.manager;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType;
|
||||
import com.hibiscusmc.hmccosmetics.hooks.Hooks;
|
||||
@@ -10,6 +11,8 @@ import com.hibiscusmc.hmccosmetics.util.packets.PacketManager;
|
||||
import com.ticxo.modelengine.api.ModelEngineAPI;
|
||||
import com.ticxo.modelengine.api.model.ActiveModel;
|
||||
import com.ticxo.modelengine.api.model.ModeledEntity;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.ArmorStand;
|
||||
import org.bukkit.entity.Entity;
|
||||
@@ -64,6 +67,26 @@ public class UserBackpackManager {
|
||||
|
||||
private void spawn(CosmeticBackpackType cosmeticBackpackType) {
|
||||
if (this.invisibleArmorStand != null) return;
|
||||
|
||||
attach(cosmeticBackpackType);
|
||||
|
||||
// No one should be using ME because it barely works but some still use it, so it's here
|
||||
if (cosmeticBackpackType.getModelName() != null && Hooks.isActiveHook("ModelEngine")) {
|
||||
if (ModelEngineAPI.api.getModelRegistry().getBlueprint(cosmeticBackpackType.getModelName()) == null) {
|
||||
MessagesUtil.sendDebugMessages("Invalid Model Engine Blueprint " + cosmeticBackpackType.getModelName(), Level.SEVERE);
|
||||
return;
|
||||
}
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getOrCreateModeledEntity(invisibleArmorStand);
|
||||
ActiveModel model = ModelEngineAPI.createActiveModel(ModelEngineAPI.getBlueprint(cosmeticBackpackType.getModelName()));
|
||||
model.setCanHurt(false);
|
||||
modeledEntity.addModel(model, false);
|
||||
}
|
||||
|
||||
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Finish");
|
||||
}
|
||||
|
||||
private void attach(CosmeticBackpackType cosmeticBackpackType) {
|
||||
if (this.invisibleArmorStand != null) return;
|
||||
this.invisibleArmorStand = (ArmorStand) NMSHandlers.getHandler().spawnBackpack(user, cosmeticBackpackType);
|
||||
|
||||
List<Player> outsideViewers = user.getUserBackpackManager().getCloudManager().refreshViewers(user.getEntity().getLocation());
|
||||
@@ -89,20 +112,10 @@ public class UserBackpackManager {
|
||||
if (!user.getHidden()) NMSHandlers.getHandler().equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, cosmeticBackpackType.getFirstPersonBackpack(), owner);
|
||||
}
|
||||
PacketManager.sendRidingPacket(entity.getEntityId(), user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers);
|
||||
}
|
||||
|
||||
// No one should be using ME because it barely works but some still use it, so it's here
|
||||
if (cosmeticBackpackType.getModelName() != null && Hooks.isActiveHook("ModelEngine")) {
|
||||
if (ModelEngineAPI.api.getModelRegistry().getBlueprint(cosmeticBackpackType.getModelName()) == null) {
|
||||
MessagesUtil.sendDebugMessages("Invalid Model Engine Blueprint " + cosmeticBackpackType.getModelName(), Level.SEVERE);
|
||||
return;
|
||||
}
|
||||
ModeledEntity modeledEntity = ModelEngineAPI.getOrCreateModeledEntity(invisibleArmorStand);
|
||||
ActiveModel model = ModelEngineAPI.createActiveModel(ModelEngineAPI.getBlueprint(cosmeticBackpackType.getModelName()));
|
||||
model.setCanHurt(false);
|
||||
modeledEntity.addModel(model, false);
|
||||
}
|
||||
|
||||
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Finish");
|
||||
public void reattach() {
|
||||
attach((CosmeticBackpackType) user.getCosmetic(CosmeticSlot.BACKPACK));
|
||||
}
|
||||
|
||||
public void despawnBackpack() {
|
||||
|
||||
Reference in New Issue
Block a user