|
| 1 | +package team.unstudio.udpl.util; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.util.UUID; |
| 5 | + |
| 6 | +import javax.annotation.Nonnull; |
| 7 | +import javax.annotation.Nullable; |
| 8 | + |
| 9 | +import org.apache.commons.lang.Validate; |
| 10 | +import org.bukkit.Location; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.inventory.ItemStack; |
| 13 | + |
| 14 | +import com.comphenix.protocol.PacketType; |
| 15 | +import com.comphenix.protocol.ProtocolLibrary; |
| 16 | +import com.comphenix.protocol.ProtocolManager; |
| 17 | +import com.comphenix.protocol.events.PacketContainer; |
| 18 | +import com.google.common.base.Strings; |
| 19 | + |
| 20 | +public final class EntityUtils { |
| 21 | + |
| 22 | + private EntityUtils(){} |
| 23 | + |
| 24 | + private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); |
| 25 | + private static int nextEntityID = Integer.MAX_VALUE; |
| 26 | + |
| 27 | + public Result sendFakeItemEntity(@Nonnull Player player,@Nonnull ItemStack itemStack,@Nonnull Location location,@Nullable String displayName){ |
| 28 | + Validate.notNull(player); |
| 29 | + Validate.notNull(itemStack); |
| 30 | + Validate.notNull(location); |
| 31 | + |
| 32 | + int entityID = nextEntityID--; |
| 33 | + PacketContainer spawnEntityLiving = protocolManager.createPacket(PacketType.Play.Server.SPAWN_ENTITY_LIVING); |
| 34 | + spawnEntityLiving.getIntegers().write(0, entityID); //Entity ID |
| 35 | + spawnEntityLiving.getUUIDs().write(0, UUID.randomUUID()); //Entity UUID |
| 36 | + spawnEntityLiving.getBytes().write(0, (byte) 2); //Entity Type |
| 37 | + spawnEntityLiving.getDoubles().write(0, location.getX()) |
| 38 | + .write(1, location.getY()) |
| 39 | + .write(2, location.getZ()); |
| 40 | + spawnEntityLiving.getBytes().write(1, (byte) 0) |
| 41 | + .write(2, (byte) 0); |
| 42 | + spawnEntityLiving.getIntegers().write(0, 1); //Data |
| 43 | + spawnEntityLiving.getShorts().write(0, (short) 0) |
| 44 | + .write(1, (short) 0) |
| 45 | + .write(2, (short) 0); |
| 46 | + |
| 47 | + PacketContainer entityMetadata = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA); |
| 48 | + entityMetadata.getIntegers().write(0, entityID); |
| 49 | + entityMetadata.getBytes().write(0, (byte) 0); |
| 50 | + entityMetadata.getIntegers().write(1, 300); |
| 51 | + entityMetadata.getStrings().write(0, Strings.nullToEmpty(displayName)); |
| 52 | + entityMetadata.getBooleans().write(0, !Strings.isNullOrEmpty(displayName)) |
| 53 | + .write(1, false) |
| 54 | + .write(2, true); |
| 55 | + entityMetadata.getItemModifier().write(0, itemStack); |
| 56 | + try { |
| 57 | + protocolManager.sendServerPacket(player, spawnEntityLiving); |
| 58 | + protocolManager.sendServerPacket(player, entityMetadata); |
| 59 | + return Result.success(); |
| 60 | + } catch (InvocationTargetException e) { |
| 61 | + return Result.failure(e); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments