Last active
July 27, 2017 16:28
-
-
Save cchudant/522c90ad38f77661bb6a014e264a5a67 to your computer and use it in GitHub Desktop.
Quelques méthodes utilitaires pour Spigot/Bukkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Renvoie <code>true</code> si l'inventaire donné possède assez de place pour obtenir | |
| * l'item donné. | |
| * <p> | |
| * Cette méthode utilise l'API StreamAPI de Java8 ainsi que les lambda, pour plus d'information. | |
| * <p> | |
| * Exemple: | |
| * <pre> | |
| * {@code | |
| * //Donne un diamant quand un joueur lance un oeuf. | |
| * //Si aucune place n'est trouvé, le diamant sera jeté aux pieds du joueur | |
| * @EventHandler | |
| * public void onEggThrow(PlayerEggThrowEvent event) | |
| * { | |
| * Player player = event.getPlayer(); | |
| * Inventory inv = player.getInventory(); | |
| * ItemStack item = new ItemStack(Material.DIAMOND); | |
| * | |
| * if (canPickup(inv, item)) //Si le joueur a assez de place pour avoir un diamant, | |
| * { | |
| * inv.addItem(item); //On lui donne le diamant | |
| * } | |
| * else //Sinon, | |
| * { | |
| * player.getWorld().dropItem(player.getLocation(), item); //On jete le diamant aux pieds du joueur | |
| * } | |
| * } | |
| * } | |
| * </pre> | |
| * | |
| * @param inv l'inventaire à tester | |
| * @param item l'item à tester | |
| * @return <code>true</code> si l'inventaire donné possède assez de place pour obtenir | |
| * l'item donné. | |
| * @author <a href="https://github.com/SkyBeastMC">SkyBeast</a> | |
| * @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">Lambda expressions</a> | |
| * @see java.util.stream.Stream | |
| * @see Inventory | |
| */ | |
| public static boolean canPickup(Inventory inv, ItemStack item) | |
| { | |
| //L'inventaire a au moins une place vide, ou au moins un slot peut être stacké avec l'item | |
| return inv.firstEmpty() != -1 || | |
| StreamSupport.stream(inv.spliterator(), false) | |
| .filter(item::isSimilar) | |
| .anyMatch(item2 -> item2.getAmount() + item.getAmount() < item2.getMaxStackSize()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment