Skip to content

Commit

Permalink
Enchantments via bukkit api. Get rid of NMS crap.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haarolean committed Oct 31, 2017
1 parent 50ff9d5 commit f8bfca1
Showing 1 changed file with 32 additions and 63 deletions.
95 changes: 32 additions & 63 deletions src/com/github/toxuin/griswold/Interactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;

import static com.github.toxuin.griswold.Griswold.log;

Expand Down Expand Up @@ -164,6 +162,7 @@ private boolean isInteger(String number) {
private final Set<Interaction> interactions = new HashSet<>();

@SuppressWarnings("unchecked")
//noinspection Duplicates
void interact(Player player, Repairer repairman) {
final ItemStack item = player.getInventory().getItemInMainHand();

Expand Down Expand Up @@ -218,6 +217,8 @@ void interact(Player player, Repairer repairman) {
player.sendMessage(String.format(Lang.name_format, repairman.name) + ChatColor.RED + Lang.chat_error);
return;
}

// ENCHANTMENTS
price = addEnchantmentPrice;
EconomyResponse r = null;
if (Griswold.economy != null && Griswold.economy.getBalance(player) < price) {
Expand All @@ -234,70 +235,32 @@ void interact(Player player, Repairer repairman) {
if (clearEnchantments) item.getEnchantments().forEach((enchantToDel, integer) ->
item.removeEnchantment(enchantToDel));


try {
Method asNMSCopy = craftItemStack.getMethod("asNMSCopy", ItemStack.class);
Object vanillaItem = asNMSCopy.invoke(null, (item.getType().equals(Material.ENCHANTED_BOOK)) ? new ItemStack(Material.BOOK) : item);
int bonus = (new Random()).nextInt(maxEnchantBonus);
Method b;
List<?> list;
if (Griswold.apiVersion.getMajor() >= 1 && Griswold.apiVersion.getMinor() >= 9) {
b = enchantmentManager.getDeclaredMethod("b", Random.class, vanillaItem.getClass(), int.class, boolean.class);
list = (List) b.invoke(null, new Random(), vanillaItem, bonus, false);
} else {
b = enchantmentManager.getDeclaredMethod("b", Random.class, vanillaItem.getClass(), int.class);
list = (List) b.invoke(null, new Random(), vanillaItem, bonus);
}

EnchantmentStorageMeta bookmeta = null;
if (item.getType().equals(Material.BOOK) || item.getType().equals(Material.ENCHANTED_BOOK)) {
// BOOKS
ItemMeta bookmeta = item.getItemMeta();
;
ItemStack bookLeftovers = null;
if (item.getType().equals(Material.BOOK)) {
if (item.getAmount() > 1)
bookLeftovers = new ItemStack(Material.BOOK, item.getAmount() - 1);
player.getInventory().remove(item);
item.setType(Material.ENCHANTED_BOOK);
item.setAmount(1);
bookmeta = (EnchantmentStorageMeta) item.getItemMeta();
} else if (item.getType().equals(Material.ENCHANTED_BOOK)) {
bookmeta = (EnchantmentStorageMeta) item.getItemMeta();
}

if (list == null) {
inter.valid = false; // INVALIDATE INTERACTION
player.sendMessage(String.format(Lang.name_format, repairman.name) + Lang.chat_enchant_failed);
return;
if (item.getAmount() > 1) {
bookLeftovers = new ItemStack(Material.BOOK, item.getAmount() - 1);
item.setAmount(1);
bookLeftovers.setItemMeta(bookmeta);
}

for (Object obj : list) {
Object instance = enchantmentInstance.cast(obj);
Field enchantmentField = enchantmentInstance.getField("enchantment");
Field levelField = enchantmentInstance.getField("level");
Object enchantment = enchantmentField.get(instance);
Field idField = enchantment.getClass().getField("id");
if (item.getType().equals(Material.ENCHANTED_BOOK) && bookmeta != null) {
bookmeta.addStoredEnchant(Enchantment.getById(Integer.parseInt(idField.get(enchantment).toString())), Integer.parseInt(levelField.get(instance).toString()), true);
} else {
item.addEnchantment(Enchantment.getById(Integer.parseInt(idField.get(enchantment).toString())), Integer.parseInt(levelField.get(instance).toString()));
}
}
if (item.getType().equals(Material.BOOK)) item.setType(Material.ENCHANTED_BOOK);
applyRandomEnchantment(item, true);

if (item.getType().equals(Material.ENCHANTED_BOOK)) {
item.setItemMeta(bookmeta);
player.getInventory().setItemInMainHand(item);
if (bookLeftovers != null) {
if (player.getInventory().firstEmpty() == -1) { // INVENTORY FULL, DROP TO PLAYER
player.getWorld().dropItemNaturally(player.getLocation(), bookLeftovers);
} else player.getInventory().addItem(bookLeftovers);
}
if (bookLeftovers != null) {
if (player.getInventory().firstEmpty() == -1) { // INVENTORY FULL, DROP TO PLAYER
player.getWorld().dropItemNaturally(player.getLocation(), bookLeftovers);
} else player.getInventory().addItem(bookLeftovers);
}
} else applyRandomEnchantment(item, false);

inter.valid = false; // INVALIDATE INTERACTION
player.sendMessage(String.format(Lang.name_format, repairman.name) + Lang.chat_enchant_success);
return;

} catch (Exception e) {
Griswold.log.log(Level.SEVERE, "AN EXCEPTON OCCURRED, REPORT THIS STACKTRACE TO DEVELOPERS", e);
}
inter.valid = false; // INVALIDATE INTERACTION
player.sendMessage(String.format(Lang.name_format, repairman.name) + Lang.chat_enchant_success);
return;
}


Expand All @@ -307,7 +270,7 @@ void interact(Player player, Repairer repairman) {

if (item.getDurability() != 0) {
// NEEDS REPAIR
if (!repairman.getType().equals(RepairerType.ENCHANT)&& !repairman.getType().equals(RepairerType.ALL)) {
if (!repairman.getType().equals(RepairerType.ENCHANT) && !repairman.getType().equals(RepairerType.ALL)) {
// CANNOT REPAIR
player.sendMessage(String.format(Lang.name_format, repairman.name) + Lang.chat_needs_repair);
return;
Expand Down Expand Up @@ -394,11 +357,11 @@ private double getPrice(Repairer repairman, ItemStack item) {
return price * repairman.getCost();
}

private void applyRandomEnchantment(ItemStack item) {
private void applyRandomEnchantment(ItemStack item, boolean isBook) {
List<Enchantment> candidates = new ArrayList<>();

Arrays.asList(Enchantment.values()).forEach((ench) -> { // I guess collections are overrated in bukkit
if (ench.canEnchantItem(item)) candidates.add(ench);
if (ench.canEnchantItem(item) || isBook) candidates.add(ench);
});

item.getEnchantments().keySet().forEach((applied) ->
Expand All @@ -410,7 +373,13 @@ private void applyRandomEnchantment(ItemStack item) {
final int level = (ench.getStartLevel() == ench.getMaxLevel()) ? ench.getMaxLevel() :
ThreadLocalRandom.current().nextInt(ench.getStartLevel(), ench.getMaxLevel());

item.addEnchantment(ench, level);
if (!isBook) {
item.addEnchantment(ench, level);
return;
}
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
meta.addStoredEnchant(ench, level, true);
item.setItemMeta(meta);
}

protected void finalize() throws Throwable {
Expand Down

0 comments on commit f8bfca1

Please sign in to comment.