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,122 +1268,131 @@ 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) {
// Code replace them back onto their original scoreboard team return;
Scoreboard scoreboard = player.getScoreboard(); }
Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
Team ldTeam = null;
for (Team t : scoreboard.getTeams()) { // Code replace them back onto their original scoreboard team
if (!t.hasEntry(player.getName())) Scoreboard scoreboard = player.getScoreboard();
continue; Team team = originalTeam == null ? null : scoreboard.getTeam(originalTeam);
Team ldTeam = null;
ldTeam = t; for (Team t : scoreboard.getTeams()) {
break; if (!t.hasEntry(player.getName()))
} continue;
if (DisguiseConfig.isWarnScoreboardConflict()) { ldTeam = t;
if (ldTeam == null || !ldTeam.getName().equals(teamDisguise)) { break;
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) { if (DisguiseConfig.isWarnScoreboardConflict()) {
getLogger().warning( if (ldTeam == null || !ldTeam.getName().equals(teamDisguise)) {
"Scoreboard conflict, the collisions for a self disguise player team has been " + getLogger().warning("Scoreboard conflict, the self disguise player was not on the expected team!");
"unexpectedly modifed!"); } 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 != null) {
if (!ldTeam.getName().equals("LD_Pushing") && !ldTeam.getName().endsWith("_LDP")) { if (!ldTeam.getName().equals("LD_Pushing") && !ldTeam.getName().endsWith("_LDP")) {
// Its not a team assigned by Lib's Disguises // Its not a team assigned by Lib's Disguises
ldTeam = null; ldTeam = null;
}
} }
}
if (team != null) { if (team != null) {
team.addEntry(player.getName()); team.addEntry(player.getName());
} else if (ldTeam != null) { } else if (ldTeam != null) {
ldTeam.removeEntry(player.getName()); ldTeam.removeEntry(player.getName());
} }
if (ldTeam != null && ldTeam.getEntries().isEmpty()) { if (ldTeam != null && ldTeam.getEntries().isEmpty()) {
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) {
// Code to stop player pushing return;
Scoreboard scoreboard = player.getScoreboard(); }
Team prevTeam = null;
Team ldTeam = null;
String ldTeamName = "LD_Pushing";
for (Team t : scoreboard.getTeams()) { // Code to stop player pushing
if (!t.hasEntry(player.getName())) Scoreboard scoreboard = player.getScoreboard();
continue; Team prevTeam = null;
Team ldTeam = null;
String ldTeamName = "LD_Pushing";
prevTeam = t; for (Team t : scoreboard.getTeams()) {
break; 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 prevTeam = t;
if (prevTeam != null && !(prevTeam.getName().equals("LD_Pushing") || prevTeam.getName().endsWith("_LDP"))) { break;
// 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;
}
// 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 (pOption == DisguisePushing.CREATE_SCOREBOARD) {
// If they have a team, we'll reuse that name. Otherwise go for another name // Remember his old team so we can give him it back later
ldTeamName = (prevTeam == null ? "No Team" : prevTeam.getName()); preDisguiseTeam.put(player.getUniqueId(), prevTeam.getName());
} else {
// Give the teamname a custom name // We're modifying the scoreboard
ldTeamName = ldTeamName.substring(0, Math.min(12, ldTeamName.length())) + "_LDP"; ldTeam = prevTeam;
} }
} else {
prevTeam = null;
}
if (ldTeam == null && (ldTeam = scoreboard.getTeam(ldTeamName)) == null) { // If we are creating a new scoreboard because the current one must not be modified
ldTeam = scoreboard.registerNewTeam(ldTeamName); 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())) if (ldTeam.canSeeFriendlyInvisibles() && DisguiseConfig.isDisableFriendlyInvisibles()) {
ldTeam.addEntry(player.getName()); ldTeam.setCanSeeFriendlyInvisibles(false);
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);
}
} }
} }
@ -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.
LibsPackets transformed = PacketsManager.transformPacket(event.getPacket(), disguise, observer, observer);
if (transformed.isUnhandled()) {
transformed.getPackets().add(event.getPacket());
} }
transformed.setPacketType(event.getPacketType()); // 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);
for (PacketContainer packet : transformed.getPackets()) { if (transformed.isUnhandled()) {
if (packet.getType() != Server.PLAYER_INFO) { transformed.getPackets().add(packet);
if (packet.equals(event.getPacket())) { }
packet = packet.shallowClone();
transformed.setSpawnPacketCheck(event.getPacketType());
for (PacketContainer newPacket : transformed.getPackets()) {
if (newPacket.getType() != Server.PLAYER_INFO) {
if (newPacket.equals(packet)) {
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);
} }
} }