diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java index d9b71338..91d5ca72 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java @@ -33,21 +33,21 @@ public class CosmeticBackpackType extends Cosmetic { return; } - if (loc.getWorld() != user.getBackpackEntity().getBukkitLivingEntity().getWorld()) { - user.getBackpackEntity().getBukkitLivingEntity().teleport(loc); + if (loc.getWorld() != user.getBackpackEntity().getWorld()) { + user.getBackpackEntity().teleport(loc); } - user.getBackpackEntity().moveTo(loc.getX(), loc.getY(), loc.getZ()); + user.getBackpackEntity().teleport(loc); if (player.getPassengers().isEmpty()) { //HMCCosmeticsPlugin.getInstance().getLogger().info("No passengers"); - user.getBackpackEntity().getBukkitLivingEntity().teleport(loc); - player.addPassenger(user.getBackpackEntity().getBukkitEntity()); + user.getBackpackEntity().teleport(loc); + player.addPassenger(user.getBackpackEntity()); } else { //HMCCosmeticsPlugin.getInstance().getLogger().info("Passengers: " + player.getPassengers()); } - user.getBackpackEntity().getBukkitLivingEntity().setRotation(loc.getYaw(), loc.getPitch()); + user.getBackpackEntity().setRotation(loc.getYaw(), loc.getPitch()); user.showBackpack(); } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java index d67d23b3..472f372e 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java @@ -2,6 +2,7 @@ package com.hibiscusmc.hmccosmetics.entities; import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.config.Settings; +import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; import com.ticxo.modelengine.api.ModelEngineAPI; import com.ticxo.modelengine.api.model.ActiveModel; import com.ticxo.modelengine.api.model.ModeledEntity; @@ -21,7 +22,7 @@ public class BalloonEntity { public BalloonEntity(Location location) { this.uniqueID = UUID.randomUUID(); - this.balloonID = Entity.nextEntityId(); + this.balloonID = NMSHandlers.getHandler().getNextEntityId(); this.modelEntity = new MEGEntity(location.add(Settings.getBalloonOffset())); } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java index fd11e95e..efd1a914 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java @@ -1,10 +1,10 @@ package com.hibiscusmc.hmccosmetics.listener; import com.hibiscusmc.hmccosmetics.database.Database; -import com.hibiscusmc.hmccosmetics.entities.InvisibleArmorstand; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.user.CosmeticUsers; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; @@ -25,8 +25,8 @@ public class PlayerConnectionListener implements Listener { if (user == null) { // Remove any passengers if a user failed to initialize. Bugs can cause this to happen if (!event.getPlayer().getPassengers().isEmpty()) { for (Entity entity : event.getPlayer().getPassengers()) { - if (entity instanceof InvisibleArmorstand) { - ((InvisibleArmorstand) entity).setHealth(0); + if (entity.getType() == EntityType.ARMOR_STAND) { + entity.remove(); entity.remove(); } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java index 2cfb280b..58a401db 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java @@ -79,7 +79,7 @@ public class PlayerGameListener implements Listener { if (user.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { user.hideBackpack(); - user.getBackpackEntity().getBukkitLivingEntity().teleport(event.getTo()); + user.getBackpackEntity().teleport(event.getTo()); Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> { user.showBackpack(); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java index 182f2c7b..a7e0c9f7 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java @@ -1,9 +1,24 @@ package com.hibiscusmc.hmccosmetics.nms; +import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType; +import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType; +import com.hibiscusmc.hmccosmetics.entities.BalloonEntity; +import com.hibiscusmc.hmccosmetics.user.CosmeticUser; +import org.bukkit.Location; +import org.bukkit.entity.Entity; + public interface NMSHandler { int getNextEntityId(); + Entity getEntity(int entityId); + + Entity getInvisibleArmorstand(Location loc); + + Entity spawnBackpack(CosmeticUser user, CosmeticBackpackType cosmeticBackpackType); + + BalloonEntity spawnBalloon(CosmeticUser user, CosmeticBalloonType cosmeticBalloonType); + default boolean getSupported () { return false; } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java index 26e73285..3b255cf9 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -9,7 +9,8 @@ import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticArmorType; import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType; import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType; import com.hibiscusmc.hmccosmetics.entities.BalloonEntity; -import com.hibiscusmc.hmccosmetics.entities.InvisibleArmorstand; +import com.hibiscusmc.hmccosmetics.nms.NMSHandler; +import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; import com.hibiscusmc.hmccosmetics.util.PlayerUtils; import com.hibiscusmc.hmccosmetics.util.packets.PacketManager; import net.minecraft.world.entity.EquipmentSlot; @@ -18,8 +19,7 @@ import org.bukkit.Color; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; +import org.bukkit.entity.*; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -35,7 +35,7 @@ public class CosmeticUser { private UUID uniqueId; private HashMap playerCosmetics = new HashMap<>(); private Wardrobe wardrobe; - private InvisibleArmorstand invisibleArmorstand; + private ArmorStand invisibleArmorstand; private BalloonEntity balloonEntity; // Cosmetic Settings/Toggles @@ -60,10 +60,10 @@ public class CosmeticUser { } public int getArmorstandId() { - return invisibleArmorstand.getId(); + return invisibleArmorstand.getEntityId(); } - public InvisibleArmorstand getBackpackEntity() { + public Entity getBackpackEntity() { return this.invisibleArmorstand; } public BalloonEntity getBalloonEntity() { @@ -186,38 +186,19 @@ public class CosmeticUser { List sentTo = PlayerUtils.getNearbyPlayers(player.getLocation()); if (this.invisibleArmorstand != null) return; - this.invisibleArmorstand = new InvisibleArmorstand(player.getLocation()); - ItemStack item = getUserCosmeticItem(cosmeticBackpackType); + this.invisibleArmorstand = (ArmorStand) NMSHandlers.getHandler().spawnBackpack(this, cosmeticBackpackType); - invisibleArmorstand.setItemSlot(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(item)); - ((CraftWorld) player.getWorld()).getHandle().addFreshEntity(invisibleArmorstand, CreatureSpawnEvent.SpawnReason.CUSTOM); - - //PacketManager.armorStandMetaPacket(invisibleArmorstand.getBukkitEntity(), sentTo); - //PacketManager.ridingMountPacket(player.getEntityId(), invisibleArmorstand.getId(), sentTo); - - player.addPassenger(invisibleArmorstand.getBukkitEntity()); + player.addPassenger(invisibleArmorstand); } public void spawnBalloon(CosmeticBalloonType cosmeticBalloonType) { Player player = Bukkit.getPlayer(getUniqueId()); - List sentTo = PlayerUtils.getNearbyPlayers(player.getLocation()); - Location newLoc = player.getLocation().clone().add(Settings.getBalloonOffset()); if (this.balloonEntity != null) return; - BalloonEntity balloonEntity1 = new BalloonEntity(player.getLocation()); - ((CraftWorld) player.getWorld()).getHandle().addFreshEntity(balloonEntity1.getModelEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM); - - balloonEntity1.spawnModel(cosmeticBalloonType.getModelName()); - balloonEntity1.addPlayerToModel(player, cosmeticBalloonType.getModelName()); - - PacketManager.sendEntitySpawnPacket(newLoc, balloonEntity1.getPufferfishBalloonId(), EntityType.PUFFERFISH, balloonEntity1.getPufferfishBalloonUniqueId(), sentTo); - PacketManager.sendInvisibilityPacket(balloonEntity1.getPufferfishBalloonId(), sentTo); - PacketManager.sendLeashPacket(balloonEntity1.getPufferfishBalloonId(), player.getEntityId(), sentTo); - - this.balloonEntity = balloonEntity1; + this.balloonEntity = NMSHandlers.getHandler().spawnBalloon(this, cosmeticBalloonType); } public void despawnBalloon() { @@ -233,8 +214,9 @@ public class CosmeticUser { public void despawnBackpack() { Player player = Bukkit.getPlayer(getUniqueId()); if (invisibleArmorstand == null) return; - invisibleArmorstand.getBukkitLivingEntity().setHealth(0); - invisibleArmorstand.remove(net.minecraft.world.entity.Entity.RemovalReason.DISCARDED); + invisibleArmorstand.setHealth(0); + invisibleArmorstand.remove(); + //invisibleArmorstand.remove(net.minecraft.world.entity.Entity.RemovalReason.DISCARDED); this.invisibleArmorstand = null; } @@ -274,8 +256,8 @@ public class CosmeticUser { if (hideBackpack == true) return; if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { //CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK); - getPlayer().removePassenger(invisibleArmorstand.getBukkitEntity()); - invisibleArmorstand.getBukkitLivingEntity().getEquipment().clear(); + getPlayer().removePassenger(invisibleArmorstand); + invisibleArmorstand.getEquipment().clear(); hideBackpack = true; } } @@ -284,9 +266,9 @@ public class CosmeticUser { if (hideBackpack == false) return; if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK); - getPlayer().addPassenger(invisibleArmorstand.getBukkitEntity()); + getPlayer().addPassenger(invisibleArmorstand); ItemStack item = getUserCosmeticItem(cosmeticBackpackType); - invisibleArmorstand.setItemSlot(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(item)); + invisibleArmorstand.getEquipment().setHelmet(item); hideBackpack = false; } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/Wardrobe.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/Wardrobe.java index 0a2a3772..b7327e53 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/Wardrobe.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/Wardrobe.java @@ -4,6 +4,7 @@ import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.config.Settings; import com.hibiscusmc.hmccosmetics.config.WardrobeSettings; import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot; +import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; import com.hibiscusmc.hmccosmetics.util.ServerUtils; import com.hibiscusmc.hmccosmetics.util.packets.PacketManager; import net.minecraft.world.entity.Entity; @@ -31,8 +32,8 @@ public class Wardrobe { private boolean active; public Wardrobe(CosmeticUser user) { - NPC_ID = Entity.nextEntityId(); - ARMORSTAND_ID = Entity.nextEntityId(); + NPC_ID = NMSHandlers.getHandler().getNextEntityId(); + ARMORSTAND_ID = NMSHandlers.getHandler().getNextEntityId(); WARDROBE_UUID = UUID.randomUUID(); VIEWER = user; } @@ -78,7 +79,7 @@ public class Wardrobe { // Misc if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { - PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getId(), viewer); + PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getEntityId(), viewer); } if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BALLOON)) { @@ -120,7 +121,7 @@ public class Wardrobe { VIEWER.showPlayer(); if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { - PacketManager.ridingMountPacket(player.getEntityId(), VIEWER.getBackpackEntity().getId(), viewer); + PacketManager.ridingMountPacket(player.getEntityId(), VIEWER.getBackpackEntity().getEntityId(), viewer); } if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BALLOON)) { @@ -168,8 +169,8 @@ public class Wardrobe { if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) { PacketManager.sendTeleportPacket(VIEWER.getArmorstandId(), location, false, viewer); - PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getId(), viewer); - VIEWER.getBackpackEntity().getBukkitEntity().setRotation(nextyaw, 0); + PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getEntityId(), viewer); + VIEWER.getBackpackEntity().setRotation(nextyaw, 0); } if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BALLOON)) { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/ServerUtils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/ServerUtils.java index ea0cf218..adcfb4be 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/ServerUtils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/ServerUtils.java @@ -1,10 +1,7 @@ package com.hibiscusmc.hmccosmetics.util; -import net.minecraft.server.level.ServerLevel; -import org.bukkit.Bukkit; +import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; import org.bukkit.GameMode; -import org.bukkit.craftbukkit.v1_19_R1.CraftServer; -import org.jetbrains.annotations.Nullable; public class ServerUtils { @@ -22,19 +19,7 @@ public class ServerUtils { }; } - @Nullable public static org.bukkit.entity.Entity getEntity(int entityId) { - net.minecraft.world.entity.Entity entity = getNMSEntity(entityId); - if (entity == null) return null; - return entity.getBukkitEntity(); - } - @Nullable - public static net.minecraft.world.entity.Entity getNMSEntity(int entityId) { - for (ServerLevel world : ((CraftServer) Bukkit.getServer()).getHandle().getServer().getAllLevels()) { - net.minecraft.world.entity.Entity entity = world.getEntity(entityId); - if (entity == null) return null; - return entity; - } - return null; + return NMSHandlers.getHandler().getEntity(entityId); } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/InvisibleArmorstand.java b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/InvisibleArmorstand.java similarity index 93% rename from common/src/main/java/com/hibiscusmc/hmccosmetics/entities/InvisibleArmorstand.java rename to v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/InvisibleArmorstand.java index 5a408d04..dd933908 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/InvisibleArmorstand.java +++ b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/InvisibleArmorstand.java @@ -1,4 +1,4 @@ -package com.hibiscusmc.hmccosmetics.entities; +package com.hibiscusmc.hmccosmetics.nms.v1_19_R1; import net.minecraft.world.entity.decoration.ArmorStand; import net.minecraft.world.level.Level; diff --git a/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/NMSHandler.java b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/NMSHandler.java index a84b9f1d..2711efad 100644 --- a/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/NMSHandler.java +++ b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/NMSHandler.java @@ -1,7 +1,27 @@ package com.hibiscusmc.hmccosmetics.nms.v1_19_R1; +import com.hibiscusmc.hmccosmetics.config.Settings; +import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType; +import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType; +import com.hibiscusmc.hmccosmetics.entities.BalloonEntity; +import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; +import com.hibiscusmc.hmccosmetics.user.CosmeticUser; +import com.hibiscusmc.hmccosmetics.util.PlayerUtils; +import com.hibiscusmc.hmccosmetics.util.packets.PacketManager; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EquipmentSlot; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_19_R1.CraftServer; import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.List; public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { @Override @@ -9,7 +29,61 @@ public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { return Entity.nextEntityId(); } + @Override + public org.bukkit.entity.Entity getEntity(int entityId) { + net.minecraft.world.entity.Entity entity = getNMSEntity(entityId); + if (entity == null) return null; + return entity.getBukkitEntity(); + } + private net.minecraft.world.entity.Entity getNMSEntity(int entityId) { + for (ServerLevel world : ((CraftServer) Bukkit.getServer()).getHandle().getServer().getAllLevels()) { + net.minecraft.world.entity.Entity entity = world.getEntity(entityId); + if (entity == null) return null; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity getInvisibleArmorstand(Location loc) { + InvisibleArmorstand invisibleArmorstand = new InvisibleArmorstand(loc); + return invisibleArmorstand.getBukkitEntity(); + } + + @Override + public org.bukkit.entity.Entity spawnBackpack(CosmeticUser user, CosmeticBackpackType cosmeticBackpackType) { + InvisibleArmorstand invisibleArmorstand = new InvisibleArmorstand(user.getPlayer().getLocation()); + + ItemStack item = user.getUserCosmeticItem(cosmeticBackpackType); + + invisibleArmorstand.setItemSlot(EquipmentSlot.HEAD, CraftItemStack.asNMSCopy(item)); + ((CraftWorld) user.getPlayer().getWorld()).getHandle().addFreshEntity(invisibleArmorstand, CreatureSpawnEvent.SpawnReason.CUSTOM); + + return invisibleArmorstand.getBukkitLivingEntity(); + //PacketManager.armorStandMetaPacket(invisibleArmorstand.getBukkitEntity(), sentTo); + //PacketManager.ridingMountPacket(player.getEntityId(), invisibleArmorstand.getId(), sentTo); + + } + + @Override + public BalloonEntity spawnBalloon(CosmeticUser user, CosmeticBalloonType cosmeticBalloonType) { + Player player = user.getPlayer(); + Location newLoc = player.getLocation().clone().add(Settings.getBalloonOffset()); + + BalloonEntity balloonEntity1 = new BalloonEntity(user.getPlayer().getLocation()); + List sentTo = PlayerUtils.getNearbyPlayers(player.getLocation()); + ((CraftWorld) user.getPlayer().getWorld()).getHandle().addFreshEntity(balloonEntity1.getModelEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM); + + balloonEntity1.spawnModel(cosmeticBalloonType.getModelName()); + balloonEntity1.addPlayerToModel(player, cosmeticBalloonType.getModelName()); + + PacketManager.sendEntitySpawnPacket(newLoc, balloonEntity1.getPufferfishBalloonId(), EntityType.PUFFERFISH, balloonEntity1.getPufferfishBalloonUniqueId(), sentTo); + PacketManager.sendInvisibilityPacket(balloonEntity1.getPufferfishBalloonId(), sentTo); + PacketManager.sendLeashPacket(balloonEntity1.getPufferfishBalloonId(), player.getEntityId(), sentTo); + + return balloonEntity1; + } @Override public boolean getSupported() {