Skip to content

Commit

Permalink
Merge pull request #2 from PluginCraft-Solutions/feature/gui-manager
Browse files Browse the repository at this point in the history
Feature/gui manager
  • Loading branch information
DavidOut03 authored May 8, 2024
2 parents 6e72477 + 10cfc1f commit fce09bd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 59 deletions.
33 changes: 16 additions & 17 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
name: Java CI with Maven

name: tests
on:
push:
branches:
- main
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
24 changes: 24 additions & 0 deletions src/main/java/mcapi/davidout/manager/gui/GuiHolder.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
66 changes: 26 additions & 40 deletions src/main/java/mcapi/davidout/manager/gui/GuiManager.java
Original file line number Diff line number Diff line change
@@ -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<IGui> guis;
private Pattern VARIABLE_PATTERN;
public class GuiManager implements IGuiManager, Listener {

private final List<IGui> 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<IGui> 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<Integer> 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()));
}

}
40 changes: 38 additions & 2 deletions src/main/java/mcapi/davidout/manager/gui/IGui.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
4 changes: 4 additions & 0 deletions src/main/java/mcapi/davidout/manager/gui/IGuiManager.java
Original file line number Diff line number Diff line change
@@ -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<IGui> getGuis();


Expand Down

0 comments on commit fce09bd

Please sign in to comment.