Catch errors and cancel, fix some disguises

This commit is contained in:
libraryaddict 2016-07-07 22:10:58 +12:00
parent d98333f0f1
commit 37395a1a6d
3 changed files with 205 additions and 123 deletions

View File

@ -15,6 +15,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Ambient;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
@ -24,7 +25,9 @@ import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.EulerAngle;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.MinecraftKey;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
@ -908,6 +911,58 @@ public class ReflectionManager
return null;
}
private static Object convertInvalidItem(Object value)
{
if (value instanceof Optional)
{
Optional opt = (Optional) value;
if (!opt.isPresent())
return value;
Object val = opt.get();
if (val instanceof ItemStack)
{
return Optional.of(getNmsItem((ItemStack) val));
}
else if (val instanceof BlockPosition)
{
BlockPosition pos = (BlockPosition) val;
try
{
return Optional.of(getNmsConstructor("BlockPosition", int.class, int.class, int.class).newInstance(pos.getX(),
pos.getY(), pos.getZ()));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else if (value instanceof EulerAngle)
{
EulerAngle angle = (EulerAngle) value;
try
{
return getNmsConstructor("Vector3f", float.class, float.class, float.class).newInstance((float) angle.getX(),
(float) angle.getY(), (float) angle.getZ());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (value instanceof BlockFace)
{
return getEnumDirection(((BlockFace) value).ordinal());
}
return value;
}
/**
* This creates a DataWatcherItem usable with WrappedWatchableObject
*
@ -920,6 +975,8 @@ public class ReflectionManager
if (value == null)
return null;
value = convertInvalidItem(value);
Serializer serializer;
if (value instanceof Optional)
@ -933,6 +990,12 @@ public class ReflectionManager
serializer = Registry.get(value.getClass());
}
if (serializer == null)
{
throw new IllegalArgumentException(
"Unable to find Serializer for " + value + "! Are you running the latest version of ProtocolLib?");
}
WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer);
Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class);

View File

@ -51,7 +51,18 @@ public class PacketListenerMain extends PacketAdapter
if (entity == observer)
return;
PacketContainer[][] packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity);
PacketContainer[][] packets;
try
{
packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity);
}
catch (Exception ex)
{
ex.printStackTrace();
event.setCancelled(true);
return;
}
if (packets == null)
{

View File

@ -42,6 +42,8 @@ public class PacketListenerViewDisguises extends PacketAdapter
if (event.isCancelled())
return;
try
{
final Player observer = event.getPlayer();
if (observer.getName().contains("UNKNOWN[")) // If the player is temporary
@ -189,4 +191,10 @@ public class PacketListenerViewDisguises extends PacketAdapter
}
}
}
catch (Exception ex)
{
event.setCancelled(true);
ex.printStackTrace();
}
}
}