Skip to content

Commit

Permalink
Added getInfoButton() implementation. Added tell() to Player methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubix327 committed May 28, 2022
1 parent 76af542 commit b104a16
Showing 3 changed files with 140 additions and 49 deletions.
168 changes: 128 additions & 40 deletions src/main/java/org/mineacademy/fo/menu/AdvancedMenu.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mineacademy.fo.Common;
import org.mineacademy.fo.ItemUtil;
import org.mineacademy.fo.Messenger;
import org.mineacademy.fo.SoundUtil;
import org.mineacademy.fo.menu.button.Button;
import org.mineacademy.fo.menu.model.ItemCreator;
@@ -189,37 +192,10 @@ public ItemStack getItem() {
}

/**
* Make the button from the tool. It gives player one piece of this tool.<br>
* This button gets its additional lore depending on if player has the tool
* in the inventory from {@link #getAlreadyHaveLore()} and {@link #getClickToGetLore()}
* so you can override them to set your custom items and messages.<br>
* Or you can override this whole method to set your custom items and logic.
* @param tool the tool we should give
* @return the button
* See {@link #getMenuButton(ItemStack, Class)}
*/
protected Button getToolButton(Tool tool){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
if (!player.getInventory().contains(tool.getItem())){
SoundUtil.Play.POP_HIGH(player);
player.getInventory().addItem(tool.getItem());
}
else{
SoundUtil.Play.POP_LOW(player);
player.getInventory().removeItem(tool.getItem());
}
refreshMenu();
}

@Override
public ItemStack getItem() {
boolean hasTool = hasItem(player, tool);
List<String> lore = hasTool ? getAlreadyHaveLore() : getClickToGetLore();

return ItemCreator.of(tool.getItem()).lores(lore).glow(hasTool).build().make();
}
};
protected Button getMenuButton(Class<? extends AdvancedMenu> to){
return getMenuButton(MenuUtil.defaultMenuItem, to);
}

/**
@@ -257,6 +233,47 @@ private AdvancedMenu newInstanceOf(Player player, Class<? extends AdvancedMenu>
throw new NullPointerException("Could not create a new instance of " + menu.getName() + " class.");
}

/**
* Make the button from the tool. It gives player one piece of this tool.<br>
* This button gets its additional lore depending on if player has the tool
* in the inventory from {@link #getAlreadyHaveLore()} and {@link #getClickToGetLore()}
* so you can override them to set your custom items and messages.<br>
* Or you can override this whole method to set your custom items and logic.
* @param tool the tool we should give
* @return the button
*/
protected Button getToolButton(Tool tool){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
if (!player.getInventory().contains(tool.getItem())){
SoundUtil.Play.POP_HIGH(player);
player.getInventory().addItem(tool.getItem());
}
else{
SoundUtil.Play.POP_LOW(player);
player.getInventory().removeItem(tool.getItem());
}
refreshMenu();
}

@Override
public ItemStack getItem() {
boolean hasTool = hasItem(player, tool);
List<String> lore = hasTool ? getAlreadyHaveLore() : getClickToGetLore();

return ItemCreator.of(tool.getItem()).lores(lore).glow(hasTool).build().make();
}
};
}

/**
* Checks if the player has the specified tool in the inventory.
*/
private boolean hasItem(Player player, Tool tool){
return player.getInventory().contains(tool.getItem());
}

/**
* Get the additional item lore of the tool if the player already has this tool in the inventory.
*/
@@ -271,25 +288,54 @@ protected List<String> getClickToGetLore(){
return Arrays.asList("", "&2Click to get this item");
}

/**
* See {@link #getInfoButton(ItemStack)}
*/
protected Button getInfoButton(){
return getInfoButton(MenuUtil.defaultInfoItem);
}

/**
* Get the button that shows info about the menu.
* By default, does nothing when clicked, but you can override it and add your behavior.
* This button gets its info from {@link #getInfo()}. So you can override it and set your custom information.
* This button gets its info from {@link #getInfoLore()}. So you can override it and set your custom information.
* @return the button
*/
protected Button getInfoButton(){
protected Button getInfoButton(ItemStack item){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
}

@Override
public ItemStack getItem() {
return Button.makeInfo(getInfo()).getItem();
return ItemCreator.of(item).name(getInfoName()).lores(Arrays.asList(getInfoLore())).hideTags(true).build().make();
}
};
}

/**
* Get the name of the button that shows info about the menu.<br>
* Override it to set your own name.
* @see #getInfoButton(ItemStack)
*/
protected String getInfoName(){
return "&f" + ItemUtil.bountifyCapitalized(getTitle()) + " Menu Information";
}

/**
* Get the lore of the button that shows info about the menu.<br>
* Override it to set your own lore (info).
* @see #getInfoButton(ItemStack)
*/
protected String[] getInfoLore() {
return new String[]{
"",
"&7Override &fgetInfoName() &7and &fgetInfoLore()",
"&7in " + getClass().getSimpleName() + " &7to set your own menu description."
};
}

/**
* For {@link AdvancedMenu}, set the slots that should NOT be filled with {@link #wrapperItem}.<br>
* For {@link AdvancedMenuPagged}, set the slots the main elements can only be placed on.
@@ -335,13 +381,6 @@ protected final void setLockedSlots(String figure){
}
}

/**
* Checks if the player has the specified tool in the inventory.
*/
private boolean hasItem(Player player, Tool tool){
return player.getInventory().contains(tool.getItem());
}

@Override
protected void onMenuClick(Player player, int slot, InventoryAction action, ClickType click, ItemStack cursor, ItemStack clicked, boolean cancelled) {
if (getButtons().containsKey(slot)){
@@ -367,4 +406,53 @@ public ItemStack getItemAt(int slot) {
public AdvancedMenu newInstance() {
return this;
}

/**
* Send a message to the {@link #getPlayer()}
*/
public void tell(String... messages) {
Common.tell(this.player, messages);
}

/**
* Send an information message to the {@link #getPlayer()}
*/
public void tellInfo(String message) {
Messenger.info(this.player, message);
}

/**
* Send a success message to the {@link #getPlayer()}
*/
public void tellSuccess(String message) {
Messenger.success(this.player, message);
}

/**
* Send a warning message to the {@link #getPlayer()}
*/
public void tellWarn(String message) {
Messenger.warn(this.player, message);
}

/**
* Send an error message to the {@link #getPlayer()}
*/
public void tellError(String message) {
Messenger.error(this.player, message);
}

/**
* Send a question message to the {@link #getPlayer()}
*/
public void tellQuestion(String message) {
Messenger.question(this.player, message);
}

/**
* Send an announcement message to the {@link #getPlayer()}
*/
public void tellAnnounce(String message) {
Messenger.announce(this.player, message);
}
}
19 changes: 10 additions & 9 deletions src/main/java/org/mineacademy/fo/menu/Menu.java
Original file line number Diff line number Diff line change
@@ -499,6 +499,7 @@ protected final void redraw() {

/**
* Draws the bottom bar for the player inventory
* Adds the info and the return button.
*/
private Map<Integer, ItemStack> compileBottomBar0() {
final Map<Integer, ItemStack> items = new HashMap<>();
@@ -519,49 +520,49 @@ private Map<Integer, ItemStack> compileBottomBar0() {
/**
* Send a message to the {@link #getViewer()}
*/
public final void tell(String... messages) {
public void tell(String... messages) {
Common.tell(this.viewer, messages);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellInfo(String message) {
public void tellInfo(String message) {
Messenger.info(this.viewer, message);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellSuccess(String message) {
public void tellSuccess(String message) {
Messenger.success(this.viewer, message);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellWarn(String message) {
public void tellWarn(String message) {
Messenger.warn(this.viewer, message);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellError(String message) {
public void tellError(String message) {
Messenger.error(this.viewer, message);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellQuestion(String message) {
public void tellQuestion(String message) {
Messenger.question(this.viewer, message);
}

/**
* Send a message to the {@link #getViewer()}
*/
public final void tellAnnounce(String message) {
public void tellAnnounce(String message) {
Messenger.announce(this.viewer, message);
}

@@ -655,7 +656,7 @@ protected int getInfoButtonPosition() {
*/
@Deprecated
protected boolean addReturnButton() {
return true;
return false;
}

/**
@@ -666,7 +667,7 @@ protected boolean addReturnButton() {
*/
@Deprecated
protected boolean addInfoButton() {
return true;
return false;
}

/**
2 changes: 2 additions & 0 deletions src/main/java/org/mineacademy/fo/menu/MenuUtil.java
Original file line number Diff line number Diff line change
@@ -11,5 +11,7 @@ public class MenuUtil {

public static ItemStack defaultReturnBackItem = ItemCreator.of(CompMaterial.OAK_DOOR, "&7Go back").build().make();
public static ItemStack defaultRefreshItem = ItemCreator.of(CompMaterial.REDSTONE, "&7Refresh menu").build().make();
public static ItemStack defaultInfoItem = ItemCreator.of(CompMaterial.BOOK).build().make();
public static ItemStack defaultMenuItem = ItemCreator.of(CompMaterial.APPLE).build().make();

}

0 comments on commit b104a16

Please sign in to comment.