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; private static boolean hidingArmorFromSelf;
@Getter @Getter
@Setter @Setter
private static boolean hidingCreativeEquipmentFromSelf;
@Getter
@Setter
private static boolean hidingHeldItemFromSelf; private static boolean hidingHeldItemFromSelf;
@Getter @Getter
@Setter @Setter
@ -579,6 +582,7 @@ public class DisguiseConfig {
setEquipmentPacketsEnabled(config.getBoolean("PacketsEnabled.Equipment")); setEquipmentPacketsEnabled(config.getBoolean("PacketsEnabled.Equipment"));
setExplicitDisguisePermissions(config.getBoolean("Permissions.ExplicitDisguises")); setExplicitDisguisePermissions(config.getBoolean("Permissions.ExplicitDisguises"));
setHideArmorFromSelf(config.getBoolean("RemoveArmor")); setHideArmorFromSelf(config.getBoolean("RemoveArmor"));
setHidingCreativeEquipmentFromSelf(config.getBoolean("RemoveCreativeEquipment"));
setHideDisguisedPlayers(config.getBoolean("HideDisguisedPlayersFromTab")); setHideDisguisedPlayers(config.getBoolean("HideDisguisedPlayersFromTab"));
setHideHeldItemFromSelf(config.getBoolean("RemoveHeldItem")); setHideHeldItemFromSelf(config.getBoolean("RemoveHeldItem"));
setHorseSaddleable(config.getBoolean("SaddleableHorse")); 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.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -27,17 +29,17 @@ public class PacketListenerInventory extends PacketAdapter {
private LibsDisguises libsDisguises; private LibsDisguises libsDisguises;
public PacketListenerInventory(LibsDisguises plugin) { public PacketListenerInventory(LibsDisguises plugin) {
super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, PacketType.Play.Client.HELD_ITEM_SLOT,
PacketType.Play.Client.HELD_ITEM_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.WINDOW_CLICK);
PacketType.Play.Client.WINDOW_CLICK);
libsDisguises = plugin; libsDisguises = plugin;
} }
@Override @Override
public void onPacketReceiving(final PacketEvent event) { public void onPacketReceiving(final PacketEvent event) {
if (event.isCancelled() || event.isPlayerTemporary()) if (event.isCancelled() || event.isPlayerTemporary()) {
return; return;
}
final Player player = event.getPlayer(); final Player player = event.getPlayer();
@ -55,11 +57,19 @@ public class PacketListenerInventory extends PacketAdapter {
return; return;
} }
if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) {
return;
}
Disguise disguise = DisguiseAPI.getDisguise(player, player); 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 player is disguised, views self disguises and has a inventory modifier
if (disguise != null && DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) && if (disguise == null || !DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) ||
(disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) {
return;
}
// If they are in creative and clicked on a slot // If they are in creative and clicked on a slot
if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) { if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) {
int slot = event.getPacket().getIntegers().read(0); int slot = event.getPacket().getIntegers().read(0);
@ -77,13 +87,11 @@ public class PacketListenerInventory extends PacketAdapter {
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -101,13 +109,11 @@ public class PacketListenerInventory extends PacketAdapter {
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -135,14 +141,12 @@ public class PacketListenerInventory extends PacketAdapter {
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
org.bukkit.inventory.ItemStack newHeld = player.getInventory() org.bukkit.inventory.ItemStack newHeld = player.getInventory().getItem(event.getPacket().getIntegers().read(0));
.getItem(event.getPacket().getIntegers().read(0));
// If his new weapon isn't air either! // If his new weapon isn't air either!
if (newHeld != null && newHeld.getType() != Material.AIR) { if (newHeld != null && newHeld.getType() != Material.AIR) {
@ -156,8 +160,7 @@ public class PacketListenerInventory extends PacketAdapter {
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -192,8 +195,7 @@ public class PacketListenerInventory extends PacketAdapter {
clickedItem = player.getItemOnCursor(); clickedItem = player.getItemOnCursor();
} }
if (clickedItem != null && clickedItem.getType() != Material.AIR && if (clickedItem != null && clickedItem.getType() != Material.AIR && clickedItem.getType() != Material.ELYTRA) {
clickedItem.getType() != Material.ELYTRA) {
// If the slot is a armor slot // If the slot is a armor slot
if (slot >= 5 && slot <= 8) { if (slot >= 5 && slot <= 8) {
if (disguise.isHidingArmorFromSelf()) { if (disguise.isHidingArmorFromSelf()) {
@ -203,13 +205,11 @@ public class PacketListenerInventory extends PacketAdapter {
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -225,13 +225,11 @@ public class PacketListenerInventory extends PacketAdapter {
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, 0); mods.write(0, 0);
mods.write(1, slot); mods.write(1, slot);
mods.write(2, mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -239,7 +237,7 @@ public class PacketListenerInventory extends PacketAdapter {
} }
} }
} }
}
} }
@Override @Override
@ -247,8 +245,11 @@ public class PacketListenerInventory extends PacketAdapter {
Player player = event.getPlayer(); Player player = event.getPlayer();
// If the inventory is the players inventory // If the inventory is the players inventory
if (event.isPlayerTemporary() || player.getVehicle() != null || if (event.isPlayerTemporary() || player.getVehicle() != null || event.getPacket().getIntegers().read(0) != 0) {
event.getPacket().getIntegers().read(0) != 0) { return;
}
if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) {
return; return;
} }
@ -264,9 +265,6 @@ public class PacketListenerInventory extends PacketAdapter {
// If the server is setting the slot // If the server is setting the slot
// Need to set it to air if its in a place it shouldn't be. // 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 // 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) { if (event.getPacketType() == Server.SET_SLOT) {
// The raw slot // The raw slot
// nms code has the start of the hotbar being 36. // 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) { if (item != null && item.getType() != Material.AIR && item.getType() != Material.ELYTRA) {
event.setPacket(event.getPacket().shallowClone()); event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2, event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
} }
} }
// Else if its a hotbar slot // Else if its a hotbar slot
@ -298,8 +295,7 @@ public class PacketListenerInventory extends PacketAdapter {
if (item != null && item.getType() != Material.AIR) { if (item != null && item.getType() != Material.AIR) {
event.setPacket(event.getPacket().shallowClone()); event.setPacket(event.getPacket().shallowClone());
event.getPacket().getModifier().write(2, event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR)));
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 # For self disguises, they need to have the armor and the held item removed
# Else they see floating armor, floating held items. # Else they see floating armor, floating held items.
# This turns the items invisible in the disguised players inventory. It does not actually remove them! # 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 RemoveArmor: true
RemoveHeldItem: false 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. # 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 # 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