1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Lets resize jobs browse GUI if jobs can't fit in current one

This commit is contained in:
Zrips 2018-11-12 17:17:28 +02:00
parent 12232cb9fa
commit 1d3b6fc73f
3 changed files with 45 additions and 38 deletions

View File

@ -1,16 +1,18 @@
package com.gamingmesh.jobs.Gui; package com.gamingmesh.jobs.Gui;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List;
import org.bukkit.inventory.Inventory;
import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.Job;
public class GuiInfoList { public class GuiInfoList {
private String name; private String name;
private List<Job> jobList = new ArrayList<>(); private HashMap<Integer, Job> jobList = new HashMap<Integer, Job>();
private Boolean jobInfo = false; private Boolean jobInfo = false;
private int backButton = 27; private int backButton = 27;
private Inventory inv = null;
public GuiInfoList(String name) { public GuiInfoList(String name) {
this.name = name; this.name = name;
@ -28,12 +30,12 @@ public class GuiInfoList {
return name; return name;
} }
public List<Job> getJobList() { public HashMap<Integer, Job> getJobList() {
return jobList; return jobList;
} }
public void setJobList(List<Job> jobList) { public void addJob(int slot, Job job) {
this.jobList = jobList; this.jobList.put(slot, job);
} }
public void setJobInfo(Boolean jobInfo) { public void setJobInfo(Boolean jobInfo) {
@ -43,4 +45,12 @@ public class GuiInfoList {
public Boolean isJobInfo() { public Boolean isJobInfo() {
return jobInfo; return jobInfo;
} }
public Inventory getInv() {
return inv;
}
public void setInv(Inventory inv) {
this.inv = inv;
}
} }

View File

@ -22,6 +22,7 @@ import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.JobInfo;
import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.Debug;
public class GuiManager { public class GuiManager {
@ -45,28 +46,7 @@ public class GuiManager {
public Job getJobBySlot(Player player, int slot) { public Job getJobBySlot(Player player, int slot) {
GuiInfoList info = GuiList.get(player.getUniqueId()); GuiInfoList info = GuiList.get(player.getUniqueId());
List<Job> JobsList = info.getJobList(); return info.getJobList().get(slot);
int i = 0;
int pos = Jobs.getGCManager().getJobsGUIStartPosition() - 1;
int group = 0;
main: for (int z = 0; z < JobsList.size(); z++) {
group++;
if (group > Jobs.getGCManager().getJobsGUIGroupAmount()) {
group = 1;
pos += Jobs.getGCManager().getJobsGUISkipAmount();
}
pos++;
if (i >= JobsList.size())
break main;
if (pos == slot)
return JobsList.get(i);
i++;
}
return null;
} }
public Inventory CreateJobsGUI(Player player) { public Inventory CreateJobsGUI(Player player) {
@ -80,13 +60,9 @@ public class GuiManager {
} }
GuiInfoList guiInfo = new GuiInfoList(player.getName()); GuiInfoList guiInfo = new GuiInfoList(player.getName());
guiInfo.setJobList(JobsList);
// Inventory topinv = player.getOpenInventory().getTopInventory();
// if (topinv != null && !GuiList.containsKey(player.getName())) {
if (!this.isInGui(player)) if (!this.isInGui(player))
player.closeInventory(); player.closeInventory();
// }
GuiList.put(player.getUniqueId(), guiInfo); GuiList.put(player.getUniqueId(), guiInfo);
@ -100,17 +76,30 @@ public class GuiManager {
if (title.length() > 32) if (title.length() > 32)
title = title.substring(0, 30) + ".."; title = title.substring(0, 30) + "..";
// Resizing GUI in case we have more jobs then we could fit in current settup
GuiSize = GuiSize - 18 - ((Jobs.getGCManager().getJobsGUIRows() - 2) * 2) < JobsList.size() ? 2 * 9 + ((JobsList.size() % 9) * 9) : GuiSize;
// Lets avoid oversized GUI
GuiSize = GuiSize > 54 ? 54 : GuiSize;
Inventory GuiInv = Bukkit.createInventory(null, GuiSize, title); Inventory GuiInv = Bukkit.createInventory(null, GuiSize, title);
int i = 0; int i = 0;
int pos = Jobs.getGCManager().getJobsGUIStartPosition() - 1; int pos = Jobs.getGCManager().getJobsGUIStartPosition() - 1;
// Changing start position to 0 in case we have more jobs then we can fit in current setup
pos = JobsList.size() > 28 ? JobsList.size() <= 42 ? 0 : -1 : pos;
int group = 0; int group = 0;
main: for (int z = 0; z < JobsList.size(); z++) { main: for (int z = 0; z < JobsList.size(); z++) {
group++; group++;
if (group > Jobs.getGCManager().getJobsGUIGroupAmount()) { if (group > Jobs.getGCManager().getJobsGUIGroupAmount()) {
group = 1; group = 1;
// Only add skip if we can fit all of them in max sized Gui
if (JobsList.size() <= 42){
pos += Jobs.getGCManager().getJobsGUISkipAmount(); pos += Jobs.getGCManager().getJobsGUISkipAmount();
}
} }
// pos += 2; // pos += 2;
@ -165,6 +154,7 @@ public class GuiManager {
meta.setLore(Lore); meta.setLore(Lore);
GuiItem.setItemMeta(meta); GuiItem.setItemMeta(meta);
guiInfo.addJob(pos, job);
GuiInv.setItem(pos, GuiItem); GuiInv.setItem(pos, GuiItem);
i++; i++;
// } // }
@ -180,6 +170,7 @@ public class GuiManager {
GuiInv.setItem(y, filler); GuiInv.setItem(y, filler);
} }
} }
guiInfo.setInv(GuiInv);
return GuiInv; return GuiInv;
} }
@ -276,7 +267,13 @@ public class GuiManager {
items.add(one); items.add(one);
} }
int GuiSize = Jobs.getGCManager().getJobsGUIRows() * 9; GuiInfoList mainGui = GuiList.get(player.getUniqueId());
if (mainGui == null) {
CreateJobsGUI(player);
mainGui = GuiList.get(player.getUniqueId());
}
int GuiSize = mainGui != null && mainGui.getInv() != null ? mainGui.getInv().getSize() : Jobs.getGCManager().getJobsGUIRows() * 9;
int backButton = Jobs.getGCManager().getJobsGUIBackButton(); int backButton = Jobs.getGCManager().getJobsGUIBackButton();
String title = Jobs.getLanguage().getMessage("command.info.gui.jobinfo", "[jobname]", job.getName()); String title = Jobs.getLanguage().getMessage("command.info.gui.jobinfo", "[jobname]", job.getName());

View File

@ -248,14 +248,14 @@ public class JobsListener implements Listener {
if (job != null) { if (job != null) {
Inventory inv = Jobs.getGUIManager().CreateJobsSubGUI(player, job); Inventory inv = Jobs.getGUIManager().CreateJobsSubGUI(player, job);
Inventory top = player.getOpenInventory().getTopInventory(); Inventory top = player.getOpenInventory().getTopInventory();
if (top.getSize() == Jobs.getGCManager().getJobsGUIRows() * 9) // if (top.getSize() == Jobs.getGCManager().getJobsGUIRows() * 9)
top.setContents(inv.getContents()); top.setContents(inv.getContents());
} }
} else if (joblist.isJobInfo()) { } else if (joblist.isJobInfo()) {
if (slot == joblist.getbackButton()) { if (slot == joblist.getbackButton()) {
Inventory inv = Jobs.getGUIManager().CreateJobsGUI(player); Inventory inv = Jobs.getGUIManager().CreateJobsGUI(player);
Inventory top = player.getOpenInventory().getTopInventory(); Inventory top = player.getOpenInventory().getTopInventory();
if (top.getSize() == Jobs.getGCManager().getJobsGUIRows() * 9) // if (top.getSize() == Jobs.getGCManager().getJobsGUIRows() * 9)
top.setContents(inv.getContents()); top.setContents(inv.getContents());
} }
} else if (!Jobs.getGCManager().JobsGUISwitcheButtons && event.getClick() == ClickType.RIGHT || } else if (!Jobs.getGCManager().JobsGUISwitcheButtons && event.getClick() == ClickType.RIGHT ||