Skip to content

Commit

Permalink
v2.5.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Apr 17, 2024
1 parent 2c1394f commit 979705c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>su.nightexpress.nightcore</groupId>
<artifactId>nightcore</artifactId>
<version>2.5.2</version>
<version>2.5.2.1</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package su.nightexpress.nightcore.api.event;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.menu.api.Menu;

public class PlayerOpenMenuEvent extends Event implements Cancellable {

public static final HandlerList HANDLER_LIST = new HandlerList();

private final Player player;
private final Menu menu;

private boolean cancelled;

public PlayerOpenMenuEvent(@NotNull Player player, @NotNull Menu menu) {
this.player = player;
this.menu = menu;
}

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

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

@NotNull
public Player getPlayer() {
return player;
}

@NotNull
public Menu getMenu() {
return menu;
}

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

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
6 changes: 6 additions & 0 deletions src/main/java/su/nightexpress/nightcore/menu/api/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ default void flush(@NotNull MenuViewer viewer) {

void flush(@NotNull Player player);

default boolean isViewer(@NotNull Player player) {
return this.getViewer(player) != null;
}

void close();

void close(@NotNull Player player);

boolean close(@NotNull NightCorePlugin plugin);

void runNextTick(@NotNull Runnable runnable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.NightCorePlugin;
import su.nightexpress.nightcore.api.event.PlayerOpenMenuEvent;
import su.nightexpress.nightcore.dialog.Dialog;
import su.nightexpress.nightcore.menu.api.Menu;
import su.nightexpress.nightcore.menu.link.Linked;
Expand Down Expand Up @@ -42,8 +43,7 @@ public static void purge(@NotNull Player player) {
Menu menu = getMenu(player);
if (menu == null) return;

menu.close();
PLAYER_MENUS.remove(player.getUniqueId());
menu.close(player);
}

protected final P plugin;
Expand Down Expand Up @@ -86,10 +86,22 @@ public void clear() {

@Override
public void close() {
new HashSet<>(this.getViewers()).forEach(viewer -> viewer.getPlayer().closeInventory());
new HashSet<>(this.getViewers()).forEach(viewer -> this.close(viewer.getPlayer()));
this.viewers.clear();
}

@Override
public void close(@NotNull Player player) {
Menu current = getMenu(player);

if (current == this && player.getOpenInventory().getType() != InventoryType.CRAFTING) {
player.closeInventory();
}
else {
this.onClose(player);
}
}

@Override
public boolean close(@NotNull NightCorePlugin plugin) {
if (this.plugin == plugin) {
Expand Down Expand Up @@ -128,6 +140,13 @@ public boolean open(@NotNull MenuViewer viewer) {
public boolean open(@NotNull Player player) {
if (!this.canOpen(player)) return false;

PlayerOpenMenuEvent event = new PlayerOpenMenuEvent(player, this);
this.plugin.getPluginManager().callEvent(event);
if (event.isCancelled()) {
//this.close(player);
return false;
}

MenuOptions options = new MenuOptions(this.getOptions());
MenuViewer viewer = this.getViewerOrCreate(player);

Expand Down Expand Up @@ -190,14 +209,18 @@ public void onDrag(@NotNull MenuViewer viewer, @NotNull InventoryDragEvent event

@Override
public void onClose(@NotNull MenuViewer viewer, @NotNull InventoryCloseEvent event) {
Player player = viewer.getPlayer();
this.onClose(viewer.getPlayer());
}

this.getViewersMap().remove(player.getUniqueId());
this.getItems().removeIf(menuItem -> menuItem.getOptions().canBeDestroyed(viewer));
public void onClose(@NotNull Player player) {
MenuViewer viewer = this.viewers.remove(player.getUniqueId());
if (viewer != null) {
this.getItems().removeIf(menuItem -> menuItem.getOptions().canBeDestroyed(viewer));
}
PLAYER_MENUS.remove(player.getUniqueId());

// Do not clear link if entered Editor, so it can reopen menu without data loss when done.
if (this instanceof Linked<?> linked && !Dialog.contains(player)) {
if (viewer != null && this instanceof Linked<?> linked && !Dialog.contains(player)) {
linked.getLink().clear(viewer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ public static UniSound of(@NotNull Sound sound, float volume, float pitch) {
public static UniSound read(@NotNull FileConfig cfg, @NotNull String path) {
String soundName = ConfigValue.create(path + ".Name", "null",
"Sound name. You can use Spigot sound names, or ones from your resource pack.",
"Spigot Sounds: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html").read(cfg);
"Spigot Sounds: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html"
).read(cfg);

float volume = ConfigValue.create(path + ".Volume", 0.8F,
"Sound volume. From 0.0 to 1.0.").read(cfg).floatValue();
float volume = ConfigValue.create(path + ".Volume", 0.8D,
"Sound volume. From 0.0 to 1.0."
).read(cfg).floatValue();

float pitch = ConfigValue.create(path + ".Pitch", 1D,
"Sound speed. From 0.5 to 2.0").read(cfg).floatValue();
"Sound speed. From 0.5 to 2.0"
).read(cfg).floatValue();

Sound soundType = StringUtil.getEnum(soundName, Sound.class).orElse(null);

Expand Down

0 comments on commit 979705c

Please sign in to comment.