Small code cleanup
This commit is contained in:
parent
dcff07e5bb
commit
b00f85cfbb
@ -1268,122 +1268,131 @@ public class DisguiseUtilities {
|
||||
String originalTeam = preDisguiseTeam.remove(player.getUniqueId());
|
||||
String teamDisguise = disguiseTeam.remove(player.getUniqueId());
|
||||
|
||||
if (teamDisguise != null && DisguiseConfig.getPushingOption() != DisguisePushing.IGNORE_SCOREBOARD) {
|
||||
// Code replace them back onto their original scoreboard team
|
||||
Scoreboard scoreboard = player.getScoreboard();
|
||||
Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
|
||||
Team ldTeam = null;
|
||||
if (teamDisguise == null || DisguiseConfig.getPushingOption() == DisguisePushing.IGNORE_SCOREBOARD) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Team t : scoreboard.getTeams()) {
|
||||
if (!t.hasEntry(player.getName()))
|
||||
continue;
|
||||
// Code replace them back onto their original scoreboard team
|
||||
Scoreboard scoreboard = player.getScoreboard();
|
||||
Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
|
||||
Team ldTeam = null;
|
||||
|
||||
ldTeam = t;
|
||||
break;
|
||||
}
|
||||
for (Team t : scoreboard.getTeams()) {
|
||||
if (!t.hasEntry(player.getName()))
|
||||
continue;
|
||||
|
||||
if (DisguiseConfig.isWarnScoreboardConflict()) {
|
||||
if (ldTeam == null || !ldTeam.getName().equals(teamDisguise)) {
|
||||
getLogger().warning("Scoreboard conflict, the self disguise player was not on the expected team!");
|
||||
} else {
|
||||
OptionStatus collisions = ldTeam.getOption(Option.COLLISION_RULE);
|
||||
ldTeam = t;
|
||||
break;
|
||||
}
|
||||
|
||||
if (collisions != OptionStatus.NEVER && collisions != OptionStatus.FOR_OTHER_TEAMS) {
|
||||
getLogger().warning(
|
||||
"Scoreboard conflict, the collisions for a self disguise player team has been " +
|
||||
"unexpectedly modifed!");
|
||||
}
|
||||
if (DisguiseConfig.isWarnScoreboardConflict()) {
|
||||
if (ldTeam == null || !ldTeam.getName().equals(teamDisguise)) {
|
||||
getLogger().warning("Scoreboard conflict, the self disguise player was not on the expected team!");
|
||||
} else {
|
||||
OptionStatus collisions = ldTeam.getOption(Option.COLLISION_RULE);
|
||||
|
||||
if (collisions != OptionStatus.NEVER && collisions != OptionStatus.FOR_OTHER_TEAMS) {
|
||||
getLogger().warning(
|
||||
"Scoreboard conflict, the collisions for a self disguise player team has been " +
|
||||
"unexpectedly modifed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ldTeam != null) {
|
||||
if (!ldTeam.getName().equals("LD_Pushing") && !ldTeam.getName().endsWith("_LDP")) {
|
||||
// Its not a team assigned by Lib's Disguises
|
||||
ldTeam = null;
|
||||
}
|
||||
if (ldTeam != null) {
|
||||
if (!ldTeam.getName().equals("LD_Pushing") && !ldTeam.getName().endsWith("_LDP")) {
|
||||
// Its not a team assigned by Lib's Disguises
|
||||
ldTeam = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (team != null) {
|
||||
team.addEntry(player.getName());
|
||||
} else if (ldTeam != null) {
|
||||
ldTeam.removeEntry(player.getName());
|
||||
}
|
||||
if (team != null) {
|
||||
team.addEntry(player.getName());
|
||||
} else if (ldTeam != null) {
|
||||
ldTeam.removeEntry(player.getName());
|
||||
}
|
||||
|
||||
if (ldTeam != null && ldTeam.getEntries().isEmpty()) {
|
||||
ldTeam.unregister();
|
||||
}
|
||||
if (ldTeam != null && ldTeam.getEntries().isEmpty()) {
|
||||
ldTeam.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setupSelfDisguiseScoreboard(Player player) {
|
||||
// They're already in a disguise team
|
||||
if (disguiseTeam.containsKey(player.getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
DisguisePushing pOption = DisguiseConfig.getPushingOption();
|
||||
|
||||
if (pOption != DisguisePushing.IGNORE_SCOREBOARD) {
|
||||
// Code to stop player pushing
|
||||
Scoreboard scoreboard = player.getScoreboard();
|
||||
Team prevTeam = null;
|
||||
Team ldTeam = null;
|
||||
String ldTeamName = "LD_Pushing";
|
||||
if (pOption == DisguisePushing.IGNORE_SCOREBOARD) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Team t : scoreboard.getTeams()) {
|
||||
if (!t.hasEntry(player.getName()))
|
||||
continue;
|
||||
// Code to stop player pushing
|
||||
Scoreboard scoreboard = player.getScoreboard();
|
||||
Team prevTeam = null;
|
||||
Team ldTeam = null;
|
||||
String ldTeamName = "LD_Pushing";
|
||||
|
||||
prevTeam = t;
|
||||
break;
|
||||
}
|
||||
for (Team t : scoreboard.getTeams()) {
|
||||
if (!t.hasEntry(player.getName()))
|
||||
continue;
|
||||
|
||||
// If the player is in a team already and the team isn't one controlled by Lib's Disguises
|
||||
if (prevTeam != null && !(prevTeam.getName().equals("LD_Pushing") || prevTeam.getName().endsWith("_LDP"))) {
|
||||
// If we're creating a scoreboard
|
||||
if (pOption == DisguisePushing.CREATE_SCOREBOARD) {
|
||||
// Remember his old team so we can give him it back later
|
||||
preDisguiseTeam.put(player.getUniqueId(), prevTeam.getName());
|
||||
} else {
|
||||
// We're modifying the scoreboard
|
||||
ldTeam = prevTeam;
|
||||
}
|
||||
} else {
|
||||
prevTeam = null;
|
||||
}
|
||||
prevTeam = t;
|
||||
break;
|
||||
}
|
||||
|
||||
// If we are creating a new scoreboard because the current one must not be modified
|
||||
// If the player is in a team already and the team isn't one controlled by Lib's Disguises
|
||||
if (prevTeam != null && !(prevTeam.getName().equals("LD_Pushing") || prevTeam.getName().endsWith("_LDP"))) {
|
||||
// If we're creating a scoreboard
|
||||
if (pOption == DisguisePushing.CREATE_SCOREBOARD) {
|
||||
// If they have a team, we'll reuse that name. Otherwise go for another name
|
||||
ldTeamName = (prevTeam == null ? "No Team" : prevTeam.getName());
|
||||
|
||||
// Give the teamname a custom name
|
||||
ldTeamName = ldTeamName.substring(0, Math.min(12, ldTeamName.length())) + "_LDP";
|
||||
// Remember his old team so we can give him it back later
|
||||
preDisguiseTeam.put(player.getUniqueId(), prevTeam.getName());
|
||||
} else {
|
||||
// We're modifying the scoreboard
|
||||
ldTeam = prevTeam;
|
||||
}
|
||||
} else {
|
||||
prevTeam = null;
|
||||
}
|
||||
|
||||
if (ldTeam == null && (ldTeam = scoreboard.getTeam(ldTeamName)) == null) {
|
||||
ldTeam = scoreboard.registerNewTeam(ldTeamName);
|
||||
// If we are creating a new scoreboard because the current one must not be modified
|
||||
if (pOption == DisguisePushing.CREATE_SCOREBOARD) {
|
||||
// If they have a team, we'll reuse that name. Otherwise go for another name
|
||||
ldTeamName = (prevTeam == null ? "No Team" : prevTeam.getName());
|
||||
|
||||
// Give the teamname a custom name
|
||||
ldTeamName = ldTeamName.substring(0, Math.min(12, ldTeamName.length())) + "_LDP";
|
||||
}
|
||||
|
||||
if (ldTeam == null && (ldTeam = scoreboard.getTeam(ldTeamName)) == null) {
|
||||
ldTeam = scoreboard.registerNewTeam(ldTeamName);
|
||||
}
|
||||
|
||||
disguiseTeam.put(player.getUniqueId(), ldTeam.getName());
|
||||
|
||||
if (!ldTeam.hasEntry(player.getName()))
|
||||
ldTeam.addEntry(player.getName());
|
||||
|
||||
if (pOption == DisguisePushing.CREATE_SCOREBOARD && prevTeam != null) {
|
||||
ldTeam.setAllowFriendlyFire(prevTeam.allowFriendlyFire());
|
||||
ldTeam.setCanSeeFriendlyInvisibles(prevTeam.canSeeFriendlyInvisibles());
|
||||
ldTeam.setDisplayName(prevTeam.getDisplayName());
|
||||
ldTeam.setPrefix(prevTeam.getPrefix());
|
||||
ldTeam.setSuffix(prevTeam.getSuffix());
|
||||
|
||||
for (Option option : Team.Option.values()) {
|
||||
ldTeam.setOption(option, prevTeam.getOption(option));
|
||||
}
|
||||
}
|
||||
|
||||
disguiseTeam.put(player.getUniqueId(), ldTeam.getName());
|
||||
if (ldTeam.getOption(Option.COLLISION_RULE) != OptionStatus.NEVER && DisguiseConfig.isModifyCollisions()) {
|
||||
ldTeam.setOption(Option.COLLISION_RULE, OptionStatus.NEVER);
|
||||
}
|
||||
|
||||
if (!ldTeam.hasEntry(player.getName()))
|
||||
ldTeam.addEntry(player.getName());
|
||||
|
||||
if (pOption == DisguisePushing.CREATE_SCOREBOARD && prevTeam != null) {
|
||||
ldTeam.setAllowFriendlyFire(prevTeam.allowFriendlyFire());
|
||||
ldTeam.setCanSeeFriendlyInvisibles(prevTeam.canSeeFriendlyInvisibles());
|
||||
ldTeam.setDisplayName(prevTeam.getDisplayName());
|
||||
ldTeam.setPrefix(prevTeam.getPrefix());
|
||||
ldTeam.setSuffix(prevTeam.getSuffix());
|
||||
|
||||
for (Option option : Team.Option.values()) {
|
||||
ldTeam.setOption(option, prevTeam.getOption(option));
|
||||
}
|
||||
}
|
||||
|
||||
if (ldTeam.getOption(Option.COLLISION_RULE) != OptionStatus.NEVER && DisguiseConfig.isModifyCollisions()) {
|
||||
ldTeam.setOption(Option.COLLISION_RULE, OptionStatus.NEVER);
|
||||
}
|
||||
|
||||
if (ldTeam.canSeeFriendlyInvisibles() && DisguiseConfig.isDisableFriendlyInvisibles()) {
|
||||
ldTeam.setCanSeeFriendlyInvisibles(false);
|
||||
}
|
||||
if (ldTeam.canSeeFriendlyInvisibles() && DisguiseConfig.isDisableFriendlyInvisibles()) {
|
||||
ldTeam.setCanSeeFriendlyInvisibles(false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1645,7 +1654,7 @@ public class DisguiseUtilities {
|
||||
if (transformed.isUnhandled())
|
||||
transformed.addPacket(packet);
|
||||
|
||||
transformed.setPacketType(packet.getType());
|
||||
transformed.setSpawnPacketCheck(packet.getType());
|
||||
|
||||
for (PacketContainer p : transformed.getPackets()) {
|
||||
p = p.deepClone();
|
||||
@ -1668,7 +1677,7 @@ public class DisguiseUtilities {
|
||||
public static void setupFakeDisguise(final Disguise disguise) {
|
||||
Entity e = disguise.getEntity();
|
||||
|
||||
// If the disguises entity is null, or the disguised entity isn't a player return
|
||||
// If the disguises entity is null, or the disguised entity isn't a player; return
|
||||
if (!(e instanceof Player) || !getDisguises().containsKey(e.getUniqueId()) ||
|
||||
!getDisguises().get(e.getUniqueId()).contains(disguise)) {
|
||||
return;
|
||||
|
@ -60,7 +60,7 @@ public class PacketsManager {
|
||||
return disguise;
|
||||
}
|
||||
|
||||
public void setPacketType(PacketType type) {
|
||||
public void setSpawnPacketCheck(PacketType type) {
|
||||
isSpawnPacket = type.name().contains("SPAWN") && type.name().contains("ENTITY");
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ public class PacketsManager {
|
||||
|
||||
public void addDelayedPacket(PacketContainer packet, int ticksDelayed) {
|
||||
if (!delayedPackets.containsKey(ticksDelayed))
|
||||
delayedPackets.put(ticksDelayed, new ArrayList<PacketContainer>());
|
||||
delayedPackets.put(ticksDelayed, new ArrayList<>());
|
||||
|
||||
delayedPackets.get(ticksDelayed).add(packet);
|
||||
}
|
||||
@ -681,7 +681,7 @@ public class PacketsManager {
|
||||
soundsListener = new PacketListenerSounds(libsDisguises);
|
||||
|
||||
// Self disguise (/vsd) listener
|
||||
viewDisguisesListener = new PacketListenerViewDisguises(libsDisguises);
|
||||
viewDisguisesListener = new PacketListenerViewSelfDisguise(libsDisguises);
|
||||
|
||||
inventoryListener = new PacketListenerInventory(libsDisguises);
|
||||
}
|
||||
@ -951,13 +951,19 @@ public class PacketsManager {
|
||||
sentPacket.getType() == Server.REL_ENTITY_MOVE) {
|
||||
if (disguise.getType() == DisguiseType.RABBIT && (sentPacket.getType() == Server.REL_ENTITY_MOVE ||
|
||||
sentPacket.getType() == Server.REL_ENTITY_MOVE_LOOK)) {
|
||||
// Rabbit robbing...
|
||||
if (entity.getMetadata("LibsRabbitHop").isEmpty() ||
|
||||
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() < 100 ||
|
||||
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() > 500) {
|
||||
if (entity.getMetadata("LibsRabbitHop").isEmpty() ||
|
||||
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() >
|
||||
500) {
|
||||
// When did the rabbit disguise last hop
|
||||
long lastHop = 999999;
|
||||
|
||||
// If hop meta exists, set the last hop time
|
||||
if (!entity.getMetadata("LibsRabbitHop").isEmpty()) {
|
||||
// Last hop was 3 minutes ago, so subtract current time with the last hop time and get 3
|
||||
// minutes ago in milliseconds
|
||||
lastHop = System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong();
|
||||
}
|
||||
|
||||
// If last hop was less than 0.1 or more than 0.5 seconds ago
|
||||
if (lastHop < 100 || lastHop > 500) {
|
||||
if (lastHop > 500) {
|
||||
entity.removeMetadata("LibsRabbitHop", libsDisguises);
|
||||
entity.setMetadata("LibsRabbitHop",
|
||||
new FixedMetadataValue(libsDisguises, System.currentTimeMillis()));
|
||||
|
@ -66,7 +66,7 @@ public class PacketListenerMain extends PacketAdapter {
|
||||
return;
|
||||
}
|
||||
|
||||
packets.setPacketType(event.getPacketType());
|
||||
packets.setSpawnPacketCheck(event.getPacketType());
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
|
@ -1,172 +1,177 @@
|
||||
package me.libraryaddict.disguise.utilities.packetlisteners;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.PacketType.Play.Server;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.ListenerPriority;
|
||||
import com.comphenix.protocol.events.PacketAdapter;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.events.PacketEvent;
|
||||
import com.comphenix.protocol.reflect.StructureModifier;
|
||||
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
||||
|
||||
import me.libraryaddict.disguise.DisguiseAPI;
|
||||
import me.libraryaddict.disguise.LibsDisguises;
|
||||
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
||||
import me.libraryaddict.disguise.utilities.PacketsManager;
|
||||
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
|
||||
import me.libraryaddict.disguise.utilities.ReflectionManager;
|
||||
|
||||
public class PacketListenerViewDisguises extends PacketAdapter {
|
||||
public PacketListenerViewDisguises(LibsDisguises plugin) {
|
||||
super(plugin, ListenerPriority.HIGH, Server.NAMED_ENTITY_SPAWN, Server.ATTACH_ENTITY, Server.REL_ENTITY_MOVE,
|
||||
Server.REL_ENTITY_MOVE_LOOK, Server.ENTITY_LOOK, Server.ENTITY_TELEPORT, Server.ENTITY_HEAD_ROTATION,
|
||||
Server.ENTITY_METADATA, Server.ENTITY_EQUIPMENT, Server.ANIMATION, Server.BED, Server.ENTITY_EFFECT,
|
||||
Server.ENTITY_VELOCITY, Server.UPDATE_ATTRIBUTES, Server.ENTITY_STATUS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPacketSending(final PacketEvent event) {
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
try {
|
||||
final Player observer = event.getPlayer();
|
||||
|
||||
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
|
||||
return;
|
||||
|
||||
if (event.getPacket().getIntegers().read(0) != observer.getEntityId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!DisguiseAPI.isSelfDisguised(observer)) {
|
||||
if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
|
||||
Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
|
||||
|
||||
if (disguise != null && disguise.isSelfDisguiseVisible()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
final Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
|
||||
|
||||
if (disguise == null)
|
||||
return;
|
||||
|
||||
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
|
||||
LibsPackets transformed = PacketsManager.transformPacket(event.getPacket(), disguise, observer, observer);
|
||||
|
||||
if (transformed.isUnhandled()) {
|
||||
transformed.getPackets().add(event.getPacket());
|
||||
}
|
||||
|
||||
transformed.setPacketType(event.getPacketType());
|
||||
|
||||
for (PacketContainer packet : transformed.getPackets()) {
|
||||
if (packet.getType() != Server.PLAYER_INFO) {
|
||||
if (packet.equals(event.getPacket())) {
|
||||
packet = packet.shallowClone();
|
||||
}
|
||||
|
||||
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
|
||||
}
|
||||
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (ArrayList<PacketContainer> packets : transformed.getDelayedPackets()) {
|
||||
for (PacketContainer packet : packets) {
|
||||
if (packet.getType() != Server.PLAYER_INFO) {
|
||||
if (packet.equals(event.getPacket())) {
|
||||
packet = packet.shallowClone();
|
||||
}
|
||||
|
||||
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transformed.sendDelayed(observer);
|
||||
|
||||
if (event.getPacketType() == Server.ENTITY_METADATA) {
|
||||
event.setPacket(event.getPacket().deepClone());
|
||||
|
||||
for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) {
|
||||
if (watch.getIndex() == 0) {
|
||||
byte b = (byte) watch.getValue();
|
||||
|
||||
byte a = (byte) (b | 1 << 5);
|
||||
|
||||
if ((b & 1 << 3) != 0)
|
||||
a = (byte) (a | 1 << 3);
|
||||
|
||||
watch.setValue(a);
|
||||
}
|
||||
}
|
||||
} else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) {
|
||||
event.setCancelled(true);
|
||||
|
||||
PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA);
|
||||
|
||||
StructureModifier<Object> mods = packet.getModifier();
|
||||
|
||||
mods.write(0, observer.getEntityId());
|
||||
|
||||
List<WrappedWatchableObject> watchableList = new ArrayList<>();
|
||||
Byte b = 1 << 5;
|
||||
|
||||
if (observer.isSprinting())
|
||||
b = (byte) (b | 1 << 3);
|
||||
|
||||
WrappedWatchableObject watch = ReflectionManager.createWatchable(0, b);
|
||||
|
||||
if (watch != null)
|
||||
watchableList.add(watch);
|
||||
|
||||
packet.getWatchableCollectionModifier().write(0, watchableList);
|
||||
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet);
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (event.getPacketType() == Server.ANIMATION) {
|
||||
if (event.getPacket().getIntegers().read(1) != 2) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
} else if (event.getPacketType() == Server.ATTACH_ENTITY || event
|
||||
.getPacketType() == Server.REL_ENTITY_MOVE || event
|
||||
.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event
|
||||
.getPacketType() == Server.ENTITY_LOOK || event.getPacketType() == Server.ENTITY_TELEPORT || event
|
||||
.getPacketType() == Server.ENTITY_HEAD_ROTATION || event
|
||||
.getPacketType() == Server.ENTITY_EQUIPMENT) {
|
||||
event.setCancelled(true);
|
||||
} else if (event.getPacketType() == Server.ENTITY_STATUS) {
|
||||
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() && event.getPacket()
|
||||
.getBytes().read(0) == 2) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
event.setCancelled(true);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
package me.libraryaddict.disguise.utilities.packetlisteners;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.PacketType.Play.Server;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.ListenerPriority;
|
||||
import com.comphenix.protocol.events.PacketAdapter;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.events.PacketEvent;
|
||||
import com.comphenix.protocol.reflect.StructureModifier;
|
||||
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
|
||||
import me.libraryaddict.disguise.DisguiseAPI;
|
||||
import me.libraryaddict.disguise.LibsDisguises;
|
||||
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
||||
import me.libraryaddict.disguise.utilities.PacketsManager;
|
||||
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
|
||||
import me.libraryaddict.disguise.utilities.ReflectionManager;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PacketListenerViewSelfDisguise extends PacketAdapter {
|
||||
public PacketListenerViewSelfDisguise(LibsDisguises plugin) {
|
||||
super(plugin, ListenerPriority.HIGH, Server.NAMED_ENTITY_SPAWN, Server.ATTACH_ENTITY, Server.REL_ENTITY_MOVE,
|
||||
Server.REL_ENTITY_MOVE_LOOK, Server.ENTITY_LOOK, Server.ENTITY_TELEPORT, Server.ENTITY_HEAD_ROTATION,
|
||||
Server.ENTITY_METADATA, Server.ENTITY_EQUIPMENT, Server.ANIMATION, Server.BED, Server.ENTITY_EFFECT,
|
||||
Server.ENTITY_VELOCITY, Server.UPDATE_ATTRIBUTES, Server.ENTITY_STATUS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPacketSending(final PacketEvent event) {
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
try {
|
||||
final Player observer = event.getPlayer();
|
||||
|
||||
if (observer.getName().contains("UNKNOWN[")) {// If the player is temporary
|
||||
return;
|
||||
}
|
||||
|
||||
PacketContainer packet = event.getPacket();
|
||||
|
||||
// If packet isn't meant for the disguised player's self disguise
|
||||
if (packet.getIntegers().read(0) != observer.getEntityId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!DisguiseAPI.isSelfDisguised(observer)) {
|
||||
if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
|
||||
Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
|
||||
|
||||
if (disguise != null && disguise.isSelfDisguiseVisible()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
final Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
|
||||
|
||||
if (disguise == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
|
||||
LibsPackets transformed = PacketsManager.transformPacket(packet, disguise, observer, observer);
|
||||
|
||||
if (transformed.isUnhandled()) {
|
||||
transformed.getPackets().add(packet);
|
||||
}
|
||||
|
||||
transformed.setSpawnPacketCheck(event.getPacketType());
|
||||
|
||||
for (PacketContainer newPacket : transformed.getPackets()) {
|
||||
if (newPacket.getType() != Server.PLAYER_INFO) {
|
||||
if (newPacket.equals(packet)) {
|
||||
newPacket = newPacket.shallowClone();
|
||||
}
|
||||
|
||||
newPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
|
||||
}
|
||||
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, newPacket, false);
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (ArrayList<PacketContainer> packets : transformed.getDelayedPackets()) {
|
||||
for (PacketContainer newPacket : packets) {
|
||||
if (newPacket.getType() == Server.PLAYER_INFO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newPacket.equals(packet)) {
|
||||
newPacket = newPacket.shallowClone();
|
||||
}
|
||||
|
||||
newPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
|
||||
}
|
||||
}
|
||||
|
||||
transformed.sendDelayed(observer);
|
||||
|
||||
if (event.getPacketType() == Server.ENTITY_METADATA) {
|
||||
event.setPacket(packet.deepClone());
|
||||
|
||||
for (WrappedWatchableObject watch : packet.getWatchableCollectionModifier().read(0)) {
|
||||
if (watch.getIndex() == 0) {
|
||||
byte b = (byte) watch.getValue();
|
||||
|
||||
byte a = (byte) (b | 1 << 5);
|
||||
|
||||
if ((b & 1 << 3) != 0)
|
||||
a = (byte) (a | 1 << 3);
|
||||
|
||||
watch.setValue(a);
|
||||
}
|
||||
}
|
||||
} else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) {
|
||||
event.setCancelled(true);
|
||||
|
||||
PacketContainer metaPacket = new PacketContainer(Server.ENTITY_METADATA);
|
||||
|
||||
StructureModifier<Object> mods = metaPacket.getModifier();
|
||||
|
||||
mods.write(0, observer.getEntityId());
|
||||
|
||||
List<WrappedWatchableObject> watchableList = new ArrayList<>();
|
||||
byte b = 1 << 5;
|
||||
|
||||
if (observer.isSprinting())
|
||||
b = (byte) (b | 1 << 3);
|
||||
|
||||
WrappedWatchableObject watch = ReflectionManager.createWatchable(0, b);
|
||||
|
||||
if (watch != null)
|
||||
watchableList.add(watch);
|
||||
|
||||
metaPacket.getWatchableCollectionModifier().write(0, watchableList);
|
||||
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, metaPacket);
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (event.getPacketType() == Server.ANIMATION) {
|
||||
if (packet.getIntegers().read(1) != 2) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
} else if (event.getPacketType() == Server.ATTACH_ENTITY ||
|
||||
event.getPacketType() == Server.REL_ENTITY_MOVE ||
|
||||
event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK ||
|
||||
event.getPacketType() == Server.ENTITY_LOOK || event.getPacketType() == Server.ENTITY_TELEPORT ||
|
||||
event.getPacketType() == Server.ENTITY_HEAD_ROTATION ||
|
||||
event.getPacketType() == Server.ENTITY_EQUIPMENT) {
|
||||
event.setCancelled(true);
|
||||
} else if (event.getPacketType() == Server.ENTITY_STATUS) {
|
||||
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() &&
|
||||
packet.getBytes().read(0) == 2) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
event.setCancelled(true);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user