Skip to content

Commit

Permalink
Version 2.4a
Browse files Browse the repository at this point in the history
    Bugfixes
        CEnchantingProbability is now a float
        Enchantment max size is now enforced on signs
        Enchantment max size is now correct
        Items now require permissions to buy
        Fixed Runecrafting adding duplicate enchants
        No longer cancelling drag-events without any reason
  • Loading branch information
Taiterio authored and Taiterio committed Jan 31, 2016
1 parent d78fc7c commit b763da2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 117 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CustomEnchantments
main: com.taiter.ce.Main
version: 2.4
version: 2.4a

softdepend: [WorldGuard, WorldEdit, Vault]

Expand Down
188 changes: 98 additions & 90 deletions src/com/taiter/ce/CEListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ else if (useRuneCrafting)
CEventHandler.updateRunecraftingInventory(event.getInventory());
return;
}
event.setCancelled(true);
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -232,20 +231,20 @@ public void inventoryMenu(final InventoryClickEvent event) {

// Opens the item inventory and loads the permissions if needed
if (topInv.getTitle().equals(Tools.prefix + "Main Menu"))
if(event.getRawSlot() == 4) {
p.closeInventory();
p.openInventory(Tools.getItemMenu(p));
return;
} else if(event.getRawSlot() == 6) {
if(p.hasPermission("ce.*") || p.hasPermission("ce.runecrafting")) {
if (event.getRawSlot() == 4) {
p.closeInventory();
p.openInventory(Tools.getNextInventory(clickedItem.getItemMeta().getDisplayName()));
return;
} else {
p.sendMessage(ChatColor.RED + "You do not have permission to use this!");
p.openInventory(Tools.getItemMenu(p));
return;
} else if (event.getRawSlot() == 6) {
if (p.hasPermission("ce.*") || p.hasPermission("ce.runecrafting")) {
p.closeInventory();
p.openInventory(Tools.getNextInventory(clickedItem.getItemMeta().getDisplayName()));
return;
} else {
p.sendMessage(ChatColor.RED + "You do not have permission to use this!");
return;
}
}
}

// These are the specific menus, clicking one of them will lead
// to the enchanting menu, which needs to be 'notified' of the
Expand Down Expand Up @@ -292,9 +291,14 @@ public void inventoryMenu(final InventoryClickEvent event) {
return;
}

if (topInv.getTitle().equals(Tools.prefix + "Items"))
if (topInv.getTitle().equals(Tools.prefix + "Items")) {
CItem ci = Tools.getItemByDisplayname(clickedItem.getItemMeta().getDisplayName());
if(!p.hasPermission("ce.item.*") && !p.hasPermission("ce.item." + ci.getOriginalName())) {
p.sendMessage(ChatColor.RED + "You do not have permission to buy this Item!");
return;
}

if (p.getInventory().firstEmpty() != -1) {
CItem ci = Tools.getItemByDisplayname(clickedItem.getItemMeta().getDisplayName());
double cost = ci.getCost();
//Check cost
if (Main.hasEconomy && !p.isOp() && cost > 0) {
Expand Down Expand Up @@ -327,7 +331,8 @@ public void inventoryMenu(final InventoryClickEvent event) {
p.sendMessage(ChatColor.RED + "You do not have enough space in your inventory!");
return;
}

}

if (topInv.getTitle().equals(Tools.prefix + "Level selection")) {
String enchantmentName = clickedItem.getItemMeta().getDisplayName();
CEnchantment ce = EnchantManager.getEnchantment(enchantmentName);
Expand Down Expand Up @@ -508,7 +513,7 @@ else if (e.getAction().toString().startsWith("RIGHT")) {
if (e.getClickedBlock() != null && e.getClickedBlock().getType().equals(Material.ANVIL)) {
ItemStack i = p.getItemInHand();
if (EnchantManager.hasEnchantments(i) || EnchantManager.isEnchantmentBook(i)) {
if(!p.hasPermission("ce.*") && !p.hasPermission("ce.runecrafting"))
if (!p.hasPermission("ce.*") && !p.hasPermission("ce.runecrafting"))
return;
e.setCancelled(true);
p.setItemInHand(new ItemStack(Material.AIR));
Expand All @@ -534,95 +539,98 @@ else if (e.getAction().toString().startsWith("RIGHT")) {
// Sign shop
if (e.getClickedBlock() != null && e.getClickedBlock().getType().toString().contains("SIGN"))
if (((Sign) e.getClickedBlock().getState()).getLine(0).equals("[CustomEnchant]")) {
if (!Main.hasEconomy) {
p.performCommand("ce menu");
} else if (p.getItemInHand().getType() != Material.AIR) {
Sign sign = ((Sign) e.getClickedBlock().getState());
CEnchantment ce = EnchantManager.getEnchantment(sign.getLine(1));
if (ce == null)
EnchantManager.getEnchantment(sign.getLine(1));
if (ce == null)
for (CEnchantment ceT : EnchantManager.getEnchantments())
if (EnchantManager.containsEnchantment(sign.getLine(1), ceT))
ce = ceT;
if (ce == null)
return;
if (Main.hasEconomy)
if (p.getItemInHand().getType() != Material.AIR) {
Sign sign = ((Sign) e.getClickedBlock().getState());
CEnchantment ce = EnchantManager.getEnchantment(sign.getLine(1));
if (ce == null)
EnchantManager.getEnchantment(sign.getLine(1));
if (ce == null)
for (CEnchantment ceT : EnchantManager.getEnchantments())
if (EnchantManager.containsEnchantment(sign.getLine(1), ceT))
ce = ceT;
if (ce == null)
return;

ItemStack inHand = p.getItemInHand();
if (!Tools.isApplicable(inHand, ce)) {
p.sendMessage(ChatColor.RED + "This enchantment can not be applied to this item.");
return;
}
ItemStack inHand = p.getItemInHand();
if (!Tools.isApplicable(inHand, ce)) {
p.sendMessage(ChatColor.RED + "This enchantment can not be applied to this item.");
return;
}

int cost = 0;
try {
cost = Integer.parseInt(sign.getLine(3).replaceAll("\\D+", ""));
} catch (NumberFormatException ex) {
return;
}
int cost = 0;
try {
cost = Integer.parseInt(sign.getLine(3).replaceAll("\\D+", ""));
} catch (NumberFormatException ex) {
return;
}

if (inHand.hasItemMeta() && inHand.getItemMeta().hasLore()) {
List<String> lore = inHand.getItemMeta().getLore();
for (int i = 0; i < lore.size(); i++)
if (EnchantManager.containsEnchantment(lore.get(i), ce)) {
int newLevel = EnchantManager.getLevel(lore.get(i)) + 1;
if (newLevel <= ce.getEnchantmentMaxLevel()) {
if (Main.econ.getBalance(p.getName()) >= cost) {
EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
if (ecr.transactionSuccess()) {
p.sendMessage(ChatColor.GREEN + "Upgraded enchantment " + ce.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.WHITE + cost + " "
+ ((cost == 1) ? Main.econ.currencyNameSingular() : Main.econ.currencyNamePlural()) + ChatColor.GREEN + ".");
List<String> lore = new ArrayList<String>();
ItemMeta im = inHand.getItemMeta();

if (inHand.getItemMeta().hasLore()) {
lore = inHand.getItemMeta().getLore();

if (EnchantManager.getEnchantments(lore).size() == EnchantManager.getMaxEnchants()) {
p.sendMessage(ChatColor.RED + "You already have the maximum amount of enchantments!");
return;
}

for (int i = 0; i < lore.size(); i++)
if (EnchantManager.containsEnchantment(lore.get(i), ce)) {
int newLevel = EnchantManager.getLevel(lore.get(i)) + 1;
if (newLevel <= ce.getEnchantmentMaxLevel()) {
if (Main.econ.getBalance(p.getName()) >= cost) {
EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
if (ecr.transactionSuccess()) {
p.sendMessage(ChatColor.GREEN + "Upgraded enchantment " + ce.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.WHITE + cost + " "
+ ((cost == 1) ? Main.econ.currencyNameSingular() : Main.econ.currencyNamePlural()) + ChatColor.GREEN + ".");
} else {
p.sendMessage(ChatColor.RED + "An economy error has occured:");
p.sendMessage(ChatColor.RED + ecr.errorMessage);
return;
}
} else {
p.sendMessage(ChatColor.RED + "An economy error has occured:");
p.sendMessage(ChatColor.RED + ecr.errorMessage);
p.sendMessage(ChatColor.RED + "You do not have enough money to buy this!");
return;
}
lore.set(i, ce.getDisplayName() + " " + EnchantManager.intToLevel(newLevel));
im.setLore(lore);
inHand.setItemMeta(im);
return;
} else {
p.sendMessage(ChatColor.RED + "You do not have enough money to buy this!");
p.sendMessage(ChatColor.RED + "You already have the maximum level of this enchantment!");
return;
}
lore.set(i, ce.getDisplayName() + " " + EnchantManager.intToLevel(newLevel));
ItemMeta im = inHand.getItemMeta();
im.setLore(lore);
inHand.setItemMeta(im);
return;
} else {
p.sendMessage(ChatColor.RED + "You already have the maximum level of this enchantment!");
return;
}
}
}

List<String> lore = new ArrayList<String>();
ItemMeta im = inHand.getItemMeta();

if (inHand.getItemMeta().hasLore())
lore = im.getLore();
}

if (Main.econ.getBalance(p.getName()) >= cost) {
EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
if (ecr.transactionSuccess())
p.sendMessage(ChatColor.GREEN + "Bought enchantment " + ce.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.WHITE + cost + " "
+ ((cost == 1) ? Main.econ.currencyNameSingular() : Main.econ.currencyNamePlural()) + ChatColor.GREEN + ".");
else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "An economy error has occured:");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + ecr.errorMessage);
if (Main.econ.getBalance(p.getName()) >= cost) {
EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
if (ecr.transactionSuccess())
p.sendMessage(ChatColor.GREEN + "Bought enchantment " + ce.getDisplayName() + ChatColor.GREEN + " for " + ChatColor.WHITE + cost + " "
+ ((cost == 1) ? Main.econ.currencyNameSingular() : Main.econ.currencyNamePlural()) + ChatColor.GREEN + ".");
else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "An economy error has occured:");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + ecr.errorMessage);
return;
}
} else {
p.sendMessage(ChatColor.RED + "You do not have enough money to buy this!");
return;
}

lore.add(ce.getDisplayName() + " I");
im.setLore(lore);
inHand.setItemMeta(im);
if (!inHand.containsEnchantment(EnchantManager.getGlowEnchantment()))
inHand.addUnsafeEnchantment(EnchantManager.getGlowEnchantment(), 0);
return;

} else {
p.sendMessage(ChatColor.RED + "You do not have enough money to buy this!");
p.sendMessage(ChatColor.RED + "You do not have an item in your hand.");
return;
}

lore.add(ce.getDisplayName() + " I");
im.setLore(lore);
inHand.setItemMeta(im);
return;

} else {
p.sendMessage(ChatColor.RED + "You do not have an item in your hand.");
return;
}
}

}
Expand Down Expand Up @@ -745,7 +753,7 @@ public void SignChangeEvent(SignChangeEvent e) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void EnchantItemEvent(EnchantItemEvent e) {
if (e.getExpLevelCost() == 30)
if (Tools.random.nextInt(100) < (Integer.parseInt(Main.config.getString("Global.Enchantments.CEnchantingProbability"))))
if (Tools.random.nextInt(100) < (Float.parseFloat(Main.config.getString("Global.Enchantments.CEnchantingProbability"))))
CEventHandler.handleEnchanting(e);
}

Expand Down
26 changes: 14 additions & 12 deletions src/com/taiter/ce/CEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,39 +537,41 @@ public void run() {

if (EnchantManager.isEnchantmentBook(bot)) {
ItemStack item = top.clone();
HashMap<CEnchantment, Integer> list = EnchantManager.getEnchantmentLevels(bot.getItemMeta().getLore());
HashMap<CEnchantment, Integer> botList = EnchantManager.getEnchantmentLevels(bot.getItemMeta().getLore());
HashMap<CEnchantment, Integer> topList = EnchantManager.getEnchantmentLevels(top.getItemMeta().getLore());

if (stackEnchantments) {
if (EnchantManager.isEnchantmentBook(top)) {
HashMap<CEnchantment, Integer> topList = EnchantManager.getEnchantmentLevels(top.getItemMeta().getLore());
for (CEnchantment ce : topList.keySet())
if (list.containsKey(ce)) {
int newLevel = list.get(ce) + topList.get(ce);
if (botList.containsKey(ce)) {
int newLevel = botList.get(ce) + topList.get(ce);
if (newLevel > ce.getEnchantmentMaxLevel())
newLevel = ce.getEnchantmentMaxLevel();
list.replace(ce, newLevel);
botList.replace(ce, newLevel);
} else {
if (list.size() < EnchantManager.getMaxEnchants())
list.put(ce, topList.get(ce));
if (botList.size() < EnchantManager.getMaxEnchants())
botList.put(ce, topList.get(ce));
else
break;
}
inv.setItem(2, EnchantManager.getEnchantBook(list));
inv.setItem(2, EnchantManager.getEnchantBook(botList));
return;
}
} else if (EnchantManager.hasEnchantments(top) || EnchantManager.isEnchantmentBook(top)) {
return;
}

for (CEnchantment ce : list.keySet())
if (Tools.isApplicable(item, ce))
item = EnchantManager.addEnchant(item, ce, list.get(ce));
if (item.getEnchantments().size() > 0) {
for (CEnchantment ce : botList.keySet())
if (!topList.containsKey(ce) && Tools.isApplicable(item, ce))
item = EnchantManager.addEnchant(item, ce, botList.get(ce));
if (EnchantManager.getEnchantments(item.getItemMeta().getLore()).size() > topList.size()) {
inv.setItem(2, item);
} else {
ItemMeta im = item.getItemMeta();
im.setDisplayName(ChatColor.DARK_RED + "Incompatible Enchantment");
im.setLore(new ArrayList<String>());
item.setItemMeta(im);
item.setType(Material.BARRIER);
inv.setItem(2, item);
}
return;
Expand Down
2 changes: 1 addition & 1 deletion src/com/taiter/ce/CeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public void run() {
if (!cEnchants.isEmpty()) {
HashMap<CEnchantment, Integer> list = new HashMap<CEnchantment, Integer>();
for (String e : cEnchants) {
list.put(EnchantManager.getEnchantment(e), EnchantManager.getLevel(e));
list.put(EnchantManager.getEnchantment(e), Integer.parseInt(e.split(" ")[1]));
}

if (newItem.getType().equals(Material.BOOK))
Expand Down
Loading

0 comments on commit b763da2

Please sign in to comment.