-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PluginCraft-Solutions/feature/gui-manager
Feature/gui manager
- Loading branch information
Showing
5 changed files
with
108 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters