public static void handleEditBookEvent(EntityPlayer player, ItemStack newBookItem) { int itemInHandIndex = player.inventory.itemInHandIndex; PlayerEditBookEvent editBookEvent = new PlayerEditBookEvent(player.getBukkitEntity(), player.inventory.itemInHandIndex, (BookMeta) CraftItemStack.getItemMeta(player.inventory.getItemInHand()), (BookMeta) CraftItemStack.getItemMeta(newBookItem), newBookItem.getItem() == Items.WRITTEN_BOOK); player.world.getServer().getPluginManager().callEvent(editBookEvent); ItemStack itemInHand = player.inventory.getItem(itemInHandIndex); // If they've got the same item in their hand, it'll need to be updated. if (itemInHand != null && itemInHand.getItem() == Items.BOOK_AND_QUILL) { if (!editBookEvent.isCancelled()) { CraftItemStack.setItemMeta(itemInHand, editBookEvent.getNewBookMeta()); if (editBookEvent.isSigning()) { itemInHand.setItem(Items.WRITTEN_BOOK); } } // Client will have updated its idea of the book item; we need to overwrite that Slot slot = player.activeContainer.getSlot(player.inventory, itemInHandIndex); player.playerConnection.sendPacket(new PacketPlayOutSetSlot(player.activeContainer.windowId, slot.rawSlotIndex, itemInHand)); } }
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); }
public void detectListNameConflict(EntityPlayer entityPlayer) { // Collisions will make for invisible people for (int i = 0; i < getHandle().players.size(); ++i) { EntityPlayer testEntityPlayer = (EntityPlayer) getHandle().players.get(i); // We have a problem! if (testEntityPlayer != entityPlayer && testEntityPlayer.listName.equals(entityPlayer.listName)) { String oldName = entityPlayer.listName; int spaceLeft = 16 - oldName.length(); if (spaceLeft <= 1) { // We also hit the list name length limit! entityPlayer.listName = oldName.subSequence(0, oldName.length() - 2 - spaceLeft) + String.valueOf(System.currentTimeMillis() % 99); } else { entityPlayer.listName = oldName + String.valueOf(System.currentTimeMillis() % 99); } return; } } }
public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayer 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; }
public static Container callInventoryOpenEvent(EntityPlayer player, Container container) { if (player.activeContainer != player.defaultContainer) { // fire INVENTORY_CLOSE if one already open player.playerConnection.a(new PacketPlayInCloseWindow(player.activeContainer.windowId)); } CraftServer server = player.world.getServer(); CraftPlayer craftPlayer = player.getBukkitEntity(); player.activeContainer.transferTo(container, craftPlayer); InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView()); server.getPluginManager().callEvent(event); if (event.isCancelled()) { container.transferTo(player.activeContainer, craftPlayer); return null; } return container; }
public static PlayerDeathEvent callPlayerDeathEvent(EntityPlayer victim, List<org.bukkit.inventory.ItemStack> drops, String deathMessage) { CraftPlayer entity = victim.getBukkitEntity(); PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage); 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(); for (org.bukkit.inventory.ItemStack stack : event.getDrops()) { if (stack == null || stack.getType() == Material.AIR) continue; world.dropItemNaturally(entity.getLocation(), stack); } return event; }
public static Container callInventoryOpenEvent(EntityPlayer player, Container container) { if (player.activeContainer != player.defaultContainer) { // fire INVENTORY_CLOSE if one already open player.playerConnection.handleContainerClose(new Packet101CloseWindow(player.activeContainer.windowId)); } CraftServer server = player.world.getServer(); CraftPlayer craftPlayer = player.getBukkitEntity(); player.activeContainer.transferTo(container, craftPlayer); InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView()); server.getPluginManager().callEvent(event); if (event.isCancelled()) { container.transferTo(player.activeContainer, craftPlayer); return null; } return container; }
public static void handleEditBookEvent(EntityPlayer player, ItemStack newBookItem) { int itemInHandIndex = player.inventory.itemInHandIndex; PlayerEditBookEvent editBookEvent = new PlayerEditBookEvent(player.getBukkitEntity(), player.inventory.itemInHandIndex, (BookMeta) CraftItemStack.getItemMeta(player.inventory.getItemInHand()), (BookMeta) CraftItemStack.getItemMeta(newBookItem), newBookItem.id == Item.WRITTEN_BOOK.id); player.world.getServer().getPluginManager().callEvent(editBookEvent); ItemStack itemInHand = player.inventory.getItem(itemInHandIndex); // If they've got the same item in their hand, it'll need to be updated. if (itemInHand.id == Item.BOOK_AND_QUILL.id) { if (!editBookEvent.isCancelled()) { CraftItemStack.setItemMeta(itemInHand, editBookEvent.getNewBookMeta()); if (editBookEvent.isSigning()) { itemInHand.id = Item.WRITTEN_BOOK.id; } } // Client will have updated its idea of the book item; we need to overwrite that Slot slot = player.activeContainer.a((IInventory) player.inventory, itemInHandIndex); player.playerConnection.sendPacket(new Packet103SetSlot(player.activeContainer.windowId, slot.g, itemInHand)); } }
/** * Gets the range an entity should be 'tracked' by players and visible in * the client. * * @param entity * @param defaultRange Default range defined by Mojang * @return */ public static int getEntityTrackingRange(Entity entity, int defaultRange) { SpigotWorldConfig config = entity.world.spigotConfig; int range = defaultRange; if ( entity instanceof EntityPlayer ) { range = config.playerTrackingRange; } else if ( entity.defaultActivationState || entity instanceof EntityGhast ) { range = defaultRange; } else if ( entity.activationType == 1 ) { range = config.monsterTrackingRange; } else if ( entity.activationType == 2 ) { range = config.animalTrackingRange; } else if ( entity instanceof EntityItemFrame || entity instanceof EntityPainting || entity instanceof EntityItem || entity instanceof EntityExperienceOrb ) { range = config.miscTrackingRange; } return Math.min( config.maxTrackingRange, range ); }
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); }
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); }
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("")); }
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<>(EntityPlayer.class, 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); }
public EntityPlayer getMojang() { if (this.isOnline()) { return this.getPlayer().getMojang(); } else { return null; } }
public List<String> tabComplete(net.minecraft.server.ICommandListener sender, String message) { if (!(sender instanceof EntityPlayer)) { return ImmutableList.of(); } Player player = ((EntityPlayer) sender).getBukkitEntity(); if (message.startsWith("/")) { return tabCompleteCommand(player, message); } else { return tabCompleteChat(player, message); } }
public void setItemOnCursor(ItemStack item) { net.minecraft.server.ItemStack stack = CraftItemStack.asNMSCopy(item); getHandle().inventory.setCarried(stack); if (this instanceof CraftPlayer) { ((EntityPlayer) getHandle()).broadcastCarriedItem(); // Send set slot for cursor } }
private void openCustomInventory(Inventory inventory, EntityPlayer player, int windowType) { if (player.playerConnection == null) return; Container container = new CraftContainer(inventory, this, player.nextContainerCounter()); container = CraftEventFactory.callInventoryOpenEvent(player, container); if(container == null) return; String title = container.getBukkitView().getTitle(); int size = container.getBukkitView().getTopInventory().getSize(); player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(container.windowId, windowType, title, size, true)); getHandle().activeContainer = container; getHandle().activeContainer.addSlotListener(player); }
public void setHealth(double health) { if ((health < 0) || (health > getMaxHealth())) { throw new IllegalArgumentException("Health must be between 0 and " + getMaxHealth()); } if (entity instanceof EntityPlayer && health == 0) { ((EntityPlayer) entity).die(DamageSource.GENERIC); } getHandle().setHealth((float) health); }
public Player getPlayer() { for (Object obj : server.getHandle().players) { EntityPlayer player = (EntityPlayer) obj; if (player.getUniqueID().equals(getUniqueId())) { return (player.playerConnection != null) ? player.playerConnection.getPlayer() : null; } } return null; }
public static Cancellable handleStatisticsIncrease(EntityHuman entityHuman, net.minecraft.server.Statistic statistic, int current, int incrementation) { Player player = ((EntityPlayer) entityHuman).getBukkitEntity(); Event event; if (statistic instanceof net.minecraft.server.Achievement) { if (current != 0) { return null; } event = new PlayerAchievementAwardedEvent(player, CraftStatistic.getBukkitAchievement((net.minecraft.server.Achievement) statistic)); } else { org.bukkit.Statistic stat = CraftStatistic.getBukkitStatistic(statistic); switch (stat) { case FALL_ONE_CM: case BOAT_ONE_CM: case CLIMB_ONE_CM: case DIVE_ONE_CM: case FLY_ONE_CM: case HORSE_ONE_CM: case MINECART_ONE_CM: case PIG_ONE_CM: case PLAY_ONE_TICK: case SWIM_ONE_CM: case WALK_ONE_CM: // Do not process event for these - too spammy return null; default: } if (stat.getType() == Type.UNTYPED) { event = new PlayerStatisticIncrementEvent(player, stat, current, current + incrementation); } else if (stat.getType() == Type.ENTITY) { EntityType entityType = CraftStatistic.getEntityTypeFromStatistic(statistic); event = new PlayerStatisticIncrementEvent(player, stat, current, current + incrementation, entityType); } else { Material material = CraftStatistic.getMaterialFromStatistic(statistic); event = new PlayerStatisticIncrementEvent(player, stat, current, current + incrementation, material); } } entityHuman.world.getServer().getPluginManager().callEvent(event); return (Cancellable) event; }
@Override public void setItem(int index, ItemStack item) { super.setItem(index, item); if (this.getHolder() == null) return; EntityPlayer player = ((CraftPlayer) this.getHolder()).getHandle(); if (player.playerConnection == 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 < PlayerInventory.getHotbarSize()) index = index + 36; else if (index > 35) index = 8 - (index - 36); player.playerConnection.sendPacket(new PacketPlayOutSetSlot(player.defaultContainer.windowId, index, CraftItemStack.asNMSCopy(item))); }
private void openCustomInventory(Inventory inventory, EntityPlayer player, int windowType) { if (player.playerConnection == null) return; Container container = new CraftContainer(inventory, this, player.nextContainerCounter()); container = CraftEventFactory.callInventoryOpenEvent(player, container); if(container == null) return; String title = container.getBukkitView().getTitle(); int size = container.getBukkitView().getTopInventory().getSize(); player.playerConnection.sendPacket(new Packet100OpenWindow(container.windowId, windowType, title, size, true)); getHandle().activeContainer = container; getHandle().activeContainer.addSlotListener(player); }
public Player getPlayerExact(String name) { Validate.notNull(name, "Name cannot be null"); // Almura start EntityPlayer entityPlayer = playerList.getPlayer(name); return (entityPlayer != null) ? entityPlayer.getBukkitEntity() : null; // Almura end }