1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 22:13:25 +01:00
Jobs/com/gamingmesh/jobs/config/ShopManager.java

480 lines
14 KiB
Java
Raw Normal View History

2016-03-30 15:42:36 +02:00
package com.gamingmesh.jobs.config;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobItems;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.PlayerPoints;
import com.gamingmesh.jobs.container.ShopItem;
2016-06-09 17:35:24 +02:00
import com.gamingmesh.jobs.stuff.Perm;
2016-03-30 15:42:36 +02:00
public class ShopManager {
private Jobs plugin;
2016-03-30 15:42:36 +02:00
public List<ShopItem> list = new ArrayList<ShopItem>();
2016-06-25 09:13:06 +02:00
public HashMap<String, Integer> GuiList = new HashMap<String, Integer>();
2016-03-30 15:42:36 +02:00
public ShopManager(Jobs plugin) {
2016-03-30 15:42:36 +02:00
this.plugin = plugin;
}
public List<ShopItem> getShopItemList() {
return list;
}
2016-06-25 09:13:06 +02:00
public void openInventory(Player player, int page) {
Inventory inv = Jobs.getShopManager().CreateJobsGUI(player, page);
if (inv == null) {
player.sendMessage(Jobs.getLanguage().getMessage("command.shop.info.cantOpen"));
return;
}
Inventory topinv = player.getOpenInventory().getTopInventory();
if (topinv != null)
player.closeInventory();
Jobs.getShopManager().GuiList.put(player.getName(), page);
player.openInventory(inv);
}
public void checkSlot(Player player, int slot, int page) {
List<ShopItem> ls = getItemsByPage(page);
2016-06-25 17:56:59 +02:00
int GuiSize = getGuiSize(ls, page);
2016-06-25 09:13:06 +02:00
if (slot == getPrevButtonSlot(GuiSize, page)) {
openInventory(player, page - 1);
return;
}
if (slot == getnextButtonSlot(GuiSize, page)) {
openInventory(player, page + 1);
return;
}
2016-03-30 15:42:36 +02:00
2016-06-25 09:13:06 +02:00
if (slot > ls.size() - 1)
return;
ShopItem item = ls.get(slot);
2016-03-30 15:42:36 +02:00
PlayerPoints pointsInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getUniqueId());
2016-06-09 17:35:24 +02:00
if (!Perm.hasPermission(player, "jobs.items.bypass")) {
2016-03-30 15:42:36 +02:00
for (String onePerm : item.getRequiredPerm()) {
2016-06-09 17:35:24 +02:00
if (!Perm.hasPermission(player, onePerm)) {
2016-03-30 15:42:36 +02:00
player.sendMessage(Jobs.getLanguage().getMessage("command.shop.info.NoPermForItem"));
return;
}
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
if (jPlayer == null)
return;
for (Entry<String, Integer> oneJob : item.getRequiredJobs().entrySet()) {
Job tempJob = Jobs.getJob(oneJob.getKey());
if (tempJob == null)
continue;
JobProgression playerJob = jPlayer.getJobProgression(tempJob);
if (playerJob == null || playerJob.getLevel() < oneJob.getValue()) {
player.sendMessage(Jobs.getLanguage().getMessage("command.shop.info.NoJobReqForitem",
"%jobname%", tempJob.getName(),
"%joblevel%", oneJob.getValue()));
return;
}
}
if (pointsInfo == null || pointsInfo.getCurrentPoints() < item.getPrice()) {
player.sendMessage(Jobs.getLanguage().getMessage("command.shop.info.NoPoints"));
return;
}
}
for (String one : item.getCommands()) {
if (one.toLowerCase().startsWith("msg "))
player.sendMessage(one.substring(4, one.length()).replace("[player]", player.getName()));
else
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), one.replace("[player]", player.getName()));
}
for (JobItems one : item.getitems()) {
@SuppressWarnings("deprecation")
Material mat = Material.getMaterial(one.getId());
if (mat == null)
continue;
ItemStack itemStack = new ItemStack(mat, one.getAmount(), (byte) one.getData());
ItemMeta meta = itemStack.getItemMeta();
if (one.getName() != null)
meta.setDisplayName(one.getName());
if (one.getLore() != null)
meta.setLore(one.getLore());
itemStack.setItemMeta(meta);
if (itemStack.getType() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) itemStack.getItemMeta();
for (Entry<Enchantment, Integer> oneEnch : one.getEnchants().entrySet()) {
2016-03-30 15:42:36 +02:00
bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true);
}
if (bookMeta != null)
itemStack.setItemMeta(bookMeta);
} else
for (Entry<Enchantment, Integer> oneEnch : one.getEnchants().entrySet()) {
2016-03-30 15:42:36 +02:00
itemStack.addUnsafeEnchantment(oneEnch.getKey(), oneEnch.getValue());
}
player.getInventory().addItem(itemStack);
}
2016-06-09 17:35:24 +02:00
if (!Perm.hasPermission(player, "jobs.items.bypass")) {
2016-03-30 15:42:36 +02:00
pointsInfo.takePoints(item.getPrice());
player.sendMessage(Jobs.getLanguage().getMessage("command.shop.info.Paid", "%amount%", item.getPrice()));
}
2016-06-25 09:13:06 +02:00
player.getOpenInventory().getTopInventory().setContents(CreateJobsGUI(player, page).getContents());
2016-03-30 15:42:36 +02:00
}
2016-06-25 09:13:06 +02:00
private List<ShopItem> getItemsByPage(Integer page) {
List<ShopItem> ls = new ArrayList<ShopItem>();
for (ShopItem one : list) {
if (one.getPage() == page)
ls.add(one);
}
return ls;
}
2016-03-30 15:42:36 +02:00
2016-06-25 17:56:59 +02:00
private static int getGuiSize(List<ShopItem> ls, int page) {
2016-03-30 15:42:36 +02:00
int GuiSize = 9;
2016-06-25 09:13:06 +02:00
if (ls.size() > 9)
2016-03-30 15:42:36 +02:00
GuiSize = 18;
2016-06-25 09:13:06 +02:00
if (ls.size() > 18)
2016-03-30 15:42:36 +02:00
GuiSize = 27;
2016-06-25 09:13:06 +02:00
if (ls.size() > 27)
2016-03-30 15:42:36 +02:00
GuiSize = 36;
2016-06-25 09:13:06 +02:00
if (ls.size() > 36)
2016-03-30 15:42:36 +02:00
GuiSize = 45;
2016-06-25 09:13:06 +02:00
if (ls.size() == 45)
2016-03-30 15:42:36 +02:00
GuiSize = 54;
2016-06-25 09:13:06 +02:00
if (page > 1 && GuiSize < 54)
GuiSize += 9;
return GuiSize;
}
2016-06-25 17:56:59 +02:00
private static int getPrevButtonSlot(int GuiSize, int page) {
2016-06-25 09:13:06 +02:00
int prev = -1;
if (page > 1)
prev = GuiSize - 9;
return prev;
}
private int getnextButtonSlot(int GuiSize, int page) {
int next = -1;
List<ShopItem> lsnext = getItemsByPage(page + 1);
if (!lsnext.isEmpty())
next = GuiSize - 1;
return next;
}
public Inventory CreateJobsGUI(Player player, Integer page) {
List<ShopItem> ls = getItemsByPage(page);
if (ls.isEmpty())
return null;
int GuiSize = getGuiSize(ls, page);
2016-03-30 15:42:36 +02:00
String title = Jobs.getLanguage().getMessage("command.shop.info.title");
if (title.length() > 32)
title = title.substring(0, 30) + "..";
PlayerPoints pointsInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getUniqueId());
double points = 0D;
if (pointsInfo != null)
points = (int) (pointsInfo.getCurrentPoints() * 100.0) / 100.0;
Inventory GuiInv = Bukkit.createInventory(null, GuiSize, title);
2016-06-25 09:13:06 +02:00
for (int i = 0; i < ls.size(); i++) {
2016-03-30 15:42:36 +02:00
2016-06-25 09:13:06 +02:00
ShopItem item = ls.get(i);
2016-03-30 15:42:36 +02:00
ArrayList<String> Lore = new ArrayList<String>();
@SuppressWarnings("deprecation")
Material mat = Material.getMaterial(item.getIconId());
if (item.isHideWithoutPerm()) {
for (String onePerm : item.getRequiredPerm()) {
2016-06-09 17:35:24 +02:00
if (!Perm.hasPermission(player, onePerm)) {
2016-03-30 15:42:36 +02:00
mat = Material.STONE_BUTTON;
Lore.add(Jobs.getLanguage().getMessage("command.shop.info.NoPermToBuy"));
break;
}
}
}
if (mat == null)
mat = Material.STONE_BUTTON;
ItemStack GUIitem = new ItemStack(mat, item.getIconAmount(), (byte) item.getIconData());
ItemMeta meta = GUIitem.getItemMeta();
if (item.getIconName() != null)
meta.setDisplayName(item.getIconName());
Lore.addAll(item.getIconLore());
Lore.add(Jobs.getLanguage().getMessage("command.shop.info.currentPoints", "%currentpoints%", points));
Lore.add(Jobs.getLanguage().getMessage("command.shop.info.price", "%price%", item.getPrice()));
if (!item.getRequiredJobs().isEmpty()) {
Lore.add(Jobs.getLanguage().getMessage("command.shop.info.reqJobs"));
for (Entry<String, Integer> one : item.getRequiredJobs().entrySet()) {
String jobColor = "";
String levelColor = "";
Job job = Jobs.getJob(one.getKey());
JobProgression prog = Jobs.getPlayerManager().getJobsPlayer(player).getJobProgression(job);
if (prog == null) {
jobColor = ChatColor.DARK_RED.toString();
levelColor = ChatColor.DARK_RED.toString();
}
if (prog != null && prog.getLevel() < one.getValue())
levelColor = ChatColor.DARK_RED.toString();
Lore.add(Jobs.getLanguage().getMessage("command.shop.info.reqJobsList", "%jobsname%", jobColor + one.getKey(), "%level%", levelColor + one
.getValue()));
}
}
meta.setLore(Lore);
GUIitem.setItemMeta(meta);
GuiInv.setItem(i, GUIitem);
}
2016-06-25 09:13:06 +02:00
ItemStack Item = new ItemStack(Material.ARROW);
ItemMeta meta = Item.getItemMeta();
int pervSlot = getPrevButtonSlot(GuiSize, page);
if (pervSlot != -1) {
meta.setDisplayName(Jobs.getLanguage().getMessage("command.help.output.prev"));
Item.setItemMeta(meta);
GuiInv.setItem(pervSlot, Item);
}
int nextSlot = getnextButtonSlot(GuiSize, page);
if (nextSlot != -1) {
meta.setDisplayName(Jobs.getLanguage().getMessage("command.help.output.next"));
Item.setItemMeta(meta);
GuiInv.setItem(nextSlot, Item);
}
2016-03-30 15:42:36 +02:00
return GuiInv;
}
public void load() {
list.clear();
File file = new File(plugin.getDataFolder(), "shopItems.yml");
YamlConfiguration f = YamlConfiguration.loadConfiguration(file);
if (!f.isConfigurationSection("Items"))
return;
ConfigurationSection ConfCategory = f.getConfigurationSection("Items");
ArrayList<String> categoriesList = new ArrayList<String>(ConfCategory.getKeys(false));
if (categoriesList.size() == 0)
return;
2016-06-25 09:13:06 +02:00
int i = 0;
int y = 1;
2016-03-30 15:42:36 +02:00
for (String category : categoriesList) {
ConfigurationSection NameSection = ConfCategory.getConfigurationSection(category);
if (!NameSection.isDouble("Price")) {
Jobs.getPluginLogger().severe("Shop item " + category + " has an invalid Price property. Skipping!");
continue;
}
double price = NameSection.getDouble("Price");
if (!NameSection.isInt("Icon.Id")) {
Jobs.getPluginLogger().severe("Shop item " + category + " has an invalid Icon Id property. Skipping!");
continue;
}
int IconId = NameSection.getInt("Icon.Id");
ShopItem Sitem = new ShopItem(category, price, IconId);
if (NameSection.isInt("Icon.Data"))
Sitem.setIconData(NameSection.getInt("Icon.Data"));
if (NameSection.isInt("Icon.Amount"))
Sitem.setIconAmount(NameSection.getInt("Icon.Amount"));
if (NameSection.isString("Icon.Name"))
Sitem.setIconName(ChatColor.translateAlternateColorCodes('&', NameSection.getString("Icon.Name")));
if (NameSection.isList("Icon.Lore")) {
List<String> lore = new ArrayList<String>();
if (NameSection.getStringList("Icon.Lore") != null)
for (String eachLine : NameSection.getStringList("Icon.Lore")) {
lore.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine));
}
Sitem.setIconLore(lore);
}
if (NameSection.isBoolean("Icon.HideWithoutPermission")) {
Sitem.setHideWithoutPerm(NameSection.getBoolean("Icon.HideWithoutPermission"));
}
if (NameSection.isList("RequiredPermission"))
Sitem.setRequiredPerm(NameSection.getStringList("RequiredPermission"));
if (NameSection.isList("RequiredJobLevels")) {
HashMap<String, Integer> RequiredJobs = new HashMap<String, Integer>();
for (String one : NameSection.getStringList("RequiredJobLevels")) {
if (!one.contains("-"))
continue;
String job = one.split("-")[0];
int lvl = -1;
try {
lvl = Integer.parseInt(one.split("-")[1]);
} catch (NumberFormatException e) {
continue;
}
RequiredJobs.put(job, lvl);
}
Sitem.setRequiredJobs(RequiredJobs);
}
if (NameSection.isList("PerformCommands")) {
List<String> cmd = new ArrayList<String>();
if (NameSection.getStringList("PerformCommands") != null)
for (String eachLine : NameSection.getStringList("PerformCommands")) {
cmd.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine));
}
Sitem.setCommands(cmd);
}
if (NameSection.isConfigurationSection("GiveItems")) {
ConfigurationSection itemsSection = NameSection.getConfigurationSection("GiveItems");
Set<String> itemKeys = itemsSection.getKeys(false);
List<JobItems> items = new ArrayList<JobItems>();
for (String oneItemName : itemKeys) {
ConfigurationSection itemSection = itemsSection.getConfigurationSection(oneItemName);
String node = oneItemName.toLowerCase();
int id = itemSection.getInt("Id");
int data = 0;
if (itemSection.isInt("Data"))
data = itemSection.getInt("Data");
int amount = 1;
if (itemSection.isInt("Amount"))
amount = itemSection.getInt("Amount");
String name = null;
if (itemSection.isString("Name"))
name = org.bukkit.ChatColor.translateAlternateColorCodes('&', itemSection.getString("Name"));
List<String> lore = new ArrayList<String>();
if (itemSection.getStringList("Lore") != null)
for (String eachLine : itemSection.getStringList("Lore")) {
lore.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine));
}
HashMap<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
if (itemSection.getStringList("Enchants") != null)
for (String eachLine : itemSection.getStringList("Enchants")) {
if (!eachLine.contains("="))
continue;
Enchantment ench = Enchantment.getByName(eachLine.split("=")[0]);
Integer level = -1;
try {
level = Integer.parseInt(eachLine.split("=")[1]);
} catch (NumberFormatException e) {
continue;
}
if (ench != null && level != -1)
enchants.put(ench, level);
}
items.add(new JobItems(node, id, data, amount, name, lore, enchants, 0, 0, 0));
}
Sitem.setitems(items);
}
2016-06-25 09:13:06 +02:00
// if (list.size() >= 54) {
// Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Jobs] To many jobs shop items, max allowed is 54! Only first 54 items will be used!");
// break;
// }
i++;
if (i > 45) {
i = 1;
y++;
2016-03-30 15:42:36 +02:00
}
2016-06-25 09:13:06 +02:00
Sitem.setSlot(i);
Sitem.setPage(y);
2016-03-30 15:42:36 +02:00
list.add(Sitem);
}
if (!list.isEmpty())
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] " + list.size() + " shop items loaded!");
return;
}
public void CloseInventories() {
2016-06-25 09:13:06 +02:00
for (Entry<String, Integer> one : GuiList.entrySet()) {
Player player = Bukkit.getPlayer(one.getKey());
2016-03-30 15:42:36 +02:00
if (player != null) {
player.closeInventory();
}
}
}
}