9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-31 12:56:39 +00:00

Refactor entities into managers

This commit is contained in:
LoJoSho
2023-02-05 12:28:37 -06:00
parent 8b223228ce
commit 5ecfb08412
13 changed files with 163 additions and 128 deletions

View File

@@ -317,7 +317,7 @@ public class CosmeticCommand implements CommandExecutor {
}
player.sendMessage("Passengers -> " + player.getPassengers());
if (user.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
player.sendMessage("Backpack Location -> " + user.getBackpackEntity().getLocation());
player.sendMessage("Backpack Location -> " + user.getUserBackpackManager().getArmorstand().getLocation());
}
player.sendMessage("Cosmetics -> " + user.getCosmetic());
player.sendMessage("EntityId -> " + player.getEntityId());

View File

@@ -25,15 +25,15 @@ public class CosmeticBackpackType extends Cosmetic {
if (user.isInWardrobe()) return;
if (loc.getWorld() != user.getBackpackEntity().getWorld()) {
user.getBackpackEntity().teleport(loc);
if (loc.getWorld() != user.getUserBackpackManager().getArmorstand().getWorld()) {
user.getUserBackpackManager().getArmorstand().teleport(loc);
}
user.getBackpackEntity().teleport(loc);
user.getUserBackpackManager().getArmorstand().teleport(loc);
PacketManager.sendRidingPacket(player.getEntityId(), user.getBackpackEntity().getEntityId(), loc);
PacketManager.sendRidingPacket(player.getEntityId(), user.getUserBackpackManager().getFirstArmorstandId(), loc);
user.getBackpackEntity().setRotation(loc.getYaw(), loc.getPitch());
user.showBackpack();
user.getUserBackpackManager().getArmorstand().setRotation(loc.getYaw(), loc.getPitch());
user.getUserBackpackManager().showBackpack();
}
}

View File

@@ -2,10 +2,9 @@ package com.hibiscusmc.hmccosmetics.cosmetic.types;
import com.hibiscusmc.hmccosmetics.config.Settings;
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
import com.hibiscusmc.hmccosmetics.entities.BalloonEntity;
import com.hibiscusmc.hmccosmetics.user.manager.UserBalloonManager;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.util.packets.PacketManager;
import org.apache.commons.lang3.EnumUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@@ -13,7 +12,6 @@ import org.bukkit.util.Vector;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import java.util.HashMap;
import java.util.List;
public class CosmeticBalloonType extends Cosmetic {
@@ -58,20 +56,20 @@ public class CosmeticBalloonType extends Cosmetic {
List<Player> viewer = PacketManager.getViewers(player.getLocation());
viewer.add(player);
BalloonEntity balloonEntity = user.getBalloonEntity();
UserBalloonManager userBalloonManager = user.getBalloonEntity();
if (player.getLocation().getWorld() != balloonEntity.getLocation().getWorld()) {
balloonEntity.getModelEntity().teleport(newLocation);
PacketManager.sendTeleportPacket(balloonEntity.getPufferfishBalloonId(), newLocation, false, viewer);
if (player.getLocation().getWorld() != userBalloonManager.getLocation().getWorld()) {
userBalloonManager.getModelEntity().teleport(newLocation);
PacketManager.sendTeleportPacket(userBalloonManager.getPufferfishBalloonId(), newLocation, false, viewer);
return;
}
Vector velocity = newLocation.toVector().subtract(currentLocation.toVector());
balloonEntity.setVelocity(velocity.multiply(1.1));
balloonEntity.setLocation(newLocation);
userBalloonManager.setVelocity(velocity.multiply(1.1));
userBalloonManager.setLocation(newLocation);
PacketManager.sendTeleportPacket(balloonEntity.getPufferfishBalloonId(), newLocation, false, viewer);
if (!user.getHidden()) PacketManager.sendLeashPacket(balloonEntity.getPufferfishBalloonId(), player.getEntityId(), viewer);
PacketManager.sendTeleportPacket(userBalloonManager.getPufferfishBalloonId(), newLocation, false, viewer);
if (!user.getHidden()) PacketManager.sendLeashPacket(userBalloonManager.getPufferfishBalloonId(), player.getEntityId(), viewer);
}
public String getModelName() {

View File

@@ -99,9 +99,9 @@ public class PlayerGameListener implements Listener {
}
if (user.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
user.hideBackpack();
user.getUserBackpackManager().hideBackpack();
user.getBackpackEntity().teleport(event.getTo());
user.getUserBackpackManager().getArmorstand().teleport(event.getTo());
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> {
user.updateCosmetic();

View File

@@ -3,7 +3,7 @@ package com.hibiscusmc.hmccosmetics.nms;
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
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.manager.UserBalloonManager;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
@@ -25,7 +25,7 @@ public interface NMSHandler {
Entity spawnBackpack(CosmeticUser user, CosmeticBackpackType cosmeticBackpackType);
BalloonEntity spawnBalloon(CosmeticUser user, CosmeticBalloonType cosmeticBalloonType);
UserBalloonManager spawnBalloon(CosmeticUser user, CosmeticBalloonType cosmeticBalloonType);
void equipmentSlotUpdate(
int entityId,

View File

@@ -10,7 +10,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.cosmetic.types.CosmeticMainhandType;
import com.hibiscusmc.hmccosmetics.entities.BalloonEntity;
import com.hibiscusmc.hmccosmetics.user.manager.UserBackpackManager;
import com.hibiscusmc.hmccosmetics.user.manager.UserBalloonManager;
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
import com.hibiscusmc.hmccosmetics.util.InventoryUtils;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
@@ -35,19 +36,14 @@ public class CosmeticUser {
private int taskId;
private HashMap<CosmeticSlot, Cosmetic> playerCosmetics = new HashMap<>();
private Wardrobe wardrobe;
private ArmorStand invisibleArmorstand;
private BalloonEntity balloonEntity;
private UserBalloonManager userBalloonManager;
private UserBackpackManager userBackpackManager;
// Cosmetic Settings/Toggles
private boolean hideBackpack;
private boolean hideCosmetics;
private HiddenReason hiddenReason;
private HashMap<CosmeticSlot, Color> colors = new HashMap<>();
public CosmeticUser() {
// Empty
}
public CosmeticUser(UUID uuid) {
this.uniqueId = uuid;
tick();
@@ -84,16 +80,8 @@ public class CosmeticUser {
public Collection<Cosmetic> getCosmetic() {
return playerCosmetics.values();
}
public int getArmorstandId() {
return invisibleArmorstand.getEntityId();
}
public Entity getBackpackEntity() {
return this.invisibleArmorstand;
}
public BalloonEntity getBalloonEntity() {
return this.balloonEntity;
public UserBalloonManager getBalloonEntity() {
return this.userBalloonManager;
}
public void addPlayerCosmetic(Cosmetic cosmetic) {
@@ -287,22 +275,25 @@ public class CosmeticUser {
}
public void spawnBackpack(CosmeticBackpackType cosmeticBackpackType) {
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Start");
Player player = Bukkit.getPlayer(getUniqueId());
this.userBackpackManager = new UserBackpackManager(this);
userBackpackManager.spawnBackpack(cosmeticBackpackType);
}
if (this.invisibleArmorstand != null) return;
public void despawnBackpack() {
userBackpackManager.despawnBackpack();
userBackpackManager = null;
}
this.invisibleArmorstand = (ArmorStand) NMSHandlers.getHandler().spawnBackpack(this, cosmeticBackpackType);
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Finish");
public UserBackpackManager getUserBackpackManager() {
return userBackpackManager;
}
public void spawnBalloon(CosmeticBalloonType cosmeticBalloonType) {
Player player = Bukkit.getPlayer(getUniqueId());
if (this.balloonEntity != null) return;
if (this.userBalloonManager != null) return;
this.balloonEntity = NMSHandlers.getHandler().spawnBalloon(this, cosmeticBalloonType);
this.userBalloonManager = NMSHandlers.getHandler().spawnBalloon(this, cosmeticBalloonType);
List<Player> viewer = PlayerUtils.getNearbyPlayers(player);
viewer.add(player);
@@ -311,21 +302,13 @@ public class CosmeticUser {
}
public void despawnBalloon() {
if (this.balloonEntity == null) return;
if (this.userBalloonManager == null) return;
List<Player> sentTo = PlayerUtils.getNearbyPlayers(getPlayer().getLocation());
PacketManager.sendEntityDestroyPacket(balloonEntity.getPufferfishBalloonId(), sentTo);
PacketManager.sendEntityDestroyPacket(userBalloonManager.getPufferfishBalloonId(), sentTo);
this.balloonEntity.remove();
this.balloonEntity = null;
}
public void despawnBackpack() {
Player player = Bukkit.getPlayer(getUniqueId());
if (invisibleArmorstand == null) return;
invisibleArmorstand.setHealth(0);
invisibleArmorstand.remove();
this.invisibleArmorstand = null;
this.userBalloonManager.remove();
this.userBalloonManager = null;
}
public void respawnBackpack() {
@@ -388,24 +371,6 @@ public class CosmeticUser {
}
}
public void hideBackpack() {
if (hideBackpack == true) return;
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
invisibleArmorstand.getEquipment().clear();
hideBackpack = true;
}
}
public void showBackpack() {
if (hideBackpack == false) return;
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK);
ItemStack item = getUserCosmeticItem(cosmeticBackpackType);
invisibleArmorstand.getEquipment().setHelmet(item);
hideBackpack = false;
}
}
public void hideCosmetics(HiddenReason reason) {
if (hideCosmetics == true) return;
PlayerCosmeticHideEvent event = new PlayerCosmeticHideEvent(this, reason);
@@ -422,7 +387,7 @@ public class CosmeticUser {
PacketManager.sendLeashPacket(getBalloonEntity().getPufferfishBalloonId(), -1, viewer);
}
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
invisibleArmorstand.getEquipment().clear();
userBackpackManager.getArmorstand().getEquipment().clear();
}
updateCosmetic();
MessagesUtil.sendDebugMessages("HideCosmetics");
@@ -448,7 +413,7 @@ public class CosmeticUser {
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK);
ItemStack item = getUserCosmeticItem(cosmeticBackpackType);
invisibleArmorstand.getEquipment().setHelmet(item);
userBackpackManager.getArmorstand().getEquipment().setHelmet(item);
}
updateCosmetic();
MessagesUtil.sendDebugMessages("ShowCosmetics");

View File

@@ -100,7 +100,7 @@ public class Wardrobe {
// Misc
if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getEntityId(), viewer);
PacketManager.ridingMountPacket(NPC_ID, VIEWER.getUserBackpackManager().getFirstArmorstandId(), viewer);
}
if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BALLOON)) {
@@ -236,10 +236,10 @@ public class Wardrobe {
}
if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
PacketManager.sendTeleportPacket(VIEWER.getArmorstandId(), location, false, viewer);
PacketManager.ridingMountPacket(NPC_ID, VIEWER.getBackpackEntity().getEntityId(), viewer);
VIEWER.getBackpackEntity().setRotation(nextyaw, 0);
PacketManager.sendEntityDestroyPacket(VIEWER.getArmorstandId(), outsideViewers);
PacketManager.sendTeleportPacket(VIEWER.getUserBackpackManager().getFirstArmorstandId(), location, false, viewer);
PacketManager.ridingMountPacket(NPC_ID, VIEWER.getUserBackpackManager().getFirstArmorstandId(), viewer);
VIEWER.getUserBackpackManager().getArmorstand().setRotation(nextyaw, 0);
PacketManager.sendEntityDestroyPacket(VIEWER.getUserBackpackManager().getFirstArmorstandId(), outsideViewers);
}
if (VIEWER.hasCosmeticInSlot(CosmeticSlot.BALLOON)) {

View File

@@ -0,0 +1,72 @@
package com.hibiscusmc.hmccosmetics.user.manager;
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType;
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
import org.bukkit.entity.ArmorStand;
import org.bukkit.inventory.ItemStack;
public class UserBackpackManager {
private boolean hideBackpack;
private ArmorStand invisibleArmorstand;
private CosmeticUser user;
private BackpackType backpackType;
public UserBackpackManager(CosmeticUser user) {
this.user = user;
hideBackpack = false;
backpackType = BackpackType.NORMAL;
}
public int getFirstArmorstandId() {
return invisibleArmorstand.getEntityId();
}
public ArmorStand getArmorstand() {
return invisibleArmorstand;
}
public void spawnBackpack(CosmeticBackpackType cosmeticBackpackType) {
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Start");
if (this.invisibleArmorstand != null) return;
this.invisibleArmorstand = (ArmorStand) NMSHandlers.getHandler().spawnBackpack(user, cosmeticBackpackType);
MessagesUtil.sendDebugMessages("spawnBackpack Bukkit - Finish");
}
public void despawnBackpack() {
if (invisibleArmorstand == null) return;
invisibleArmorstand.setHealth(0);
invisibleArmorstand.remove();
this.invisibleArmorstand = null;
}
public void hideBackpack() {
if (user.getHidden() == true) return;
getArmorstand().getEquipment().clear();
hideBackpack = true;
}
public void showBackpack() {
if (hideBackpack == false) return;
CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) user.getCosmetic(CosmeticSlot.BACKPACK);
ItemStack item = user.getUserCosmeticItem(cosmeticBackpackType);
getArmorstand().getEquipment().setHelmet(item);
hideBackpack = false;
}
public void setVisibility(boolean shown) {
hideBackpack = shown;
}
public enum BackpackType {
NORMAL,
FIRST_PERSON // First person not yet implemented
}
}

View File

@@ -1,4 +1,4 @@
package com.hibiscusmc.hmccosmetics.entities;
package com.hibiscusmc.hmccosmetics.user.manager;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import com.hibiscusmc.hmccosmetics.config.Settings;
@@ -19,14 +19,14 @@ import org.bukkit.util.Vector;
import java.util.UUID;
import java.util.logging.Level;
public class BalloonEntity {
public class UserBalloonManager {
private BalloonType balloonType;
private final int balloonID;
private final UUID uniqueID;
private final ArmorStand modelEntity;
public BalloonEntity(Location location) {
public UserBalloonManager(Location location) {
this.uniqueID = UUID.randomUUID();
this.balloonID = NMSHandlers.getHandler().getNextEntityId();
this.modelEntity = NMSHandlers.getHandler().getMEGEntity(location.add(Settings.getBalloonOffset()));