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

Added DeluxeMenusOpenMenuEvent and DeluxeMenusPreOpenMenuEvent #60

Open
wants to merge 1 commit 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
@@ -0,0 +1,31 @@
package com.extendedclip.deluxemenus.events;

import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.menu.MenuHolder;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;

public class DeluxeMenusOpenMenuEvent extends PlayerEvent {
private static final HandlerList HANDLERS = new HandlerList();
private final MenuHolder holder;

public DeluxeMenusOpenMenuEvent(Player player, MenuHolder holder) {
super(player);
this.holder = holder;
}

public MenuHolder getHolder() {
return holder;
}

public static HandlerList getHandlerList() {
return HANDLERS;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.extendedclip.deluxemenus.events;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;

public class DeluxeMenusPreOpenMenuEvent extends PlayerEvent implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();

private boolean cancelled = false;

public DeluxeMenusPreOpenMenuEvent(Player player) {
super(player);
}

public static HandlerList getHandlerList() {
return HANDLERS;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean b) {
this.cancelled = b;
}
}

12 changes: 12 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.action.ClickHandler;
import com.extendedclip.deluxemenus.dupe.MenuItemMarker;
import com.extendedclip.deluxemenus.events.DeluxeMenusOpenMenuEvent;
import com.extendedclip.deluxemenus.events.DeluxeMenusPreOpenMenuEvent;
import com.extendedclip.deluxemenus.requirement.RequirementList;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.StringUtils;
Expand Down Expand Up @@ -371,6 +373,11 @@ public void openMenu(final Player viewer, final Map<String, String> args, final
return;
}

DeluxeMenusPreOpenMenuEvent preOpenEvent = new DeluxeMenusPreOpenMenuEvent(viewer);
Bukkit.getPluginManager().callEvent(preOpenEvent);

if (preOpenEvent.isCancelled()) return;

final MenuHolder holder = new MenuHolder(viewer);
if (placeholderPlayer != null) {
holder.setPlaceholderPlayer(placeholderPlayer);
Expand Down Expand Up @@ -488,6 +495,11 @@ public void openMenu(final Player viewer, final Map<String, String> args, final
holder.startUpdatePlaceholdersTask();
}
});

Bukkit.getScheduler().runTask(DeluxeMenus.getInstance(), () -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose in doing this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the event runs on the main thread, I think that is the preferable option here. Can be ran async though, see no problem with that. But the event name would have to include Async or note it in any other way.

DeluxeMenusOpenMenuEvent openEvent = new DeluxeMenusOpenMenuEvent(viewer, holder);
Bukkit.getPluginManager().callEvent(openEvent);
});
});
}

Expand Down