org.bukkit.craftbukkit.entity.CraftPlayer Java Examples

The following examples show how to use org.bukkit.craftbukkit.entity.CraftPlayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TitleRespawn.java    From CardinalPGM with MIT License 6 votes vote down vote up
public void sendArmorStandPacket(Player player) {
    EntityPlayer nmsPlayer = ((CraftPlayer) player).getHandle();
    Location loc = player.getLocation();
    PacketPlayOutSpawnEntityLiving spawnPacket = new PacketPlayOutSpawnEntityLiving(
            Integer.MAX_VALUE, UUID.randomUUID(),     // Entity id and Entity UUID
            30,                                       // Entity type id (ArmorStand)
            loc.getX(), loc.getY() - 1.1D, loc.getZ(),// X, Y and Z Position
            0, 0, 0,                                  // X, Y and Z Motion
            (byte)2, (byte)0, (byte)2,                // Yaw, Pitch and Head Pitch
            Watchers.toList(Watchers.INVISIBLE)       // Metadata
    );
    PacketUtils.sendPacket(player, spawnPacket);
    // Create a packet to send 0 max health attribute, so that health doesn't display
    PacketUtils.sendPacket(player, PacketUtils.createHealthAttribute(Integer.MAX_VALUE));
    PacketUtils.sendPacket(player, new PacketPlayOutMount(Integer.MAX_VALUE, nmsPlayer.getId()));
    player.sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(""));
}
 
Example #2
Source File: CraftMapView.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public boolean removeRenderer(MapRenderer renderer) {
    if (renderers.contains(renderer)) {
        renderers.remove(renderer);
        for (Map.Entry<CraftPlayer, CraftMapCanvas> entry : canvases.get(renderer).entrySet()) {
            for (int x = 0; x < 128; ++x) {
                for (int y = 0; y < 128; ++y) {
                    entry.getValue().setPixel(x, y, (byte) -1);
                }
            }
        }
        canvases.remove(renderer);
        return true;
    } else {
        return false;
    }
}
 
Example #3
Source File: TitleRespawn.java    From CardinalPGM with MIT License 6 votes vote down vote up
private void playDeathAnimation(final Player player) {
    Bukkit.getScheduler().scheduleSyncDelayedTask(Cardinal.getInstance(), new Runnable() {
        @Override
        public void run() {
            EntityPlayer nmsPlayer = ((CraftPlayer) player).getHandle();

            List<Packet> packets = new ArrayList<>();
            for (EnumItemSlot slot : EnumItemSlot.values()) {
                packets.add(new PacketPlayOutEntityEquipment(nmsPlayer.getId(), slot,
                        net.minecraft.server.ItemStack.a));  // Removes armor, otherwise, a client-side glitch makes items
            }
            packets.add(PacketUtils.createMetadataPacket(nmsPlayer.getId(), Watchers.getHealth(0)));
            packets.add(new PacketPlayOutEntityStatus(nmsPlayer, (byte) 3));

            for (Player online : Bukkit.getOnlinePlayers()) {
                if (!online.equals(player)){
                    for (Packet packet : packets) {
                        PacketUtils.sendPacket(online, packet);
                    }
                }
            }
        }
    }, 1L);
}
 
Example #4
Source File: TitleRespawn.java    From CardinalPGM with MIT License 6 votes vote down vote up
private void sendWorldBorderPacket(Player player, int warningBlocks) {
    EntityPlayer nmsPlayer = ((CraftPlayer) player).getHandle();
    WorldBorder playerWorldBorder = nmsPlayer.world.getWorldBorder();
    PacketPlayOutWorldBorder worldBorder = new PacketPlayOutWorldBorder(playerWorldBorder, PacketPlayOutWorldBorder.EnumWorldBorderAction.SET_WARNING_BLOCKS);

    try {
        Field field = worldBorder.getClass().getDeclaredField("i");
        field.setAccessible(true);
        field.setInt(worldBorder, warningBlocks);
        field.setAccessible(!field.isAccessible());
    } catch (Exception e) {
        e.printStackTrace();
    }

    PacketUtils.sendPacket(player, worldBorder);
}
 
Example #5
Source File: VanillaCommandWrapper.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
private static net.minecraft.command.ICommandSender getListener(CommandSender sender)
{
    if ( sender instanceof CraftPlayer )
    {
        return new PlayerListener( ( (CraftPlayer) sender ).getHandle() );
    }
    if ( sender instanceof CraftBlockCommandSender )
    {
        CraftBlockCommandSender commandBlock = (CraftBlockCommandSender) sender;
        Block block = commandBlock.getBlock();
        return ( (net.minecraft.tileentity.TileEntityCommandBlock) ( (CraftWorld) block.getWorld() ).getTileEntityAt( block.getX(), block.getY(), block.getZ() ) ).func_145993_a();
    }
    if ( sender instanceof CraftMinecartCommand )
    {
        return ( (net.minecraft.entity.EntityMinecartCommandBlock) ( (CraftMinecartCommand) sender ).getHandle() ).func_145822_e();
    }
    return new ConsoleListener(sender); // Assume console/rcon
}
 
Example #6
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayerMP victim, List<org.bukkit.inventory.ItemStack> drops, String deathMessage, boolean keepInventory) {
    CraftPlayer entity = victim.getBukkitEntity();
    PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
    event.setKeepInventory(keepInventory);
    org.bukkit.World world = entity.getWorld();
    Bukkit.getServer().getPluginManager().callEvent(event);

    victim.keepLevel = event.getKeepLevel();
    victim.newLevel = event.getNewLevel();
    victim.newTotalExp = event.getNewTotalExp();
    victim.expToDrop = event.getDroppedExp();
    victim.newExp = event.getNewExp();

    if (event.getKeepInventory()) {
        return event;
    }

    for (org.bukkit.inventory.ItemStack stack : event.getDrops()) {
        if (stack == null || stack.getType() == Material.AIR) continue;

        world.dropItemNaturally(entity.getLocation(), stack);
    }

    return event;
}
 
Example #7
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public static Container callInventoryOpenEvent(EntityPlayerMP player, Container container, boolean cancelled) {
    if (player.openContainer != player.inventoryContainer) { // fire INVENTORY_CLOSE if one already open
        player.connection.processCloseWindow(new CPacketCloseWindow(player.openContainer.windowId));
    }

    CraftServer server = player.world.getServer();
    CraftPlayer craftPlayer = player.getBukkitEntity();
    player.openContainer.transferTo(container, craftPlayer);

    InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView());
    event.setCancelled(cancelled);
    server.getPluginManager().callEvent(event);

    if (event.isCancelled()) {
        container.transferTo(player.openContainer, craftPlayer);
        return null;
    }

    return container;
}
 
Example #8
Source File: VanillaCommandWrapper.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
private net.minecraft.command.ICommandSender getListener(CommandSender sender) {
    if (sender instanceof Player) {
        return ((CraftPlayer) sender).getHandle();
    }
    if (sender instanceof BlockCommandSender) {
        return ((CraftBlockCommandSender) sender).getTileEntity();
    }
    if (sender instanceof CommandMinecart) {
        return ((net.minecraft.entity.EntityMinecartCommandBlock) ((CraftMinecartCommand) sender).getHandle()).func_145822_e();
    }
    if (sender instanceof RemoteConsoleCommandSender) {
        return net.minecraft.network.rcon.RConConsoleSource.instance;
    }
    if (sender instanceof ConsoleCommandSender) {
        return ((CraftServer) sender.getServer()).getServer();
    }
    return null;
}
 
Example #9
Source File: CraftMapView.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
public boolean removeRenderer(MapRenderer renderer) {
    if (renderers.contains(renderer)) {
        renderers.remove(renderer);
        for (Map.Entry<CraftPlayer, CraftMapCanvas> entry : canvases.get(renderer).entrySet()) {
            for (int x = 0; x < 128; ++x) {
                for (int y = 0; y < 128; ++y) {
                    entry.getValue().setPixel(x, y, (byte) -1);
                }
            }
        }
        canvases.remove(renderer);
        return true;
    } else {
        return false;
    }
}
 
Example #10
Source File: VanillaCommandWrapper.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
private ICommandSender getListener(CommandSender sender) {
    if (sender instanceof Player) {
        return ((CraftPlayer) sender).getHandle();
    }
    if (sender instanceof BlockCommandSender) {
        return ((CraftBlockCommandSender) sender).getTileEntity();
    }
    if (sender instanceof CommandMinecart) {
        return ((EntityMinecartCommandBlock) ((CraftMinecartCommand) sender).getHandle()).getCommandBlockLogic();
    }
    if (sender instanceof RemoteConsoleCommandSender) {
        return ((DedicatedServer) MinecraftServer.getServerCB()).rconConsoleSource;
    }
    if (sender instanceof ConsoleCommandSender) {
        return ((CraftServer) sender.getServer()).getServer();
    }
    if (sender instanceof ProxiedCommandSender) {
        return ((ProxiedNativeCommandSender) sender).getHandle();
    }
    if (sender instanceof CraftFunctionCommandSender) {
        return ((CraftFunctionCommandSender) sender).getHandle();
    }
    throw new IllegalArgumentException("Cannot make " + sender + " a vanilla command listener");
}
 
Example #11
Source File: NMSHacks.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void showFakeItems(Plugin plugin, Player viewer, Location location, org.bukkit.inventory.ItemStack item, int count, Duration duration) {
    if(count <= 0) return;

    final EntityPlayer nmsPlayer = ((CraftPlayer) viewer).getHandle();
    final int[] entityIds = new int[count];

    for(int i = 0; i < count; i++) {
        final EntityItem entity = new EntityItem(nmsPlayer.getWorld(), location.getX(), location.getY(), location.getZ(), CraftItemStack.asNMSCopy(item));

        entity.motX = randomEntityVelocity();
        entity.motY = randomEntityVelocity();
        entity.motZ = randomEntityVelocity();

        sendPacket(viewer, new PacketPlayOutSpawnEntity(entity, ENTITY_TYPE_IDS.get(org.bukkit.entity.Item.class)));
        sendPacket(viewer, new PacketPlayOutEntityMetadata(entity.getId(), entity.getDataWatcher(), true));

        entityIds[i] = entity.getId();
    }

    scheduleEntityDestroy(plugin, viewer.getUniqueId(), duration, entityIds);
}
 
Example #12
Source File: CraftMapView.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public void addRenderer(MapRenderer renderer) {
    if (!renderers.contains(renderer)) {
        renderers.add(renderer);
        canvases.put(renderer, new HashMap<CraftPlayer, CraftMapCanvas>());
        renderer.initialize(this);
    }
}
 
Example #13
Source File: CraftEventFactory.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
public static net.minecraft.inventory.Container callInventoryOpenEvent(net.minecraft.entity.player.EntityPlayerMP player, net.minecraft.inventory.Container container, boolean closeInv) {
    if (player.openContainer != player.inventoryContainer && closeInv) { // fire INVENTORY_CLOSE if one already open
// Cauldron end
        player.playerNetServerHandler.processCloseWindow(new net.minecraft.network.play.client.C0DPacketCloseWindow(player.openContainer.windowId));
    }

    CraftServer server = player.worldObj.getServer();
    CraftPlayer craftPlayer = player.getBukkitEntity();
    // Cauldron start - vanilla compatibility
    try {
        player.openContainer.transferTo(container, craftPlayer);
    }
    catch (AbstractMethodError e) {
        // do nothing
    }
    // Cauldron end
    InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView());
    if (container.getBukkitView() != null) server.getPluginManager().callEvent(event); // Cauldron - allow vanilla mods to bypass

    if (event.isCancelled()) {
        container.transferTo(player.openContainer, craftPlayer);
        // Cauldron start - handle close for modded containers
        if (!closeInv) { // fire INVENTORY_CLOSE if one already open
            player.openContainer = container; // make sure the right container is processed
            player.closeScreen();
            player.openContainer = player.inventoryContainer;
        }
        // Cauldron end
        return null;
    }

    return container;
}
 
Example #14
Source File: CraftMapView.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
public void addRenderer(MapRenderer renderer) {
    if (!renderers.contains(renderer)) {
        renderers.add(renderer);
        canvases.put(renderer, new HashMap<CraftPlayer, CraftMapCanvas>());
        renderer.initialize(this);
    }
}
 
Example #15
Source File: NMSHacks.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void useEntity(Player user, Entity target, boolean attack, EquipmentSlot hand) {
    ((CraftPlayer) user).getHandle().playerConnection.a(new PacketPlayInUseEntity(
        target.getEntityId(),
        attack ? PacketPlayInUseEntity.EnumEntityUseAction.ATTACK : PacketPlayInUseEntity.EnumEntityUseAction.INTERACT,
        null,
        hand == EquipmentSlot.OFF_HAND ? EnumHand.OFF_HAND : EnumHand.MAIN_HAND
    ));
}
 
Example #16
Source File: NMSHacks.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void playDeathAnimation(Player player) {
    EntityPlayer handle = ((CraftPlayer) player).getHandle();
    PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(handle.getId(), handle.getDataWatcher(), false);

    // Add/replace health to zero
    boolean replaced = false;
    DataWatcher.Item<Float> zeroHealth = new DataWatcher.Item<>(EntityLiving.HEALTH, 0f);

    if(packet.b != null) {
        for(int i = 0; i < packet.b.size(); i++) {
            DataWatcher.Item<?> item = packet.b.get(i);
            if(EntityLiving.HEALTH.equals(item.a())) {
                packet.b.set(i, zeroHealth);
                replaced = true;
            }
        }
    }

    if(!replaced) {
        if(packet.b == null) {
            packet.b = Collections.singletonList(zeroHealth);
        } else {
            packet.b.add(zeroHealth);
        }
    }

    sendPacketToViewers(player, packet);
}
 
Example #17
Source File: CraftWorld.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public void setFullTime(long time) {
    world.setWorldTime(time);

    // Forces the client to update to the new time immediately
    for (Player p : getPlayers()) {
        CraftPlayer cp = (CraftPlayer) p;
        if (cp.getHandle().connection == null) {
            continue;
        }

        cp.getHandle().connection.sendPacket(new SPacketTimeUpdate(cp.getHandle().world.getTotalWorldTime(), cp.getHandle().getPlayerTime(), cp.getHandle().world.getGameRules().getBoolean("doDaylightCycle")));
    }
}
 
Example #18
Source File: CraftContainer.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean getCanCraft(EntityPlayer entityhuman) {
    if (cachedType == view.getType() && cachedSize == getSize() && cachedTitle.equals(view.getTitle())) {
        return true;
    }
    // If the window type has changed for some reason, update the player
    // This method will be called every tick or something, so it's
    // as good a place as any to put something like this.
    boolean typeChanged = (cachedType != view.getType());
    cachedType = view.getType();
    cachedTitle = view.getTitle();
    if (view.getPlayer() instanceof CraftPlayer) {
        CraftPlayer player = (CraftPlayer) view.getPlayer();
        String type = getNotchInventoryType(cachedType);
        IInventory top = ((CraftInventory) view.getTopInventory()).getInventory();
        InventoryPlayer bottom = (InventoryPlayer) ((CraftInventory) view.getBottomInventory()).getInventory();
        this.inventoryItemStacks.clear();
        this.inventorySlots.clear();
        if (typeChanged) {
            setupSlots(top, bottom, player.getHandle());
        }
        int size = getSize();
        player.getHandle().connection.sendPacket(new SPacketOpenWindow(this.windowId, type, new TextComponentString(cachedTitle), size));
        player.updateInventory();
    }
    return true;
}
 
Example #19
Source File: CraftServer.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public com.destroystokyo.paper.profile.PlayerProfile createProfile(@Nullable UUID uuid, @Nullable String name) {
    Player player = uuid != null ? Bukkit.getPlayer(uuid) : (name != null ? Bukkit.getPlayerExact(name) : null);
    if (player != null) {
        return new com.destroystokyo.paper.profile.CraftPlayerProfile((CraftPlayer) player);
    }
    return new com.destroystokyo.paper.profile.CraftPlayerProfile(uuid, name);
}
 
Example #20
Source File: CraftServer.java    From Thermos with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<CraftPlayer> getOnlinePlayers() {
    return ImmutableList.copyOf(this.playerView);
}
 
Example #21
Source File: PacketUtils.java    From CardinalPGM with MIT License 4 votes vote down vote up
public static void sendPacket(Player player, Packet packet) {
    if (player == null) return;
    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
 
Example #22
Source File: ItemKit.java    From CardinalPGM with MIT License 4 votes vote down vote up
public static void setPlayerItem(Player player, int slot, ItemStack item) {
    ((CraftPlayer)player).getHandle().c(slot, CraftItemStack.asNMSCopy(item));
}
 
Example #23
Source File: PlayerBoundingBox.java    From CardinalPGM with MIT License 4 votes vote down vote up
@EventHandler
public void onZombieAttack(PlayerUseUnknownEntityEvent event) {
    if (shouldShow(event.getPlayer()) && event.isAttack() && this.zombieID.contains(event.getEntityId())) {
        ((CraftPlayer)event.getPlayer()).getHandle().attack(((CraftPlayer)Bukkit.getPlayer(player)).getHandle());
    }
}
 
Example #24
Source File: CraftInventoryPlayer.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setItem(int index, ItemStack item) {
    super.setItem(index, item);
    if (this.getHolder() == null) return;
    EntityPlayerMP player = ((CraftPlayer) this.getHolder()).getHandle();
    if (player.connection == null) return;
    // PacketPlayOutSetSlot places the items differently than setItem()
    //
    // Between, and including, index 9 (the first index outside of the hotbar) and index 35 (the last index before
    // armor slots) both PacketPlayOutSetSlot and setItem() places the items in the player's inventory the same way.
    // Index 9 starts at the upper left corner of the inventory and moves to the right as it increases. When it
    // reaches the end of the line it goes back to the left side of the new line in the inventory. Basically, it
    // follows the path your eyes would follow as you read a book.
    //
    // The player's hotbar is indexed 0-8 in setItem(). The order goes: 0-8 hotbar, 9-35 normal inventory, 36 boots,
    // 37 leggings, 38 chestplate, and 39 helmet. For indexes > 39 an ArrayIndexOutOfBoundsException will be thrown.
    //
    // PacketPlayOutSetSlot works very differently. Slots 0-8 are as follows: 0 crafting output, 1-4 crafting input,
    // 5 helmet, 6 chestplate, 7 leggings, and 8 boots. Then, 9-35 work exactly the same as setItem(). The hotbar
    // for PacketPlayOutSetSlot starts at index 36, and continues to index 44. Items placed where index is < 0 or
    // > 44 have no action. Basically, the upper part of the player's inventory (crafting area and armor slots) is
    // the first "row" of 9 slots for PacketPlayOutSetSlot. From there the rows work as normal, from left to right
    // all the way down, including the hotbar.
    //
    // With this in mind, we have to modify the index we give PacketPlayOutSetSlot to match the index we intended
    // with setItem(). First, if the index is 0-8, we need to add 36, or 4 rows worth of slots, to the index. This
    // will push the item down to the correct spot in the hotbar.
    //
    // Now when index is > 35 (if index > 39 an ArrayIndexOutOfBoundsException will be thrown, so we need not worry
    // about it) then we need to reset the index, and then count backwards  from the "top" of the inventory. That is
    // to say, we first find (index - 36), which will give us the index required for the armor slots. Now, we need
    // to reverse the order of the index from 8. That means we need 0 to correspond to 8, 1 to correspond to 7,
    // 2 to correspond to 6, and 3 to correspond to 5. We do this simply by taking the result of (index - 36) and
    // subtracting that value from 8.
    if (index < InventoryPlayer.getHotbarSize()) {
        index += 36;
    } else if (index > 39) {
        index += 5; // Off hand
    } else if (index > 35) {
        index = 8 - (index - 36);
    }
    player.connection.sendPacket(new SPacketSetSlot(player.inventoryContainer.windowId, index, CraftItemStack.asNMSCopy(item)));
}
 
Example #25
Source File: CraftInventoryPlayer.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public void setHeldItemSlot(int slot) {
    Validate.isTrue(slot >= 0 && slot < InventoryPlayer.getHotbarSize(), "Slot is not between 0 and 8 inclusive");
    this.getInventory().currentItem = slot;
    ((CraftPlayer) this.getHolder()).getHandle().connection.sendPacket(new SPacketHeldItemChange(slot));
}
 
Example #26
Source File: PlayerTabEntry.java    From CardinalPGM with MIT License 4 votes vote down vote up
private static Property getPlayerSkin(Player player) {
    return getPlayerSkin(((CraftPlayer) player).getProfile());
}
 
Example #27
Source File: PlayerTabEntry.java    From CardinalPGM with MIT License 4 votes vote down vote up
public int getPing() {
    return ((CraftPlayer) player).getHandle().ping;
}
 
Example #28
Source File: PlayerTabEntry.java    From CardinalPGM with MIT License 4 votes vote down vote up
private String getDisplayName() {
    // The method is broken, removes black color, https://hub.spigotmc.org/jira/browse/SPIGOT-2711
    //return player.getPlayerListName();
    return CraftChatMessage.fromComponent(((CraftPlayer) player).getHandle().listName, EnumChatFormat.WHITE);
}
 
Example #29
Source File: CraftServer.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<CraftPlayer> getOnlinePlayers() {
    return this.playerView;
}
 
Example #30
Source File: CraftScoreboardManager.java    From Thermos with GNU General Public License v3.0 4 votes vote down vote up
public CraftScoreboard getPlayerBoard(CraftPlayer player) {
    CraftScoreboard board = playerBoards.get(player);
    return (CraftScoreboard) (board == null ? getMainScoreboard() : board);
}