From 0905071dd2c914c4e1f84b640c4977547083a582 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Fri, 20 Jan 2023 16:08:28 -0600 Subject: [PATCH] Balloon rework for items as balloons --- .../cosmetic/types/CosmeticBalloonType.java | 4 +- .../hmccosmetics/entities/BalloonEntity.java | 115 ++++++++++++------ .../hmccosmetics/nms/NMSHandler.java | 3 +- .../hmccosmetics/user/CosmeticUser.java | 2 +- .../hmccosmetics/nms/v1_19_R1/MEGEntity.java | 23 ++-- .../hmccosmetics/nms/v1_19_R1/NMSHandler.java | 9 +- .../hmccosmetics/nms/v1_19_R2/MEGEntity.java | 32 ++--- .../hmccosmetics/nms/v1_19_R2/NMSHandler.java | 9 +- 8 files changed, 119 insertions(+), 78 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java index aa53e3af..26810265 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java @@ -43,9 +43,9 @@ public class CosmeticBalloonType extends Cosmetic { return; } - Vector velocity = newLocation.clone().toVector().subtract(currentLocation.clone().toVector()); + Vector velocity = newLocation.toVector().subtract(currentLocation.toVector()); + balloonEntity.setVelocity(velocity.multiply(1.1)); balloonEntity.setLocation(newLocation); - balloonEntity.setVelocity(velocity); PacketManager.sendTeleportPacket(balloonEntity.getPufferfishBalloonId(), newLocation, false, viewer); if (!user.getHidden()) PacketManager.sendLeashPacket(balloonEntity.getPufferfishBalloonId(), player.getEntityId(), viewer); 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 758c3ee2..cd0b1903 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/entities/BalloonEntity.java @@ -1,6 +1,7 @@ package com.hibiscusmc.hmccosmetics.entities; import com.hibiscusmc.hmccosmetics.config.Settings; +import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType; import com.hibiscusmc.hmccosmetics.nms.NMSHandlers; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import com.ticxo.modelengine.api.ModelEngineAPI; @@ -8,20 +9,21 @@ import com.ticxo.modelengine.api.model.ActiveModel; import com.ticxo.modelengine.api.model.ModeledEntity; import org.bukkit.Color; import org.bukkit.Location; +import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import java.util.UUID; import java.util.logging.Level; -// This includes the Pufferfish (The Pufferfish that's what the player leashes to) and the model (MEGEntity) public class BalloonEntity { + private BalloonType balloonType; private final int balloonID; private final UUID uniqueID; - private final Entity modelEntity; - private ModeledEntity modeledEntity; + private final ArmorStand modelEntity; public BalloonEntity(Location location) { this.uniqueID = UUID.randomUUID(); @@ -29,62 +31,91 @@ public class BalloonEntity { this.modelEntity = NMSHandlers.getHandler().getMEGEntity(location.add(Settings.getBalloonOffset())); } - public void spawnModel(final String id, Color color) { + public void spawnModel(CosmeticBalloonType cosmeticBalloonType, Color color) { + // redo this + if (cosmeticBalloonType.getModelName() != null) { + balloonType = BalloonType.MODELENGINE; + } else { + if (cosmeticBalloonType.getItem() != null) { + balloonType = BalloonType.ITEM; + } else { + balloonType = BalloonType.NONE; + } + } + MessagesUtil.sendDebugMessages("balloontype is " + balloonType); - MessagesUtil.sendDebugMessages("Attempting Spawning for " + id); - if (ModelEngineAPI.api.getModelRegistry().getBlueprint(id) == null) { - MessagesUtil.sendDebugMessages("Invalid Model Engine Blueprint " + id, Level.SEVERE); - return; + if (balloonType == BalloonType.MODELENGINE) { + String id = cosmeticBalloonType.getModelName(); + MessagesUtil.sendDebugMessages("Attempting Spawning for " + id); + if (ModelEngineAPI.api.getModelRegistry().getBlueprint(id) == null) { + MessagesUtil.sendDebugMessages("Invalid Model Engine Blueprint " + id, Level.SEVERE); + return; + } + ModeledEntity modeledEntity = ModelEngineAPI.getOrCreateModeledEntity(modelEntity); + ActiveModel model = ModelEngineAPI.createActiveModel(ModelEngineAPI.getBlueprint(id)); + model.setCanHurt(false); + modeledEntity.addModel(model, false); + if (color != null) { + modeledEntity.getModels().forEach((d, singleModel) -> { + singleModel.getRendererHandler().setColor(color); + singleModel.getRendererHandler().update(); + }); + } + } else { + modelEntity.getEquipment().setHelmet(cosmeticBalloonType.getItem()); } - ModeledEntity modeledEntity = ModelEngineAPI.getOrCreateModeledEntity(modelEntity); - ActiveModel model = ModelEngineAPI.createActiveModel(ModelEngineAPI.getBlueprint(id)); - model.setCanHurt(false); - modeledEntity.addModel(model, false); - if (color != null) { - modeledEntity.getModels().forEach((d, singleModel) -> { - singleModel.getRendererHandler().setColor(color); - singleModel.getRendererHandler().update(); - }); - } - this.modeledEntity = modeledEntity; } public void remove() { - final ModeledEntity entity = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); + if (balloonType == BalloonType.MODELENGINE) { + final ModeledEntity entity = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); - if (entity == null) return; + if (entity == null) return; - for (final Player player : entity.getRangeManager().getPlayerInRange()) { - entity.hideFromPlayer(player); + for (final Player player : entity.getRangeManager().getPlayerInRange()) { + entity.hideFromPlayer(player); + } + entity.destroy(); } - //ModelEngineAPI.removeModeledEntity(megEntity.getUniqueId()); - entity.destroy(); modelEntity.remove(); } - public void addPlayerToModel(final Player player, final String id) { - addPlayerToModel(player, id, null); + public void addPlayerToModel(final Player player, final CosmeticBalloonType cosmeticBalloonType) { + addPlayerToModel(player, cosmeticBalloonType, null); } - public void addPlayerToModel(final Player player, final String id, Color color) { - final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); - if (model == null) { - spawnModel(id, color); - MessagesUtil.sendDebugMessages("model is null"); + public void addPlayerToModel(final Player player, final CosmeticBalloonType cosmeticBalloonType, Color color) { + if (balloonType == BalloonType.MODELENGINE) { + final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); + if (model == null) { + spawnModel(cosmeticBalloonType, color); + MessagesUtil.sendDebugMessages("model is null"); + return; + } + //if (model.getRangeManager().getPlayerInRange().contains(player)) return; + model.showToPlayer(player); + MessagesUtil.sendDebugMessages("Show to player"); return; } - //if (model.getRangeManager().getPlayerInRange().contains(player)) return; - model.showToPlayer(player); - MessagesUtil.sendDebugMessages("Show to player"); + if (balloonType == BalloonType.ITEM) { + modelEntity.getEquipment().setHelmet(cosmeticBalloonType.getItem()); + } } public void removePlayerFromModel(final Player player) { - final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); + if (balloonType == BalloonType.MODELENGINE) { + final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId()); - if (model == null) return; + if (model == null) return; - model.hideFromPlayer(player); - MessagesUtil.sendDebugMessages("Hidden from player"); + model.hideFromPlayer(player); + MessagesUtil.sendDebugMessages("Hidden from player"); + return; + } + if (balloonType == BalloonType.ITEM) { + modelEntity.getEquipment().clear(); + return; + } } public Entity getModelEntity() { @@ -118,4 +149,10 @@ public class BalloonEntity { public void setVelocity(Vector vector) { this.getModelEntity().setVelocity(vector); } + + public enum BalloonType { + MODELENGINE, + ITEM, + NONE + } } 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 69814600..58d35b81 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/nms/NMSHandler.java @@ -6,6 +6,7 @@ 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.ArmorStand; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -20,7 +21,7 @@ public interface NMSHandler { Entity getInvisibleArmorstand(Location loc); - Entity getMEGEntity(Location loc); + ArmorStand getMEGEntity(Location loc); Entity spawnBackpack(CosmeticUser user, CosmeticBackpackType cosmeticBackpackType); 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 a0456b2c..c30a368f 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -432,7 +432,7 @@ public class CosmeticUser { hideCosmetics = false; if (hasCosmeticInSlot(CosmeticSlot.BALLOON)) { CosmeticBalloonType balloonType = (CosmeticBalloonType) getCosmetic(CosmeticSlot.BALLOON); - getBalloonEntity().addPlayerToModel(getPlayer(), balloonType.getModelName()); + getBalloonEntity().addPlayerToModel(getPlayer(), balloonType); List viewer = PlayerUtils.getNearbyPlayers(getPlayer()); PacketManager.sendLeashPacket(getBalloonEntity().getPufferfishBalloonId(), getPlayer().getEntityId(), viewer); } diff --git a/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/MEGEntity.java b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/MEGEntity.java index 40e1b6ff..4acf8fe9 100644 --- a/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/MEGEntity.java +++ b/v1_19_R1/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R1/MEGEntity.java @@ -3,26 +3,27 @@ package com.hibiscusmc.hmccosmetics.nms.v1_19_R1; import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.animal.Cod; +import net.minecraft.world.entity.decoration.ArmorStand; import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.persistence.PersistentDataType; -public class MEGEntity extends Cod { +public class MEGEntity extends ArmorStand { public MEGEntity(Location loc) { - super(EntityType.COD, ((CraftWorld) loc.getWorld()).getHandle()); + super(EntityType.ARMOR_STAND, ((CraftWorld) loc.getWorld()).getHandle()); this.setPos(loc.getX(), loc.getY(), loc.getZ()); - MessagesUtil.sendDebugMessages("Spawned MEGEntity at " + loc); - getBukkitLivingEntity().setInvisible(true); - getBukkitLivingEntity().setInvulnerable(true); // NOTE - CREATIVE PLAYERS CAN DESTROY IT STILL - getBukkitLivingEntity().setAI(false); - getBukkitLivingEntity().setGravity(false); - getBukkitLivingEntity().setSilent(true); - getBukkitLivingEntity().setCollidable(false); - persist = false; + MessagesUtil.sendDebugMessages("Spawned MEGEntity at " + loc); + setInvisible(true); + setNoGravity(true); + setSilent(true); + setInvulnerable(true); + setSmall(true); + setMarker(true); + + persist = false; getBukkitEntity().getPersistentDataContainer().set(new NamespacedKey(HMCCosmeticsPlugin.getInstance(), "cosmeticMob"), PersistentDataType.SHORT, Short.valueOf("1")); ((CraftWorld) loc.getWorld()).getHandle().addFreshEntity(this); 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 a8ba6eee..d2a4ffd5 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 @@ -33,6 +33,7 @@ import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_19_R1.scoreboard.CraftScoreboard; +import org.bukkit.entity.ArmorStand; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.entity.CreatureSpawnEvent; @@ -71,8 +72,8 @@ public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { } @Override - public org.bukkit.entity.Entity getMEGEntity(Location loc) { - return new MEGEntity(loc).getBukkitEntity(); + public ArmorStand getMEGEntity(Location loc) { + return (ArmorStand) new MEGEntity(loc).getBukkitEntity(); } @Override @@ -103,8 +104,8 @@ public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { List sentTo = PlayerUtils.getNearbyPlayers(player.getLocation()); balloonEntity1.getModelEntity().teleport(user.getPlayer().getLocation().add(Settings.getBalloonOffset())); - balloonEntity1.spawnModel(cosmeticBalloonType.getModelName(), user.getCosmeticColor(cosmeticBalloonType.getSlot())); - balloonEntity1.addPlayerToModel(player, cosmeticBalloonType.getModelName(), user.getCosmeticColor(cosmeticBalloonType.getSlot())); + balloonEntity1.spawnModel(cosmeticBalloonType, user.getCosmeticColor(cosmeticBalloonType.getSlot())); + balloonEntity1.addPlayerToModel(player, cosmeticBalloonType, user.getCosmeticColor(cosmeticBalloonType.getSlot())); PacketManager.sendEntitySpawnPacket(newLoc, balloonEntity1.getPufferfishBalloonId(), EntityType.PUFFERFISH, balloonEntity1.getPufferfishBalloonUniqueId(), sentTo); PacketManager.sendInvisibilityPacket(balloonEntity1.getPufferfishBalloonId(), sentTo); diff --git a/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/MEGEntity.java b/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/MEGEntity.java index c6ccd1ea..b4202780 100644 --- a/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/MEGEntity.java +++ b/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/MEGEntity.java @@ -3,28 +3,28 @@ package com.hibiscusmc.hmccosmetics.nms.v1_19_R2; import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.animal.Cod; +import net.minecraft.world.entity.decoration.ArmorStand; import org.bukkit.Location; import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_19_R2.CraftWorld; import org.bukkit.persistence.PersistentDataType; -public class MEGEntity extends Cod { +public class MEGEntity extends ArmorStand { + public MEGEntity(Location loc) { + super(EntityType.ARMOR_STAND, ((CraftWorld) loc.getWorld()).getHandle()); + this.setPos(loc.getX(), loc.getY(), loc.getZ()); - public MEGEntity(Location loc) { - super(EntityType.COD, ((CraftWorld) loc.getWorld()).getHandle()); - this.setPos(loc.getX(), loc.getY(), loc.getZ()); - MessagesUtil.sendDebugMessages("Spawned MEGEntity at " + loc); - getBukkitLivingEntity().setInvisible(true); - getBukkitLivingEntity().setInvulnerable(true); // NOTE - CREATIVE PLAYERS CAN DESTROY IT STILL - getBukkitLivingEntity().setAI(false); - getBukkitLivingEntity().setGravity(false); - getBukkitLivingEntity().setSilent(true); - getBukkitLivingEntity().setCollidable(false); - persist = false; + MessagesUtil.sendDebugMessages("Spawned MEGEntity at " + loc); + setInvisible(true); + setNoGravity(true); + setSilent(true); + setInvulnerable(true); + setSmall(true); + setMarker(true); - getBukkitEntity().getPersistentDataContainer().set(new NamespacedKey(HMCCosmeticsPlugin.getInstance(), "cosmeticMob"), PersistentDataType.SHORT, Short.valueOf("1")); + persist = false; + getBukkitEntity().getPersistentDataContainer().set(new NamespacedKey(HMCCosmeticsPlugin.getInstance(), "cosmeticMob"), PersistentDataType.SHORT, Short.valueOf("1")); - ((CraftWorld) loc.getWorld()).getHandle().addFreshEntity(this); - } + ((CraftWorld) loc.getWorld()).getHandle().addFreshEntity(this); + } } diff --git a/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/NMSHandler.java b/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/NMSHandler.java index 251169f9..d9f6ce75 100644 --- a/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/NMSHandler.java +++ b/v1_19_R2/src/main/java/com/hibiscusmc/hmccosmetics/nms/v1_19_R2/NMSHandler.java @@ -33,6 +33,7 @@ import org.bukkit.craftbukkit.v1_19_R2.CraftWorld; import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_19_R2.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_19_R2.scoreboard.CraftScoreboard; +import org.bukkit.entity.ArmorStand; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.entity.CreatureSpawnEvent; @@ -71,8 +72,8 @@ public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { } @Override - public org.bukkit.entity.Entity getMEGEntity(Location loc) { - return new MEGEntity(loc).getBukkitEntity(); + public ArmorStand getMEGEntity(Location loc) { + return (ArmorStand) new MEGEntity(loc).getBukkitEntity(); } @Override @@ -103,8 +104,8 @@ public class NMSHandler implements com.hibiscusmc.hmccosmetics.nms.NMSHandler { List sentTo = PlayerUtils.getNearbyPlayers(player.getLocation()); balloonEntity1.getModelEntity().teleport(user.getPlayer().getLocation().add(Settings.getBalloonOffset())); - balloonEntity1.spawnModel(cosmeticBalloonType.getModelName(), user.getCosmeticColor(cosmeticBalloonType.getSlot())); - balloonEntity1.addPlayerToModel(player, cosmeticBalloonType.getModelName(), user.getCosmeticColor(cosmeticBalloonType.getSlot())); + balloonEntity1.spawnModel(cosmeticBalloonType, user.getCosmeticColor(cosmeticBalloonType.getSlot())); + balloonEntity1.addPlayerToModel(player, cosmeticBalloonType, user.getCosmeticColor(cosmeticBalloonType.getSlot())); PacketManager.sendEntitySpawnPacket(newLoc, balloonEntity1.getPufferfishBalloonId(), EntityType.PUFFERFISH, balloonEntity1.getPufferfishBalloonUniqueId(), sentTo); PacketManager.sendInvisibilityPacket(balloonEntity1.getPufferfishBalloonId(), sentTo);