mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-27 10:59:14 +00:00
Improved wardrobe viewing
This commit is contained in:
@@ -17,11 +17,10 @@ import io.github.fisher2911.hmccosmetics.hook.item.ItemsAdderHook;
|
||||
import io.github.fisher2911.hmccosmetics.listener.ClickListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.CosmeticFixListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.JoinListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.MoveListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.PlayerShiftListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.RespawnListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.TeleportListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.WardrobeClickListener;
|
||||
import io.github.fisher2911.hmccosmetics.listener.WardrobeListener;
|
||||
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
|
||||
import io.github.fisher2911.hmccosmetics.message.Messages;
|
||||
import io.github.fisher2911.hmccosmetics.message.Translation;
|
||||
@@ -127,8 +126,7 @@ public class HMCCosmetics extends JavaPlugin {
|
||||
new RespawnListener(this),
|
||||
new CosmeticFixListener(this),
|
||||
new PlayerShiftListener(this),
|
||||
new MoveListener(this),
|
||||
new WardrobeClickListener(this)
|
||||
new WardrobeListener(this)
|
||||
).
|
||||
forEach(
|
||||
listener -> this.getServer().getPluginManager()
|
||||
|
||||
@@ -63,6 +63,7 @@ public class Database {
|
||||
final UUID uuid = entity.getUniqueId();
|
||||
final int armorStandId = getNextEntityId();
|
||||
final int balloonId = getNextEntityId();
|
||||
final int wardrobeViewerId = getNextEntityId();
|
||||
final Wardrobe wardrobe = this.createNewWardrobe(uuid);
|
||||
Threads.getInstance().execute(
|
||||
() -> {
|
||||
@@ -80,7 +81,8 @@ public class Database {
|
||||
new EntityIds(
|
||||
entity.getEntityId(),
|
||||
armorStandId,
|
||||
balloonId
|
||||
balloonId,
|
||||
wardrobeViewerId
|
||||
),
|
||||
armorItems,
|
||||
wardrobe
|
||||
@@ -97,13 +99,14 @@ public class Database {
|
||||
uuid,
|
||||
PlayerArmor.empty(),
|
||||
wardrobe,
|
||||
new EntityIds(entity.getEntityId(), armorStandId, balloonId)
|
||||
new EntityIds(entity.getEntityId(), armorStandId, balloonId, wardrobeViewerId)
|
||||
));
|
||||
}
|
||||
|
||||
public void loadNPCUser(final int id, final Entity entity, final Consumer<NPCUser> onComplete) {
|
||||
final int armorStandId = getNextEntityId();
|
||||
final int balloonId = getNextEntityId();
|
||||
final int wardrobeViewerId = getNextEntityId();
|
||||
Threads.getInstance().execute(
|
||||
() -> {
|
||||
try {
|
||||
@@ -120,7 +123,8 @@ public class Database {
|
||||
new EntityIds(
|
||||
entity.getEntityId(),
|
||||
armorStandId,
|
||||
balloonId
|
||||
balloonId,
|
||||
wardrobeViewerId
|
||||
),
|
||||
armorItems
|
||||
);
|
||||
@@ -137,7 +141,7 @@ public class Database {
|
||||
onComplete.accept(new NPCUser(
|
||||
id,
|
||||
PlayerArmor.empty(),
|
||||
new EntityIds(entity.getEntityId(), armorStandId, balloonId)
|
||||
new EntityIds(entity.getEntityId(), armorStandId, balloonId, wardrobeViewerId)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -213,6 +217,7 @@ public class Database {
|
||||
ownerUUID,
|
||||
PlayerArmor.empty(),
|
||||
new EntityIds(
|
||||
getNextEntityId(),
|
||||
getNextEntityId(),
|
||||
getNextEntityId(),
|
||||
getNextEntityId()
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
|
||||
import io.github.fisher2911.hmccosmetics.user.EntityIds;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -19,8 +20,6 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class DatabaseConverter {
|
||||
|
||||
private static final int CURRENT_VERSION = 2;
|
||||
@@ -94,6 +93,7 @@ public class DatabaseConverter {
|
||||
new EntityIds(
|
||||
-1,
|
||||
Database.getNextEntityId(),
|
||||
Database.getNextEntityId(),
|
||||
Database.getNextEntityId()
|
||||
)
|
||||
);
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
@@ -99,6 +100,13 @@ public class CosmeticFixListener implements Listener {
|
||||
this.registerInventoryClickListener();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onHoldItem(final PlayerItemHeldEvent event) {
|
||||
this.userManager.get(event.getPlayer().getUniqueId()).ifPresent(user -> {
|
||||
if (user.isWardrobeActive()) event.setCancelled(true);
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onShiftClick(final InventoryClickEvent event) {
|
||||
if (event.getClick() != ClickType.SHIFT_LEFT && event.getClick() != ClickType.SHIFT_RIGHT) return;
|
||||
@@ -143,17 +151,6 @@ public class CosmeticFixListener implements Listener {
|
||||
final WrapperPlayClientClickWindow.WindowClickType clickType = packet.getWindowClickType();
|
||||
EquipmentSlot slot = getPacketArmorSlot(slotClicked);
|
||||
if (slot == null) {
|
||||
// final Equipment equipment = ;
|
||||
// final var itemList = ;
|
||||
// player.sendMessage("Sending delayed task");
|
||||
// taskManager.submit(new DelayedTask(() -> {
|
||||
// userManager.sendUpdatePacket(
|
||||
// player.getEntityId(),
|
||||
// player,
|
||||
// getItemList(user, Equipment.fromEntityEquipment(player.getEquipment()), Collections.emptySet())
|
||||
// );
|
||||
// player.sendMessage("In delayed task");
|
||||
// }, 40));
|
||||
return;
|
||||
}
|
||||
final EntityEquipment entityEquipment = player.getEquipment();
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package io.github.fisher2911.hmccosmetics.listener;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import io.github.fisher2911.hmccosmetics.user.UserManager;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MoveListener implements Listener {
|
||||
|
||||
private final HMCCosmetics plugin;
|
||||
private final UserManager userManager;
|
||||
|
||||
public MoveListener(final HMCCosmetics plugin) {
|
||||
this.plugin = plugin;
|
||||
this.userManager = this.plugin.getUserManager();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onMove(final PlayerMoveEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
final Optional<User> optional = this.userManager.get(player.getUniqueId());
|
||||
if (optional.isEmpty()) return;
|
||||
if (optional.get().getWardrobe().isCameraLocked()) event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
@@ -6,16 +6,15 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class WardrobeClickListener implements Listener {
|
||||
public class WardrobeListener implements Listener {
|
||||
|
||||
private final HMCCosmetics plugin;
|
||||
private final UserManager userManager;
|
||||
|
||||
public WardrobeClickListener(final HMCCosmetics plugin) {
|
||||
public WardrobeListener(final HMCCosmetics plugin) {
|
||||
this.plugin = plugin;
|
||||
this.userManager = this.plugin.getUserManager();
|
||||
}
|
||||
@@ -31,4 +30,12 @@ public class WardrobeClickListener implements Listener {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(final EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof final Player player)) return;
|
||||
this.userManager.get(player.getUniqueId()).ifPresent(user -> {
|
||||
if (user.isWardrobeActive()) event.setCancelled(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -299,6 +299,16 @@ public class PacketManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCameraPacket(final int entityId, final Player... sendTo) {
|
||||
for (final Player p : sendTo) {
|
||||
PacketEvents.getAPI().getPlayerManager().sendPacketAsync(p, new WrapperPlayServerCamera(entityId));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCameraPacket(final int entityId, final Collection<? extends Player> sendTo) {
|
||||
sendCameraPacket(entityId, sendTo.toArray(new Player[0]));
|
||||
}
|
||||
|
||||
// public static void sendSoundPacket(
|
||||
// final Player player,
|
||||
// final Location location,
|
||||
@@ -451,10 +461,6 @@ public class PacketManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendSpectatePacket(final int entityId) {
|
||||
|
||||
}
|
||||
|
||||
public static com.github.retrooper.packetevents.protocol.player.Equipment getEquipment(
|
||||
final ItemStack itemStack,
|
||||
final org.bukkit.inventory.EquipmentSlot slot
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.github.fisher2911.hmccosmetics.packet;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
|
||||
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||
|
||||
public class WrapperPlayServerCamera extends PacketWrapper<WrapperPlayServerCamera> {
|
||||
|
||||
private int entityID;
|
||||
|
||||
public WrapperPlayServerCamera(final int entityID) {
|
||||
super(PacketType.Play.Server.CAMERA);
|
||||
this.entityID = entityID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read() {
|
||||
this.entityID = this.readVarInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy(WrapperPlayServerCamera wrapper) {
|
||||
this.entityID = wrapper.entityID;
|
||||
}
|
||||
|
||||
public void write() {
|
||||
this.writeVarInt(this.entityID);
|
||||
}
|
||||
|
||||
public int getEntityId() {
|
||||
return this.entityID;
|
||||
}
|
||||
|
||||
public void setEntityIds(int entityID) {
|
||||
this.entityID = entityID;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,31 +11,37 @@ public class EntityIds {
|
||||
this.ids = ids;
|
||||
}
|
||||
|
||||
public EntityIds(final int self, final int armorStand, final int balloon) {
|
||||
public EntityIds(final int self, final int armorStand, final int balloon, final int wardrobeViewer) {
|
||||
final Map<Type, Integer> ids = new EnumMap<>(Type.class);
|
||||
ids.put(Type.SELF, self);
|
||||
ids.put(Type.ARMOR_STAND, armorStand);
|
||||
ids.put(Type.BALLOON, balloon);
|
||||
ids.put(Type.WARDROBE_VIEWER, wardrobeViewer);
|
||||
this.ids = ids;
|
||||
}
|
||||
|
||||
public int self() {
|
||||
return ids.getOrDefault(Type.SELF, -1);
|
||||
return this.ids.getOrDefault(Type.SELF, -1);
|
||||
}
|
||||
|
||||
public int armorStand() {
|
||||
return ids.getOrDefault(Type.ARMOR_STAND, -1);
|
||||
return this.ids.getOrDefault(Type.ARMOR_STAND, -1);
|
||||
}
|
||||
|
||||
public int balloon() {
|
||||
return ids.getOrDefault(Type.BALLOON, -1);
|
||||
return this.ids.getOrDefault(Type.BALLOON, -1);
|
||||
}
|
||||
|
||||
public int wardrobeViewer() {
|
||||
return this.ids.getOrDefault(Type.WARDROBE_VIEWER, -1);
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
|
||||
SELF,
|
||||
ARMOR_STAND,
|
||||
BALLOON
|
||||
BALLOON,
|
||||
WARDROBE_VIEWER
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ public class User extends BaseUser<UUID> {
|
||||
final Player player = this.getPlayer();
|
||||
if (player == null || player.isDead()) return false;
|
||||
if (player.getUniqueId().equals(other.getUniqueId()) && this.hidden) return false;
|
||||
if (this.isWardrobeActive()) return false;
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (itemStack != null && itemStack.getType() == Material.TRIDENT) {
|
||||
final ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
|
||||
@@ -25,8 +25,9 @@ public class UserFactory {
|
||||
final Class<T> type,
|
||||
final Entity entity,
|
||||
final int armorStandId,
|
||||
final int balloonId
|
||||
) {
|
||||
final int balloonId,
|
||||
final int wardrobeViewerId
|
||||
) {
|
||||
final UUID uuid = entity.getUniqueId();
|
||||
final int entityId = entity.getEntityId();
|
||||
if (type.equals(User.class)) {
|
||||
@@ -37,7 +38,8 @@ public class UserFactory {
|
||||
new EntityIds(
|
||||
entityId,
|
||||
armorStandId,
|
||||
balloonId
|
||||
balloonId,
|
||||
wardrobeViewerId
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -51,7 +53,8 @@ public class UserFactory {
|
||||
new EntityIds(
|
||||
entityId,
|
||||
armorStandId,
|
||||
balloonId
|
||||
balloonId,
|
||||
wardrobeViewerId
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -175,6 +175,14 @@ public class UserManager {
|
||||
return items;
|
||||
}
|
||||
|
||||
public List<com.github.retrooper.packetevents.protocol.player.Equipment> getEmptyItemList() {
|
||||
final List<com.github.retrooper.packetevents.protocol.player.Equipment> items = new ArrayList<>();
|
||||
for (final EquipmentSlot slot : EquipmentSlot.values()) {
|
||||
items.add(PacketManager.getEquipment(new ItemStack(Material.AIR), slot));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItem(final BaseUser<?> user, final ArmorItem armorItem, final boolean sendPacket) {
|
||||
ArmorItem previous = user.getPlayerArmor().getItem(armorItem.getType());
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.fisher2911.hmccosmetics.user;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.config.WardrobeSettings;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
@@ -25,10 +26,7 @@ public class Wardrobe extends User {
|
||||
private final HMCCosmetics plugin;
|
||||
private final UUID ownerUUID;
|
||||
private boolean active;
|
||||
private boolean cameraLocked;
|
||||
|
||||
private boolean spawned;
|
||||
|
||||
private Location currentLocation;
|
||||
|
||||
public Wardrobe(
|
||||
@@ -46,38 +44,53 @@ public class Wardrobe extends User {
|
||||
}
|
||||
|
||||
public void spawnFakePlayer(final Player viewer) {
|
||||
final UserManager userManager = this.plugin.getUserManager();
|
||||
final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings();
|
||||
if (settings.inDistanceOfStatic(viewer.getLocation())) {
|
||||
this.currentLocation = settings.getWardrobeLocation();
|
||||
new TaskChain(this.plugin).chain(
|
||||
() -> {
|
||||
viewer.teleport(settings.getViewerLocation());
|
||||
this.cameraLocked = true;
|
||||
this.hidePlayer();
|
||||
}
|
||||
).execute();
|
||||
|
||||
} else if (this.currentLocation == null) {
|
||||
this.currentLocation = viewer.getLocation().clone();
|
||||
this.currentLocation.setPitch(0);
|
||||
this.currentLocation.setYaw(0);
|
||||
} else if (this.spawned) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setActive(true);
|
||||
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(
|
||||
this.plugin,
|
||||
() -> {
|
||||
if (settings.inDistanceOfStatic(viewer.getLocation())) {
|
||||
this.currentLocation = settings.getWardrobeLocation();
|
||||
userManager.get(viewer.getUniqueId()).ifPresent(user -> userManager.sendUpdatePacket(user, userManager.getEmptyItemList()));
|
||||
PacketManager.sendEntitySpawnPacket(
|
||||
settings.getViewerLocation(),
|
||||
this.entityIds.wardrobeViewer(),
|
||||
EntityTypes.ARMOR_STAND,
|
||||
viewer
|
||||
);
|
||||
PacketManager.sendCameraPacket(
|
||||
this.entityIds.wardrobeViewer(),
|
||||
viewer
|
||||
);
|
||||
PacketManager.sendInvisibilityPacket(
|
||||
this.entityIds.wardrobeViewer(),
|
||||
viewer
|
||||
);
|
||||
PacketManager.sendLookPacket(
|
||||
this.entityIds.wardrobeViewer(),
|
||||
settings.getViewerLocation(),
|
||||
viewer
|
||||
);
|
||||
PacketManager.sendInvisibilityPacket(
|
||||
viewer.getEntityId(),
|
||||
viewer
|
||||
);
|
||||
this.hidePlayer();
|
||||
this.setActive(true);
|
||||
} else if (this.currentLocation == null) {
|
||||
this.currentLocation = viewer.getLocation().clone();
|
||||
this.currentLocation.setPitch(0);
|
||||
this.currentLocation.setYaw(0);
|
||||
} else if (this.spawned) {
|
||||
return;
|
||||
}
|
||||
final int entityId = this.getEntityId();
|
||||
PacketManager.sendFakePlayerInfoPacket(viewer, this.getId(), viewer);
|
||||
PacketManager.sendFakePlayerSpawnPacket(this.currentLocation, this.getId(), entityId, viewer);
|
||||
// this.updateOutsideCosmetics(viewer, this.currentLocation, plugin.getSettings());
|
||||
PacketManager.sendLookPacket(entityId, this.currentLocation, viewer);
|
||||
PacketManager.sendRotationPacket(entityId, this.currentLocation, true, viewer);
|
||||
PacketManager.sendPlayerOverlayPacket(entityId, viewer);
|
||||
final UserManager userManager = this.plugin.getUserManager();
|
||||
userManager.get(viewer.getUniqueId()).
|
||||
ifPresent(user -> {
|
||||
int index = 0;
|
||||
@@ -112,9 +125,15 @@ public class Wardrobe extends User {
|
||||
this.despawnBalloon();
|
||||
PacketManager.sendEntityDestroyPacket(entityId, viewer);
|
||||
PacketManager.sendRemovePlayerPacket(viewer, this.id, viewer);
|
||||
PacketManager.sendEntityDestroyPacket(
|
||||
this.entityIds.wardrobeViewer(),
|
||||
viewer
|
||||
);
|
||||
PacketManager.sendCameraPacket(
|
||||
viewer.getEntityId(),
|
||||
viewer
|
||||
);
|
||||
this.showPlayer(this.plugin.getUserManager());
|
||||
this.cameraLocked = false;
|
||||
this.currentLocation = null;
|
||||
final Collection<ArmorItem> armorItems = new ArrayList<>(this.getPlayerArmor().getArmorItems());
|
||||
if (settings.isApplyCosmeticsOnClose()) {
|
||||
final Optional<User> optionalUser = userManager.get(this.ownerUUID);
|
||||
@@ -132,16 +151,22 @@ public class Wardrobe extends User {
|
||||
));
|
||||
}
|
||||
this.getPlayerArmor().clear();
|
||||
Bukkit.getScheduler().runTask(this.plugin, () -> {
|
||||
if (viewer == null || !viewer.isOnline()) return;
|
||||
viewer.teleport(settings.getLeaveLocation());
|
||||
});
|
||||
|
||||
if (settings.isAlwaysDisplay()) {
|
||||
this.currentLocation = settings.getWardrobeLocation();
|
||||
if (this.currentLocation == null) return;
|
||||
this.spawnFakePlayer(viewer);
|
||||
}
|
||||
new TaskChain(this.plugin).chain(
|
||||
() -> {
|
||||
if (!viewer.isOnline()) return;
|
||||
if (!this.currentLocation.equals(settings.getWardrobeLocation())) return;
|
||||
this.currentLocation = null;
|
||||
if (settings.isAlwaysDisplay()) {
|
||||
this.currentLocation = settings.getWardrobeLocation();
|
||||
if (this.currentLocation == null) return;
|
||||
this.spawnFakePlayer(viewer);
|
||||
}
|
||||
},
|
||||
true
|
||||
).chain(
|
||||
() -> viewer.teleport(settings.getLeaveLocation())
|
||||
).
|
||||
execute();
|
||||
},
|
||||
settings.getDespawnDelay()
|
||||
);
|
||||
@@ -173,10 +198,6 @@ public class Wardrobe extends User {
|
||||
return current + rotationSpeed;
|
||||
}
|
||||
|
||||
public boolean isCameraLocked() {
|
||||
return this.active && this.cameraLocked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermissionToUse(final ArmorItem armorItem) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user