Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/gui manager #2

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading