refactor
This commit is contained in:
parent
a9527f7677
commit
351eeb1544
@ -1,5 +1,5 @@
|
|||||||
name: Blacksmith
|
name: Blacksmith
|
||||||
author: aPunch
|
author: aPunch
|
||||||
version: 1.0
|
version: 1.0
|
||||||
main: net.apunch.blacksmith.Blacksmith
|
main: net.apunch.blacksmith.BlacksmithPlugin
|
||||||
depend: [Citizens, Vault]
|
depend: [Citizens, Vault]
|
@ -1,140 +1,241 @@
|
|||||||
package net.apunch.blacksmith;
|
package net.apunch.blacksmith;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
import net.apunch.blacksmith.util.Settings;
|
import java.util.HashMap;
|
||||||
import net.apunch.blacksmith.util.Settings.Setting;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import net.citizensnpcs.api.CitizensAPI;
|
import java.util.Random;
|
||||||
import net.citizensnpcs.api.util.DataKey;
|
|
||||||
import net.milkbowl.vault.economy.Economy;
|
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
|
|
||||||
public class Blacksmith extends JavaPlugin {
|
import net.apunch.blacksmith.util.Settings.Setting;
|
||||||
private Settings config;
|
|
||||||
private Economy economy;
|
|
||||||
|
|
||||||
@Override
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
public void onDisable() {
|
import net.citizensnpcs.api.trait.Character;
|
||||||
config.save();
|
import net.citizensnpcs.api.trait.SaveId;
|
||||||
|
import net.citizensnpcs.api.util.DataKey;
|
||||||
|
|
||||||
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
|
@SaveId("blacksmith")
|
||||||
|
public class Blacksmith extends Character {
|
||||||
|
private static final int[] enchantments = new int[Enchantment.values().length];
|
||||||
|
|
||||||
|
private final BlacksmithPlugin plugin;
|
||||||
|
private final List<Material> reforgeableItems = new ArrayList<Material>();
|
||||||
|
private final Map<String, Calendar> cooldowns = new HashMap<String, Calendar>();
|
||||||
|
private ReforgeSession session;
|
||||||
|
|
||||||
|
// Defaults
|
||||||
|
private String busyWithPlayerMsg = Setting.BUSY_WITH_PLAYER_MESSAGE.asString();
|
||||||
|
private String busyReforgingMsg = Setting.BUSY_WITH_REFORGE_MESSAGE.asString();
|
||||||
|
private String costMsg = Setting.COST_MESSAGE.asString();
|
||||||
|
private String invalidItemMsg = Setting.INVALID_ITEM_MESSAGE.asString();
|
||||||
|
private String startReforgeMsg = Setting.START_REFORGE_MESSAGE.asString();
|
||||||
|
private String successMsg = Setting.SUCCESS_MESSAGE.asString();
|
||||||
|
private String failMsg = Setting.FAIL_MESSAGE.asString();
|
||||||
|
private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString();
|
||||||
|
private String cooldownUnexpiredMsg = Setting.COOLDOWN_UNEXPIRED_MESSAGE.asString();
|
||||||
|
private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt();
|
||||||
|
private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt();
|
||||||
|
private int reforgeCooldown = Setting.REFORGE_COOLDOWN.asInt();
|
||||||
|
private int failChance = Setting.FAIL_CHANCE.asInt();
|
||||||
|
private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt();
|
||||||
|
private boolean dropItem = Setting.DROP_ITEM.asBoolean();
|
||||||
|
|
||||||
|
public Blacksmith() {
|
||||||
|
plugin = (BlacksmithPlugin) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith");
|
||||||
|
int i = 0;
|
||||||
|
for (Enchantment enchantment : Enchantment.values())
|
||||||
|
enchantments[i++] = enchantment.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void load(DataKey key) {
|
||||||
config = new Settings(this);
|
for (DataKey sub : key.getRelative("reforgeable-items").getIntegerSubKeys())
|
||||||
config.load();
|
if (Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')) != null)
|
||||||
|
reforgeableItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')));
|
||||||
|
|
||||||
// Setup Vault
|
// Override defaults if they exist
|
||||||
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(
|
if (key.keyExists("messages.busy-with-player"))
|
||||||
Economy.class);
|
busyWithPlayerMsg = key.getString("messages.busy-with-player");
|
||||||
if (economyProvider != null)
|
if (key.keyExists("messages.busy-with-reforge"))
|
||||||
economy = economyProvider.getProvider();
|
busyReforgingMsg = key.getString("messages.busy-with-reforge");
|
||||||
|
if (key.keyExists("messages.cost"))
|
||||||
CitizensAPI.getCharacterManager().register(BlacksmithCharacter.class);
|
costMsg = key.getString("messages.cost");
|
||||||
|
if (key.keyExists("messages.invalid-item"))
|
||||||
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
|
invalidItemMsg = key.getString("messages.invalid-item");
|
||||||
|
if (key.keyExists("messages.start-reforge"))
|
||||||
|
startReforgeMsg = key.getString("messages.start-reforge");
|
||||||
|
if (key.keyExists("messages.successful-reforge"))
|
||||||
|
successMsg = key.getString("messages.successful-reforge");
|
||||||
|
if (key.keyExists("messages.fail-reforge"))
|
||||||
|
failMsg = key.getString("messages.fail-reforge");
|
||||||
|
if (key.keyExists("messages.insufficient-funds"))
|
||||||
|
insufficientFundsMsg = key.getString("messages.insufficient-funds");
|
||||||
|
if (key.keyExists("messages.cooldown-not-expired"))
|
||||||
|
cooldownUnexpiredMsg = key.getString("messages.cooldown-not-expired");
|
||||||
|
if (key.keyExists("delays-in-seconds.minimum"))
|
||||||
|
minReforgeDelay = key.getInt("delays-in-seconds.minimum");
|
||||||
|
if (key.keyExists("delays-in-seconds.maximum"))
|
||||||
|
maxReforgeDelay = key.getInt("delays-in-seconds.maximum");
|
||||||
|
if (key.keyExists("delays-in-seconds.reforge-cooldown"))
|
||||||
|
reforgeCooldown = key.getInt("delays-in-seconds.reforge-cooldown");
|
||||||
|
if (key.keyExists("percent-chance-to-fail-reforge"))
|
||||||
|
failChance = key.getInt("percent-chance-to-fail-reforge");
|
||||||
|
if (key.keyExists("maximum-enchantments"))
|
||||||
|
maxEnchantments = key.getInt("maximum-enchantments");
|
||||||
|
if (key.keyExists("drop-item"))
|
||||||
|
dropItem = key.getBoolean("drop-item");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTool(ItemStack item) {
|
@Override
|
||||||
switch (item.getType()) {
|
public void onRightClick(NPC npc, Player player) {
|
||||||
case WOOD_PICKAXE:
|
if (!player.hasPermission("blacksmith.reforge"))
|
||||||
case WOOD_SPADE:
|
return;
|
||||||
case WOOD_HOE:
|
|
||||||
case WOOD_SWORD:
|
if (cooldowns.get(player.getName()) != null) {
|
||||||
case WOOD_AXE:
|
if (!Calendar.getInstance().after(cooldowns.get(player.getName()))) {
|
||||||
case STONE_PICKAXE:
|
npc.chat(player, cooldownUnexpiredMsg);
|
||||||
case STONE_SPADE:
|
return;
|
||||||
case STONE_HOE:
|
}
|
||||||
case STONE_SWORD:
|
cooldowns.remove(player.getName());
|
||||||
case STONE_AXE:
|
}
|
||||||
case GOLD_PICKAXE:
|
|
||||||
case GOLD_SPADE:
|
ItemStack hand = player.getItemInHand();
|
||||||
case GOLD_HOE:
|
if (session != null) {
|
||||||
case GOLD_SWORD:
|
if (!session.isInSession(player)) {
|
||||||
case GOLD_AXE:
|
npc.chat(busyWithPlayerMsg);
|
||||||
case IRON_PICKAXE:
|
return;
|
||||||
case IRON_SPADE:
|
}
|
||||||
case IRON_HOE:
|
|
||||||
case IRON_SWORD:
|
if (session.isRunning()) {
|
||||||
case IRON_AXE:
|
npc.chat(player, busyReforgingMsg);
|
||||||
case DIAMOND_PICKAXE:
|
return;
|
||||||
case DIAMOND_SPADE:
|
}
|
||||||
case DIAMOND_HOE:
|
if (session.handleClick())
|
||||||
case DIAMOND_SWORD:
|
session = null;
|
||||||
case DIAMOND_AXE:
|
else
|
||||||
case BOW:
|
reforge(npc, player);
|
||||||
case FLINT_AND_STEEL:
|
} else {
|
||||||
case FISHING_ROD:
|
if ((!plugin.isTool(hand) && !plugin.isArmor(hand))
|
||||||
case SHEARS:
|
|| (!reforgeableItems.isEmpty() && !reforgeableItems.contains(hand.getType()))) {
|
||||||
|
npc.chat(player, invalidItemMsg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
session = new ReforgeSession(player, npc);
|
||||||
|
npc.chat(player, costMsg.replace("<price>", plugin.formatCost(player)).replace("<item>",
|
||||||
|
hand.getType().name().toLowerCase().replace('_', ' ')));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(DataKey key) {
|
||||||
|
for (int i = 0; i < reforgeableItems.size(); i++)
|
||||||
|
key.getRelative("reforgeable-items").setString(String.valueOf(i),
|
||||||
|
reforgeableItems.get(i).name().toLowerCase().replace('_', '-'));
|
||||||
|
|
||||||
|
key.setString("messages.busy-with-player", busyWithPlayerMsg);
|
||||||
|
key.setString("messages.busy-with-reforge", busyReforgingMsg);
|
||||||
|
key.setString("messages.cost", costMsg);
|
||||||
|
key.setString("messages.invalid-item", invalidItemMsg);
|
||||||
|
key.setString("messages.start-reforge", startReforgeMsg);
|
||||||
|
key.setString("messages.successful-reforge", successMsg);
|
||||||
|
key.setString("messages.fail-reforge", failMsg);
|
||||||
|
key.setString("messages.insufficient-funds", insufficientFundsMsg);
|
||||||
|
key.setString("messages.cooldown-not-expired", cooldownUnexpiredMsg);
|
||||||
|
key.setInt("delays-in-seconds.minimum", minReforgeDelay);
|
||||||
|
key.setInt("delays-in-seconds.maximum", maxReforgeDelay);
|
||||||
|
key.setInt("delays-in-seconds.reforge-cooldown", reforgeCooldown);
|
||||||
|
key.setInt("percent-chance-to-fail-reforge", failChance);
|
||||||
|
key.setInt("maximum-enchantments", maxEnchantments);
|
||||||
|
key.setBoolean("drop-item", dropItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInsufficientFundsMessage() {
|
||||||
|
return insufficientFundsMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reforge(NPC npc, Player player) {
|
||||||
|
npc.chat(player, startReforgeMsg);
|
||||||
|
plugin.withdraw(player);
|
||||||
|
session.setTask(plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin,
|
||||||
|
new ReforgeTask(npc, player), (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20));
|
||||||
|
if (npc.getBukkitEntity() instanceof Player)
|
||||||
|
((Player) npc.getBukkitEntity()).setItemInHand(player.getItemInHand());
|
||||||
|
player.setItemInHand(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReforgeTask implements Runnable {
|
||||||
|
private final NPC npc;
|
||||||
|
private final Player player;
|
||||||
|
private final ItemStack reforge;
|
||||||
|
|
||||||
|
private ReforgeTask(NPC npc, Player player) {
|
||||||
|
this.npc = npc;
|
||||||
|
this.player = player;
|
||||||
|
reforge = player.getItemInHand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
npc.chat(player, reforgeItemInHand() ? successMsg : failMsg);
|
||||||
|
if (npc.getBukkitEntity() instanceof Player)
|
||||||
|
((Player) npc.getBukkitEntity()).setItemInHand(null);
|
||||||
|
if (dropItem)
|
||||||
|
player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), reforge);
|
||||||
|
else {
|
||||||
|
for (ItemStack stack : player.getInventory().addItem(reforge).values())
|
||||||
|
player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), stack);
|
||||||
|
}
|
||||||
|
session = null;
|
||||||
|
// Start cooldown
|
||||||
|
Calendar wait = Calendar.getInstance();
|
||||||
|
wait.add(Calendar.SECOND, reforgeCooldown);
|
||||||
|
cooldowns.put(player.getName(), wait);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean reforgeItemInHand() {
|
||||||
|
Random random = new Random();
|
||||||
|
if (random.nextInt(100) < failChance) {
|
||||||
|
for (Enchantment enchantment : reforge.getEnchantments().keySet()) {
|
||||||
|
// Remove or downgrade enchantments
|
||||||
|
if (random.nextBoolean())
|
||||||
|
reforge.removeEnchantment(enchantment);
|
||||||
|
else {
|
||||||
|
if (reforge.getEnchantmentLevel(enchantment) > 1) {
|
||||||
|
reforge.removeEnchantment(enchantment);
|
||||||
|
reforge.addEnchantment(enchantment, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Damage the item
|
||||||
|
short durability = (short) (reforge.getDurability() + reforge.getDurability() * random.nextInt(8));
|
||||||
|
short maxDurability = reforge.getType().getMaxDurability();
|
||||||
|
if (durability <= 0)
|
||||||
|
durability = (short) (maxDurability / 3);
|
||||||
|
else if (reforge.getDurability() + durability > maxDurability)
|
||||||
|
durability = (short) (maxDurability - random.nextInt(maxDurability - 25));
|
||||||
|
reforge.setDurability(durability);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Add random enchantments
|
||||||
|
int chance = 10;
|
||||||
|
if (reforge.getDurability() == 0)
|
||||||
|
chance *= 4;
|
||||||
|
else
|
||||||
|
reforge.setDurability((short) 0);
|
||||||
|
for (int i = 0; i < chance; i++) {
|
||||||
|
if (reforge.getEnchantments().keySet().size() == maxEnchantments)
|
||||||
|
break;
|
||||||
|
Enchantment enchantment = Enchantment.getById(enchantments[random.nextInt(enchantments.length)]);
|
||||||
|
if (enchantment.canEnchantItem(reforge))
|
||||||
|
reforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel()) + 1);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isArmor(ItemStack item) {
|
|
||||||
switch (item.getType()) {
|
|
||||||
case LEATHER_HELMET:
|
|
||||||
case LEATHER_CHESTPLATE:
|
|
||||||
case LEATHER_LEGGINGS:
|
|
||||||
case LEATHER_BOOTS:
|
|
||||||
case CHAINMAIL_HELMET:
|
|
||||||
case CHAINMAIL_CHESTPLATE:
|
|
||||||
case CHAINMAIL_LEGGINGS:
|
|
||||||
case CHAINMAIL_BOOTS:
|
|
||||||
case GOLD_HELMET:
|
|
||||||
case GOLD_CHESTPLATE:
|
|
||||||
case GOLD_LEGGINGS:
|
|
||||||
case GOLD_BOOTS:
|
|
||||||
case IRON_HELMET:
|
|
||||||
case IRON_CHESTPLATE:
|
|
||||||
case IRON_LEGGINGS:
|
|
||||||
case IRON_BOOTS:
|
|
||||||
case DIAMOND_HELMET:
|
|
||||||
case DIAMOND_CHESTPLATE:
|
|
||||||
case DIAMOND_LEGGINGS:
|
|
||||||
case DIAMOND_BOOTS:
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean doesPlayerHaveEnough(Player player) {
|
|
||||||
return economy.getBalance(player.getName()) - getCost(player.getItemInHand()) >= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String formatCost(Player player) {
|
|
||||||
return economy.format(getCost(player.getItemInHand()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void withdraw(Player player) {
|
|
||||||
economy.withdrawPlayer(player.getName(), getCost(player.getItemInHand()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private double getCost(ItemStack item) {
|
|
||||||
DataKey root = config.getConfig().getKey("");
|
|
||||||
double price = Setting.BASE_PRICE.asDouble();
|
|
||||||
if (root.keyExists("base-prices." + item.getType().name().toLowerCase().replace('_', '-')))
|
|
||||||
price = root.getDouble("base-prices." + item.getType().name().toLowerCase().replace('_', '-'));
|
|
||||||
|
|
||||||
// Adjust price based on durability and enchantments
|
|
||||||
price += (item.getType().getMaxDurability() - item.getDurability());
|
|
||||||
|
|
||||||
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
|
|
||||||
for (Enchantment enchantment : item.getEnchantments().keySet()) {
|
|
||||||
if (root.keyExists("enchantment-modifiers." + enchantment.getName().toLowerCase().replace('_', '-')))
|
|
||||||
enchantmentModifier = root.getDouble("enchantment-modifiers."
|
|
||||||
+ enchantment.getName().toLowerCase().replace('_', '-'));
|
|
||||||
price += enchantmentModifier * item.getEnchantmentLevel(enchantment);
|
|
||||||
}
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,241 +0,0 @@
|
|||||||
package net.apunch.blacksmith;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
import net.apunch.blacksmith.util.Settings.Setting;
|
|
||||||
|
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
|
||||||
import net.citizensnpcs.api.trait.Character;
|
|
||||||
import net.citizensnpcs.api.trait.SaveId;
|
|
||||||
import net.citizensnpcs.api.util.DataKey;
|
|
||||||
|
|
||||||
@SaveId("blacksmith")
|
|
||||||
public class BlacksmithCharacter extends Character {
|
|
||||||
private static final int[] enchantments = new int[Enchantment.values().length];
|
|
||||||
|
|
||||||
private final Blacksmith plugin;
|
|
||||||
private final List<Material> reforgeableItems = new ArrayList<Material>();
|
|
||||||
private final Map<String, Calendar> cooldowns = new HashMap<String, Calendar>();
|
|
||||||
private ReforgeSession session;
|
|
||||||
|
|
||||||
// Defaults
|
|
||||||
private String busyWithPlayerMsg = Setting.BUSY_WITH_PLAYER_MESSAGE.asString();
|
|
||||||
private String busyReforgingMsg = Setting.BUSY_WITH_REFORGE_MESSAGE.asString();
|
|
||||||
private String costMsg = Setting.COST_MESSAGE.asString();
|
|
||||||
private String invalidItemMsg = Setting.INVALID_ITEM_MESSAGE.asString();
|
|
||||||
private String startReforgeMsg = Setting.START_REFORGE_MESSAGE.asString();
|
|
||||||
private String successMsg = Setting.SUCCESS_MESSAGE.asString();
|
|
||||||
private String failMsg = Setting.FAIL_MESSAGE.asString();
|
|
||||||
private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString();
|
|
||||||
private String cooldownUnexpiredMsg = Setting.COOLDOWN_UNEXPIRED_MESSAGE.asString();
|
|
||||||
private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt();
|
|
||||||
private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt();
|
|
||||||
private int reforgeCooldown = Setting.REFORGE_COOLDOWN.asInt();
|
|
||||||
private int failChance = Setting.FAIL_CHANCE.asInt();
|
|
||||||
private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt();
|
|
||||||
private boolean dropItem = Setting.DROP_ITEM.asBoolean();
|
|
||||||
|
|
||||||
public BlacksmithCharacter() {
|
|
||||||
plugin = (Blacksmith) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith");
|
|
||||||
int i = 0;
|
|
||||||
for (Enchantment enchantment : Enchantment.values())
|
|
||||||
enchantments[i++] = enchantment.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void load(DataKey key) {
|
|
||||||
for (DataKey sub : key.getRelative("reforgeable-items").getIntegerSubKeys())
|
|
||||||
if (Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')) != null)
|
|
||||||
reforgeableItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')));
|
|
||||||
|
|
||||||
// Override defaults if they exist
|
|
||||||
if (key.keyExists("messages.busy-with-player"))
|
|
||||||
busyWithPlayerMsg = key.getString("messages.busy-with-player");
|
|
||||||
if (key.keyExists("messages.busy-with-reforge"))
|
|
||||||
busyReforgingMsg = key.getString("messages.busy-with-reforge");
|
|
||||||
if (key.keyExists("messages.cost"))
|
|
||||||
costMsg = key.getString("messages.cost");
|
|
||||||
if (key.keyExists("messages.invalid-item"))
|
|
||||||
invalidItemMsg = key.getString("messages.invalid-item");
|
|
||||||
if (key.keyExists("messages.start-reforge"))
|
|
||||||
startReforgeMsg = key.getString("messages.start-reforge");
|
|
||||||
if (key.keyExists("messages.successful-reforge"))
|
|
||||||
successMsg = key.getString("messages.successful-reforge");
|
|
||||||
if (key.keyExists("messages.fail-reforge"))
|
|
||||||
failMsg = key.getString("messages.fail-reforge");
|
|
||||||
if (key.keyExists("messages.insufficient-funds"))
|
|
||||||
insufficientFundsMsg = key.getString("messages.insufficient-funds");
|
|
||||||
if (key.keyExists("messages.cooldown-not-expired"))
|
|
||||||
cooldownUnexpiredMsg = key.getString("messages.cooldown-not-expired");
|
|
||||||
if (key.keyExists("delays-in-seconds.minimum"))
|
|
||||||
minReforgeDelay = key.getInt("delays-in-seconds.minimum");
|
|
||||||
if (key.keyExists("delays-in-seconds.maximum"))
|
|
||||||
maxReforgeDelay = key.getInt("delays-in-seconds.maximum");
|
|
||||||
if (key.keyExists("delays-in-seconds.reforge-cooldown"))
|
|
||||||
reforgeCooldown = key.getInt("delays-in-seconds.reforge-cooldown");
|
|
||||||
if (key.keyExists("percent-chance-to-fail-reforge"))
|
|
||||||
failChance = key.getInt("percent-chance-to-fail-reforge");
|
|
||||||
if (key.keyExists("maximum-enchantments"))
|
|
||||||
maxEnchantments = key.getInt("maximum-enchantments");
|
|
||||||
if (key.keyExists("drop-item"))
|
|
||||||
dropItem = key.getBoolean("drop-item");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRightClick(NPC npc, Player player) {
|
|
||||||
if (!player.hasPermission("blacksmith.reforge"))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (cooldowns.get(player.getName()) != null) {
|
|
||||||
if (!Calendar.getInstance().after(cooldowns.get(player.getName()))) {
|
|
||||||
npc.chat(player, cooldownUnexpiredMsg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cooldowns.remove(player.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack hand = player.getItemInHand();
|
|
||||||
if (session != null) {
|
|
||||||
if (!session.isInSession(player)) {
|
|
||||||
npc.chat(busyWithPlayerMsg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.isRunning()) {
|
|
||||||
npc.chat(player, busyReforgingMsg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (session.handleClick())
|
|
||||||
session = null;
|
|
||||||
else
|
|
||||||
reforge(npc, player);
|
|
||||||
} else {
|
|
||||||
if ((!plugin.isTool(hand) && !plugin.isArmor(hand))
|
|
||||||
|| (!reforgeableItems.isEmpty() && !reforgeableItems.contains(hand.getType()))) {
|
|
||||||
npc.chat(player, invalidItemMsg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
session = new ReforgeSession(player, npc);
|
|
||||||
npc.chat(player, costMsg.replace("<price>", plugin.formatCost(player)).replace("<item>",
|
|
||||||
hand.getType().name().toLowerCase().replace('_', ' ')));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save(DataKey key) {
|
|
||||||
for (int i = 0; i < reforgeableItems.size(); i++)
|
|
||||||
key.getRelative("reforgeable-items").setString(String.valueOf(i),
|
|
||||||
reforgeableItems.get(i).name().toLowerCase().replace('_', '-'));
|
|
||||||
|
|
||||||
key.setString("messages.busy-with-player", busyWithPlayerMsg);
|
|
||||||
key.setString("messages.busy-with-reforge", busyReforgingMsg);
|
|
||||||
key.setString("messages.cost", costMsg);
|
|
||||||
key.setString("messages.invalid-item", invalidItemMsg);
|
|
||||||
key.setString("messages.start-reforge", startReforgeMsg);
|
|
||||||
key.setString("messages.successful-reforge", successMsg);
|
|
||||||
key.setString("messages.fail-reforge", failMsg);
|
|
||||||
key.setString("messages.insufficient-funds", insufficientFundsMsg);
|
|
||||||
key.setString("messages.cooldown-not-expired", cooldownUnexpiredMsg);
|
|
||||||
key.setInt("delays-in-seconds.minimum", minReforgeDelay);
|
|
||||||
key.setInt("delays-in-seconds.maximum", maxReforgeDelay);
|
|
||||||
key.setInt("delays-in-seconds.reforge-cooldown", reforgeCooldown);
|
|
||||||
key.setInt("percent-chance-to-fail-reforge", failChance);
|
|
||||||
key.setInt("maximum-enchantments", maxEnchantments);
|
|
||||||
key.setBoolean("drop-item", dropItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getInsufficientFundsMessage() {
|
|
||||||
return insufficientFundsMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reforge(NPC npc, Player player) {
|
|
||||||
npc.chat(player, startReforgeMsg);
|
|
||||||
plugin.withdraw(player);
|
|
||||||
session.setTask(plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin,
|
|
||||||
new ReforgeTask(npc, player), (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20));
|
|
||||||
if (npc.getBukkitEntity() instanceof Player)
|
|
||||||
((Player) npc.getBukkitEntity()).setItemInHand(player.getItemInHand());
|
|
||||||
player.setItemInHand(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ReforgeTask implements Runnable {
|
|
||||||
private final NPC npc;
|
|
||||||
private final Player player;
|
|
||||||
private final ItemStack reforge;
|
|
||||||
|
|
||||||
private ReforgeTask(NPC npc, Player player) {
|
|
||||||
this.npc = npc;
|
|
||||||
this.player = player;
|
|
||||||
reforge = player.getItemInHand();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
npc.chat(player, reforgeItemInHand() ? successMsg : failMsg);
|
|
||||||
if (npc.getBukkitEntity() instanceof Player)
|
|
||||||
((Player) npc.getBukkitEntity()).setItemInHand(null);
|
|
||||||
if (dropItem)
|
|
||||||
player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), reforge);
|
|
||||||
else {
|
|
||||||
for (ItemStack stack : player.getInventory().addItem(reforge).values())
|
|
||||||
player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), stack);
|
|
||||||
}
|
|
||||||
session = null;
|
|
||||||
// Start cooldown
|
|
||||||
Calendar wait = Calendar.getInstance();
|
|
||||||
wait.add(Calendar.SECOND, reforgeCooldown);
|
|
||||||
cooldowns.put(player.getName(), wait);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean reforgeItemInHand() {
|
|
||||||
Random random = new Random();
|
|
||||||
if (random.nextInt(100) < failChance) {
|
|
||||||
for (Enchantment enchantment : reforge.getEnchantments().keySet()) {
|
|
||||||
// Remove or downgrade enchantments
|
|
||||||
if (random.nextBoolean())
|
|
||||||
reforge.removeEnchantment(enchantment);
|
|
||||||
else {
|
|
||||||
if (reforge.getEnchantmentLevel(enchantment) > 1) {
|
|
||||||
reforge.removeEnchantment(enchantment);
|
|
||||||
reforge.addEnchantment(enchantment, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Damage the item
|
|
||||||
short durability = (short) (reforge.getDurability() + reforge.getDurability() * random.nextInt(8));
|
|
||||||
short maxDurability = reforge.getType().getMaxDurability();
|
|
||||||
if (durability <= 0)
|
|
||||||
durability = (short) (maxDurability / 3);
|
|
||||||
else if (reforge.getDurability() + durability > maxDurability)
|
|
||||||
durability = (short) (maxDurability - random.nextInt(maxDurability - 25));
|
|
||||||
reforge.setDurability(durability);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Add random enchantments
|
|
||||||
int chance = 10;
|
|
||||||
if (reforge.getDurability() == 0)
|
|
||||||
chance *= 4;
|
|
||||||
else
|
|
||||||
reforge.setDurability((short) 0);
|
|
||||||
for (int i = 0; i < chance; i++) {
|
|
||||||
if (reforge.getEnchantments().keySet().size() == maxEnchantments)
|
|
||||||
break;
|
|
||||||
Enchantment enchantment = Enchantment.getById(enchantments[random.nextInt(enchantments.length)]);
|
|
||||||
if (enchantment.canEnchantItem(reforge))
|
|
||||||
reforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel()) + 1);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
140
src/net/apunch/blacksmith/BlacksmithPlugin.java
Normal file
140
src/net/apunch/blacksmith/BlacksmithPlugin.java
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
package net.apunch.blacksmith;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import net.apunch.blacksmith.util.Settings;
|
||||||
|
import net.apunch.blacksmith.util.Settings.Setting;
|
||||||
|
|
||||||
|
import net.citizensnpcs.api.CitizensAPI;
|
||||||
|
import net.citizensnpcs.api.util.DataKey;
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class BlacksmithPlugin extends JavaPlugin {
|
||||||
|
private Settings config;
|
||||||
|
private Economy economy;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
config.save();
|
||||||
|
|
||||||
|
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
config = new Settings(this);
|
||||||
|
config.load();
|
||||||
|
|
||||||
|
// Setup Vault
|
||||||
|
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(
|
||||||
|
Economy.class);
|
||||||
|
if (economyProvider != null)
|
||||||
|
economy = economyProvider.getProvider();
|
||||||
|
|
||||||
|
CitizensAPI.getCharacterManager().register(Blacksmith.class);
|
||||||
|
|
||||||
|
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTool(ItemStack item) {
|
||||||
|
switch (item.getType()) {
|
||||||
|
case WOOD_PICKAXE:
|
||||||
|
case WOOD_SPADE:
|
||||||
|
case WOOD_HOE:
|
||||||
|
case WOOD_SWORD:
|
||||||
|
case WOOD_AXE:
|
||||||
|
case STONE_PICKAXE:
|
||||||
|
case STONE_SPADE:
|
||||||
|
case STONE_HOE:
|
||||||
|
case STONE_SWORD:
|
||||||
|
case STONE_AXE:
|
||||||
|
case GOLD_PICKAXE:
|
||||||
|
case GOLD_SPADE:
|
||||||
|
case GOLD_HOE:
|
||||||
|
case GOLD_SWORD:
|
||||||
|
case GOLD_AXE:
|
||||||
|
case IRON_PICKAXE:
|
||||||
|
case IRON_SPADE:
|
||||||
|
case IRON_HOE:
|
||||||
|
case IRON_SWORD:
|
||||||
|
case IRON_AXE:
|
||||||
|
case DIAMOND_PICKAXE:
|
||||||
|
case DIAMOND_SPADE:
|
||||||
|
case DIAMOND_HOE:
|
||||||
|
case DIAMOND_SWORD:
|
||||||
|
case DIAMOND_AXE:
|
||||||
|
case BOW:
|
||||||
|
case FLINT_AND_STEEL:
|
||||||
|
case FISHING_ROD:
|
||||||
|
case SHEARS:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isArmor(ItemStack item) {
|
||||||
|
switch (item.getType()) {
|
||||||
|
case LEATHER_HELMET:
|
||||||
|
case LEATHER_CHESTPLATE:
|
||||||
|
case LEATHER_LEGGINGS:
|
||||||
|
case LEATHER_BOOTS:
|
||||||
|
case CHAINMAIL_HELMET:
|
||||||
|
case CHAINMAIL_CHESTPLATE:
|
||||||
|
case CHAINMAIL_LEGGINGS:
|
||||||
|
case CHAINMAIL_BOOTS:
|
||||||
|
case GOLD_HELMET:
|
||||||
|
case GOLD_CHESTPLATE:
|
||||||
|
case GOLD_LEGGINGS:
|
||||||
|
case GOLD_BOOTS:
|
||||||
|
case IRON_HELMET:
|
||||||
|
case IRON_CHESTPLATE:
|
||||||
|
case IRON_LEGGINGS:
|
||||||
|
case IRON_BOOTS:
|
||||||
|
case DIAMOND_HELMET:
|
||||||
|
case DIAMOND_CHESTPLATE:
|
||||||
|
case DIAMOND_LEGGINGS:
|
||||||
|
case DIAMOND_BOOTS:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doesPlayerHaveEnough(Player player) {
|
||||||
|
return economy.getBalance(player.getName()) - getCost(player.getItemInHand()) >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String formatCost(Player player) {
|
||||||
|
return economy.format(getCost(player.getItemInHand()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void withdraw(Player player) {
|
||||||
|
economy.withdrawPlayer(player.getName(), getCost(player.getItemInHand()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private double getCost(ItemStack item) {
|
||||||
|
DataKey root = config.getConfig().getKey("");
|
||||||
|
double price = Setting.BASE_PRICE.asDouble();
|
||||||
|
if (root.keyExists("base-prices." + item.getType().name().toLowerCase().replace('_', '-')))
|
||||||
|
price = root.getDouble("base-prices." + item.getType().name().toLowerCase().replace('_', '-'));
|
||||||
|
|
||||||
|
// Adjust price based on durability and enchantments
|
||||||
|
price += (item.getType().getMaxDurability() - item.getDurability());
|
||||||
|
|
||||||
|
double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble();
|
||||||
|
for (Enchantment enchantment : item.getEnchantments().keySet()) {
|
||||||
|
if (root.keyExists("enchantment-modifiers." + enchantment.getName().toLowerCase().replace('_', '-')))
|
||||||
|
enchantmentModifier = root.getDouble("enchantment-modifiers."
|
||||||
|
+ enchantment.getName().toLowerCase().replace('_', '-'));
|
||||||
|
price += enchantmentModifier * item.getEnchantmentLevel(enchantment);
|
||||||
|
}
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public class ReforgeSession {
|
public class ReforgeSession {
|
||||||
private final Blacksmith plugin;
|
private final BlacksmithPlugin plugin;
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final ItemStack reforge;
|
private final ItemStack reforge;
|
||||||
private final NPC npc;
|
private final NPC npc;
|
||||||
@ -17,7 +17,7 @@ public class ReforgeSession {
|
|||||||
reforge = player.getItemInHand();
|
reforge = player.getItemInHand();
|
||||||
this.npc = npc;
|
this.npc = npc;
|
||||||
|
|
||||||
plugin = (Blacksmith) player.getServer().getPluginManager().getPlugin("Blacksmith");
|
plugin = (BlacksmithPlugin) player.getServer().getPluginManager().getPlugin("Blacksmith");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return is the session should end
|
// Return is the session should end
|
||||||
@ -28,7 +28,7 @@ public class ReforgeSession {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!plugin.doesPlayerHaveEnough(player)) {
|
if (!plugin.doesPlayerHaveEnough(player)) {
|
||||||
npc.chat(player, ((BlacksmithCharacter) npc.getCharacter()).getInsufficientFundsMessage());
|
npc.chat(player, ((Blacksmith) npc.getCharacter()).getInsufficientFundsMessage());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -2,7 +2,7 @@ package net.apunch.blacksmith.util;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import net.apunch.blacksmith.Blacksmith;
|
import net.apunch.blacksmith.BlacksmithPlugin;
|
||||||
|
|
||||||
import net.citizensnpcs.api.util.DataKey;
|
import net.citizensnpcs.api.util.DataKey;
|
||||||
import net.citizensnpcs.api.util.YamlStorage;
|
import net.citizensnpcs.api.util.YamlStorage;
|
||||||
@ -10,7 +10,7 @@ import net.citizensnpcs.api.util.YamlStorage;
|
|||||||
public class Settings {
|
public class Settings {
|
||||||
private final YamlStorage config;
|
private final YamlStorage config;
|
||||||
|
|
||||||
public Settings(Blacksmith plugin) {
|
public Settings(BlacksmithPlugin plugin) {
|
||||||
config = new YamlStorage(plugin.getDataFolder() + File.separator + "config.yml", "Blacksmith Configuration");
|
config = new YamlStorage(plugin.getDataFolder() + File.separator + "config.yml", "Blacksmith Configuration");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user