Skip to content

Commit

Permalink
Merge pull request #1 from CrazyDev/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
HugoSilvaF authored Nov 12, 2018
2 parents 3994beb + 54739ac commit 85cf4c7
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 174 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# GUI
A simple GUI lib of inventory for minecraft plugins.

Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
173 changes: 71 additions & 102 deletions src/com/gmail/hugosilvaf2/gui/GUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,110 +17,78 @@
package com.gmail.hugosilvaf2.gui;

import com.gmail.hugosilvaf2.gui.lib.GUI;
import com.gmail.hugosilvaf2.gui.lib.GUIObject;
import com.gmail.hugosilvaf2.gui.lib.Result;
import com.gmail.hugosilvaf2.gui.lib.Source;
import com.gmail.hugosilvaf2.gui.lib.listener.InventoryListener;
import com.gmail.hugosilvaf2.gui.lib.sections.Section;
import com.gmail.hugosilvaf2.gui.lib.sections.Sections;
import java.util.ArrayList;
import com.gmail.hugosilvaf2.gui.lib.updater.Updater;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

public class GUIManager
extends ArrayList<GUI>
implements Listener {
public class GUIManager {

private Plugin plugin;
private Sections sections;
private static ConcurrentLinkedQueue<GUI> cache;

public GUIManager(Plugin plugin) {
this.plugin = plugin;
sections = new Sections();
Bukkit.getPluginManager().registerEvents(this, plugin);
private static Plugin plugin;
private static Sections sections;
private static Updater updater;

private static InventoryListener listener;

/**
* Inicia as configurações da lib
* @param p
*/
public static void start(Plugin p) {
plugin = plugin != null ? plugin : p;
cache = cache != null ? cache : new ConcurrentLinkedQueue<>();
sections = sections != null ? sections : new Sections();
updater = updater != null ? updater : new Updater(plugin);
listener = listener != null ? listener : new InventoryListener(plugin);

Bukkit.getPluginManager().registerEvents(listener, plugin);

updater.start();
}

/**
* Obtém as seções
*
* @return
*/
public static Sections getSections(){
return sections;
}

@EventHandler
public void onClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) {
return;
}

final Player whoClicked = (Player) event.getWhoClicked();

if (!(sections.hasSection(whoClicked))) {
return;
}
Section section = sections.getSection(whoClicked);
if ((event.getCurrentItem() != null) && (!event.getCurrentItem().getType().equals(Material.AIR))) {
ItemStack currentItem = event.getCurrentItem();
final GUIObject guio = section.getNowPage().get(event.getSlot());

if ((guio != null) && (guio.getIcon().equals(currentItem))) {
event.setCancelled(guio.isCancelClick());
Result result = guio.getOnClick().click(new Source(whoClicked, section, event.getClick(), new boolean[]{event.isLeftClick(), event.isRightClick(), event.isShiftClick()}));
if (result.equals(Result.NEXT_PAGE)) {
section.nextPage();
return;
}
if (result.equals(Result.PREVIOUS_PAGE)) {
section.previousPage();
return;
}
if (result.equals(Result.OPEN_NEW)) {
whoClicked.closeInventory();
BukkitRunnable runnable = new BukkitRunnable() {
public void run() {
openGUI(whoClicked, guio.getOpenNewGUI());
}
};
runnable.runTaskLater(plugin, 5L);
return;
}
if (result.equals(Result.NOTHING)) {
return;
}
if (result.equals(Result.CLOSE)) {
whoClicked.closeInventory();
sections.remove(section);
return;
}
}
}
// to cancel move and place items for GUI
if (!section.getNowPage().moveItems()) {
if (section.compareTo(event.getInventory())) {
if (event.getRawSlot() >= (section.getInventory().getSize() - 1) && event.getRawSlot() <= (section.getInventory().getSize() - 1) + 36) {
if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
event.setCancelled(true);
}
}

if (event.getRawSlot() >= 0 && event.getRawSlot() <= (section.getInventory().getSize() - 1)) {
if (event.getAction() == InventoryAction.PLACE_ALL || event.getAction() == InventoryAction.PLACE_ONE) {
event.setCancelled(true);
}
}
}
}
/**
* Obtém o updater
*
* @return
*/
public static Updater getUpdater() {
return updater;
}

/**
* Registra um GUI
*
* @param gui
* @return
*/
public static void register(GUI gui) {
cache.add(gui);
}

@EventHandler
public void onClose(InventoryCloseEvent event) {
if (((event.getPlayer() instanceof Player))
&& (sections.hasSection((Player) event.getPlayer()))) {
sections.remove(sections.getSection((Player) event.getPlayer()));
}
/**
* Remove o registro
*
* @param gui
* @return
*/
public static void remove(GUI gui) {
cache.remove(gui);
}

/**
Expand All @@ -129,8 +97,8 @@ public void onClose(InventoryCloseEvent event) {
* @param player
* @param name
*/
public void openGUI(Player player, String name) {
openGUI(player, getGUI(name));
public static void openToPlayer(Player player, String name) {
openToPlayer(player, getGUI(name));
}

/**
Expand All @@ -139,7 +107,7 @@ public void openGUI(Player player, String name) {
* @param player
* @param gui
*/
public void openGUI(Player player, GUI gui) {
public static void openToPlayer(Player player, GUI gui) {
Section section = new Section(gui, player, Bukkit.createInventory(player, gui.getFirst().size(), gui.getTitle()));
section.updateInventory();
sections.add(section);
Expand All @@ -152,28 +120,29 @@ public void openGUI(Player player, GUI gui) {
* @param name
* @return
*/
public GUI getGUI(String name) {
public static GUI getGUI(String name) {
return getOptionalGUI(name).get();
}


/**
* Obtém o Optional<GUI>
* Checa através do nome se o GUI existe, se sim retorna true, se não
* retorna false.
*
* @param name
* @return
*/
public Optional<GUI> getOptionalGUI(String name) {
return stream().filter(n -> n.getName().equals(name)).findFirst();
public static boolean existGUI(String name) {
return getOptionalGUI(name).isPresent();
}

/**
* Checa através do nome se o GUI existe, se sim retorna true, se não
* retorna false.
* Obtém o Optional<GUI>
*
* @param name
* @return
*/
public boolean existGUI(String name) {
return getOptionalGUI(name).isPresent();
public static Optional<GUI> getOptionalGUI(String name) {
return cache.stream().filter(n -> n.getName().equals(name)).findFirst();
}
}
2 changes: 1 addition & 1 deletion src/com/gmail/hugosilvaf2/gui/lib/GUIObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public abstract class GUIObject {

public interface OnClick {
public static interface OnClick {

Result click(Source scr);
}
Expand Down
43 changes: 35 additions & 8 deletions src/com/gmail/hugosilvaf2/gui/lib/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public class Pages

public static abstract class Page {

private GUIObject[] guios = new GUIObject[9];
public static interface OnUpdate {

public void update();
}

private GUIObject[] guiObjects = new GUIObject[9];
private OnUpdate updater;

// remover isso, deixar como tutorial no git, procurar como fazer sem o evento
boolean moveItems = false;

/**
Expand All @@ -37,7 +44,7 @@ public static abstract class Page {
* @return
*/
public Page addGUIObject(int index, GUIObject guio) {
guios[index] = guio;
guiObjects[index] = guio;
return this;
}

Expand All @@ -59,7 +66,7 @@ public Page addGUIObject(GUIObject guio) {
* @return
*/
public GUIObject get(int index) {
return guios[index];
return guiObjects[index];
}

/**
Expand All @@ -69,14 +76,23 @@ public GUIObject get(int index) {
* @return
*/
public GUIObject getGUIObject(ItemStack is) {
for (GUIObject object : guios) {
for (GUIObject object : guiObjects) {
if (is.equals(object.getIcon())) {
return object;
}
}
return null;
}

/**
* Obtém o updater
*
* @return
*/
public OnUpdate getUpdater() {
return updater;
}

/**
* Setar se pode mover os items no inventário True para sim, e false
* para não
Expand All @@ -99,7 +115,18 @@ public Page setMoveItems(boolean b) {
* @return
*/
public Page setSize(int i) {
this.guios = new GUIObject[i];
this.guiObjects = new GUIObject[i];
return this;
}

/**
* Seta o updater
*
* @param updater
* @return
*/
public Page setUpdater(OnUpdate updater) {
this.updater = updater;
return this;
}

Expand All @@ -118,7 +145,7 @@ public boolean moveItems() {
* @return int
*/
public int size() {
return guios.length;
return guiObjects.length;
}

/**
Expand All @@ -128,8 +155,8 @@ public int size() {
* @return int
*/
public int firstEmpty() {
for (int i = 0; i < guios.length; i++) {
if (guios[i] == null) {
for (int i = 0; i < guiObjects.length; i++) {
if (guiObjects[i] == null) {
return i;
}
}
Expand Down
Loading

0 comments on commit 85cf4c7

Please sign in to comment.