Compare commits
No commits in common. "d5a2aa1c30ec850cd65d0964d6119b6076f892b7" and "a46e066669a07892ccd3c4b9133fe5b22b2d08d1" have entirely different histories.
d5a2aa1c30
...
a46e066669
@ -173,7 +173,7 @@ public class ChatUtil {
|
|||||||
/**
|
/**
|
||||||
* Do like {@link String#join(CharSequence, Iterable)}, but for components, and the last separator is different from
|
* Do like {@link String#join(CharSequence, Iterable)}, but for components, and the last separator is different from
|
||||||
* the others. It is useful when enumerating things in a sentence, for instance :
|
* the others. It is useful when enumerating things in a sentence, for instance :
|
||||||
* <code>"a thing<u>, </u>a thing<u> and </u>a thing"</code>
|
* <code>"a thing<u>, </u>a thing<u>and </u>a thing"</code>
|
||||||
* (the coma being the usual separator, and {@code " and "} being the final separator).
|
* (the coma being the usual separator, and {@code " and "} being the final separator).
|
||||||
* @param regularSeparator the separator used everywhere except between the two last components to join.
|
* @param regularSeparator the separator used everywhere except between the two last components to join.
|
||||||
* @param finalSeparator the separator used between the two last components to join.
|
* @param finalSeparator the separator used between the two last components to join.
|
||||||
|
@ -1,8 +1,17 @@
|
|||||||
package fr.pandacube.lib.paper.gui;
|
package fr.pandacube.lib.paper.gui;
|
||||||
|
|
||||||
import fr.pandacube.lib.chat.Chat;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||||
|
import net.kyori.adventure.text.ComponentLike;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
@ -12,15 +21,20 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
|
|||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import fr.pandacube.lib.chat.Chat;
|
||||||
import java.util.Map;
|
import fr.pandacube.lib.paper.util.ItemStackBuilder;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An inventory based GUI.
|
* An inventory based GUI.
|
||||||
*/
|
*/
|
||||||
public class GUIInventory implements Listener {
|
public class GUIInventory implements Listener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used as parameter of {@link #buildButton(ItemStack, Integer, ComponentLike, List, Map)} to indicate that a button should
|
||||||
|
* shine like an enchanted object, without showing enchant information in the hover text.
|
||||||
|
*/
|
||||||
|
public static final Map<Enchantment, Integer> FAKE_ENCHANT = ImmutableMap.of(Enchantment.DURABILITY, 1);
|
||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final Inventory inv;
|
private final Inventory inv;
|
||||||
private Consumer<InventoryCloseEvent> onCloseEvent;
|
private Consumer<InventoryCloseEvent> onCloseEvent;
|
||||||
@ -195,6 +209,71 @@ public class GUIInventory implements Listener {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static ItemStack buildButton(ItemStack base, Integer amount, ComponentLike displayName,
|
||||||
|
List<? extends ComponentLike> lores, Map<Enchantment, Integer> enchantments) {
|
||||||
|
|
||||||
|
ItemStackBuilder iStackBuilder = ItemStackBuilder.of(base);
|
||||||
|
|
||||||
|
if (amount != null)
|
||||||
|
iStackBuilder.amount(amount);
|
||||||
|
if (displayName != null)
|
||||||
|
iStackBuilder.displayName(displayName);
|
||||||
|
if (lores != null)
|
||||||
|
iStackBuilder.lore(lores);
|
||||||
|
if (enchantments != null) {
|
||||||
|
if (enchantments == FAKE_ENCHANT)
|
||||||
|
iStackBuilder.fakeEnchant();
|
||||||
|
else {
|
||||||
|
for (Entry<Enchantment, Integer> e : enchantments.entrySet()) {
|
||||||
|
iStackBuilder.enchant(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return iStackBuilder.build();
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(ItemStack base, ComponentLike displayName, List<? extends ComponentLike> lores, Map<Enchantment, Integer> enchantments) {
|
||||||
|
return buildButton(base, null, displayName, lores, enchantments);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(ItemStack base, ComponentLike displayName, Map<Enchantment, Integer> enchantments) {
|
||||||
|
return buildButton(base, null, displayName, null, enchantments);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(ItemStack base, Integer amount, ComponentLike displayName, List<? extends ComponentLike> lores) {
|
||||||
|
return buildButton(base, amount, displayName, lores, null);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(ItemStack base, ComponentLike displayName, List<? extends ComponentLike> lores) {
|
||||||
|
return buildButton(base, null, displayName, lores, null);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(ItemStack base, ComponentLike displayName) {
|
||||||
|
return buildButton(base, null, displayName, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static ItemStack buildButton(Material m, ComponentLike displayName, List<? extends ComponentLike> lores, Map<Enchantment, Integer> enchantments) {
|
||||||
|
return buildButton(new ItemStack(m), null, displayName, lores, enchantments);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(Material m, ComponentLike displayName, Map<Enchantment, Integer> enchantments) {
|
||||||
|
return buildButton(new ItemStack(m), null, displayName, null, enchantments);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(Material m, ComponentLike displayName, List<? extends ComponentLike> lores) {
|
||||||
|
return buildButton(new ItemStack(m), null, displayName, lores, null);
|
||||||
|
}
|
||||||
|
public static ItemStack buildButton(Material m, ComponentLike displayName) {
|
||||||
|
return buildButton(new ItemStack(m), null, displayName, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static int nbLineForNbElements(int nb) {
|
public static int nbLineForNbElements(int nb) {
|
||||||
return nb / 9 + ((nb % 9 == 0) ? 0 : 1);
|
return nb / 9 + ((nb % 9 == 0) ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
@ -123,14 +123,12 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default PaperClientOptions getClientOptions() {
|
PaperClientOptions getClientOptions();
|
||||||
return new PaperClientOptions(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides various configuration values of the Minecraft client.
|
* Provides various configuration values of the Minecraft client.
|
||||||
*/
|
*/
|
||||||
class PaperClientOptions implements AbstractOnlinePlayer.ClientOptions {
|
abstract class PaperClientOptions implements AbstractOnlinePlayer.ClientOptions {
|
||||||
|
|
||||||
private final PaperOnlinePlayer op;
|
private final PaperOnlinePlayer op;
|
||||||
|
|
||||||
@ -200,8 +198,8 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isTextFilteringEnabled() {
|
public boolean isTextFilteringEnabled() { // needs reflection to get the actual value
|
||||||
return op.getBukkitPlayer().getClientOption(ClientOption.TEXT_FILTERING_ENABLED);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -80,6 +80,10 @@ public class ItemStackBuilder {
|
|||||||
return (cachedMeta != null) ? cachedMeta : (cachedMeta = stack.getItemMeta());
|
return (cachedMeta != null) ? cachedMeta : (cachedMeta = stack.getItemMeta());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateMeta() {
|
||||||
|
stack.setItemMeta(cachedMeta);
|
||||||
|
}
|
||||||
|
|
||||||
public ItemStackBuilder meta(Consumer<ItemMeta> metaUpdater) {
|
public ItemStackBuilder meta(Consumer<ItemMeta> metaUpdater) {
|
||||||
return meta(metaUpdater, ItemMeta.class);
|
return meta(metaUpdater, ItemMeta.class);
|
||||||
}
|
}
|
||||||
@ -145,56 +149,29 @@ public class ItemStackBuilder {
|
|||||||
return addLoreAfter(Arrays.asList(lores));
|
return addLoreAfter(Arrays.asList(lores));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Supports unsafe enchants.
|
|
||||||
*/
|
|
||||||
public ItemStackBuilder enchant(Enchantment enchantment, int level) {
|
public ItemStackBuilder enchant(Enchantment enchantment, int level) {
|
||||||
return meta(m -> m.addEnchant(enchantment, level, true));
|
return meta(m -> m.addEnchant(enchantment, level, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackBuilder flags(ItemFlag... flags) {
|
public ItemStackBuilder flags(ItemFlag... flags) {
|
||||||
return flags(true, flags);
|
return meta(m -> m.addItemFlags(flags));
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder flags(boolean add, ItemFlag... flags) {
|
|
||||||
return add ? meta(m -> m.addItemFlags(flags))
|
|
||||||
: meta(m -> m.removeItemFlags(flags));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackBuilder hideEnchants() {
|
public ItemStackBuilder hideEnchants() {
|
||||||
return hideEnchants(true);
|
return flags(ItemFlag.HIDE_ENCHANTS);
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder hideEnchants(boolean hide) {
|
|
||||||
return flags(hide, ItemFlag.HIDE_ENCHANTS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackBuilder hideAttributes() {
|
public ItemStackBuilder hideAttributes() {
|
||||||
return hideAttributes(true);
|
return flags(ItemFlag.HIDE_ATTRIBUTES);
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder hideAttributes(boolean hide) {
|
|
||||||
return flags(hide, ItemFlag.HIDE_ATTRIBUTES);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackBuilder fakeEnchant() {
|
public ItemStackBuilder fakeEnchant() {
|
||||||
return fakeEnchant(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder fakeEnchant(boolean apply) {
|
|
||||||
if (apply) {
|
|
||||||
enchant(Enchantment.DURABILITY, 1);
|
enchant(Enchantment.DURABILITY, 1);
|
||||||
return hideEnchants();
|
return hideEnchants();
|
||||||
}
|
}
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder unbreakable() {
|
public ItemStackBuilder unbreakable() {
|
||||||
return unbreakable(true);
|
return meta(m -> m.setUnbreakable(true));
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStackBuilder unbreakable(boolean unbreakable) {
|
|
||||||
return meta(m -> m.setUnbreakable(unbreakable));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStackBuilder canDestroy(Set<Material> destroyable) {
|
public ItemStackBuilder canDestroy(Set<Material> destroyable) {
|
||||||
|
Loading…
Reference in New Issue
Block a user