Skip to content

Commit

Permalink
🚑️ Events deprecated + 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusSlover committed Jan 3, 2025
1 parent 6493734 commit ecc95dd
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 112 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

# 💻 For Developers

This library is designed to work specifically with PaperMC version 1.21.1.<br/>
This library is designed to work specifically with PaperMC version 1.21.3.<br/>
Running on Java 21. Use with other Minecraft versions at your own risk.

Based on our testing, version 4.2.0 of Plus is the most compatible with older versions of Minecraft.
If you are using a Minecraft version other than 1.21.1, we recommend using Plus version 4.3.1.
For Minecraft 1.21.1 or newer, use the latest version of Plus for optimal compatibility.
If you are using a Minecraft version other than 1.21.3, we recommend using Plus version 4.3.1.
For Minecraft 1.21.3 or newer, use the latest version of Plus for optimal compatibility.

## 🐘 Gradle
### Groovy
Expand All @@ -23,7 +23,7 @@ repositories {
}
dependencies {
implementation 'com.marcusslover:plus:4.3.1-SNAPSHOT'
implementation 'com.marcusslover:plus:4.3.3-SNAPSHOT'
}
```
### Kotlin
Expand All @@ -33,7 +33,7 @@ repositories {
}

dependencies {
implementation("com.marcusslover:plus:4.3.1-SNAPSHOT")
implementation("com.marcusslover:plus:4.3.3-SNAPSHOT")
}
```
## 🦢 Maven
Expand All @@ -46,6 +46,6 @@ dependencies {
<dependency>
<groupId>com.marcusslover</groupId>
<artifactId>plus</artifactId>
<version>4.3.1-SNAPSHOT</version>
<version>4.3.3-SNAPSHOT</version>
</dependency>
```
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ repositories {
}

dependencies {
compileOnly 'org.jetbrains:annotations:24.0.0'
compileOnly 'io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT'
compileOnly 'com.comphenix.protocol:ProtocolLib:4.8.0'
compileOnly 'org.jetbrains:annotations:26.0.1'
compileOnly 'io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT'
compileOnly 'com.comphenix.protocol:ProtocolLib:5.3.0'

compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok:1.18.34'
compileOnly 'org.projectlombok:lombok:1.18.36'
annotationProcessor 'org.projectlombok:lombok:1.18.36'

testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.4'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.4'
testImplementation 'io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT'
}

group 'com.marcusslover'
version '4.3.1-SNAPSHOT'
version '4.3.3-SNAPSHOT'
ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

java {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/marcusslover/plus/lib/events/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.function.Consumer;

// not sure if this is a good idea
@Deprecated
public class Events {

/**
Expand Down
194 changes: 97 additions & 97 deletions src/main/java/com/marcusslover/plus/lib/item/MenuManager.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.marcusslover.plus.lib.item;

import com.marcusslover.plus.lib.events.EventReference;
import com.marcusslover.plus.lib.events.Events;
import com.marcusslover.plus.lib.item.Button.DetectableArea;
import com.marcusslover.plus.lib.item.Canvas.ItemDecorator;
import com.marcusslover.plus.lib.item.event.PlayerMenuOpenEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
Expand All @@ -19,138 +20,138 @@
import java.util.List;
import java.util.Set;

public final class MenuManager {
public final class MenuManager implements Listener {
private final @NotNull Plugin plugin;
private final @NotNull List<@NotNull Menu> menus = new LinkedList<>();

private final @NotNull EventReference<InventoryCloseEvent> closeEvent;
private final @NotNull EventReference<InventoryClickEvent> clickEvent;

// Global click context
private @Nullable Canvas.GenericClick genericClick = null;

public MenuManager(@NotNull Plugin plugin) {
this.plugin = plugin;
this.closeEvent = Events.listen(InventoryCloseEvent.class).handler(event -> {
InventoryView view = event.getView();
Inventory inventory = view.getTopInventory();
Player player = (Player) event.getPlayer();
Bukkit.getPluginManager().registerEvents(this, plugin);
}

if (!(inventory.getHolder() instanceof Canvas canvas)) {
return;
}
@EventHandler
public void onClose(InventoryCloseEvent event) {
InventoryView view = event.getView();
Inventory inventory = view.getTopInventory();
Player player = (Player) event.getPlayer();

// empty or invalid inventory shouldn't be handled
if (canvas.assosiatedInventory() == null) return;
if (!(inventory.getHolder() instanceof Canvas canvas)) {
return;
}

// handle the close event action
Canvas.ClickContext closeInventory = canvas.closeInventory();
if (closeInventory != null) {
Canvas.CloseInventory function = closeInventory.closeInventory();
if (function != null) {
try {
function.onClose(player, event, canvas);
} catch (Throwable e) {
if (closeInventory.throwableConsumer() != null) {
closeInventory.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
// empty or invalid inventory shouldn't be handled
if (canvas.assosiatedInventory() == null) return;

// handle the close event action
Canvas.ClickContext closeInventory = canvas.closeInventory();
if (closeInventory != null) {
Canvas.CloseInventory function = closeInventory.closeInventory();
if (function != null) {
try {
function.onClose(player, event, canvas);
} catch (Throwable e) {
if (closeInventory.throwableConsumer() != null) {
closeInventory.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
}
}
}

Menu menu = canvas.assosiatedMenu();
Canvas remove = menu.canvasMap().remove(player.getUniqueId());
if (remove != null) {
remove.clear(); // help the gc
}
menu.close(canvas, player); // call the close method so developers can handle it
Menu menu = canvas.assosiatedMenu();
Canvas remove = menu.canvasMap().remove(player.getUniqueId());
if (remove != null) {
remove.clear(); // help the gc
}
menu.close(canvas, player); // call the close method so developers can handle it
}

}).asRegistered(plugin);
@EventHandler
public void onClick(InventoryClickEvent event) {
InventoryView view = event.getView();
Inventory inventory = view.getTopInventory();

this.clickEvent = Events.listen(InventoryClickEvent.class).handler(event -> {
InventoryView view = event.getView();
Inventory inventory = view.getTopInventory();
if (!(inventory.getHolder() instanceof Canvas canvas)) {
return;
}

if (!(inventory.getHolder() instanceof Canvas canvas)) {
return;
}
event.setCancelled(true);
int slot = event.getRawSlot();
int size = inventory.getSize();

event.setCancelled(true);
int slot = event.getRawSlot();
int size = inventory.getSize();
Item item = Item.of(event.getCurrentItem());

Item item = Item.of(event.getCurrentItem());
// handle the generic click context (global)
if (this.genericClick != null) {
try {
this.genericClick.onClick((Player) event.getWhoClicked(), item, event, canvas);
} catch (Throwable e) {
Bukkit.getLogger().warning(e.getMessage());
}
}

// handle the generic click context (global)
if (this.genericClick != null) {
Canvas.ClickContext genericClick = canvas.genericClick();
if (genericClick != null) {
Canvas.ButtonClick buttonClick = genericClick.click();
if (buttonClick != null) {
try {
this.genericClick.onClick((Player) event.getWhoClicked(), item, event, canvas);
buttonClick.onClick((Player) event.getWhoClicked(), item, event, canvas);
} catch (Throwable e) {
Bukkit.getLogger().warning(e.getMessage());
if (genericClick.throwableConsumer() != null) {
genericClick.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
}
}
}

Canvas.ClickContext genericClick = canvas.genericClick();
if (genericClick != null) {
Canvas.ButtonClick buttonClick = genericClick.click();
if (slot > size) {
Canvas.ClickContext selfInventory = canvas.selfInventory();
if (selfInventory != null) {
Canvas.ButtonClick buttonClick = selfInventory.click();
if (buttonClick != null) {
try {
buttonClick.onClick((Player) event.getWhoClicked(), item, event, canvas);
} catch (Throwable e) {
if (genericClick.throwableConsumer() != null) {
genericClick.throwableConsumer().accept(e);
if (selfInventory.throwableConsumer() != null) {
selfInventory.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
}
}
}
return;
}

if (slot > size) {
Canvas.ClickContext selfInventory = canvas.selfInventory();
if (selfInventory != null) {
Canvas.ButtonClick buttonClick = selfInventory.click();
if (buttonClick != null) {
try {
buttonClick.onClick((Player) event.getWhoClicked(), item, event, canvas);
} catch (Throwable e) {
if (selfInventory.throwableConsumer() != null) {
selfInventory.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
}
canvas.buttons().stream()
.filter(button -> button.within(slot))
.findFirst()
.ifPresent(button -> {
Player player = (Player) event.getWhoClicked();
Canvas.ClickContext context = button.clickContext();
if (context == null) {
return;
}
}
return;
}

canvas.buttons().stream()
.filter(button -> button.within(slot))
.findFirst()
.ifPresent(button -> {
Player player = (Player) event.getWhoClicked();
Canvas.ClickContext context = button.clickContext();
if (context == null) {
return;
}
Canvas.ButtonClick click = context.click();
if (click == null) {
return;
}
try {
click.onClick(player, item, event, canvas);
} catch (Throwable e) {
if (context.throwableConsumer() != null) {
context.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
Canvas.ButtonClick click = context.click();
if (click == null) {
return;
}
try {
click.onClick(player, item, event, canvas);
} catch (Throwable e) {
if (context.throwableConsumer() != null) {
context.throwableConsumer().accept(e);
} else {
Bukkit.getLogger().warning(e.getMessage());
}
});
}).asRegistered(plugin);
}
});
}

@Deprecated
Expand Down Expand Up @@ -370,7 +371,6 @@ private void updateInventory(@NotNull Player player, @NotNull Inventory inventor
*/
public void clearMenus() {
this.menus.clear();
this.clickEvent.unregister();
this.closeEvent.unregister();
HandlerList.unregisterAll(this);
}
}

0 comments on commit ecc95dd

Please sign in to comment.