Small code cleanup

This commit is contained in:
libraryaddict 2018-11-23 13:27:12 +13:00
parent dcff07e5bb
commit b00f85cfbb
4 changed files with 295 additions and 275 deletions

View File

@ -1268,7 +1268,10 @@ public class DisguiseUtilities {
String originalTeam = preDisguiseTeam.remove(player.getUniqueId()); String originalTeam = preDisguiseTeam.remove(player.getUniqueId());
String teamDisguise = disguiseTeam.remove(player.getUniqueId()); String teamDisguise = disguiseTeam.remove(player.getUniqueId());
if (teamDisguise != null && DisguiseConfig.getPushingOption() != DisguisePushing.IGNORE_SCOREBOARD) { if (teamDisguise == null || DisguiseConfig.getPushingOption() == DisguisePushing.IGNORE_SCOREBOARD) {
return;
}
// Code replace them back onto their original scoreboard team // Code replace them back onto their original scoreboard team
Scoreboard scoreboard = player.getScoreboard(); Scoreboard scoreboard = player.getScoreboard();
Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam); Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
@ -1313,12 +1316,19 @@ public class DisguiseUtilities {
ldTeam.unregister(); ldTeam.unregister();
} }
} }
}
public static void setupSelfDisguiseScoreboard(Player player) { public static void setupSelfDisguiseScoreboard(Player player) {
// They're already in a disguise team
if (disguiseTeam.containsKey(player.getUniqueId())) {
return;
}
DisguisePushing pOption = DisguiseConfig.getPushingOption(); DisguisePushing pOption = DisguiseConfig.getPushingOption();
if (pOption != DisguisePushing.IGNORE_SCOREBOARD) { if (pOption == DisguisePushing.IGNORE_SCOREBOARD) {
return;
}
// Code to stop player pushing // Code to stop player pushing
Scoreboard scoreboard = player.getScoreboard(); Scoreboard scoreboard = player.getScoreboard();
Team prevTeam = null; Team prevTeam = null;
@ -1385,7 +1395,6 @@ public class DisguiseUtilities {
ldTeam.setCanSeeFriendlyInvisibles(false); ldTeam.setCanSeeFriendlyInvisibles(false);
} }
} }
}
/** /**
* Splits a string while respecting quotes. * Splits a string while respecting quotes.
@ -1645,7 +1654,7 @@ public class DisguiseUtilities {
if (transformed.isUnhandled()) if (transformed.isUnhandled())
transformed.addPacket(packet); transformed.addPacket(packet);
transformed.setPacketType(packet.getType()); transformed.setSpawnPacketCheck(packet.getType());
for (PacketContainer p : transformed.getPackets()) { for (PacketContainer p : transformed.getPackets()) {
p = p.deepClone(); p = p.deepClone();
@ -1668,7 +1677,7 @@ public class DisguiseUtilities {
public static void setupFakeDisguise(final Disguise disguise) { public static void setupFakeDisguise(final Disguise disguise) {
Entity e = disguise.getEntity(); 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()) || if (!(e instanceof Player) || !getDisguises().containsKey(e.getUniqueId()) ||
!getDisguises().get(e.getUniqueId()).contains(disguise)) { !getDisguises().get(e.getUniqueId()).contains(disguise)) {
return; return;

View File

@ -60,7 +60,7 @@ public class PacketsManager {
return disguise; return disguise;
} }
public void setPacketType(PacketType type) { public void setSpawnPacketCheck(PacketType type) {
isSpawnPacket = type.name().contains("SPAWN") && type.name().contains("ENTITY"); isSpawnPacket = type.name().contains("SPAWN") && type.name().contains("ENTITY");
} }
@ -78,7 +78,7 @@ public class PacketsManager {
public void addDelayedPacket(PacketContainer packet, int ticksDelayed) { public void addDelayedPacket(PacketContainer packet, int ticksDelayed) {
if (!delayedPackets.containsKey(ticksDelayed)) if (!delayedPackets.containsKey(ticksDelayed))
delayedPackets.put(ticksDelayed, new ArrayList<PacketContainer>()); delayedPackets.put(ticksDelayed, new ArrayList<>());
delayedPackets.get(ticksDelayed).add(packet); delayedPackets.get(ticksDelayed).add(packet);
} }
@ -681,7 +681,7 @@ public class PacketsManager {
soundsListener = new PacketListenerSounds(libsDisguises); soundsListener = new PacketListenerSounds(libsDisguises);
// Self disguise (/vsd) listener // Self disguise (/vsd) listener
viewDisguisesListener = new PacketListenerViewDisguises(libsDisguises); viewDisguisesListener = new PacketListenerViewSelfDisguise(libsDisguises);
inventoryListener = new PacketListenerInventory(libsDisguises); inventoryListener = new PacketListenerInventory(libsDisguises);
} }
@ -951,13 +951,19 @@ public class PacketsManager {
sentPacket.getType() == Server.REL_ENTITY_MOVE) { sentPacket.getType() == Server.REL_ENTITY_MOVE) {
if (disguise.getType() == DisguiseType.RABBIT && (sentPacket.getType() == Server.REL_ENTITY_MOVE || if (disguise.getType() == DisguiseType.RABBIT && (sentPacket.getType() == Server.REL_ENTITY_MOVE ||
sentPacket.getType() == Server.REL_ENTITY_MOVE_LOOK)) { sentPacket.getType() == Server.REL_ENTITY_MOVE_LOOK)) {
// Rabbit robbing... // When did the rabbit disguise last hop
if (entity.getMetadata("LibsRabbitHop").isEmpty() || long lastHop = 999999;
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() < 100 ||
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() > 500) { // If hop meta exists, set the last hop time
if (entity.getMetadata("LibsRabbitHop").isEmpty() || if (!entity.getMetadata("LibsRabbitHop").isEmpty()) {
System.currentTimeMillis() - entity.getMetadata("LibsRabbitHop").get(0).asLong() > // Last hop was 3 minutes ago, so subtract current time with the last hop time and get 3
500) { // 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.removeMetadata("LibsRabbitHop", libsDisguises);
entity.setMetadata("LibsRabbitHop", entity.setMetadata("LibsRabbitHop",
new FixedMetadataValue(libsDisguises, System.currentTimeMillis())); new FixedMetadataValue(libsDisguises, System.currentTimeMillis()));

View File

@ -66,7 +66,7 @@ public class PacketListenerMain extends PacketAdapter {
return; return;
} }
packets.setPacketType(event.getPacketType()); packets.setSpawnPacketCheck(event.getPacketType());
event.setCancelled(true); event.setCancelled(true);

View File

@ -1,11 +1,5 @@
package me.libraryaddict.disguise.utilities.packetlisteners; 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;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
@ -15,16 +9,20 @@ 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 com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
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.PacketsManager; import me.libraryaddict.disguise.utilities.PacketsManager;
import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets; import me.libraryaddict.disguise.utilities.PacketsManager.LibsPackets;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager;
import org.bukkit.entity.Player;
public class PacketListenerViewDisguises extends PacketAdapter { import java.lang.reflect.InvocationTargetException;
public PacketListenerViewDisguises(LibsDisguises plugin) { 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, 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.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_METADATA, Server.ENTITY_EQUIPMENT, Server.ANIMATION, Server.BED, Server.ENTITY_EFFECT,
@ -39,10 +37,14 @@ public class PacketListenerViewDisguises extends PacketAdapter {
try { try {
final Player observer = event.getPlayer(); final Player observer = event.getPlayer();
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary if (observer.getName().contains("UNKNOWN[")) {// If the player is temporary
return; return;
}
if (event.getPacket().getIntegers().read(0) != observer.getEntityId()) { PacketContainer packet = event.getPacket();
// If packet isn't meant for the disguised player's self disguise
if (packet.getIntegers().read(0) != observer.getEntityId()) {
return; return;
} }
@ -60,29 +62,30 @@ public class PacketListenerViewDisguises extends PacketAdapter {
final Disguise disguise = DisguiseAPI.getDisguise(observer, observer); final Disguise disguise = DisguiseAPI.getDisguise(observer, observer);
if (disguise == null) if (disguise == null) {
return; return;
}
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them. // 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); LibsPackets transformed = PacketsManager.transformPacket(packet, disguise, observer, observer);
if (transformed.isUnhandled()) { if (transformed.isUnhandled()) {
transformed.getPackets().add(event.getPacket()); transformed.getPackets().add(packet);
} }
transformed.setPacketType(event.getPacketType()); transformed.setSpawnPacketCheck(event.getPacketType());
for (PacketContainer packet : transformed.getPackets()) { for (PacketContainer newPacket : transformed.getPackets()) {
if (packet.getType() != Server.PLAYER_INFO) { if (newPacket.getType() != Server.PLAYER_INFO) {
if (packet.equals(event.getPacket())) { if (newPacket.equals(packet)) {
packet = packet.shallowClone(); newPacket = newPacket.shallowClone();
} }
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); newPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
} }
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, newPacket, false);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
@ -90,23 +93,25 @@ public class PacketListenerViewDisguises extends PacketAdapter {
} }
for (ArrayList<PacketContainer> packets : transformed.getDelayedPackets()) { for (ArrayList<PacketContainer> packets : transformed.getDelayedPackets()) {
for (PacketContainer packet : packets) { for (PacketContainer newPacket : packets) {
if (packet.getType() != Server.PLAYER_INFO) { if (newPacket.getType() == Server.PLAYER_INFO) {
if (packet.equals(event.getPacket())) { continue;
packet = packet.shallowClone();
} }
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); if (newPacket.equals(packet)) {
newPacket = newPacket.shallowClone();
} }
newPacket.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
} }
} }
transformed.sendDelayed(observer); transformed.sendDelayed(observer);
if (event.getPacketType() == Server.ENTITY_METADATA) { if (event.getPacketType() == Server.ENTITY_METADATA) {
event.setPacket(event.getPacket().deepClone()); event.setPacket(packet.deepClone());
for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) { for (WrappedWatchableObject watch : packet.getWatchableCollectionModifier().read(0)) {
if (watch.getIndex() == 0) { if (watch.getIndex() == 0) {
byte b = (byte) watch.getValue(); byte b = (byte) watch.getValue();
@ -121,14 +126,14 @@ public class PacketListenerViewDisguises extends PacketAdapter {
} else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) { } else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) {
event.setCancelled(true); event.setCancelled(true);
PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); PacketContainer metaPacket = new PacketContainer(Server.ENTITY_METADATA);
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = metaPacket.getModifier();
mods.write(0, observer.getEntityId()); mods.write(0, observer.getEntityId());
List<WrappedWatchableObject> watchableList = new ArrayList<>(); List<WrappedWatchableObject> watchableList = new ArrayList<>();
Byte b = 1 << 5; byte b = 1 << 5;
if (observer.isSprinting()) if (observer.isSprinting())
b = (byte) (b | 1 << 3); b = (byte) (b | 1 << 3);
@ -138,28 +143,28 @@ public class PacketListenerViewDisguises extends PacketAdapter {
if (watch != null) if (watch != null)
watchableList.add(watch); watchableList.add(watch);
packet.getWatchableCollectionModifier().write(0, watchableList); metaPacket.getWatchableCollectionModifier().write(0, watchableList);
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, metaPacket);
} }
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else if (event.getPacketType() == Server.ANIMATION) { } else if (event.getPacketType() == Server.ANIMATION) {
if (event.getPacket().getIntegers().read(1) != 2) { if (packet.getIntegers().read(1) != 2) {
event.setCancelled(true); event.setCancelled(true);
} }
} else if (event.getPacketType() == Server.ATTACH_ENTITY || event } else if (event.getPacketType() == Server.ATTACH_ENTITY ||
.getPacketType() == Server.REL_ENTITY_MOVE || event event.getPacketType() == Server.REL_ENTITY_MOVE ||
.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK ||
.getPacketType() == Server.ENTITY_LOOK || event.getPacketType() == Server.ENTITY_TELEPORT || event event.getPacketType() == Server.ENTITY_LOOK || event.getPacketType() == Server.ENTITY_TELEPORT ||
.getPacketType() == Server.ENTITY_HEAD_ROTATION || event event.getPacketType() == Server.ENTITY_HEAD_ROTATION ||
.getPacketType() == Server.ENTITY_EQUIPMENT) { event.getPacketType() == Server.ENTITY_EQUIPMENT) {
event.setCancelled(true); event.setCancelled(true);
} else if (event.getPacketType() == Server.ENTITY_STATUS) { } else if (event.getPacketType() == Server.ENTITY_STATUS) {
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() && event.getPacket() if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() &&
.getBytes().read(0) == 2) { packet.getBytes().read(0) == 2) {
event.setCancelled(true); event.setCancelled(true);
} }
} }