diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 88ac1a1..2ad3b1a 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,5 +1,4 @@ -name: Java CI with Maven - +name: tests on: push: branches: @@ -7,22 +6,22 @@ on: pull_request: branches: - main - + - development jobs: - build: - + run_tests: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up JDK 1.8 - uses: actions/setup-java@v2 + - name: Checkout the repository + uses: actions/checkout@v2 + - name: Set up JDK 8 + uses: actions/setup-java@v1 with: - java-version: '1.8' - - - name: Build with Maven - run: mvn -B clean package --file pom.xml - - - name: Run tests - run: mvn test --file pom.xml + java-version: 8 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Run tests with Maven + run: mvn -B test --file pom.xml \ No newline at end of file diff --git a/src/main/java/mcapi/davidout/manager/gui/GuiHolder.java b/src/main/java/mcapi/davidout/manager/gui/GuiHolder.java new file mode 100644 index 0000000..132ce12 --- /dev/null +++ b/src/main/java/mcapi/davidout/manager/gui/GuiHolder.java @@ -0,0 +1,24 @@ +package mcapi.davidout.manager.gui; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +import java.util.UUID; + +public class GuiHolder implements InventoryHolder { + + private final UUID uuid; + + public GuiHolder(UUID inventoryUUID) { + this.uuid = inventoryUUID; + } + + public UUID getInventoryUUID() { + return this.uuid; + } + + @Override + public Inventory getInventory() { + return null; + } +} diff --git a/src/main/java/mcapi/davidout/manager/gui/GuiManager.java b/src/main/java/mcapi/davidout/manager/gui/GuiManager.java index 1054cf2..085e72e 100644 --- a/src/main/java/mcapi/davidout/manager/gui/GuiManager.java +++ b/src/main/java/mcapi/davidout/manager/gui/GuiManager.java @@ -1,71 +1,57 @@ package mcapi.davidout.manager.gui; -import mcapi.davidout.utils.TextUtils; -import org.bukkit.Bukkit; -import org.bukkit.entity.HumanEntity; import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; -import java.util.ArrayList; -import java.util.EventListener; -import java.util.List; -import java.util.regex.Pattern; +import java.util.*; -public class GuiManager implements IGuiManager, EventListener { - - private List guis; - private Pattern VARIABLE_PATTERN; +public class GuiManager implements IGuiManager, Listener { + private final List guis; public GuiManager() { this.guis = new ArrayList<>(); - this.VARIABLE_PATTERN = Pattern.compile("\\{\\w+\\}"); + } + + + @Override + public void registerGui(IGui gui) { + this.guis.add(gui); } @Override public List getGuis() { - return guis; + return this.guis; } + @EventHandler public void onClick(InventoryClickEvent e) { - Inventory clickedInvetory = e.getClickedInventory(); - HumanEntity entity = e.getWhoClicked(); - - if (entity.getOpenInventory().getTopInventory().equals(entity.getInventory())) { + if(e.getView().getTopInventory() == null) { return; } - IGui gui = guis.stream().filter(iGui -> namesMatch( - formatTitle( iGui.getTitle() ), - formatTitle( clickedInvetory.getTitle() ) - ) - ).findFirst().orElse(null); + Inventory topInventory = e.getView().getTopInventory(); + if (!(topInventory.getHolder() instanceof GuiHolder)) { + return; // or handle the case when the holder is not a GuiHolder + } + + GuiHolder holder = (GuiHolder) topInventory.getHolder(); + IGui openGui = this.guis.stream().filter(gui -> + gui.getUUID().equals(holder.getInventoryUUID()) + ).findFirst() + .orElse(null); - if(gui == null) { + if(openGui == null) { return; } - this.openInventory(entity, gui); - } - private String formatTitle(String title) { - return TextUtils.formatColorCodes(title); + openGui.onClick(e); } - private boolean namesMatch(String originalName, String nameToCheck) { - List variableIndexes = TextUtils.getVariableIndexes(originalName, VARIABLE_PATTERN); - return TextUtils.removeVariables( - variableIndexes, originalName - ).equals( - TextUtils.removeVariables( - variableIndexes, nameToCheck - ) - ); - } - private void openInventory(HumanEntity entity, IGui gui) { - Inventory inv = Bukkit.createInventory(null, gui.getRows() * 9, formatTitle(gui.getTitle())); - } + } diff --git a/src/main/java/mcapi/davidout/manager/gui/IGui.java b/src/main/java/mcapi/davidout/manager/gui/IGui.java index ef910c0..320ae62 100644 --- a/src/main/java/mcapi/davidout/manager/gui/IGui.java +++ b/src/main/java/mcapi/davidout/manager/gui/IGui.java @@ -1,13 +1,49 @@ package mcapi.davidout.manager.gui; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; +import java.util.UUID; public abstract class IGui { - public abstract String getTitle(); + private final UUID identifier; + + public IGui(UUID inventoryUUID) { + this.identifier = inventoryUUID; + } + + public IGui() { + this.identifier = UUID.randomUUID(); + } + + public void openInventory(Player p, Object... arguments) { + Inventory inventory = this.createInventory( + Bukkit.createInventory(new GuiHolder(this.identifier), this.getRows() * 9, this.getTitle(p, arguments)), + p, + arguments + ); + + p.openInventory(inventory); + } + + public boolean isGui(Inventory inventory) { + return inventory.getHolder() instanceof GuiHolder && ((GuiHolder)inventory).getInventoryUUID().equals(this.identifier); + } + + public abstract String getTitle(Player p, Object... arguments); + public abstract int getRows(); - public abstract void createInventory(Object... args); + public abstract Inventory createInventory(Inventory inventory, Player player, Object... objects); + + public abstract void onClick(InventoryClickEvent e); + + public UUID getUUID() { + return this.identifier; + } + } diff --git a/src/main/java/mcapi/davidout/manager/gui/IGuiManager.java b/src/main/java/mcapi/davidout/manager/gui/IGuiManager.java index 8c3d333..582fd8e 100644 --- a/src/main/java/mcapi/davidout/manager/gui/IGuiManager.java +++ b/src/main/java/mcapi/davidout/manager/gui/IGuiManager.java @@ -1,9 +1,13 @@ package mcapi.davidout.manager.gui; +import org.bukkit.entity.Player; + import java.util.List; +import java.util.Objects; public interface IGuiManager { + public void registerGui(IGui gui); List getGuis();