mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-27 10:59:14 +00:00
Cosmetic Fixes
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
package io.github.fisher2911.hmccosmetics.listener;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.user.Equipment;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import io.github.fisher2911.hmccosmetics.user.UserManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class ClickListener implements Listener {
|
||||
|
||||
@@ -56,15 +60,18 @@ public class ClickListener implements Listener {
|
||||
this.plugin = plugin;
|
||||
this.userManager = this.plugin.getUserManager();
|
||||
}
|
||||
/*
|
||||
|
||||
// @EventHandler
|
||||
// public void onArmorSlotClick(final InventoryClickEvent event) {
|
||||
// final int slot = event.getSlot();
|
||||
// if (!(event.getWhoClicked() instanceof final Player player)) return;
|
||||
// if (slot >= 36 && slot <= 40) {
|
||||
// this.fixInventory(player);
|
||||
// }
|
||||
// }
|
||||
@EventHandler
|
||||
public void onArmorSlotClick(final InventoryClickEvent event) {
|
||||
final int slot = event.getSlot();
|
||||
if (!(event.getWhoClicked() instanceof Player)) return;
|
||||
if (slot >= 36 && slot <= 40) {
|
||||
this.fixInventory(((Player) event.getWhoClicked()).getPlayer());
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
//
|
||||
// @EventHandler
|
||||
// public void onBlockClick(final PlayerInteractEvent event) {
|
||||
@@ -102,22 +109,25 @@ public class ClickListener implements Listener {
|
||||
// this.userManager.get(player.getUniqueId()).ifPresent(this::doRunnable);
|
||||
// }
|
||||
//
|
||||
// private void fixInventory(final Player player) {
|
||||
// final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
|
||||
//
|
||||
// if (optionalUser.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// this.doRunnable(optionalUser.get());
|
||||
// }
|
||||
//
|
||||
// private void doRunnable(final User user) {
|
||||
// Bukkit.getScheduler().runTaskLaterAsynchronously(
|
||||
// this.plugin, () -> this.userManager.updateCosmetics(user),
|
||||
// 1);
|
||||
// }
|
||||
//
|
||||
/*
|
||||
private void fixInventory(final Player player) {
|
||||
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.doRunnable(optionalUser.get());
|
||||
}
|
||||
|
||||
private void doRunnable(final User user) {
|
||||
Equipment equip = this.userManager.getItemList(user, user.getEquipment(), Collections.emptySet());
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(
|
||||
this.plugin, () -> this.userManager.sendUpdatePacket(user, equip),
|
||||
1);
|
||||
}
|
||||
*/
|
||||
|
||||
// private void checkFix(final Player player, final int clickedSlot, final ItemStack itemStack) {
|
||||
// final EquipmentSlot slot = this.getArmorSlot(itemStack.getType());
|
||||
// if (slot == null) return;
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class CosmeticFixListener implements Listener {
|
||||
|
||||
@@ -162,23 +163,24 @@ public class CosmeticFixListener implements Listener {
|
||||
current
|
||||
);
|
||||
});
|
||||
|
||||
final WrapperPlayClientWindowClick packet = new WrapperPlayClientWindowClick();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateOnClick(final Player player, final EquipmentSlot slot, final User user, final ArmorItem.Type type, final ItemStack current) {
|
||||
//plugin.getLogger().log(Level.INFO, "updateOnClick (171)");
|
||||
|
||||
final Location location = player.getLocation();
|
||||
final Equipment equipment = Equipment.fromEntityEquipment(player.getEquipment());
|
||||
final Equipment equipment = userManager.getItemList(user);
|
||||
final ItemStack cosmetic = userManager.getCosmeticItem(
|
||||
user.getPlayerArmor().getItem(type),
|
||||
current,
|
||||
ArmorItem.Status.APPLIED,
|
||||
slot
|
||||
);
|
||||
//plugin.getLogger().log(Level.INFO, "Set cosmetic in " + slot + " to " + cosmetic);
|
||||
if (cosmetic != null && cosmetic.getType() != Material.AIR) equipment.setItem(slot, cosmetic);
|
||||
//plugin.getLogger().log(Level.INFO, "Set cosmetic in " + slot + " to " + cosmetic + "(done)");
|
||||
|
||||
final Equipment items =
|
||||
userManager.getItemList(user, equipment, Collections.emptySet());
|
||||
@@ -187,17 +189,20 @@ public class CosmeticFixListener implements Listener {
|
||||
final ArmorItem.Type t = ArmorItem.Type.fromWrapper(e);
|
||||
ItemStack air = new ItemStack(Material.AIR);
|
||||
if (t == null) {
|
||||
//plugin.getLogger().log(Level.INFO, "T is null");
|
||||
equipment.setItem(e, air);
|
||||
return;
|
||||
}
|
||||
final ArmorItem armorItem = user.getPlayerArmor().getItem(t);
|
||||
final ItemStack i = equipment.getItem(e);
|
||||
if (i == null) {
|
||||
//plugin.getLogger().log(Level.INFO, "I is null");
|
||||
equipment.setItem(e, air);
|
||||
return;
|
||||
}
|
||||
Boolean remove = armorItem.isEmpty() && i.equals(equipment.getItem(t.getSlot()));
|
||||
if (remove) {
|
||||
//plugin.getLogger().log(Level.INFO, "Boolean is true");
|
||||
equipment.setItem(e, air);
|
||||
return;
|
||||
}
|
||||
@@ -231,8 +236,8 @@ public class CosmeticFixListener implements Listener {
|
||||
if (windowId != 0) return;
|
||||
final int size = items.size();
|
||||
final PlayerArmor playerArmor = user.getPlayerArmor();
|
||||
final List<Equipment> equipmentList = new ArrayList<>();
|
||||
final Equipment equip = user.getEquipment();
|
||||
//final List<Equipment> equipmentList = new ArrayList<>();
|
||||
final Equipment equip = userManager.getItemList(user);
|
||||
for (final ArmorItem armorItem : playerArmor.getArmorItems()) {
|
||||
final ArmorItem.Type type = armorItem.getType();
|
||||
final EquipmentSlot slot = type.getSlot();
|
||||
@@ -250,72 +255,17 @@ public class CosmeticFixListener implements Listener {
|
||||
slot
|
||||
));
|
||||
if ((current).equals(setTo)) continue;
|
||||
equipmentList.add(PacketManager.getEquipment(setTo, slot));
|
||||
equip.setItem(slot, setTo);
|
||||
//plugin.getLogger().log(Level.INFO, "Setto " + setTo);
|
||||
//equipmentList.add(PacketManager.getEquipment(setTo, slot));
|
||||
}
|
||||
userManager.sendUpdatePacket(
|
||||
user,
|
||||
equip
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
//final WrapperPlayServerWindowItems packet = new WrapperPlayServerWindowItems(event);
|
||||
|
||||
|
||||
|
||||
//final int windowId = packet.getWindowId();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
PacketEvents.getAPI().getEventManager().registerListener(
|
||||
new PacketListenerAbstract() {
|
||||
@Override
|
||||
public void onPacketSend(PacketSendEvent event) {
|
||||
if (event.getPacketType() != PacketType.Play.Server.WINDOW_ITEMS) return;
|
||||
final WrapperPlayServerWindowItems packet = new WrapperPlayServerWindowItems(event);
|
||||
if (!(event.getPlayer() instanceof final Player player)) return;
|
||||
final int windowId = packet.getWindowId();
|
||||
final List<ItemStack> itemStacks = packet.getItems();
|
||||
taskManager.submit(() -> {
|
||||
final Optional<User> optionalUser = userManager.get(player.getUniqueId());
|
||||
if (optionalUser.isEmpty()) return;
|
||||
final User user = optionalUser.get();
|
||||
if (windowId != 0) return;
|
||||
final int size = itemStacks.size();
|
||||
final PlayerArmor playerArmor = user.getPlayerArmor();
|
||||
final List<Equipment> equipmentList = new ArrayList<>();
|
||||
for (final ArmorItem armorItem : playerArmor.getArmorItems()) {
|
||||
final ArmorItem.Type type = armorItem.getType();
|
||||
final EquipmentSlot slot = type.getSlot();
|
||||
if (slot == null) continue;
|
||||
final int packetSlot = getPacketArmorSlot(slot);
|
||||
if (packetSlot == -1) continue;
|
||||
if (packetSlot >= size) continue;
|
||||
|
||||
final ItemStack current = (itemStacks.get(packetSlot));
|
||||
final ItemStack setTo =
|
||||
(userManager.getCosmeticItem(
|
||||
armorItem,
|
||||
current,
|
||||
ArmorItem.Status.APPLIED,
|
||||
slot
|
||||
));
|
||||
if ((current).equals(setTo)) continue;
|
||||
equipmentList.add(PacketManager.getEquipment(setTo, slot));
|
||||
}
|
||||
userManager.sendUpdatePacket(
|
||||
user,
|
||||
equipmentList
|
||||
);
|
||||
});
|
||||
packet.setItems(itemStacks);
|
||||
}
|
||||
}
|
||||
);
|
||||
*/
|
||||
}
|
||||
|
||||
private int getPacketArmorSlot(final EquipmentSlot slot) {
|
||||
@@ -414,10 +364,11 @@ public class CosmeticFixListener implements Listener {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
private void fixCosmetics(final Player player) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(this.plugin,
|
||||
() -> this.userManager.updateCosmetics(player.getUniqueId()), 2);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ public class PacketManager {
|
||||
write(1, location.getY()).
|
||||
write(2, location.getZ());
|
||||
packet.getIntegers().write(1, 1);
|
||||
p.sendMessage("Packet sent");
|
||||
//p.sendMessage("Packet sent");
|
||||
//packet.getIntegers().write(2, 0);
|
||||
//packet.getIntegers().write(3, 0);
|
||||
//packet.getIntegers().write(4, 0);
|
||||
@@ -313,9 +313,10 @@ public class PacketManager {
|
||||
for (EquipmentSlot slot : equipment.keys()) {
|
||||
if (itemBukkitSlot(slot) != null) list.add(new Pair<>(itemBukkitSlot(slot), equipment.getItem(slot)));
|
||||
}
|
||||
if (list == null) return;
|
||||
packet.getSlotStackPairLists().write(0, list);
|
||||
|
||||
for (Player p : sendTo) {
|
||||
//p.sendMessage("Offhand Cosmetic: " + equipment.getItem(EquipmentSlot.OFF_HAND));
|
||||
sendPacketAsync(p, packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.fisher2911.hmccosmetics.user;
|
||||
|
||||
import com.comphenix.protocol.wrappers.EnumWrappers;
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.api.CosmeticItem;
|
||||
import io.github.fisher2911.hmccosmetics.api.event.CosmeticChangeEvent;
|
||||
@@ -105,14 +106,14 @@ public class UserManager {
|
||||
this.sendUpdatePacket(
|
||||
user,
|
||||
other,
|
||||
user.getEquipment()
|
||||
getItemList(user)
|
||||
);
|
||||
}
|
||||
|
||||
public void updateCosmetics(final BaseUser<?> user) {
|
||||
this.sendUpdatePacket(
|
||||
user,
|
||||
user.getEquipment()
|
||||
getItemList(user)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -144,6 +145,12 @@ public class UserManager {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public Equipment getItemList(
|
||||
final BaseUser<?> user
|
||||
) {
|
||||
return getItemList(user, new Equipment(), Collections.emptySet());
|
||||
}
|
||||
|
||||
public Equipment getItemList(
|
||||
final BaseUser<?> user,
|
||||
final Equipment equipment,
|
||||
@@ -287,35 +294,36 @@ public class UserManager {
|
||||
final ItemStack itemStack = this.getCosmeticItem(armorItem, wearing, ArmorItem.Status.APPLIED, slot);
|
||||
//final List<Equipment> itemList = new ArrayList<>();
|
||||
//itemList.add(PacketManager.getEquipment(itemStack, slot));
|
||||
Equipment equip = Equipment.fromEntityEquipment(user);
|
||||
Equipment equip = user.getEquipment();
|
||||
equip.setItem(slot, itemStack);
|
||||
this.sendUpdatePacket(user, equip);
|
||||
}
|
||||
|
||||
public void sendUpdatePacket(
|
||||
final BaseUser<?> user,
|
||||
Equipment items
|
||||
Equipment equipment
|
||||
) {
|
||||
// final Player player = user.getPlayer();
|
||||
// if (player == null) return;
|
||||
final Location location = user.getLocation();
|
||||
if (location == null) return;
|
||||
final int entityId = user.getEntityId();
|
||||
if (items == null) return;
|
||||
if (equipment == null) return;
|
||||
for (final User otherUser : this.userMap.values()) {
|
||||
final Player other = otherUser.getPlayer();
|
||||
//other.sendMessage("Offhand Cosmetic (sendUpdatePacket) " + equipment.getItem(EquipmentSlot.OFF_HAND));
|
||||
if (other == null) continue;
|
||||
if (!user.shouldShow(other)) continue;
|
||||
if (!this.settings.getCosmeticSettings().isInViewDistance(location, other.getLocation())) continue;
|
||||
user.updateBackpack(other, this.settings);
|
||||
PacketManager.sendEquipmentPacket(items, entityId, other);
|
||||
PacketManager.sendEquipmentPacket(equipment, entityId, other);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendUpdatePacket(
|
||||
final BaseUser<?> user,
|
||||
final Player other,
|
||||
Equipment items
|
||||
Equipment equipment
|
||||
) {
|
||||
// final Player player = user.getPlayer();
|
||||
// if (player == null) return;
|
||||
@@ -325,6 +333,6 @@ public class UserManager {
|
||||
if (other == null) return;
|
||||
if (!user.shouldShow(other)) return;
|
||||
if (!this.settings.getCosmeticSettings().isInViewDistance(location, other.getLocation())) return;
|
||||
PacketManager.sendEquipmentPacket(items, entityId, other);
|
||||
PacketManager.sendEquipmentPacket(equipment, entityId, other);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user