Initial commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ProjectCranked</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>Shop</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>Utils</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,73 @@
|
||||
package net.crankedpvp.shop;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.EconomyUtils;
|
||||
import net.crankedpvp.shop.commands.SellCommand;
|
||||
import net.crankedpvp.shop.commands.ShopCommand;
|
||||
import net.crankedpvp.shop.handlers.ShopHandler;
|
||||
import net.crankedpvp.shop.handlers.ShopsFile;
|
||||
import net.crankedpvp.shop.listeners.MainMenuClick;
|
||||
import net.crankedpvp.shop.listeners.ShopClick;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@Getter
|
||||
public class ShopPlugin extends JavaPlugin {
|
||||
|
||||
@Getter
|
||||
private static ShopPlugin instance;
|
||||
@Getter
|
||||
private static Economy econ;
|
||||
|
||||
private static ShopsFile shopsFile;
|
||||
private static ShopHandler shopHandler;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
econ = EconomyUtils.getEconomy();
|
||||
reloadConfig();
|
||||
|
||||
ShopPlugin.loadStuff();
|
||||
|
||||
this.getCommand("shop").setExecutor(new ShopCommand());
|
||||
this.getCommand("sell").setExecutor(new SellCommand());
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new MainMenuClick(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new ShopClick(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
instance = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reloadConfig() {
|
||||
super.reloadConfig();
|
||||
saveDefaultConfig();
|
||||
getConfig().options().copyDefaults(true);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
public static void loadStuff() {
|
||||
shopsFile = new ShopsFile(getInstance());
|
||||
shopHandler = new ShopHandler(getInstance());
|
||||
}
|
||||
|
||||
|
||||
public static ShopsFile getShopsFile() {
|
||||
return shopsFile;
|
||||
}
|
||||
|
||||
public static ShopHandler getShopHandler() {
|
||||
return shopHandler;
|
||||
}
|
||||
|
||||
public static String color(String input) {
|
||||
return ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.crankedpvp.shop.commands;
|
||||
|
||||
import net.crankedpvp.shop.ShopPlugin;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class SellCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
|
||||
if(!sender.hasPermission("shopguiplus.sell.hand")) {
|
||||
sender.sendMessage("§c§l(!)§c You haven't unlocked this feature!");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if(args.length == 0) {
|
||||
sender.sendMessage("§c§l(!)§c /sell hand - Sell held item");
|
||||
sender.sendMessage("§c§l(!)§c /sell all - Sell all items in inventory");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) sender;
|
||||
|
||||
if(args[0].equalsIgnoreCase("hand") && sender.hasPermission("shopguiplus.sell.hand")) {
|
||||
ItemStack item = p.getItemInHand();
|
||||
double price = ShopPlugin.getShopHandler().getSellPrice(item);
|
||||
if(price <= 0) {
|
||||
p.sendMessage("§c§l(!) §cCannot sell this item!");
|
||||
return true;
|
||||
}
|
||||
|
||||
p.sendMessage("§a§l(!)§f You sold §a"+item.getAmount()+"x items§f for §a$"+ NumberFormat.getInstance().format(price*item.getAmount()));
|
||||
p.setItemInHand(new ItemStack(Material.AIR));
|
||||
|
||||
ShopPlugin.getEcon().depositPlayer(p, price*item.getAmount());
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("all") && sender.hasPermission("shopguiplus.sell.all")) {
|
||||
int sell = 0;
|
||||
int amount = 0;
|
||||
for(int i = 0; i < p.getInventory().getSize(); i++) {
|
||||
ItemStack item = p.getInventory().getItem(i);
|
||||
if(item == null)
|
||||
continue;
|
||||
double price = ShopPlugin.getShopHandler().getSellPrice(item);
|
||||
if(price == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sell += price * item.getAmount();
|
||||
amount += item.getAmount();
|
||||
p.getInventory().setItem(i, new ItemStack(Material.AIR));
|
||||
}
|
||||
|
||||
p.sendMessage("§a§l(!)§f You sold §a"+amount+"x items§f for §a$"+ NumberFormat.getInstance().format(sell));
|
||||
ShopPlugin.getEcon().depositPlayer(p, sell);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package net.crankedpvp.shop.commands;
|
||||
|
||||
import net.crankedpvp.shop.ShopPlugin;
|
||||
import net.crankedpvp.shop.nbt.NBTApi;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShopCommand implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
|
||||
if(args.length > 0 && sender.hasPermission("shop.reload")) {
|
||||
ShopPlugin.loadStuff();
|
||||
ShopPlugin.getInstance().reloadConfig();
|
||||
sender.sendMessage("§aShop has been reloaded!");
|
||||
return true;
|
||||
}
|
||||
open((Player)sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void open(Player p) {
|
||||
Inventory inv = Bukkit.createInventory(null, ShopPlugin.getInstance().getConfig().getInt("shopMenuSize"),
|
||||
ShopPlugin.color(ShopPlugin.getInstance().getConfig().getString("shopMenuName")));
|
||||
for(String item : ShopPlugin.getInstance().getConfig().getConfigurationSection("shopMenuItems").getKeys(false)) {
|
||||
String path = "shopMenuItems."+item;
|
||||
|
||||
ItemStack i = new ItemStack(Material.matchMaterial(ShopPlugin.getInstance().getConfig().getString(path+".item.material")));
|
||||
i.setDurability((short) ShopPlugin.getInstance().getConfig().getInt(path+".item.damage"));
|
||||
ItemMeta im = i.getItemMeta();
|
||||
|
||||
String name = ShopPlugin.getInstance().getConfig().getString(path+".item.name");
|
||||
im.setDisplayName(ShopPlugin.color(name));
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
for(String line : ShopPlugin.getInstance().getConfig().getStringList(path+".item.lore")) {
|
||||
list.add(ShopPlugin.color(line));
|
||||
}
|
||||
|
||||
im.setLore(list);
|
||||
i.setItemMeta(im);
|
||||
|
||||
i = NBTApi.setKey(i, "menu", ShopPlugin.getInstance().getConfig().getString(path+".shop"));
|
||||
inv.setItem(ShopPlugin.getInstance().getConfig().getInt(path+".slot"), i);
|
||||
}
|
||||
|
||||
p.openInventory(inv);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package net.crankedpvp.shop.handlers;
|
||||
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
|
||||
import net.crankedpvp.shop.ShopPlugin;
|
||||
|
||||
import net.crankedpvp.shop.nbt.NBTApi;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ShopHandler {
|
||||
|
||||
// String category, ShopItem material
|
||||
private static HashMap<String, List<ShopItem>> items = new HashMap<>();
|
||||
|
||||
// String category, String invName
|
||||
private static HashMap<String, String> shopNames = new HashMap<>();
|
||||
|
||||
// int slot, String itemName
|
||||
private static HashMap<Integer, String> mainMenu = new HashMap<>();
|
||||
|
||||
public ShopHandler(JavaPlugin instance) {
|
||||
FileConfiguration fc = ShopPlugin.getShopsFile().getFc();
|
||||
for(String shop : fc.getConfigurationSection("shops").getKeys(false)) {
|
||||
String invName = ShopPlugin.color(fc.getString("shops."+shop+".name"));
|
||||
shopNames.put(shop, invName);
|
||||
|
||||
List<ShopItem> itemsList = new ArrayList<>();
|
||||
for (String item : fc.getConfigurationSection("shops." + shop + ".items").getKeys(false)) {
|
||||
String path = "shops."+shop+".items."+item;
|
||||
|
||||
boolean command = !fc.getString(path+".type").equalsIgnoreCase("item");
|
||||
boolean spawner = fc.getBoolean(path+".item.spawner");
|
||||
String mt = fc.getString(path+".item.material");
|
||||
|
||||
if(spawner) {
|
||||
mt = "MOB_SPAWNER";
|
||||
}
|
||||
|
||||
Material m = Material.matchMaterial(mt);
|
||||
|
||||
int damage = fc.getInt(path+".item.damage");
|
||||
int quantity = fc.getInt(path+".item.quantity");
|
||||
|
||||
double sellPrice = fc.getDouble(path+".sellPrice")/quantity;
|
||||
double buyPrice = fc.getDouble(path+".buyPrice")/quantity;
|
||||
|
||||
String name = fc.getString(path+".item.name");
|
||||
if(name != null) {
|
||||
name = ShopPlugin.color(name);
|
||||
}
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
if(fc.contains(path+".item.lore")) {
|
||||
for(String line : fc.getStringList(path+".item.lore")) {
|
||||
lore.add(ShopPlugin.color(line));
|
||||
}
|
||||
}
|
||||
ShopItem shopItem = new ShopItem(m, damage, command, path, buyPrice, sellPrice, quantity, name,
|
||||
!command ? null : fc.getStringList(path+".commands").get(0), !spawner ? null : fc.getString(path+".item.mob"), lore);
|
||||
itemsList.add(shopItem);
|
||||
}
|
||||
items.put(shop, itemsList);
|
||||
}
|
||||
|
||||
for(String item : instance.getConfig().getConfigurationSection("shopMenuItems").getKeys(false)) {
|
||||
mainMenu.put(instance.getConfig().getInt("shopMenuItems."+item+".slot"), item);
|
||||
}
|
||||
}
|
||||
|
||||
public String getShopNameByInvName(String input) {
|
||||
for(Map.Entry<String, String> names : shopNames.entrySet()) {
|
||||
if(!names.getValue().equalsIgnoreCase(input))
|
||||
continue;
|
||||
|
||||
return names.getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public double getSellPrice(ItemStack item) {
|
||||
if(item == null)
|
||||
return 0;
|
||||
|
||||
if(item.hasItemMeta()) {
|
||||
ItemMeta im = item.getItemMeta();
|
||||
if(im.hasLore()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(im.hasDisplayName())
|
||||
return 0;
|
||||
|
||||
if(im.hasEnchants())
|
||||
return 0;
|
||||
}
|
||||
|
||||
Material m = item.getType();
|
||||
short dur = item.getDurability();
|
||||
|
||||
if(m.equals(Material.MOB_SPAWNER))
|
||||
return 0;
|
||||
|
||||
for(Map.Entry<String, List<ShopItem>> entry : items.entrySet()) {
|
||||
for(ShopItem si : entry.getValue()) {
|
||||
if(
|
||||
si.getMaterial().equals(m) &&
|
||||
si.getDamage() == dur &&
|
||||
!si.isCommand() &&
|
||||
si.getSpawner() == null
|
||||
) {
|
||||
return si.getSellPrice();
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public double getBuyPrice(ItemStack item) {
|
||||
if(item == null)
|
||||
return 0;
|
||||
|
||||
if(item.hasItemMeta()) {
|
||||
ItemMeta im = item.getItemMeta();
|
||||
if(im.hasLore()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(im.hasDisplayName())
|
||||
return 0;
|
||||
|
||||
if(im.hasEnchants())
|
||||
return 0;
|
||||
}
|
||||
|
||||
Material m = item.getType();
|
||||
short dur = item.getDurability();
|
||||
|
||||
if(m.equals(Material.MOB_SPAWNER))
|
||||
return 0;
|
||||
|
||||
for(Map.Entry<String, List<ShopItem>> entry : items.entrySet()) {
|
||||
for(ShopItem si : entry.getValue()) {
|
||||
if(
|
||||
si.getMaterial().equals(m) &&
|
||||
si.getDamage() == dur &&
|
||||
!si.isCommand() &&
|
||||
si.getSpawner() == null
|
||||
) {
|
||||
return si.getBuyPrice();
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getInvNameByShop(String input) {
|
||||
return shopNames.get(input);
|
||||
}
|
||||
|
||||
public List<ShopItem> getItems(String shop) {
|
||||
return items.get(shop);
|
||||
}
|
||||
|
||||
public ShopItem getShopItem(Material m, int damage, String shop, String spawner, String cmd) {
|
||||
for(ShopItem si : getItems(shop).stream().filter(s->m.equals(s.getMaterial())).collect(Collectors.toList())) {
|
||||
if(si.getMaterial().equals(m) && si.getDamage() == damage) {
|
||||
if(spawner != null) {
|
||||
if(spawner.equalsIgnoreCase(si.getSpawner())) {
|
||||
return si;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(cmd != null) {
|
||||
if(cmd.equalsIgnoreCase(si.getCmd())) {
|
||||
return si;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return si;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getItemName(int slot) {
|
||||
return mainMenu.get(slot);
|
||||
}
|
||||
|
||||
public String getItemName(ItemStack i) {
|
||||
if (ItemUtils.isAirOrNull(i)) return "none";
|
||||
return NBTApi.getKey(i, "menu");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package net.crankedpvp.shop.handlers;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ShopItem {
|
||||
|
||||
private Material m;
|
||||
private boolean command;
|
||||
|
||||
private int damage;
|
||||
|
||||
private String path;
|
||||
|
||||
private double buyPrice;
|
||||
|
||||
private List<String> lore;
|
||||
private double sellPrice;
|
||||
|
||||
private int quantity;
|
||||
|
||||
String name;
|
||||
String cmd;
|
||||
String spawner;
|
||||
|
||||
public ShopItem(Material material, int damage, boolean command, String path, double buyPrice, double sellPrice, int quanity, String name, String cmd, String spawner, List<String> lore) {
|
||||
this.m = material;
|
||||
this.damage = damage;
|
||||
this.command = command;
|
||||
this.path = path;
|
||||
this.buyPrice = buyPrice;
|
||||
this.sellPrice = sellPrice;
|
||||
this.quantity = quanity;
|
||||
this.name = name;
|
||||
this.cmd = cmd;
|
||||
this.spawner = spawner;
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public boolean isCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
public double getBuyPrice() {
|
||||
return buyPrice;
|
||||
}
|
||||
|
||||
public double getSellPrice() {
|
||||
return sellPrice;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public Material getMaterial() {
|
||||
return m;
|
||||
}
|
||||
|
||||
public int getDamage() {
|
||||
return damage;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getCmd() {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public String getSpawner() {
|
||||
return spawner;
|
||||
}
|
||||
|
||||
public List<String> getLore() {
|
||||
return lore;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package net.crankedpvp.shop.handlers;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ShopsFile {
|
||||
|
||||
private File f;
|
||||
private FileConfiguration fc = new YamlConfiguration();
|
||||
|
||||
public ShopsFile(JavaPlugin instance) {
|
||||
this.f = new File(instance.getDataFolder(), "shops.yml");
|
||||
if (!this.f.exists()) instance.saveResource("shops.yml", false);
|
||||
|
||||
try {
|
||||
fc.load(f);
|
||||
} catch (Exception ev) {
|
||||
ev.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public FileConfiguration getFc() {
|
||||
return fc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package net.crankedpvp.shop.listeners;
|
||||
|
||||
import net.crankedpvp.shop.ShopPlugin;
|
||||
|
||||
import net.crankedpvp.shop.handlers.ShopItem;
|
||||
import net.crankedpvp.shop.nbt.NBTApi;
|
||||
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.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MainMenuClick implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onClick(InventoryClickEvent e) {
|
||||
if(e.getCurrentItem() == null)
|
||||
return;
|
||||
|
||||
if(e.getClickedInventory() == null)
|
||||
return;
|
||||
|
||||
if(!e.getInventory().getName().equalsIgnoreCase(ShopPlugin.color(ShopPlugin.getInstance().getConfig().getString("shopMenuName"))))
|
||||
return;
|
||||
|
||||
e.setCancelled(true);
|
||||
int slot = e.getSlot();
|
||||
|
||||
Player p = (Player) e.getWhoClicked();
|
||||
|
||||
if(p.getOpenInventory().getTopInventory().getSize()-1 < slot)
|
||||
return;
|
||||
|
||||
if (!p.getOpenInventory().getTopInventory().equals(e.getClickedInventory())) return;
|
||||
|
||||
String shop = ShopPlugin.getShopHandler().getItemName(e.getCurrentItem());
|
||||
|
||||
if(shop == null)
|
||||
return;
|
||||
|
||||
List<ShopItem> items = ShopPlugin.getShopHandler().getItems(shop);
|
||||
String invName = ShopPlugin.getShopHandler().getInvNameByShop(shop);
|
||||
if(invName == null)
|
||||
return;
|
||||
|
||||
int size = getSize(items.size());
|
||||
if(size < 54) {
|
||||
size+=9;
|
||||
}
|
||||
Inventory inv = Bukkit.createInventory(null, size, invName);
|
||||
for(ShopItem si : items) {
|
||||
ItemStack item = new ItemStack(si.getMaterial(), si.getQuantity(), (byte) si.getDamage()).clone();
|
||||
item.setDurability((short)si.getDamage());
|
||||
ItemMeta im = item.getItemMeta();
|
||||
|
||||
im.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
|
||||
|
||||
List<String> lore = new ArrayList<>(si.getLore());
|
||||
lore.add(" ");
|
||||
|
||||
if(si.getBuyPrice() > 0) {
|
||||
lore.add("§aBuy Price");
|
||||
lore.add("§a $"+NumberFormat.getInstance().format(si.getBuyPrice())+"/ea");
|
||||
lore.add(" ");
|
||||
}
|
||||
if(si.getSellPrice() > 0) {
|
||||
lore.add("§cSell Price");
|
||||
lore.add("§c $"+NumberFormat.getInstance().format(si.getSellPrice())+"/ea");
|
||||
lore.add(" ");
|
||||
}
|
||||
|
||||
lore.add("§7§m ");
|
||||
|
||||
int count = 0;
|
||||
|
||||
if(si.isCommand()) {
|
||||
if(si.getBuyPrice() > 0) {
|
||||
lore.add("§aLeft-Click §7to buy §ax" + si.getQuantity());
|
||||
}
|
||||
} else {
|
||||
if(si.getBuyPrice() > 0) {
|
||||
count = Arrays.asList(p.getOpenInventory().getBottomInventory().getContents())
|
||||
.stream().filter(l-> l == null).collect(Collectors.toList()).size()*si.getMaterial().getMaxStackSize();
|
||||
|
||||
lore.add("§aLeft-Click §7to buy §ax1");
|
||||
if(count > 0) {
|
||||
lore.add("§aShift Left-Click §7to buy §ax"+si.getMaterial().getMaxStackSize());
|
||||
lore.add("§aMiddle §7to buy inventory §ax" + count);
|
||||
lore.add(" ");
|
||||
}
|
||||
}
|
||||
|
||||
if(si.getSellPrice() > 0) {
|
||||
lore.add("§aRight-Click§7 to sell §ax1");
|
||||
lore.add("§aShift Right-Click§7 to sell §ax"+si.getMaterial().getMaxStackSize());
|
||||
}
|
||||
}
|
||||
|
||||
if(si.getName() != null) {
|
||||
im.setDisplayName(si.getName());
|
||||
}
|
||||
|
||||
im.setLore(lore);
|
||||
item.setItemMeta(im);
|
||||
|
||||
item = NBTApi.setKey(item, "buy", "" + (si.getBuyPrice() > 0));
|
||||
item = NBTApi.setKey(item, "sell", "" + (si.getSellPrice() > 0));
|
||||
item = NBTApi.setKey(item, "count", "" + count);
|
||||
item = NBTApi.setKey(item, "damage", "" + si.getDamage());
|
||||
|
||||
if(si.getSpawner() != null)
|
||||
item = NBTApi.setKey(item, "spawner", "" + si.getSpawner());
|
||||
if(si.isCommand()) {
|
||||
item = NBTApi.setKey(item, "command", si.getCmd());
|
||||
}
|
||||
|
||||
inv.addItem(item);
|
||||
}
|
||||
|
||||
ItemStack back = new ItemStack(Material.matchMaterial(ShopPlugin.getInstance().getConfig().getString("buttons.goBack.item.material")));
|
||||
ItemMeta backMeta = back.getItemMeta();
|
||||
|
||||
backMeta.setDisplayName(ShopPlugin.color(ShopPlugin.getInstance().getConfig().getString("buttons.goBack.item.name")));
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
for(String line : ShopPlugin.getInstance().getConfig().getStringList("buttons.goBack.item.lore")) {
|
||||
lore.add(ShopPlugin.color(line));
|
||||
}
|
||||
|
||||
backMeta.setLore(lore);
|
||||
back.setItemMeta(backMeta);
|
||||
|
||||
inv.setItem(inv.getSize()-1, back);
|
||||
|
||||
p.openInventory(inv);
|
||||
}
|
||||
|
||||
public static int getSlot(int size) {
|
||||
size-=5;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
private int getSize(int count) {
|
||||
double number = count/9.0;
|
||||
if(number > (int)number) {
|
||||
number++;
|
||||
}
|
||||
|
||||
int f = (int)number*9;
|
||||
if(f > 54) {
|
||||
return 54;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package net.crankedpvp.shop.listeners;
|
||||
|
||||
import net.crankedpvp.shop.ShopPlugin;
|
||||
|
||||
import net.crankedpvp.shop.commands.ShopCommand;
|
||||
import net.crankedpvp.shop.handlers.ShopItem;
|
||||
import net.crankedpvp.shop.nbt.NBTApi;
|
||||
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.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class ShopClick implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onClick(InventoryClickEvent e) {
|
||||
if (e.getCurrentItem() == null)
|
||||
return;
|
||||
|
||||
if (e.getClickedInventory() == null)
|
||||
return;
|
||||
|
||||
String shop = ShopPlugin.getShopHandler().getShopNameByInvName(e.getInventory().getName());
|
||||
if (shop == null)
|
||||
return;
|
||||
|
||||
e.setCancelled(true);
|
||||
|
||||
Player p = (Player) e.getWhoClicked();
|
||||
int slot = e.getSlot();
|
||||
|
||||
if (slot > p.getOpenInventory().getTopInventory().getSize())
|
||||
return;
|
||||
|
||||
if (!p.getOpenInventory().getTopInventory().equals(e.getClickedInventory())) return;
|
||||
|
||||
if (slot == e.getInventory().getSize() - 1) {
|
||||
ShopCommand.open(p);
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.getCurrentItem().getType().equals(Material.AIR))
|
||||
return;
|
||||
|
||||
ShopItem si = ShopPlugin.getShopHandler().getShopItem(e.getCurrentItem().getType(),
|
||||
NBTApi.containsKey(e.getCurrentItem(), "damage") ?
|
||||
Integer.parseInt(NBTApi.getKey(e.getCurrentItem(), "damage")) :
|
||||
0,
|
||||
shop,
|
||||
NBTApi.containsKey(e.getCurrentItem(), "spawner") ? NBTApi.getKey(e.getCurrentItem(), "spawner") : null,
|
||||
NBTApi.containsKey(e.getCurrentItem(), "command") ? NBTApi.getKey(e.getCurrentItem(), "command") : null
|
||||
);
|
||||
if (si == null)
|
||||
return;
|
||||
|
||||
ClickType ct = e.getClick();
|
||||
if (ct.equals(ClickType.LEFT)) {
|
||||
if (si.getBuyPrice() <= 0)
|
||||
return;
|
||||
|
||||
|
||||
if (p.getInventory().firstEmpty() == -1) {
|
||||
p.sendMessage("§c§l(!)§c Your inventory is full!");
|
||||
return;
|
||||
}
|
||||
if (!take(p, si.getBuyPrice())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (si.isCommand()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), si.getCmd().replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (si.getSpawner() != null) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ("givespawner %player% " + si.getSpawner() + " 1").replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
p.getInventory().addItem(new ItemStack(si.getMaterial(), 1, (byte) si.getDamage()));
|
||||
} else if (ct.equals(ClickType.SHIFT_LEFT)) {
|
||||
if (si.isCommand())
|
||||
return;
|
||||
|
||||
|
||||
if (p.getInventory().firstEmpty() == -1) {
|
||||
p.sendMessage("§c§l(!)§c Your inventory is full!");
|
||||
return;
|
||||
}
|
||||
if (si.getBuyPrice() <= 0)
|
||||
return;
|
||||
|
||||
if (!take(p, si.getBuyPrice() * si.getMaterial().getMaxStackSize())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (si.isCommand()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), si.getCmd().replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
if (si.getSpawner() != null) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ("givespawner %player% " + si.getSpawner() + " " + 64).replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
p.getInventory().addItem(new ItemStack(si.getMaterial(), si.getMaterial().getMaxStackSize(), (byte) si.getDamage()));
|
||||
} else if (ct.equals(ClickType.MIDDLE)) {
|
||||
if (si.getBuyPrice() <= 0)
|
||||
return;
|
||||
|
||||
|
||||
if (p.getInventory().firstEmpty() == -1) {
|
||||
p.sendMessage("§c§l(!)§c Your inventory is full!");
|
||||
return;
|
||||
}
|
||||
if (si.isCommand())
|
||||
return;
|
||||
|
||||
int amount = Integer.parseInt(NBTApi.getKey(e.getCurrentItem(), "count"));
|
||||
|
||||
if (!take(p, si.getBuyPrice() * amount)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (si.isCommand()) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), si.getCmd().replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (si.getSpawner() != null) {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ("givespawner %player% " + si.getSpawner() + " " + amount).replaceAll("%player%", p.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
for (int times = 0; times < amount / si.getMaterial().getMaxStackSize(); times++) {
|
||||
p.getInventory().addItem(new ItemStack(si.getMaterial(), si.getMaterial().getMaxStackSize(), (byte) si.getDamage()));
|
||||
}
|
||||
} else if (ct.equals(ClickType.RIGHT)) {
|
||||
if (si.getSellPrice() <= 0)
|
||||
return;
|
||||
|
||||
if (si.isCommand())
|
||||
return;
|
||||
|
||||
if (!add(p, si.getSellPrice(), new ItemStack(si.getMaterial(), 1, (byte) si.getDamage()))) {
|
||||
return;
|
||||
}
|
||||
} else if (ct.equals(ClickType.SHIFT_RIGHT)) {
|
||||
if (si.getSellPrice() <= 0)
|
||||
return;
|
||||
|
||||
if (si.isCommand())
|
||||
return;
|
||||
|
||||
if (!add(p, si.getSellPrice(), new ItemStack(si.getMaterial(), si.getMaterial().getMaxStackSize(), (byte) si.getDamage()))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean take(Player p, double amount) {
|
||||
if (ShopPlugin.getEcon().getBalance(p) < amount) {
|
||||
p.sendMessage("§c§l(!) §cCannot afford this transaction!");
|
||||
return false;
|
||||
}
|
||||
|
||||
ShopPlugin.getEcon().withdrawPlayer(p, amount);
|
||||
p.sendMessage("§c§l- $" + NumberFormat.getInstance().format(amount) + "");
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean add(Player p, double amount, ItemStack item) {
|
||||
int count = 0;
|
||||
|
||||
Material m = item.getType();
|
||||
short dur = item.getDurability();
|
||||
for (ItemStack i : p.getInventory().getContents()) {
|
||||
if (i == null)
|
||||
continue;
|
||||
|
||||
if (!i.getEnchantments().isEmpty())
|
||||
continue;
|
||||
|
||||
if (i.hasItemMeta()) {
|
||||
ItemMeta im = i.getItemMeta();
|
||||
if (im.hasDisplayName())
|
||||
continue;
|
||||
if (im.hasLore())
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i.getType().equals(m) && i.getDurability() == dur) {
|
||||
count += i.getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
if (count < item.getAmount()) {
|
||||
p.sendMessage("§cYou do not have enough materials to sell.");
|
||||
return false;
|
||||
}
|
||||
|
||||
count = item.getAmount();
|
||||
|
||||
for (int i = 0; i < p.getInventory().getSize(); i++) {
|
||||
if (count <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
ItemStack it = p.getInventory().getItem(i);
|
||||
if (it == null)
|
||||
continue;
|
||||
|
||||
if (!it.getEnchantments().isEmpty())
|
||||
continue;
|
||||
|
||||
if (it.hasItemMeta()) {
|
||||
ItemMeta im = it.getItemMeta();
|
||||
if (im.hasDisplayName())
|
||||
continue;
|
||||
if (im.hasLore())
|
||||
continue;
|
||||
}
|
||||
|
||||
if (it.getType().equals(m) && it.getDurability() == dur) {
|
||||
if (count >= it.getAmount()) {
|
||||
count -= it.getAmount();
|
||||
p.getInventory().setItem(i, new ItemStack(Material.AIR));
|
||||
p.updateInventory();
|
||||
} else {
|
||||
it.setAmount(it.getAmount() - count);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ShopPlugin.getEcon().depositPlayer(p, amount * item.getAmount());
|
||||
p.sendMessage("§a§l+ $" + NumberFormat.getInstance().format(amount * item.getAmount()) + "");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.crankedpvp.shop.nbt;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class NBTApi {
|
||||
|
||||
public static ItemStack setKey(ItemStack i, String key, String value) {
|
||||
NBTItem il = new NBTItem(i);
|
||||
il.setString(key, value);
|
||||
return il.getItem();
|
||||
}
|
||||
|
||||
public static String getKey(ItemStack i, String key) {
|
||||
NBTItem il = new NBTItem(i);
|
||||
return il.getString(key);
|
||||
}
|
||||
|
||||
public static boolean containsKey(ItemStack i, String key) {
|
||||
NBTItem il = new NBTItem(i);
|
||||
return il.hasKey(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
shopMenuName: "&8Shop Menu"
|
||||
# Size of the main GUI, valid values are 9, 18, 27, 36, 45 and 54
|
||||
shopMenuSize: 18
|
||||
|
||||
# Sell hand command settings
|
||||
sellHand:
|
||||
# When set to true, players will be able to sell any quantity of the item (the price will be calculated from the base price)
|
||||
allowAllQuantites: true
|
||||
# When set to true, /sell hand will work same way as /sell handall does
|
||||
sellsAllItems: false
|
||||
|
||||
# Sell all command settings
|
||||
sellAll:
|
||||
# When set to true, players will get a detailed summary with prices of each item stack sold
|
||||
detailedSummary: false
|
||||
|
||||
buttons:
|
||||
goBack:
|
||||
item:
|
||||
# Material name, full list can be found here: http://wiki.brcdev.net/Materials
|
||||
material: Arrow
|
||||
# Amount of the item
|
||||
amount: 1
|
||||
# (optional) Data value, for example 1 for WOOD:1 means spruce wood planks
|
||||
damage: 0
|
||||
# (optional) Custom name
|
||||
name: "&c<- Go Back"
|
||||
# (optional) Lore, can contain multiple lines
|
||||
lore:
|
||||
- "&7Click to go back."
|
||||
# Slot in each shop's GUI
|
||||
slot: 49
|
||||
|
||||
shopMenuItems:
|
||||
1:
|
||||
item:
|
||||
material: POTION
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lPotion Shop"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "PotionShop"
|
||||
slot: 0
|
||||
2:
|
||||
item:
|
||||
material: REDSTONE
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lRaid Shop"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "RaidShop"
|
||||
slot: 1
|
||||
3:
|
||||
item:
|
||||
material: MOB_SPAWNER
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lSpawner Shop"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "Spawnershop"
|
||||
slot: 2
|
||||
4:
|
||||
item:
|
||||
material: SMOOTH_BRICK
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lBuilding Blocks"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "Buildingshop"
|
||||
slot: 3
|
||||
5:
|
||||
item:
|
||||
material: EMERALD
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lOres and Gems"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "Oreandgemshop"
|
||||
slot: 4
|
||||
6:
|
||||
item:
|
||||
material: COOKED_BEEF
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lFood and Farming"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "foodandfarmingshop"
|
||||
slot: 5
|
||||
7:
|
||||
item:
|
||||
material: BLAZE_ROD
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lMob Drops"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "MobdropShop"
|
||||
slot: 6
|
||||
8:
|
||||
item:
|
||||
material: EYE_OF_ENDER
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lSpeciality"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "specialityshop"
|
||||
slot: 7
|
||||
9:
|
||||
item:
|
||||
material: BREWING_STAND_ITEM
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lBrewing"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "BrewingShop"
|
||||
slot: 8
|
||||
10:
|
||||
item:
|
||||
material: WOOL
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lWool"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "WoolShop"
|
||||
slot: 11
|
||||
11:
|
||||
item:
|
||||
material: GLASS
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lGlass"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "GlassShop"
|
||||
slot: 12
|
||||
12:
|
||||
item:
|
||||
material: CLAY
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lClay"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "ClayShop"
|
||||
slot: 14
|
||||
13:
|
||||
item:
|
||||
material: RED_ROSE
|
||||
quantity: 1
|
||||
damage: 0
|
||||
name: "&a&lFlowers"
|
||||
lore:
|
||||
- "&7Click to view this category."
|
||||
shop: "FlowerShop"
|
||||
slot: 15
|
||||
|
||||
specialElements:
|
||||
balance:
|
||||
item:
|
||||
material: GOLD_INGOT
|
||||
quantity: 1
|
||||
name: "&6&lYour balance"
|
||||
lore:
|
||||
- "&7$%balance%"
|
||||
@@ -0,0 +1,12 @@
|
||||
name: CrankedShop
|
||||
main: net.crankedpvp.shop.ShopPlugin
|
||||
version: 1.0
|
||||
depend:
|
||||
- CrankedUtils
|
||||
- Vault
|
||||
- Essentials
|
||||
commands:
|
||||
shop:
|
||||
description: "Server Items Shop"
|
||||
sell:
|
||||
description: "Sell Inventory Items"
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user