Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: {slot} placeholder for MenuItem #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public void setChance(@Nullable final String chance) {
* @return the parsed delay
*/
@SuppressWarnings("UnstableApiUsage")
public long getDelay(@NotNull final MenuHolder holder) {
public long getDelay(@NotNull final MenuHolder holder, int slot) {
if (delay == null || delay.isEmpty()) {
return 0;
}

final var parsed = Longs.tryParse(holder.setPlaceholders(delay));
final var parsed = Longs.tryParse(holder.setPlaceholders(delay, slot));
return parsed == null ? 0 : parsed;
}

Expand All @@ -120,12 +120,12 @@ public long getDelay(@NotNull final MenuHolder holder) {
* @return true if the chance has passed, false otherwise
*/
@SuppressWarnings("UnstableApiUsage")
public boolean checkChance(@NotNull final MenuHolder holder) {
public boolean checkChance(@NotNull final MenuHolder holder, int slot) {
if (chance == null) {
return true;
}

final Double parsedChance = Doubles.tryParse(holder.setPlaceholders(this.chance));
final Double parsedChance = Doubles.tryParse(holder.setPlaceholders(this.chance, slot));
if (parsedChance == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ public class ClickActionTask extends BukkitRunnable {
private final String name;
private final ActionType actionType;
private final String exec;
private final int slot;

public ClickActionTask(
@NotNull final DeluxeMenus plugin,
@NotNull final String name,
@NotNull final ActionType actionType,
@NotNull final String exec
@NotNull final String exec,
@NotNull final int slot
) {
this.plugin = plugin;
this.name = name;
this.actionType = actionType;
this.exec = exec;
this.slot = slot;
}

@Override
Expand All @@ -46,7 +49,7 @@ public void run() {
return;
}

final String executable = PlaceholderAPI.setPlaceholders((OfflinePlayer) player, exec);
final String executable = PlaceholderAPI.setPlaceholders((OfflinePlayer) player, exec.replace("{slot}", String.valueOf(this.slot)));
final MenuHolder holder = Menu.getMenuHolder(player);

switch (actionType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface ClickHandler {

void onClick(@NotNull final MenuHolder menuHolder);
void onClick(@NotNull final MenuHolder menuHolder, int slot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public boolean onCommand(
MenuHolder holder =
Menu.getMenuHolder(target) == null ? new MenuHolder(target) : Menu.getMenuHolder(target);

if (!action.checkChance(holder)) {
if (!action.checkChance(holder, -1)) {
plugin.sms(sender, Messages.CHANCE_FAIL);
return true;
}
Expand All @@ -178,13 +178,14 @@ public boolean onCommand(
plugin,
target.getName(),
action.getType(),
action.getExecutable()
).runTaskLater(plugin, action.getDelay(holder));
action.getExecutable(),
-1
).runTaskLater(plugin, action.getDelay(holder, -1));

plugin.sms(
sender,
Messages.ACTION_TO_BE_EXECUTED.message().replaceText(
AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(action.getDelay(holder))).build())
AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(action.getDelay(holder, -1))).build())
);
return true;
}
Expand All @@ -193,7 +194,8 @@ public boolean onCommand(
plugin,
target.getName(),
action.getType(),
action.getExecutable()
action.getExecutable(),
-1
).runTask(plugin);

plugin.sms(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,11 +1469,11 @@ private ClickHandler getClickHandler(FileConfiguration c, String configPath) {
handler = new ClickHandler() {

@Override
public void onClick(@NotNull final MenuHolder holder) {
public void onClick(@NotNull final MenuHolder holder, int slot) {

for (ClickAction action : actions) {

if (!action.checkChance(holder)) {
if (!action.checkChance(holder, slot)) {
continue;
}

Expand All @@ -1482,16 +1482,18 @@ public void onClick(@NotNull final MenuHolder holder) {
plugin,
holder.getViewer().getName(),
action.getType(),
holder.setArguments(action.getExecutable())
).runTaskLater(plugin, action.getDelay(holder));
holder.setArguments(action.getExecutable()),
slot
).runTaskLater(plugin, action.getDelay(holder, slot));
continue;
}

new ClickActionTask(
plugin,
holder.getViewer().getName(),
action.getType(),
holder.setArguments(action.getExecutable())
holder.setArguments(action.getExecutable()),
slot
).runTask(plugin);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,41 +154,41 @@ public void onClick(InventoryClickEvent event) {
}

if (handleClick(player, holder, item.options().clickHandler(),
item.options().clickRequirements())) {
item.options().clickRequirements(), item.options().slot())) {
return;
}

if (event.isShiftClick() && event.isLeftClick()) {
if (handleClick(player, holder, item.options().shiftLeftClickHandler(),
item.options().shiftLeftClickRequirements())) {
item.options().shiftLeftClickRequirements(), item.options().slot())) {
return;
}
}

if (event.isShiftClick() && event.isRightClick()) {
if (handleClick(player, holder, item.options().shiftRightClickHandler(),
item.options().shiftRightClickRequirements())) {
item.options().shiftRightClickRequirements(), item.options().slot())) {
return;
}
}

if (event.getClick() == ClickType.LEFT) {
if (handleClick(player, holder, item.options().leftClickHandler(),
item.options().leftClickRequirements())) {
item.options().leftClickRequirements(), item.options().slot())) {
return;
}
}

if (event.getClick() == ClickType.RIGHT) {
if (handleClick(player, holder, item.options().rightClickHandler(),
item.options().rightClickRequirements())) {
item.options().rightClickRequirements(), item.options().slot())) {
return;
}
}

if (event.getClick() == ClickType.MIDDLE) {
if (handleClick(player, holder, item.options().middleClickHandler(),
item.options().middleClickRequirements())) {
item.options().middleClickRequirements(), item.options().slot())) {
return;
}
}
Expand All @@ -204,26 +204,26 @@ public void onClick(InventoryClickEvent event) {
*/
private boolean handleClick(final @NotNull Player player, final @NotNull MenuHolder holder,
final @NotNull Optional<ClickHandler> handler,
final @NotNull Optional<RequirementList> requirements) {
final @NotNull Optional<RequirementList> requirements, int slot) {
if (handler.isEmpty()) {
return false;
}

if (requirements.isPresent()) {
final ClickHandler denyHandler = requirements.get().getDenyHandler();

if (!requirements.get().evaluate(holder)) {
if (!requirements.get().evaluate(holder, slot)) {
if (denyHandler == null) {
return true;
}

denyHandler.onClick(holder);
denyHandler.onClick(holder, slot);
return true;
}
}

this.cache.put(player.getUniqueId(), System.currentTimeMillis());
handler.get().onClick(holder);
handler.get().onClick(holder, slot);

return true;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/extendedclip/deluxemenus/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static void closeMenu(final Player p, boolean close, boolean executeClose

if (executeCloseActions) {
if (holder.getMenu().getCloseHandler() != null) {
holder.getMenu().getCloseHandler().onClick(holder);
holder.getMenu().getCloseHandler().onClick(holder, -1);
}
}

Expand Down Expand Up @@ -332,9 +332,9 @@ private boolean handleOpenRequirements(MenuHolder holder) {
return true;
}

if (!openRequirements.evaluate(holder)) {
if (!openRequirements.evaluate(holder, -1)) {
if (openRequirements.getDenyHandler() != null) {
openRequirements.getDenyHandler().onClick(holder);
openRequirements.getDenyHandler().onClick(holder, -1);
}
return false;
}
Expand All @@ -351,9 +351,9 @@ private boolean handleArgRequirements(MenuHolder holder) {
continue;
}

if (!rl.evaluate(holder)) {
if (!rl.evaluate(holder, -1)) {
if (rl.getDenyHandler() != null) {
rl.getDenyHandler().onClick(holder);
rl.getDenyHandler().onClick(holder, -1);
}
return false;
}
Expand Down Expand Up @@ -407,7 +407,7 @@ public void openMenu(final Player viewer, final Map<String, String> args, final

if (item.options().viewRequirements().isPresent()) {

if (item.options().viewRequirements().get().evaluate(holder)) {
if (item.options().viewRequirements().get().evaluate(holder, item.options().slot())) {

activeItems.add(item);
break;
Expand All @@ -428,10 +428,10 @@ public void openMenu(final Player viewer, final Map<String, String> args, final
holder.setActiveItems(activeItems);

if (this.openHandler != null) {
this.openHandler.onClick(holder);
this.openHandler.onClick(holder, -1);
}

String title = StringUtils.color(holder.setPlaceholders(this.menuTitle));
String title = StringUtils.color(holder.setPlaceholders(this.menuTitle, -1));

Inventory inventory;

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/extendedclip/deluxemenus/menu/MenuHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ public Menu getMenu() {
return Menu.getMenu(menuName);
}

public String setPlaceholders(String string) {
public String setPlaceholders(String string, int slot) {
// Set argument placeholders first
if (this.typedArgs != null && !this.typedArgs.isEmpty()) {
for (Entry<String, String> entry : typedArgs.entrySet()) {
string = string.replace("{" + entry.getKey() + "}", entry.getValue());
}
}

string = string.replace("{slot}", String.valueOf(slot));

// Then set actual PAPI placeholders
if (placeholderPlayer != null) {
return PlaceholderAPI.setPlaceholders((OfflinePlayer) placeholderPlayer, string);
Expand Down Expand Up @@ -144,7 +146,7 @@ public void refreshMenu() {

if (item.options().viewRequirements().isPresent()) {

if (item.options().viewRequirements().get().evaluate(this)) {
if (item.options().viewRequirements().get().evaluate(this, item.options().slot())) {
m = true;
active.add(item);
break;
Expand Down Expand Up @@ -242,14 +244,14 @@ public void run() {

if (item.options().dynamicAmount().isPresent()) {
try {
amt = Integer.parseInt(setPlaceholders(item.options().dynamicAmount().get()));
amt = Integer.parseInt(setPlaceholders(item.options().dynamicAmount().get(), item.options().slot()));
if (amt <= 0) {
amt = 1;
}
} catch (Exception exception) {
DeluxeMenus.printStacktrace(
"Something went wrong while updating item in slot " + item.options().slot() +
". Invalid dynamic amount: " + setPlaceholders(item.options().dynamicAmount().get()),
". Invalid dynamic amount: " + setPlaceholders(item.options().dynamicAmount().get(), item.options().slot()),
exception
);
}
Expand All @@ -258,7 +260,7 @@ public void run() {
ItemMeta meta = i.getItemMeta();

if (item.options().displayNameHasPlaceholders() && item.options().displayName().isPresent()) {
meta.setDisplayName(StringUtils.color(setPlaceholders(item.options().displayName().get())));
meta.setDisplayName(StringUtils.color(setPlaceholders(item.options().displayName().get(), item.options().slot())));
}

if (item.options().loreHasPlaceholders()) {
Expand All @@ -267,7 +269,7 @@ public void run() {

for (String line : item.options().lore()) {
updated.add(StringUtils
.color(setPlaceholders(line)));
.color(setPlaceholders(line, item.options().slot())));
}
meta.setLore(updated);
}
Expand Down
Loading