Don't remove items from creative players in self disguises

This commit is contained in:
libraryaddict 2021-05-27 13:16:20 +12:00
parent 3370a1353d
commit 15fd2c3d46
3 changed files with 163 additions and 162 deletions

View File

@ -85,6 +85,9 @@ public class DisguiseConfig {
private static boolean hidingArmorFromSelf;
@Getter
@Setter
private static boolean hidingCreativeEquipmentFromSelf;
@Getter
@Setter
private static boolean hidingHeldItemFromSelf;
@Getter
@Setter
@ -579,6 +582,7 @@ public class DisguiseConfig {
setEquipmentPacketsEnabled(config.getBoolean("PacketsEnabled.Equipment"));
setExplicitDisguisePermissions(config.getBoolean("Permissions.ExplicitDisguises"));
setHideArmorFromSelf(config.getBoolean("RemoveArmor"));
setHidingCreativeEquipmentFromSelf(config.getBoolean("RemoveCreativeEquipment"));
setHideDisguisedPlayers(config.getBoolean("HideDisguisedPlayersFromTab"));
setHideHeldItemFromSelf(config.getBoolean("RemoveHeldItem"));
setHorseSaddleable(config.getBoolean("SaddleableHorse"));

View File

@ -9,11 +9,13 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -27,17 +29,17 @@ public class PacketListenerInventory extends PacketAdapter {
private LibsDisguises libsDisguises;
public PacketListenerInventory(LibsDisguises plugin) {
super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS,
PacketType.Play.Client.HELD_ITEM_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT,
PacketType.Play.Client.WINDOW_CLICK);
super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, PacketType.Play.Client.HELD_ITEM_SLOT,
PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.WINDOW_CLICK);
libsDisguises = plugin;
}
@Override
public void onPacketReceiving(final PacketEvent event) {
if (event.isCancelled() || event.isPlayerTemporary())
if (event.isCancelled() || event.isPlayerTemporary()) {
return;
}
final Player player = event.getPlayer();
@ -55,11 +57,19 @@ public class PacketListenerInventory extends PacketAdapter {
return;
}
if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) {
return;
}
Disguise disguise = DisguiseAPI.getDisguise(player, player);
// If player isn't disguise, isn't self disguised, or isn't hiding items from themselves
// If player is disguised, views self disguises and has a inventory modifier
if (disguise != null && DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) &&
(disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) {
if (disguise == null || !DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) ||
(!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) {
return;
}
// If they are in creative and clicked on a slot
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) {
int slot = event.getPacket().getIntegers().read(0);
@ -77,13 +87,11 @@ public class PacketListenerInventory extends PacketAdapter {
mods.write(0, 0);
mods.write(1, slot);
mods.write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@ -101,13 +109,11 @@ public class PacketListenerInventory extends PacketAdapter {
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@ -135,14 +141,12 @@ public class PacketListenerInventory extends PacketAdapter {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
org.bukkit.inventory.ItemStack newHeld = player.getInventory()
.getItem(event.getPacket().getIntegers().read(0));
org.bukkit.inventory.ItemStack newHeld = player.getInventory().getItem(event.getPacket().getIntegers().read(0));
// If his new weapon isn't air either!
if (newHeld != null && newHeld.getType() != Material.AIR) {
@ -156,8 +160,7 @@ public class PacketListenerInventory extends PacketAdapter {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@ -192,8 +195,7 @@ public class PacketListenerInventory extends PacketAdapter {
clickedItem = player.getItemOnCursor();
}
if (clickedItem != null && clickedItem.getType() != Material.AIR &&
clickedItem.getType() != Material.ELYTRA) {
if (clickedItem != null && clickedItem.getType() != Material.AIR && clickedItem.getType() != Material.ELYTRA) {
// If the slot is a armor slot
if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) {
@ -203,13 +205,11 @@ public class PacketListenerInventory extends PacketAdapter {
mods.write(0, 0);
mods.write(1, slot);
mods.write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@ -225,13 +225,11 @@ public class PacketListenerInventory extends PacketAdapter {
StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0);
mods.write(1, slot);
mods.write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@ -239,7 +237,7 @@ public class PacketListenerInventory extends PacketAdapter {
}
}
}
}
}
@Override
@ -247,8 +245,11 @@ public class PacketListenerInventory extends PacketAdapter {
Player player = event.getPlayer();
// If the inventory is the players inventory
if (event.isPlayerTemporary() || player.getVehicle() != null ||
event.getPacket().getIntegers().read(0) != 0) {
if (event.isPlayerTemporary() || player.getVehicle() != null || event.getPacket().getIntegers().read(0) != 0) {
return;
}
if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) {
return;
}
@ -264,9 +265,6 @@ public class PacketListenerInventory extends PacketAdapter {
// If the server is setting the slot
// Need to set it to air if its in a place it shouldn't be.
// Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this
/**
* Done
*/
if (event.getPacketType() == Server.SET_SLOT) {
// The raw slot
// nms code has the start of the hotbar being 36.
@ -283,8 +281,7 @@ public class PacketListenerInventory extends PacketAdapter {
if (item != null && item.getType() != Material.AIR && item.getType() != Material.ELYTRA) {
event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
}
}
// Else if its a hotbar slot
@ -298,8 +295,7 @@ public class PacketListenerInventory extends PacketAdapter {
if (item != null && item.getType() != Material.AIR) {
event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2,
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
}
}
}

View File

@ -12,9 +12,10 @@ RandomDisguiseOptions: true
# For self disguises, they need to have the armor and the held item removed
# Else they see floating armor, floating held items.
# This turns the items invisible in the disguised players inventory. It does not actually remove them!
# Be warned that in creative this can actually delete the item from the inventory due to a weird bug
RemoveArmor: true
RemoveHeldItem: false
# An exception for creative mode as this can actually delete the item from the inventory due to creative being weird
RemoveCreativeEquipment: false
# If you set a disguise to burning, it will no longer be able to be shown as sneaking or invisible.
# Set this to true if you want the disguise to get the animations of the disguised entity. Such as invisible, on fire, sprinting, sneaking, blocking