Skip to content

Commit

Permalink
Added economy support through Vault
Browse files Browse the repository at this point in the history
Closes #3
Closes #4
  • Loading branch information
dentych committed Sep 2, 2015
1 parent 790b400 commit 4d37587
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/me/tychsen/enchantgui/EshopEventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void handleEnchantPage(Player p, ItemStack item, int slot) {
break;
default:
if (item.getType() != Material.AIR) {
esys.purchaseEnchant(p, item);
esys.purchaseEnchant(p, item, slot+1);
}
break;
}
Expand Down
28 changes: 25 additions & 3 deletions src/me/tychsen/enchantgui/EshopSystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.tychsen.enchantgui;

import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
Expand All @@ -18,13 +19,15 @@ public class EshopSystem {
private EshopEnchants enchants;
private EshopPermissionSys permsys;
private EshopConfig config;
private EshopVault vault;

public EshopSystem(JavaPlugin plugin) {
playerNavigation = new HashMap<>();
inventorySize = 36;
enchants = new EshopEnchants();
permsys = new EshopPermissionSys();
config = new EshopConfig(plugin);
vault = new EshopVault(plugin);
}

public void showMainMenu(Player p) {
Expand Down Expand Up @@ -53,9 +56,28 @@ public int getPlayerMenuLevel(Player p) {
}
}

public void purchaseEnchant(Player p, ItemStack item) {
p.getServer().getLogger().info("Player " + ChatColor.GREEN + p.getName() + ChatColor.RESET +
" bought an enchant!");
public void purchaseEnchant(Player p, ItemStack item, int level) {
Enchantment ench = item.getEnchantments().keySet().toArray(new Enchantment[1])[0];
ItemStack playerHand = p.getItemInHand();
int price = config.getPrice(ench, level);
String start = ChatColor.AQUA + "[EnchantGUI] " + ChatColor.WHITE;

if (playerHand == null || playerHand.getType() == Material.AIR) {
p.sendMessage(start + "You can't enchant that!");
p.closeInventory();
return;
}

if (ench.canEnchantItem(playerHand)) {
if (vault.withdrawMoney(p, price))
playerHand.addEnchantment(ench, level);
else {
p.sendMessage(start + "Insufficient funds.");
}
}
else {
p.sendMessage(start + "Enchant can't be applied to the item in your hand...");
}
}

private void generateMainMenu(Player p, Inventory inv) {
Expand Down
41 changes: 38 additions & 3 deletions src/me/tychsen/enchantgui/EshopVault.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package me.tychsen.enchantgui;

/**
* Created by Dennis on 02/09/2015.
*/
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class EshopVault {
private Economy econ = null;
private JavaPlugin plugin;

public EshopVault(JavaPlugin plugin) {
this.plugin = plugin;

if (!setupEconomy()) {
plugin.getLogger().severe("Dependency (Vault) not found. Disabling the plugin!");
plugin.getServer().getPluginManager().disablePlugin(plugin);
}
}

public boolean withdrawMoney(Player p, int amount) {
EconomyResponse r = econ.withdrawPlayer(p, amount);

if (r.transactionSuccess())
return true;
else
return false;
}

private boolean setupEconomy() {
if (plugin.getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = plugin.getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = rsp.getProvider();
return econ != null;
}
}
9 changes: 4 additions & 5 deletions src/me/tychsen/enchantgui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ public void onInventoryClickEvent(InventoryClickEvent e) {
if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR)
return;

getLogger().severe(e.getCurrentItem().toString());
try {
//try {
eshop.handleInventoryClickEvent(e);
} catch (Exception exc) {
getLogger().severe("EXCEPTION: " + exc.getMessage());
}
//} catch (Exception exc) {
// getLogger().severe("EXCEPTION: " + exc.getMessage());
//}
}

@Override
Expand Down

0 comments on commit 4d37587

Please sign in to comment.