Skip to content

Commit

Permalink
Changed Menu to AdvancedMenu in onClicked event. Added new Logger met…
Browse files Browse the repository at this point in the history
…hods.
  • Loading branch information
Rubix327 committed May 28, 2022
1 parent b104a16 commit 72d1319
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 479 deletions.
42 changes: 35 additions & 7 deletions src/main/java/org/mineacademy/fo/Logger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mineacademy.fo;

import lombok.Getter;
import lombok.Setter;
import org.mineacademy.fo.exception.CommandException;

/**
Expand All @@ -8,33 +10,59 @@
* <ul>
* <li>It does not require all messages to be String.</li>
* <li>It appends the message with a prefix.<br>
* This prefix is empty by default. You can change it by calling Logger.LOG_PREFIX.</li>
* This prefix is empty by default. You can change it by calling {@link #setLogPrefix}.</li>
* </ul>
*/
public class Logger {

public static String LOG_PREFIX = "";
@Getter @Setter
public static String logPrefix = "";

/**
* Log an info message to the console.
* @param s the message
*/
public static void info(Object s){ Common.log(LOG_PREFIX + s); }
public static void info(Object s){ Common.log(logPrefix + s); }

/**
* Log an info message without a prefix.
* @param s the message
*/
public static void infoNoPrefix(Object s){
Common.logNoPrefix(s.toString());
}

/**
* See {@link Common#logFramed}
* @param s the message
*/
public static void infoFramed(Object s){
Common.logFramed(logPrefix + s);
}

/**
* See {@link Common#logF}
* @param s the message
* @param args replacements
*/
public static void infoF(Object s, Object... args){
Common.logF(s.toString(), args);
}

/**
* Log a warning to the console.
* @param s the warning message
*/
public static void warning(Object s){
Common.warning("&e" + LOG_PREFIX + s);
Common.warning("&e" + logPrefix + s);
}

/**
* Log an error message to the console.
* @param s the error message
*/
public static void error(Object s){
Common.error(new CommandException(), "&6" + LOG_PREFIX + s);
Common.error(new CommandException(), "&6" + logPrefix + s);
}

/**
Expand All @@ -43,7 +71,7 @@ public static void error(Object s){
* @param s the message
*/
public static void error(Throwable t, Object s){
Common.error(t, "&6" + LOG_PREFIX + s);
Common.error(t, "&6" + logPrefix + s);
}

/**
Expand All @@ -52,7 +80,7 @@ public static void error(Throwable t, Object s){
* @return colorized prefixed message
*/
public static String get(String s){
return Common.colorize(LOG_PREFIX + s);
return Common.colorize(logPrefix + s);
}

}
18 changes: 12 additions & 6 deletions src/main/java/org/mineacademy/fo/menu/AdvancedMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected Button getReturnBackButton(){
protected Button getReturnBackButton(@NotNull ItemStack item){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
newInstanceOf(player, parentMenu).display();
}

Expand Down Expand Up @@ -179,7 +179,7 @@ protected Button getRefreshButton(){
protected Button getRefreshButton(@NotNull ItemStack item){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
SoundUtil.Play.POP(player);
refreshMenu();
}
Expand Down Expand Up @@ -207,7 +207,7 @@ protected Button getMenuButton(Class<? extends AdvancedMenu> to){
protected Button getMenuButton(ItemStack item, Class<? extends AdvancedMenu> to){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
newInstanceOf(player, to).display();
}

Expand Down Expand Up @@ -245,7 +245,7 @@ private AdvancedMenu newInstanceOf(Player player, Class<? extends AdvancedMenu>
protected Button getToolButton(Tool tool){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
if (!player.getInventory().contains(tool.getItem())){
SoundUtil.Play.POP_HIGH(player);
player.getInventory().addItem(tool.getItem());
Expand Down Expand Up @@ -298,13 +298,14 @@ protected Button getInfoButton(){
/**
* 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 #getInfoLore()}. So you can override it and set your custom information.
* This button gets its info from {@link #getInfoName()} and {@link #getInfoLore()}.
* So you can override them and set your custom name and lore.
* @return the button
*/
protected Button getInfoButton(ItemStack item){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
}

@Override
Expand Down Expand Up @@ -381,6 +382,11 @@ protected final void setLockedSlots(String figure){
}
}

@Override
protected void onButtonClick(Player player, int slot, InventoryAction action, ClickType click, Button button) {
super.onButtonClick(player, slot, action, click, button);
}

@Override
protected void onMenuClick(Player player, int slot, InventoryAction action, ClickType click, ItemStack cursor, ItemStack clicked, boolean cancelled) {
if (getButtons().containsKey(slot)){
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mineacademy/fo/menu/AdvancedMenuPagged.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void setElementsItems(){
private Button formPreviousButton(ItemStack itemStack){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
if (currentPage <= 1) {
CompSound.VILLAGER_NO.play(getViewer());
return;
Expand All @@ -182,7 +182,7 @@ public ItemStack getItem() {
private Button formNextButton(ItemStack itemStack){
return new Button() {
@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
if (currentPage >= getMaxPage()) {
CompSound.VILLAGER_NO.play(getViewer());
return;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mineacademy/fo/menu/MenuPagged.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Button formPreviousButton() {
final boolean canGo = currentPage > 1;

@Override
public void onClickedInMenu(final Player pl, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player pl, final AdvancedMenu menu, final ClickType click) {
if (canGo) {
currentPage = MathUtil.range(currentPage - 1, 1, pages.size());

Expand Down Expand Up @@ -230,7 +230,7 @@ public Button formNextButton() {
final boolean canGo = currentPage < pages.size();

@Override
public void onClickedInMenu(final Player pl, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player pl, final AdvancedMenu menu, final ClickType click) {
if (canGo) {
currentPage = MathUtil.range(currentPage + 1, 1, pages.size());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mineacademy/fo/menu/MenuQuantitable.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ default Button getEditQuantityButton(Menu menu) {
return new Button() {

@Override
public final void onClickedInMenu(Player pl, Menu clickedMenu, ClickType click) {
public final void onClickedInMenu(Player pl, AdvancedMenu clickedMenu, ClickType click) {
setQuantity(click == ClickType.LEFT ? getQuantity().previous() : getQuantity().next());
menu.redraw();

Expand Down
72 changes: 38 additions & 34 deletions src/main/java/org/mineacademy/fo/menu/button/Button.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package org.mineacademy.fo.menu.button;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.mineacademy.fo.conversation.SimpleDecimalPrompt;
import org.mineacademy.fo.menu.AdvancedMenu;
import org.mineacademy.fo.menu.Menu;
import org.mineacademy.fo.menu.model.ItemCreator;
import org.mineacademy.fo.remain.CompMaterial;
import org.mineacademy.fo.settings.SimpleLocalization;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
* Represents a clickable button in menu
Expand All @@ -42,26 +42,6 @@ public abstract class Button {
// Button functions
// ----------------------------------------------------------------

/**
* Called automatically from the button is clicked
*
* @param player
* @param menu
* @param click
*/
public abstract void onClickedInMenu(Player player, Menu menu, ClickType click);

/**
* The item representing this button. Tip: Use {@link ItemCreator} to create it.
*
* @return the item for this button
*/
public abstract ItemStack getItem();

// ----------------------------------------------------------------
// Static methods
// ----------------------------------------------------------------

/**
* Creates a new Nether Star button has no action on clicking
* and it is used purely to display informative text.
Expand Down Expand Up @@ -102,6 +82,10 @@ public static final DummyButton makeDummy(final ItemCreator.ItemCreatorBuilder b
return makeDummy(builder.build());
}

// ----------------------------------------------------------------
// Static methods
// ----------------------------------------------------------------

/**
* Creates a dummy button that does nothing when clicked
*
Expand Down Expand Up @@ -141,7 +125,7 @@ public ItemStack getItem() {
}

@Override
public void onClickedInMenu(final Player player, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player player, final AdvancedMenu menu, final ClickType click) {
onClickFunction.accept(player);
}
};
Expand All @@ -163,7 +147,7 @@ public ItemStack getItem() {
}

@Override
public void onClickedInMenu(final Player player, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player player, final AdvancedMenu menu, final ClickType click) {
onClickFunction.accept(player);
}
};
Expand All @@ -188,7 +172,7 @@ public ItemStack getItem() {
}

@Override
public void onClickedInMenu(final Player player, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player player, final AdvancedMenu menu, final ClickType click) {
onClickFunction.accept(player, click);
}
};
Expand All @@ -209,12 +193,32 @@ public ItemStack getItem() {
}

@Override
public void onClickedInMenu(final Player player, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player player, final AdvancedMenu menu, final ClickType click) {
SimpleDecimalPrompt.show(player, question, successAction);
}
};
}

/**
* Called automatically when the button is clicked
*
* @deprecated use {@link #onClickedInMenu(Player, AdvancedMenu, ClickType)}
*/
@Deprecated
public void onClickedInMenu(Player player, Menu menu, ClickType click){};

/**
* Called automatically when the button is clicked
*/
public abstract void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click);

/**
* The item representing this button. Tip: Use {@link ItemCreator} to create it.
*
* @return the item for this button
*/
public abstract ItemStack getItem();

@Override
public final String toString() {
final ItemStack item = getItem();
Expand Down Expand Up @@ -242,7 +246,7 @@ public static final class DummyButton extends Button {
* Do nothing when clicked
*/
@Override
public void onClickedInMenu(final Player player, final Menu menu, final ClickType click) {
public void onClickedInMenu(final Player player, final AdvancedMenu menu, final ClickType click) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.mineacademy.fo.menu.button;

import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.mineacademy.fo.Valid;
import org.mineacademy.fo.conversation.SimpleConversation;
import org.mineacademy.fo.conversation.SimplePrompt;
import org.mineacademy.fo.menu.Menu;
import org.mineacademy.fo.menu.AdvancedMenu;
import org.mineacademy.fo.menu.model.ItemCreator;
import org.mineacademy.fo.remain.CompMaterial;

import lombok.Getter;

/**
* A button that runs a server conversation
*/
Expand Down Expand Up @@ -104,7 +103,7 @@ private ButtonConversation(SimpleConversation conversation, SimplePrompt prompt,
}

@Override
public void onClickedInMenu(Player player, Menu menu, ClickType click) {
public void onClickedInMenu(Player player, AdvancedMenu menu, ClickType click) {
Valid.checkBoolean(conversation != null || prompt != null, "Conversation and prompt cannot be null!");

if (conversation != null) {
Expand Down
Loading

0 comments on commit 72d1319

Please sign in to comment.