Skip to content

Commit

Permalink
Fix plugins with paper-plugin.yml not working on 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
stefvanschie committed Jul 26, 2024
1 parent d6997f9 commit 510704b
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 21 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ updates:
directory: "/nms/abstraction"
schedule:
interval: "daily"
- package-ecosystem: "maven"
directory: "/inventory-view/iv-abstract-class"
schedule:
interval: "daily"
- package-ecosystem: "maven"
directory: "/inventory-view/iv-abstraction"
schedule:
interval: "daily"
- package-ecosystem: "maven"
directory: "/inventory-view/iv-interface"
schedule:
interval: "daily"
18 changes: 18 additions & 0 deletions IF/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>iv-abstraction</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>iv-abstract-class</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>iv-interface</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.stefvanschie.inventoryframework.gui;

import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.UUIDTagType;
import org.bukkit.NamespacedKey;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand Down Expand Up @@ -179,7 +180,8 @@ public void callAction(@NotNull InventoryClickEvent event) {
action.accept(event);
} catch (Throwable t) {
this.logger.log(Level.SEVERE, "Exception while handling click event in inventory '"
+ event.getView().getTitle() + "', slot=" + event.getSlot() + ", item=" + item.getType(), t);
+ InventoryViewUtil.getInstance().getTitle(event.getView()) + "', slot=" + event.getSlot() +
", item=" + item.getType(), t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.github.stefvanschie.inventoryframework.gui.type.*;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -65,15 +67,15 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
}

InventoryView view = event.getView();
Inventory inventory = view.getInventory(event.getRawSlot());
Inventory inventory = InventoryViewUtil.getInstance().getInventory(view, event.getRawSlot());

if (inventory == null) {
gui.callOnOutsideClick(event);
return;
}

gui.callOnGlobalClick(event);
if (inventory.equals(view.getTopInventory())) {
if (inventory.equals(InventoryViewUtil.getInstance().getTopInventory(view))) {
gui.callOnTopClick(event);
} else {
gui.callOnBottomClick(event);
Expand Down Expand Up @@ -221,17 +223,19 @@ public void resetItemsSmithingTable(@NotNull InventoryClickEvent event) {
*/
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onEntityPickupItem(@NotNull EntityPickupItemEvent event) {
if (!(event.getEntity() instanceof HumanEntity)) {
LivingEntity entity = event.getEntity();

if (!(entity instanceof HumanEntity)) {
return;
}

Gui gui = getGui(((HumanEntity) event.getEntity()).getOpenInventory().getTopInventory());
Gui gui = getGui(InventoryViewUtil.getInstance().getTopInventory(((HumanEntity) entity).getOpenInventory()));

if (gui == null || !gui.isPlayerInventoryUsed()) {
return;
}

int leftOver = gui.getHumanEntityCache().add((HumanEntity) event.getEntity(), event.getItem().getItemStack());
int leftOver = gui.getHumanEntityCache().add((HumanEntity) entity, event.getItem().getItemStack());

if (leftOver == 0) {
event.getItem().remove();
Expand Down Expand Up @@ -267,11 +271,11 @@ public void onInventoryDrag(@NotNull InventoryDragEvent event) {
boolean top = false, bottom = false;

for (int inventorySlot : inventorySlots) {
Inventory inventory = view.getInventory(inventorySlot);
Inventory inventory = InventoryViewUtil.getInstance().getInventory(view, inventorySlot);

if (view.getTopInventory().equals(inventory)) {
if (InventoryViewUtil.getInstance().getTopInventory(view).equals(inventory)) {
top = true;
} else if (view.getBottomInventory().equals(inventory)) {
} else if (InventoryViewUtil.getInstance().getBottomInventory(view).equals(inventory)) {
bottom = true;
}

Expand All @@ -291,24 +295,24 @@ public void onInventoryDrag(@NotNull InventoryDragEvent event) {
}
} else {
int index = inventorySlots.toArray(new Integer[0])[0];
InventoryType.SlotType slotType = view.getSlotType(index);
InventoryType.SlotType slotType = InventoryViewUtil.getInstance().getSlotType(view, index);

boolean even = event.getType() == DragType.EVEN;

ClickType clickType = even ? ClickType.LEFT : ClickType.RIGHT;
InventoryAction inventoryAction = even ? InventoryAction.PLACE_SOME : InventoryAction.PLACE_ONE;

ItemStack previousViewCursor = view.getCursor();
ItemStack previousViewCursor = InventoryViewUtil.getInstance().getCursor(view);
// Overwrite getCursor in inventory click event to mimic real event fired by Bukkit.
view.setCursor(event.getOldCursor());
InventoryViewUtil.getInstance().setCursor(view, event.getOldCursor());
//this is a fake click event, firing this may cause other plugins to function incorrectly, so keep it local
InventoryClickEvent inventoryClickEvent = new InventoryClickEvent(view, slotType, index, clickType,
inventoryAction);

onInventoryClick(inventoryClickEvent);
// Restore previous cursor only if someone has not changed it manually in onInventoryClick.
if (Objects.equals(view.getCursor(), event.getOldCursor())) {
view.setCursor(previousViewCursor);
if (Objects.equals(InventoryViewUtil.getInstance().getCursor(view), event.getOldCursor())) {
InventoryViewUtil.getInstance().setCursor(view, previousViewCursor);
}

event.setCancelled(inventoryClickEvent.isCancelled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.gui.type.util.NamedGui;
import com.github.stefvanschie.inventoryframework.pane.Pane;
import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.XMLUtil;
import com.github.stefvanschie.inventoryframework.util.version.Version;
import com.github.stefvanschie.inventoryframework.util.version.VersionMatcher;
Expand Down Expand Up @@ -196,7 +197,7 @@ public void show(@NotNull HumanEntity humanEntity) {
throw new IllegalStateException("Merchant could not be opened");
}

Inventory inventory = view.getTopInventory();
Inventory inventory = InventoryViewUtil.getInstance().getTopInventory(view);

addInventory(inventory, this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.stefvanschie.inventoryframework.exception.XMLReflectionException;
import com.github.stefvanschie.inventoryframework.pane.util.Mask;
import com.github.stefvanschie.inventoryframework.pane.util.Slot;
import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.SkullUtil;
import com.github.stefvanschie.inventoryframework.util.UUIDTagType;
import com.github.stefvanschie.inventoryframework.util.XMLUtil;
Expand Down Expand Up @@ -725,8 +726,8 @@ protected void callOnClick(@NotNull InventoryClickEvent event) {
} catch (Throwable t) {
throw new RuntimeException(
"Exception while handling click event in inventory '"
+ event.getView().getTitle() + "', slot=" + event.getSlot() + ", for "
+ getClass().getSimpleName() + ", x=" + getX() + ", y=" + getY()
+ InventoryViewUtil.getInstance().getTitle(event.getView()) + "', slot=" + event.getSlot() +
", for " + getClass().getSimpleName() + ", x=" + getX() + ", y=" + getY()
+ ", length=" + length + ", height=" + height,
t
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.stefvanschie.inventoryframework.util;

import com.github.stefvanschie.inventoryframework.inventoryview.abstraction.AbstractInventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.version.Version;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A utility class for working with {@link InventoryView}s across different definitions.
*
* @since 0.10.16
*/
public class InventoryViewUtil {

/**
* The underlying implementation.
*/
@Nullable
private static AbstractInventoryViewUtil IMPLEMENTATION;

/**
* Gets the instance of this class to use for the current version.
*
* @return an instance of a utility class
* @since 0.10.16
*/
@NotNull
@Contract(pure = true)
public static AbstractInventoryViewUtil getInstance() {
if (IMPLEMENTATION == null) {
if (Version.getVersion().isInventoryViewInterface()) {
IMPLEMENTATION = com.github.stefvanschie.inventoryframework.inventoryview.interface_.InventoryViewUtil.getInstance();
} else {
IMPLEMENTATION = com.github.stefvanschie.inventoryframework.inventoryview.abstractclass.InventoryViewUtil.getInstance();
}
}

return IMPLEMENTATION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.stefvanschie.inventoryframework.exception.UnsupportedVersionException;
import org.bukkit.Bukkit;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -191,6 +192,17 @@ public enum Version {
V1_19_0, V1_19_1, V1_19_2, V1_19_3, V1_19_4
);

/**
* Checks whether the {@link InventoryView} class is an interface on this version.
*
* @return true if the class is an interface, false otherwise
* @since 0.10.16
*/
@Contract(pure = true)
public boolean isInventoryViewInterface() {
return this == Version.V1_21;
}

/**
* Checks whether modern smithing tables exist on this version. Returns true if they do, otherwise false.
*
Expand Down
47 changes: 47 additions & 0 deletions inventory-view/iv-abstract-class/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>IF-parent</artifactId>
<version>0.10.16-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>iv-abstract-class</artifactId>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.stefvanschie.inventoryframework</groupId>
<artifactId>iv-abstraction</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.6-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<!-- Provided, but not accessible -->
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.stefvanschie.inventoryframework.inventoryview.abstractclass;

import com.github.stefvanschie.inventoryframework.inventoryview.abstraction.AbstractInventoryViewUtil;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A wrapper for {@link InventoryView} methods that apply when {@link InventoryView} was an abstract class.
*
* @since 0.10.16
*/
public class InventoryViewUtil implements AbstractInventoryViewUtil {

/**
* Instance of this singleton class.
*/
@NotNull
private static final InventoryViewUtil INSTANCE = new InventoryViewUtil();

@NotNull
@Override
public Inventory getBottomInventory(@NotNull InventoryView view) {
return view.getBottomInventory();
}

@Nullable
@Override
public ItemStack getCursor(@NotNull InventoryView view) {
return view.getCursor();
}

@Override
public void setCursor(@NotNull InventoryView view, @Nullable ItemStack item) {
view.setCursor(item);
}

@Nullable
@Override
public Inventory getInventory(@NotNull InventoryView view, int slot) {
return view.getInventory(slot);
}

@NotNull
@Override
public InventoryType.SlotType getSlotType(@NotNull InventoryView view, int slot) {
return view.getSlotType(slot);
}

@NotNull
@Override
public String getTitle(@NotNull InventoryView view) {
return view.getTitle();
}

@NotNull
@Override
public Inventory getTopInventory(@NotNull InventoryView view) {
return view.getTopInventory();
}

/**
* Gets the singleton instance of this class.
*
* @return the instance of this class
* @since 0.10.16
*/
@NotNull
@Contract(pure = true)
public static InventoryViewUtil getInstance() {
return INSTANCE;
}
}
Loading

0 comments on commit 510704b

Please sign in to comment.