From 1b9e2ac531a82b8da906bcae19775fd0d444af86 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Mon, 29 Aug 2022 18:39:34 -0500 Subject: [PATCH] Wardrobe Works! --- .../hmccosmetics/packet/PacketManager.java | 43 +++++++++---------- .../hmccosmetics/user/Wardrobe.java | 29 +++++++++---- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java index ecc1388d..40cc74d3 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java @@ -172,9 +172,6 @@ public class PacketManager { write(0, location.getX()). write(1, location.getY()). write(2, location.getZ()); - //Bukkit.getLogger().info("Yaw is : " + (location.getYaw())); - packet.getBytes().write(0, (byte) (location.getYaw())); - packet.getBytes().write(1, (byte) (location.getPitch())); sendPacketAsync(p, packet); } } @@ -221,13 +218,6 @@ public class PacketManager { WrappedDataWatcher wrapper = new WrappedDataWatcher(); wrapper.setObject(new WrappedDataWatcher.WrappedDataWatcherObject(0, WrappedDataWatcher.Registry.get(Byte.class)), (byte) 0x20); packet.getWatchableCollectionModifier().write(0, wrapper.getWatchableObjects()); - //packet.setMeta(String.valueOf(entityId), List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20))); - /* - sendPacketAsync(p, new WrapperPlayServerEntityMetadata( - entityId, - List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20)) - )); - */ sendPacketAsync(p, packet); } } @@ -415,7 +405,7 @@ public class PacketManager { final Player... sendTo ) { for (final Player p : sendTo) { - PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_LOOK); + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION); packet.getIntegers().write(0, entityId); packet.getBytes().write(0, (byte) (location.getYaw() * 256.0F / 360.0F)); sendPacketAsync(p, packet); @@ -699,17 +689,29 @@ public class PacketManager { /** * Sends a gamemode change packet to a player. - * @param player - * @param gamemode + * @param player Player to change their gamemode. + * @param gamemode Bukkit gamemode to change it to */ public static void sendGameModeChange( final Player player, final GameMode gamemode ) { + sendGameModeChange(player, convertGamemode(gamemode)); + } + + /** + * Sends a gamemode change packet to a player. + * @param player Player to change their gamemode. + * @param gamemode Gamemode value to change it to + */ + public static void sendGameModeChange( + final Player player, + final int gamemode + ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.GAME_STATE_CHANGE); - packet.getGameStateIDs().write(0, convertGamemode(gamemode)); - // What doest this even do? - packet.getFloat().write(0, 3f); + packet.getGameStateIDs().write(0, 3); + // Tells what event this is. This is a change gamemode event. + packet.getFloat().write(0, (float) gamemode); sendPacketAsync(player, packet); } @@ -719,16 +721,15 @@ public class PacketManager { * @param packet What packet to send to the player */ public static void sendPacketAsync(final Player player, final PacketContainer packet) { - Bukkit.getScheduler().runTaskAsynchronously(HMCCosmetics.getPlugin(HMCCosmetics.class), () -> { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - }); + Bukkit.getScheduler().runTaskAsynchronously(HMCCosmetics.getPlugin(HMCCosmetics.class), + () -> ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet)); } /** * Gets the equipment of a slot * @param itemStack the ItemStack of a slot * @param slot the slot to look at - * @return + * @return returns the equipment at a slot */ // It works now, need to redo this sytem sometime in the future... public static Equipment getEquipment( @@ -765,7 +766,6 @@ public class PacketManager { case FEET -> EnumWrappers.ItemSlot.FEET; case HAND -> EnumWrappers.ItemSlot.MAINHAND; case OFF_HAND -> EnumWrappers.ItemSlot.OFFHAND; - default -> null; }; } @@ -780,7 +780,6 @@ public class PacketManager { case CREATIVE -> 1; case ADVENTURE -> 2; case SPECTATOR -> 3; - default -> 0; // Just default to survival if it can't find anything }; } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java index 0dc5b567..4b101da5 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Wardrobe.java @@ -9,6 +9,7 @@ import io.github.fisher2911.hmccosmetics.task.SupplierTask; import io.github.fisher2911.hmccosmetics.task.Task; import io.github.fisher2911.hmccosmetics.task.TaskChain; import org.bukkit.Bukkit; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; @@ -31,6 +32,7 @@ public class Wardrobe extends User { private boolean active; private boolean spawned; private Location currentLocation; + private GameMode originalGamemode; public Wardrobe( final HMCCosmetics plugin, @@ -45,11 +47,21 @@ public class Wardrobe extends User { this.ownerUUID = ownerUUID; this.active = active; this.wardrobe = this; + this.originalGamemode = GameMode.SURVIVAL; } public void spawnFakePlayer(final Player viewer) { final UserManager userManager = this.plugin.getUserManager(); final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings(); + this.originalGamemode = viewer.getGameMode(); + Bukkit.getScheduler().runTask(this.plugin, () -> { + PacketManager.sendEntitySpawnPacket( + settings.getViewerLocation(), + this.entityIds.wardrobeViewer(), + EntityType.ARMOR_STAND, + viewer + ); + }); Bukkit.getScheduler().runTaskLaterAsynchronously( this.plugin, @@ -61,12 +73,6 @@ public class Wardrobe extends User { user.despawnAttached(); user.despawnBalloon(); }); - PacketManager.sendEntitySpawnPacket( - settings.getViewerLocation(), - this.entityIds.wardrobeViewer(), - EntityType.ARMOR_STAND, - viewer - ); PacketManager.sendCameraPacket( this.entityIds.wardrobeViewer(), viewer @@ -86,6 +92,10 @@ public class Wardrobe extends User { viewer.getEntityId(), viewer ); + PacketManager.sendGameModeChange( + viewer, + GameMode.SPECTATOR + ); this.hidePlayer(); this.setActive(true); } else if (this.currentLocation == null) { @@ -142,6 +152,9 @@ public class Wardrobe extends User { viewer ); this.showPlayer(this.plugin.getUserManager()); + PacketManager.sendGameModeChange( + viewer, originalGamemode + ); final Collection armorItems = new ArrayList<>(this.getPlayerArmor().getArmorItems()); if (settings.isApplyCosmeticsOnClose()) { final Optional optionalUser = userManager.get(this.ownerUUID); @@ -176,8 +189,8 @@ public class Wardrobe extends User { ).chain(() -> { this.despawnAttached(); this.despawnBalloon(); - }, true). - execute(); + }, true) + .execute(); }, settings.getDespawnDelay() );