<commit_before>package com.gamingmesh.jobs.Gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.CMIGUI.CMIGui;
import com.gamingmesh.jobs.CMIGUI.CMIGuiButton;
import com.gamingmesh.jobs.CMIGUI.GUIManager;
import com.gamingmesh.jobs.CMIGUI.GUIManager.GUIClickType;
import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial;
import com.gamingmesh.jobs.container.ActionType;
import com.gamingmesh.jobs.container.Boost;
import com.gamingmesh.jobs.container.CurrencyType;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobInfo;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;

public class GuiManager {

    public void openJobsBrowseGUI(Player player) {

	ArrayList<Job> JobsList = new ArrayList<>();
	for (Job job : Jobs.getJobs()) {
	    if (Jobs.getGCManager().getHideJobsWithoutPermission())
		if (!Jobs.getCommandManager().hasJobPermission(player, job))
		    continue;
	    JobsList.add(job);
	}

	JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
	List<JobProgression> pJobs = JPlayer.getJobProgression();

	CMIGui gui = new CMIGui(player);
	gui.setTitle(Jobs.getLanguage().getMessage("command.info.gui.pickjob"));
	gui.setFiller(CMIMaterial.get(Jobs.getGCManager().guiFiller));

	int GuiSize = Jobs.getGCManager().getJobsGUIRows() * 9;

	int neededSlots = JobsList.size() + ((JobsList.size() / Jobs.getGCManager().getJobsGUIGroupAmount()) * Jobs.getGCManager().getJobsGUISkipAmount()) + Jobs.getGCManager().getJobsGUIStartPosition();
	int neededRows = (int) Math.ceil(neededSlots / 9D);

	// Resizing GUI in case we have more jobs then we could fit in current setup
	GuiSize = Jobs.getGCManager().getJobsGUIRows() > neededRows ? GuiSize : neededRows * 9;

	// Lets avoid oversized GUI
	GuiSize = GuiSize > 54 ? 54 : GuiSize;

	gui.setInvSize(GuiSize);

	int i = 0;
	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;
	main: for (int z = 0; z < JobsList.size(); z++) {
	    group++;

	    if (group > Jobs.getGCManager().getJobsGUIGroupAmount()) {
		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++;
	    if (i >= JobsList.size())
		break main;
	    Job job = JobsList.get(i);

	    ArrayList<String> Lore = new ArrayList<>();

	    for (JobProgression onePJob : pJobs) {
		if (onePJob.getJob().getName().equalsIgnoreCase(job.getName()))
		    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.working"));
	    }

	    int maxlevel = job.getMaxLevel(JPlayer);
	    if (maxlevel > 0)
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.max") + maxlevel);

	    if (Jobs.getGCManager().ShowTotalWorkers)
		Lore.add(Jobs.getLanguage().getMessage("command.browse.output.totalWorkers", "[amount]", job.getTotalPlayers()));

	    if (Jobs.getGCManager().useDynamicPayment && Jobs.getGCManager().ShowPenaltyBonus)
		if (job.getBonus() < 0)
		    Lore.add(Jobs.getLanguage().getMessage("command.browse.output.penalty", "[amount]", (int) (job.getBonus() * 100) * -1));
		else
		    Lore.add(Jobs.getLanguage().getMessage("command.browse.output.bonus", "[amount]", (int) (job.getBonus() * 100)));

	    Lore.addAll(Arrays.asList(job.getDescription().split("/n")));

	    if (job.getMaxSlots() != null)
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.leftSlots") + ((job.getMaxSlots() - Jobs.getUsedSlots(job)) > 0 ? (job.getMaxSlots() - Jobs
		    .getUsedSlots(job)) : 0));

	    if (Jobs.getGCManager().ShowActionNames) {
		Lore.add("");
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.actions"));

		for (ActionType actionType : ActionType.values()) {
		    List<JobInfo> info = job.getJobInfo(actionType);
		    if (info != null && !info.isEmpty()) {
			Lore.add(Jobs.getLanguage().getMessage("command.info.output." + actionType.getName().toLowerCase() + ".info"));
		    }
		}
	    }

	    Lore.add("");
	    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.leftClick"));
	    if (JPlayer.isInJob(job))
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.middleClick"));
	    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.rightClick"));

	    ItemStack GuiItem = job.getGuiItem();

	    ItemMeta meta = GuiItem.getItemMeta();
	    meta.setDisplayName(job.getChatColor() + job.getName());
	    meta.setLore(Lore);
	    GuiItem.setItemMeta(meta);

	    gui.addButton(new CMIGuiButton(pos, GuiItem) {

		@Override
		public void click(GUIClickType type) {

		    switch (type) {
		    case Left:
		    case LeftShift:
			openSubJobsBrowseGUI(player, job);
			break;
		    case MiddleMouse:
			Jobs.getCommandManager().onCommand(player, null, "jobs", new String[] { "leave", job.getName() });
			break;
		    case Right:
		    case RightShift:
			Jobs.getCommandManager().onCommand(player, null, "jobs", new String[] { "join", job.getName() });
			break;
		    default:
			break;
		    }
		}
	    });
	    i++;

	}

	gui.fillEmptyButtons();
	gui.open();
    }

    public void openSubJobsBrowseGUI(Player player, Job job) {

	Inventory tempInv = Bukkit.createInventory(new JobsInventoryHolder(player), 54, "");

	ItemStack GuiItem = job.getGuiItem();
	JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(player);

	Boost boost = Jobs.getPlayerManager().getFinalBonus(JPlayer, job);

	int level = 1;
	JobProgression prog = JPlayer.getJobProgression(job);
	if (prog != null)
	    level = prog.getLevel();

	int numjobs = JPlayer.getJobProgression().size();

	List<ItemStack> items = new ArrayList<>();
	int i = 0;
	for (ActionType actionType : ActionType.values()) {
	    List<JobInfo> info = job.getJobInfo(actionType);

	    if (info == null || info.isEmpty())
		continue;

	    ArrayList<String> Lore = new ArrayList<>();
	    Lore.add(Jobs.getLanguage().getMessage("command.info.output." + actionType.getName().toLowerCase() + ".info"));
	    int y = 1;
	    for (int z = 0; z < info.size(); z++) {<commit_msg>Allow leaving job by using middle mouse click<commit_after>package com.gamingmesh.jobs.Gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.CMIGUI.CMIGui;
import com.gamingmesh.jobs.CMIGUI.CMIGuiButton;
import com.gamingmesh.jobs.CMIGUI.GUIManager;
import com.gamingmesh.jobs.CMIGUI.GUIManager.GUIClickType;
import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial;
import com.gamingmesh.jobs.container.ActionType;
import com.gamingmesh.jobs.container.Boost;
import com.gamingmesh.jobs.container.CurrencyType;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobInfo;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;

public class GuiManager {

    public void openJobsBrowseGUI(Player player) {

	ArrayList<Job> JobsList = new ArrayList<>();
	for (Job job : Jobs.getJobs()) {
	    if (Jobs.getGCManager().getHideJobsWithoutPermission())
		if (!Jobs.getCommandManager().hasJobPermission(player, job))
		    continue;
	    JobsList.add(job);
	}

	JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
	List<JobProgression> pJobs = JPlayer.getJobProgression();

	CMIGui gui = new CMIGui(player);
	gui.setTitle(Jobs.getLanguage().getMessage("command.info.gui.pickjob"));
	gui.setFiller(CMIMaterial.get(Jobs.getGCManager().guiFiller));

	int GuiSize = Jobs.getGCManager().getJobsGUIRows() * 9;

	int neededSlots = JobsList.size() + ((JobsList.size() / Jobs.getGCManager().getJobsGUIGroupAmount()) * Jobs.getGCManager().getJobsGUISkipAmount()) + Jobs.getGCManager().getJobsGUIStartPosition();
	int neededRows = (int) Math.ceil(neededSlots / 9D);

	// Resizing GUI in case we have more jobs then we could fit in current setup
	GuiSize = Jobs.getGCManager().getJobsGUIRows() > neededRows ? GuiSize : neededRows * 9;

	// Lets avoid oversized GUI
	GuiSize = GuiSize > 54 ? 54 : GuiSize;

	gui.setInvSize(GuiSize);

	int i = 0;
	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;
	main: for (int z = 0; z < JobsList.size(); z++) {
	    group++;

	    if (group > Jobs.getGCManager().getJobsGUIGroupAmount()) {
		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++;
	    if (i >= JobsList.size())
		break main;
	    Job job = JobsList.get(i);

	    ArrayList<String> Lore = new ArrayList<>();

	    for (JobProgression onePJob : pJobs) {
		if (onePJob.getJob().getName().equalsIgnoreCase(job.getName()))
		    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.working"));
	    }

	    int maxlevel = job.getMaxLevel(JPlayer);
	    if (maxlevel > 0)
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.max") + maxlevel);

	    if (Jobs.getGCManager().ShowTotalWorkers)
		Lore.add(Jobs.getLanguage().getMessage("command.browse.output.totalWorkers", "[amount]", job.getTotalPlayers()));

	    if (Jobs.getGCManager().useDynamicPayment && Jobs.getGCManager().ShowPenaltyBonus)
		if (job.getBonus() < 0)
		    Lore.add(Jobs.getLanguage().getMessage("command.browse.output.penalty", "[amount]", (int) (job.getBonus() * 100) * -1));
		else
		    Lore.add(Jobs.getLanguage().getMessage("command.browse.output.bonus", "[amount]", (int) (job.getBonus() * 100)));

	    Lore.addAll(Arrays.asList(job.getDescription().split("/n")));

	    if (job.getMaxSlots() != null)
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.leftSlots") + ((job.getMaxSlots() - Jobs.getUsedSlots(job)) > 0 ? (job.getMaxSlots() - Jobs
		    .getUsedSlots(job)) : 0));

	    if (Jobs.getGCManager().ShowActionNames) {
		Lore.add("");
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.actions"));

		for (ActionType actionType : ActionType.values()) {
		    List<JobInfo> info = job.getJobInfo(actionType);
		    if (info != null && !info.isEmpty()) {
			Lore.add(Jobs.getLanguage().getMessage("command.info.output." + actionType.getName().toLowerCase() + ".info"));
		    }
		}
	    }

	    Lore.add("");
	    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.leftClick"));
	    if (JPlayer.isInJob(job))
		Lore.add(Jobs.getLanguage().getMessage("command.info.gui.middleClick"));
	    Lore.add(Jobs.getLanguage().getMessage("command.info.gui.rightClick"));

	    ItemStack GuiItem = job.getGuiItem();

	    ItemMeta meta = GuiItem.getItemMeta();
	    meta.setDisplayName(job.getChatColor() + job.getName());
	    meta.setLore(Lore);
	    GuiItem.setItemMeta(meta);

	    gui.addButton(new CMIGuiButton(pos, GuiItem) {

		@Override
		public void click(GUIClickType type) {

		    switch (type) {
		    case Left:
		    case LeftShift:
			openJobsBrowseGUI(player, job);
			break;
		    case MiddleMouse:
			Jobs.getCommandManager().onCommand(player, null, "jobs", new String[] { "leave", job.getName() });
			openJobsBrowseGUI(player);
			break;
		    case Right:
		    case RightShift:
			Jobs.getCommandManager().onCommand(player, null, "jobs", new String[] { "join", job.getName() });
			openJobsBrowseGUI(player);
			break;
		    default:
			break;
		    }
		}
	    });
	    i++;

	}

	gui.fillEmptyButtons();
	gui.open();
    }

    public void openJobsBrowseGUI(Player player, Job job) {

	Inventory tempInv = Bukkit.createInventory(new JobsInventoryHolder(player), 54, "");

	ItemStack GuiItem = job.getGuiItem();
	JobsPlayer JPlayer = Jobs.getPlayerManager().getJobsPlayer(player);

	Boost boost = Jobs.getPlayerManager().getFinalBonus(JPlayer, job);

	int level = 1;
	JobProgression prog = JPlayer.getJobProgression(job);
	if (prog != null)
	    level = prog.getLevel();

	int numjobs = JPlayer.getJobProgression().size();

	List<ItemStack> items = new ArrayList<>();
	int i = 0;
	for (ActionType actionType : ActionType.values()) {
	    List<JobInfo> info = job.getJobInfo(actionType);

	    if (info == null || info.isEmpty())
		continue;

	    ArrayList<String> Lore = new ArrayList<>();
	    Lore.add(Jobs.getLanguage().getMessage("command.info.output." + actionType.getName().toLowerCase() + ".info"));
	    int y = 1;
	    for (int z = 0; z < info.size(); z++) {
{'fraction_non_alphanumeric': 0.15327017114914426, 'fraction_numerical': 0.005806845965770171, 'mean_word_length': 4.149095200629426, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4935348', 'n_tokens_mistral': 4868, 'n_tokens_neox': 4425, 'n_words': 825}