Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.01 KB | None | 0 0
  1. package cz.dubcat.attributeupgrader;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map.Entry;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Material;
  14. import org.bukkit.Sound;
  15. import org.bukkit.configuration.file.FileConfiguration;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.inventory.PlayerInventory;
  19. import org.bukkit.inventory.meta.ItemMeta;
  20. import org.bukkit.scheduler.BukkitScheduler;
  21.  
  22. import cz.dubcat.attributeupgrader.JsonBuilder.HoverAction;
  23. import cz.dubcat.attributeupgrader.constructors.Combination;
  24. import cz.dubcat.attributeupgrader.constructors.Gem;
  25. import cz.dubcat.attributeupgrader.constructors.GemType;
  26. import cz.dubcat.attributeupgrader.constructors.GlobalLogic;
  27. import cz.dubcat.attributeupgrader.constructors.McAttribute;
  28. import cz.dubcat.attributeupgrader.constructors.ReqItem;
  29. import cz.dubcat.attributeupgrader.constructors.Stat;
  30. import cz.dubcat.attributeupgrader.constructors.UpgradeLevel;
  31. import cz.dubcat.attributeupgrader.logic.Conditions;
  32. import cz.dubcat.attributeupgrader.logic.Executions;
  33. import cz.dubcat.attributeupgrader.logic.Logic;
  34. import cz.dubcat.attributeupgrader.logic.LogicAPI;
  35.  
  36. public class AuAPI {
  37.  
  38. public final String cfg_upgrade = "upgrade.";
  39. public final String cfg_lang = "lang.";
  40.  
  41. public String patr1 = "[-+](\\d+[.,]?\\d?)";
  42. public String patr2 = "([-+]?)(\\d+[.,]?\\d?)(-)(\\d+[.,]?\\d?)";
  43. public Pattern single = Pattern.compile(patr1);
  44. public Pattern doubles = Pattern.compile(patr2);
  45.  
  46. public String colorizeText(String string) {
  47. string = string.replaceAll("&0", ChatColor.BLACK + "");
  48. string = string.replaceAll("&1", ChatColor.DARK_BLUE + "");
  49. string = string.replaceAll("&2", ChatColor.DARK_GREEN + "");
  50. string = string.replaceAll("&3", ChatColor.DARK_AQUA + "");
  51. string = string.replaceAll("&4", ChatColor.DARK_RED + "");
  52. string = string.replaceAll("&5", ChatColor.DARK_PURPLE + "");
  53. string = string.replaceAll("&6", ChatColor.GOLD + "");
  54. string = string.replaceAll("&7", ChatColor.GRAY + "");
  55. string = string.replaceAll("&8", ChatColor.DARK_GRAY + "");
  56. string = string.replaceAll("&9", ChatColor.BLUE + "");
  57. string = string.replaceAll("&a", ChatColor.GREEN + "");
  58. string = string.replaceAll("&b", ChatColor.AQUA + "");
  59. string = string.replaceAll("&c", ChatColor.RED + "");
  60. string = string.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
  61. string = string.replaceAll("&e", ChatColor.YELLOW + "");
  62. string = string.replaceAll("&f", ChatColor.WHITE + "");
  63. string = string.replaceAll("&l", ChatColor.BOLD + "");
  64. string = string.replaceAll("&r", ChatColor.WHITE + "");
  65. return string;
  66. }
  67.  
  68. public String stripColours(String string) {
  69. string = string.replaceAll(ChatColor.BLACK + "", "");
  70. string = string.replaceAll(ChatColor.DARK_BLUE + "", "");
  71. string = string.replaceAll(ChatColor.DARK_GREEN + "", "");
  72. string = string.replaceAll(ChatColor.DARK_AQUA + "", "");
  73. string = string.replaceAll(ChatColor.DARK_RED + "", "");
  74. string = string.replaceAll(ChatColor.DARK_PURPLE + "", "");
  75. string = string.replaceAll(ChatColor.GOLD + "", "");
  76. string = string.replaceAll(ChatColor.GRAY + "", "");
  77. string = string.replaceAll(ChatColor.DARK_GRAY + "", "");
  78. string = string.replaceAll(ChatColor.BLUE + "", "");
  79. string = string.replaceAll(ChatColor.GREEN + "", "");
  80. string = string.replaceAll(ChatColor.AQUA + "", "");
  81. string = string.replaceAll(ChatColor.RED + "", "");
  82. string = string.replaceAll(ChatColor.LIGHT_PURPLE + "", "");
  83. string = string.replaceAll(ChatColor.YELLOW + "", "");
  84. string = string.replaceAll(ChatColor.WHITE + "", "");
  85. string = string.replaceAll(ChatColor.BOLD + "", "");
  86. string = string.replaceAll(ChatColor.WHITE + "", "");
  87. return string;
  88. }
  89.  
  90. public boolean sendMessage(Player p, String message) {
  91. p.sendMessage(colorizeText(Main.getPlugin().getConfig().getString(cfg_lang + "prefix") + message));
  92. return true;
  93. }
  94.  
  95. public FileConfiguration getConfig() {
  96. return Main.getPlugin().getConfig();
  97. }
  98.  
  99. public int getTimeAnim() {
  100. int i = 0;
  101. for (String key : getConfig().getConfigurationSection(cfg_upgrade + "animation").getKeys(false)) {
  102. i = getConfig().getInt( cfg_upgrade + "animation." + key + ".delay");
  103. }
  104. i = i + 2;
  105. return i;
  106. }
  107.  
  108. public boolean isValidItem(Player p, ItemStack item) {
  109. if (item == null || !item.hasItemMeta() || !item.getItemMeta().hasDisplayName() || !item.getItemMeta().hasLore()) {
  110.  
  111. sendMessage(p, getConfig().getString(cfg_lang + "notsupported"));
  112. return false;
  113. }
  114.  
  115. return true;
  116. }
  117.  
  118. public boolean checkPermissionToUpgrade(Player p){
  119. String permission = getConfig().getString(cfg_upgrade + "premission.perm");
  120.  
  121. if(getConfig().getBoolean(cfg_upgrade + "premission.use") && !p.hasPermission(permission)){
  122. sendMessage(p, getConfig().getString(cfg_lang + "noperm"));
  123. return false;
  124. }
  125.  
  126. return true;
  127. }
  128.  
  129.  
  130. public void reloadGems(){
  131. Main.gems.clear();
  132. for(String gem : getConfig().getConfigurationSection("gems.gems").getKeys(true)){
  133. Main.gems.add(new Gem(gem));
  134. }
  135. }
  136.  
  137. public void reloadCombinations(){
  138.  
  139. if(Main.combinations.size() != 0)
  140. Main.combinations.clear();
  141.  
  142. for(String combination : getConfig().getConfigurationSection(cfg_upgrade + "cost_combinations").getKeys(false)){
  143. double currency = getConfig().getDouble(cfg_upgrade + "cost_combinations." + combination + ".curency");
  144. Main.combinations.add(new Combination(currency, combination));
  145. }
  146. }
  147.  
  148. public void reloadLoadedStats(){
  149.  
  150. HashMap<String, Stat> stats = new HashMap<String, Stat>();
  151.  
  152. if(Main.stats != null && Main.stats.size() != 0)
  153. Main.stats.clear();
  154.  
  155. for(String stat : getConfig().getConfigurationSection(cfg_upgrade + "stats").getKeys(false)){
  156. double per_level = getConfig().getDouble(cfg_upgrade + "stats." + stat + ".perlevel");
  157.  
  158. String name;
  159. String format;
  160. Boolean cast;
  161. boolean custom = true;
  162.  
  163. if(!stat.contains("DEFAULT_MC")){
  164. name = colorizeText(getConfig().getString(cfg_upgrade + "stats." + stat + ".name"));
  165. format = colorizeText(getConfig().getString(cfg_upgrade + "stats." + stat + ".format"));
  166. cast = getConfig().getBoolean(cfg_upgrade + "stats." + stat + ".casttoint");
  167. }else{
  168. name = stat;
  169. format = "";
  170. cast = false;
  171. custom = false;
  172. }
  173.  
  174. Stat stat_cfg = new Stat(stat, per_level, name, format, cast, custom);
  175.  
  176. stats.put(stat, stat_cfg);
  177. }
  178.  
  179. Main.stats = stats;
  180. }
  181.  
  182. public void reloadWeaponLevels(){
  183. if(Main.levels.size() != 0)
  184. Main.levels.clear();
  185.  
  186. int i = 0;
  187. for(@SuppressWarnings("unused") String level : getConfig().getConfigurationSection(cfg_upgrade + "levels").getKeys(false)){
  188. Main.levels.add(new UpgradeLevel(i));
  189. i++;
  190. }
  191.  
  192. }
  193.  
  194. public void reloadGlobalLogic(){
  195. Main.global_logic = new GlobalLogic();
  196. }
  197.  
  198. public UpgradeLevel getLevel(int i){
  199. return Main.levels.get(i);
  200. }
  201.  
  202. public HashMap<String, Stat> getLoadedStats(){
  203. return Main.stats;
  204. }
  205.  
  206. public ArrayList<Combination> getCombinations(){
  207. return Main.combinations;
  208. }
  209.  
  210. public int getCapUpgradeLevel(){
  211. return Main.levels.size()-1;
  212. }
  213.  
  214. public GlobalLogic getGlobalLogic(){
  215. return Main.global_logic;
  216. }
  217.  
  218.  
  219. public ArrayList<Gem> getGems(){
  220. return Main.gems;
  221. }
  222.  
  223. public int getMaxAllowedGems(){
  224. return getConfig().getInt("gems.maxgems");
  225. }
  226.  
  227. public double getBonusChance(Player p){
  228. double bonus_val = 0;
  229. for(String bonus : getConfig().getConfigurationSection(cfg_upgrade + "permission.bonus").getKeys(false)){
  230. if(p.hasPermission(getConfig().getString(cfg_upgrade + "permission.bonus." + bonus + ".permission"))){
  231. bonus_val += getConfig().getDouble(cfg_upgrade + "permission.bonus." + bonus + ".percentage");
  232. p.sendMessage("Bonus chance of " + (getConfig().getDouble(cfg_upgrade + "permission.bonus." + bonus + ".percentage") *100));
  233. }
  234. }
  235.  
  236. return bonus_val;
  237. }
  238.  
  239.  
  240. public boolean forceDowngradeItem(ItemStack item, Player p){
  241.  
  242.  
  243. return true;
  244. }
  245.  
  246. public boolean forceUpgradeItem(ItemStack item, Player p){
  247.  
  248. return true;
  249. }
  250.  
  251.  
  252. public void insertGem(Player p, ItemStack item, ItemStack gem_item, Gem gem){
  253. AttributeApi api = Main.getApi();
  254. int global_gems = api.getGemsNumInWeapon(item, GemType.GLOBAL, gem);
  255. int internal_gems = api.getGemsNumInWeapon(item, GemType.LOCAL, gem);
  256.  
  257. if(global_gems >= getMaxAllowedGems() || internal_gems >= gem.levels.size()){
  258. p.sendMessage("You have reach max amount of gems in one item.");
  259. return;
  260. }
  261.  
  262. int amount = gem_item.getAmount();
  263.  
  264. if(amount > gem.amount){
  265. gem_item.setAmount(amount - gem.amount);
  266. p.getInventory().addItem(gem_item);
  267. }
  268.  
  269. List<String> lore_gem = gem_item.getItemMeta().getLore();
  270. List<String> new_lore = new ArrayList<String>();
  271. ItemMeta meta = item.getItemMeta();
  272.  
  273. if(item.getItemMeta().hasLore())
  274. new_lore = item.getItemMeta().getLore();
  275.  
  276. if(getConfig().getBoolean("gems.devider.enabled"))
  277. new_lore.add(colorizeText(getConfig().getString("gems.devider.value")));
  278.  
  279. new_lore.addAll(lore_gem);
  280.  
  281. meta.setLore(new_lore);
  282.  
  283.  
  284. if(gem.format){
  285. String item_name = item.getItemMeta().getDisplayName();
  286. if(internal_gems != 0){
  287. String[] split = item_name.split(" ");
  288. String waht_to_find = gem.levels.get(internal_gems).format;
  289. for(int i = 0 ; i < split.length; i++){
  290. String sp = split[i];
  291. if(sp.contains(waht_to_find)){
  292. split[i] = gem.levels.get(internal_gems+1).format;
  293. break;
  294. }
  295. }
  296.  
  297. String new_name = "";
  298. for(int i = 0 ; i < split.length; i++){
  299. if(i != 0)
  300. new_name += " ";
  301.  
  302. new_name += split[i];
  303. }
  304.  
  305. meta.setDisplayName(colorizeText(new_name));
  306. }else{
  307. meta.setDisplayName(colorizeText(item_name + " " + gem.levels.get(internal_gems+1).format));
  308. }
  309. }
  310.  
  311. item.setItemMeta(meta);
  312. item = api.setGems(item, internal_gems, gem);
  313.  
  314. p.getInventory().addItem(item);
  315. p.sendMessage("You have successfuully inserted gem!");
  316. }
  317.  
  318. public void upgrade(ItemStack item, int cur_level, Player p){
  319. AttributeApi api = Main.getApi();
  320. Combination comb = hasAnyCombination(p);
  321.  
  322. if(comb == null){
  323. p.sendMessage("doung combination");
  324. return;
  325. }
  326.  
  327. if(!item.equals(api.getHoldingItem(p))){
  328. sendMessage(p, getConfig().getString(cfg_lang + "sameweapon"));
  329. return;
  330. }
  331.  
  332.  
  333. //ny how much upgrade will occur
  334. int upgrade_by_level = comb.getUpgradeByNumber();
  335.  
  336. //if cur_weapon_level + upgradeby > cap then this
  337. if(cur_level + upgrade_by_level > getCapUpgradeLevel())
  338. upgrade_by_level = getCapUpgradeLevel() - cur_level;
  339.  
  340.  
  341. UpgradeLevel cur_upgrade_level = getLevel(cur_level);
  342. UpgradeLevel new_upgrade_level = getLevel(cur_level + upgrade_by_level);
  343.  
  344. //totalchance
  345. double chance = new_upgrade_level.getChance() + getBonusChance(p);
  346.  
  347.  
  348. //takemoney
  349. if(comb.money != 0)
  350. Main.economy.withdrawPlayer(p, comb.money);
  351.  
  352. //take items
  353. if(comb.items != null || comb.items.size() != 0){
  354. for(ItemStack pitem : p.getInventory().getContents()){
  355.  
  356. if(pitem == null || pitem.getType() == Material.AIR || !pitem.hasItemMeta() || !pitem.getItemMeta().hasDisplayName())
  357. continue;
  358.  
  359. inner : for(ReqItem citem : comb.items){
  360. String display_name = pitem.getItemMeta().getDisplayName();
  361. Material mat = pitem.getType();
  362.  
  363. if(citem.name.equals(display_name) && mat == citem.type){
  364. int amount = pitem.getAmount() - citem.amount;
  365.  
  366. if(amount > 0)
  367. pitem.setAmount(amount);
  368. else
  369. p.getInventory().remove(item);
  370.  
  371. p.updateInventory();
  372.  
  373. continue inner;
  374. }
  375. }
  376. }
  377. }
  378.  
  379. ItemMeta meta = item.getItemMeta();
  380. List<String> lore = item.getItemMeta().getLore();
  381. //on success
  382. if(chance > 1 || Math.random() < chance){
  383.  
  384.  
  385. for(Entry<String, Stat> s : getLoadedStats().entrySet()){
  386. lore = changeStatValue(lore, s.getValue(), upgrade_by_level*s.getValue().per_level_upgrade);
  387. }
  388.  
  389. meta.setLore(lore);
  390.  
  391. String display_name = item.getItemMeta().getDisplayName();
  392.  
  393. //check if format is enabled
  394. if(getConfig().getBoolean(cfg_upgrade + "format.enabled")){
  395. if(cur_level == 0){
  396. display_name += " "+colorizeText(new_upgrade_level.colour + getConfig().getString(cfg_upgrade + "format.format").replace("%number%", new_upgrade_level.level+""));
  397. }else{
  398. String[] split = display_name.split(" ");
  399. String uprade_format = getConfig().getString(cfg_upgrade + "format.format").replace("%number%", cur_upgrade_level.level+"");
  400.  
  401. for(int i = 0; i < split.length; i ++)
  402. {
  403. if(split[i].contains(uprade_format))
  404. {
  405. split[i] = colorizeText(new_upgrade_level.colour + getConfig().getString(cfg_upgrade + "format.format").replace("%number%", new_upgrade_level.level+""));
  406. break;
  407. }
  408. }
  409.  
  410. String new_name = "";
  411.  
  412. for(int i = 0; i < split.length; i ++)
  413. {
  414. if(i != 0)
  415. new_name += " ";
  416.  
  417. new_name += split[i];
  418. }
  419. display_name = new_name;
  420. }
  421.  
  422. meta.setDisplayName(display_name);
  423. }
  424. item.setItemMeta(meta);
  425.  
  426. ItemStack new_item = api.setItemLevel(item, new_upgrade_level.level);
  427.  
  428.  
  429. for(Entry<String, Stat> stat : getLoadedStats().entrySet()){
  430. Stat stats = stat.getValue();
  431. if(stats.name.contains("DEFAULT_MC_")){
  432. String stat_name = stats.name;
  433.  
  434. if(stat_name.contains("DAMAGE") || stat_name.contains("DMG")){
  435. new_item = api.increaseAttribute(new_item, McAttribute.DAMAGE, upgrade_by_level*stats.per_level_upgrade);
  436. continue;
  437. }else if(stat_name.contains("HEALTH") || stat_name.contains("HP")){
  438. new_item = api.increaseAttribute(new_item, McAttribute.HEALTH, upgrade_by_level*stats.per_level_upgrade);
  439. continue;
  440. }else if(stat_name.contains("SPEED")){
  441. new_item = api.increaseAttribute(new_item, McAttribute.SPEED, upgrade_by_level*stats.per_level_upgrade);
  442. continue;
  443. }else if(stat_name.contains("ATTACK_SPEED")){
  444. new_item = api.increaseAttribute(new_item, McAttribute.ATTACK_SPEED, upgrade_by_level*stats.per_level_upgrade);
  445. continue;
  446. }else if(stat_name.contains("ARMOR")){
  447. new_item = api.increaseAttribute(new_item, McAttribute.ARMOR, upgrade_by_level*stats.per_level_upgrade);
  448. continue;
  449. }
  450. }
  451. }
  452.  
  453.  
  454. new_item = applyLogic(p, new_item,new_upgrade_level,cur_upgrade_level, new_upgrade_level, true);
  455.  
  456. p.getInventory().setItemInMainHand(new_item);
  457. p.updateInventory();
  458.  
  459. api.particles(p);
  460.  
  461.  
  462. if(getConfig().getBoolean("upgrade.broadcast.enabled")){
  463. if(new_upgrade_level.level >= getConfig().getInt("upgrade.broadcast.minlevel"))
  464. sendRawMsg(lore, p, new_upgrade_level.colour, new_upgrade_level.level, display_name);
  465. }
  466. }else{
  467. //downgrade
  468. //ny how much upgrade will occur
  469. int downgradeby = new_upgrade_level.getDowngrade();
  470.  
  471.  
  472. if(cur_level - downgradeby < 0)
  473. downgradeby = downgradeby - cur_level;
  474.  
  475. UpgradeLevel new_downgrade_level;
  476.  
  477. if(downgradeby != 0)
  478. new_downgrade_level = getLevel(cur_level - downgradeby);
  479. else
  480. new_downgrade_level = getLevel(cur_level);
  481.  
  482. if(downgradeby != 0){
  483.  
  484.  
  485. for(Entry<String, Stat> s : getLoadedStats().entrySet()){
  486. lore = changeStatValue(lore, s.getValue(), downgradeby*s.getValue().per_level_upgrade*-1);
  487. }
  488.  
  489. meta.setLore(lore);
  490.  
  491. //displayname
  492. String display_name = item.getItemMeta().getDisplayName();
  493.  
  494. if(getConfig().getBoolean(cfg_upgrade + "format.enabled")){
  495.  
  496. if(cur_level > 0){
  497. String[] split = display_name.split(" ");
  498. String uprade_format = getConfig().getString(cfg_upgrade + "format.format").replace("%number%", cur_level+"");
  499.  
  500. for(int i = 0; i < split.length; i ++){
  501. if(split[i].contains(uprade_format)){
  502. if(new_downgrade_level.level == 0)
  503. split[i] = "";
  504. else
  505. split[i] = colorizeText(new_downgrade_level.colour + getConfig().getString(cfg_upgrade + "format.format").replace("%number%", new_downgrade_level.level+""));
  506. break;
  507. }
  508. }
  509.  
  510. String new_name = "";
  511. for(int i = 0; i < split.length; i ++){
  512.  
  513. if(i != 0)
  514. new_name += " ";
  515.  
  516. new_name += split[i];
  517. }
  518.  
  519. display_name = new_name;
  520. }
  521.  
  522.  
  523. meta.setDisplayName(display_name);
  524. }
  525. item.setItemMeta(meta);
  526.  
  527. item = api.setItemLevel(item, new_downgrade_level.level);
  528.  
  529. for(Entry<String, Stat> stat : getLoadedStats().entrySet()){
  530. Stat stats = stat.getValue();
  531. if(stats.name.contains("DEFAULT_MC_")){
  532. String stat_name = stats.name;
  533.  
  534. if(stat_name.contains("DAMAGE") || stat_name.contains("DMG")){
  535. item = api.decreaseAttribute(item, McAttribute.DAMAGE, downgradeby*stats.per_level_upgrade);
  536. continue;
  537. }else if(stat_name.contains("HEALTH") || stat_name.contains("HP")){
  538. item = api.decreaseAttribute(item, McAttribute.HEALTH, downgradeby*stats.per_level_upgrade);
  539. continue;
  540. }else if(stat_name.contains("SPEED")){
  541. item = api.decreaseAttribute(item, McAttribute.SPEED, downgradeby*stats.per_level_upgrade);
  542. continue;
  543. }else if(stat_name.contains("ATTACK_SPEED")){
  544. item = api.decreaseAttribute(item, McAttribute.ATTACK_SPEED, downgradeby*stats.per_level_upgrade);
  545. continue;
  546. }else if(stat_name.contains("ARMOR")){
  547. item = api.decreaseAttribute(item, McAttribute.ARMOR, downgradeby*stats.per_level_upgrade);
  548. continue;
  549. }
  550. }
  551. }
  552. }
  553.  
  554.  
  555. item = applyLogic(p, item,new_upgrade_level,cur_upgrade_level, new_downgrade_level, false);
  556. p.getInventory().setItemInMainHand(item);
  557. p.updateInventory();
  558. }
  559.  
  560.  
  561. }
  562.  
  563. public Combination hasAnyCombination(Player p){
  564. PlayerInventory inv = p.getInventory();
  565. ArrayList<Combination> combs = getCombinations();
  566.  
  567. for(int i = 0; i < combs.size(); i++){
  568. Combination comb = combs.get(i);
  569. ArrayList<ItemStack> found = new ArrayList<ItemStack>();
  570.  
  571. if(!comb.enabled)
  572. continue;
  573.  
  574. if(comb.money != 0 && !Main.economy.has(p, comb.money))
  575. continue;
  576.  
  577.  
  578. for(ItemStack item : inv.getContents()){
  579. if(item==null || item.getType() == Material.AIR)
  580. continue;
  581.  
  582. if(!item.hasItemMeta() || !item.getItemMeta().hasDisplayName())
  583. continue;
  584.  
  585. if(comb.items != null || comb.items.size() != 0){
  586. for(ReqItem rqitem : comb.items){
  587. if(item.getType() == rqitem.type && item.getAmount() >= rqitem.amount && item.getItemMeta().getDisplayName().equals(rqitem.name)){
  588. found.add(item);
  589. }
  590. }
  591. }
  592.  
  593. if(found.size() == comb.items.size())
  594. return comb;
  595. }
  596.  
  597. }
  598.  
  599. return null;
  600. }
  601.  
  602.  
  603. @SuppressWarnings("unchecked")
  604. public ItemStack applyLogic(Player p,ItemStack item, UpgradeLevel upgrade_level_logic, UpgradeLevel level, UpgradeLevel new_level , boolean upgrade){
  605. ArrayList<Logic> logic;
  606. LogicAPI logic_api = new LogicAPI();
  607.  
  608. if(upgrade)
  609. logic = (ArrayList<Logic>) upgrade_level_logic.conditions_upgrade.clone();
  610. else
  611. logic = (ArrayList<Logic>) upgrade_level_logic.conditions_downgrade.clone();
  612.  
  613.  
  614. GlobalLogic global = getGlobalLogic();
  615.  
  616. if(upgrade)
  617. logic.addAll(global.conditions_upgrade);
  618. else
  619. logic.addAll(global.conditions_downgrade);
  620.  
  621.  
  622. boolean lastcond = true;
  623. String lastcondkey = "nothing";
  624. for(Iterator<Logic> iterator = logic.iterator(); iterator.hasNext();){
  625. Logic log = iterator.next();
  626.  
  627. if(log.isCondition){
  628. Conditions cond = log.cond;
  629. String key = log.content;
  630.  
  631. if(log.name.contains(lastcondkey) && !lastcond)
  632. continue;
  633.  
  634. lastcondkey = log.name;
  635. String[] split = key.split(" ");
  636.  
  637. if(cond == Conditions.EQUALS || cond == Conditions.GREATER || cond == Conditions.SMALLER){
  638. String firstparam = split[0];
  639. String secparam = split[2];
  640.  
  641. System.out.println(log.content +" Name: " + log.name);
  642. System.out.println("First param: " + firstparam + " Sec param: "+secparam );
  643.  
  644. double firstnum = 0,secnum=0;
  645.  
  646. if(logic_api.checkIfRandom(firstparam)){
  647. firstnum = logic_api.getRandomNumber();
  648. }else if(logic_api.checkIfRandom(secparam)){
  649. secnum = logic_api.getRandomNumber();
  650. }
  651.  
  652. if(upgrade){
  653. if(logic_api.checkStringForUpgrade(firstparam)){
  654. firstnum = (double) (new_level.level - level.level);
  655. }else if(logic_api.checkStringForUpgrade(secparam)){
  656. secnum = (double) (new_level.level - level.level);
  657. }
  658. }else{
  659. if(logic_api.checkStringForDownpgrade(firstparam)){
  660. firstnum = (double) (level.level - new_level.level);
  661. }else if(logic_api.checkStringForDownpgrade(secparam)){
  662. secnum = (double) (level.level - new_level.level);
  663. }
  664. }
  665.  
  666. if(logic_api.isNumeric(firstparam)){
  667. firstnum = (double)Double.parseDouble(firstparam);
  668. }else if(logic_api.isNumeric(secparam)){
  669. secnum = (double)Double.parseDouble(secparam);
  670. System.out.println("Sec param"+secparam);
  671. }
  672.  
  673. if(logic_api.isStat(firstparam) != null){
  674. Stat stat = logic_api.isStat(firstparam);
  675. firstnum = getStatValue(item, stat);
  676. }else if(logic_api.isStat(secparam) != null){
  677. Stat stat = logic_api.isStat(secparam);
  678. secnum = getStatValue(item, stat);
  679. }
  680.  
  681.  
  682. if(cond == Conditions.EQUALS){
  683.  
  684. System.out.println(cond + "");
  685.  
  686. if(firstnum != secnum){
  687. lastcond = false;
  688. }
  689. }else if(cond == Conditions.SMALLER){
  690. System.out.println(cond + "");
  691.  
  692. if(firstnum > secnum || firstnum ==secnum ){
  693. lastcond = false;
  694. }
  695. }else if(cond == Conditions.GREATER){
  696. System.out.println(cond + " " + firstnum +" > " + secnum);
  697.  
  698. if(firstnum < secnum || firstnum ==secnum ){
  699. lastcond = false;
  700. }
  701. }
  702. }else if(cond == Conditions.ITEM_TYPE){
  703.  
  704. System.out.println(cond + "");
  705. Material mat = Material.valueOf(split[2]);
  706.  
  707. if(mat != item.getType())
  708. lastcond = false;
  709. }else if(cond == Conditions.ELSE){
  710.  
  711. System.out.println(cond + "");
  712.  
  713. if( lastcond == false)
  714. lastcond = true;
  715. else
  716. lastcond = false;
  717. }
  718. continue;
  719. }
  720.  
  721. if((log.name.contains(lastcondkey) && lastcond) || !log.name.contains(lastcondkey)){
  722.  
  723. System.out.println("Exectuions name : " + log.name + " Last condition key :" + lastcondkey);
  724.  
  725. Executions exec = log.exec;
  726. String content = log.content;
  727.  
  728. if(exec == Executions.MESSAGE){
  729. logic_api.sendMessage(p, content
  730. .replaceAll("%weapon_name%", (item.getItemMeta().hasDisplayName()) ? item.getItemMeta().getDisplayName() : item.getType() + "")
  731. .replaceAll("%old_level%", level.level+"")
  732. .replaceAll("%new_level%", new_level.level+"")
  733. .replaceAll("%level%", new_level.level+""));
  734. continue;
  735. }else if(exec == Executions.SOUND){
  736. p.playSound(p.getLocation(), Sound.valueOf(content), 1f,1f);
  737. continue;
  738. }else if(exec == Executions.BROADCAST){
  739. Bukkit.getServer().broadcastMessage(colorizeText(content
  740. .replaceAll("%weapon_name%", (item.getItemMeta().hasDisplayName()) ? item.getItemMeta().getDisplayName() : item.getType() + "")
  741. .replaceAll("%old_level%", level.level+"")
  742. .replaceAll("%new_level%", new_level.level+"")
  743. .replaceAll("%level%", new_level.level+"")));
  744. continue;
  745. }else if(exec == Executions.REMOVEITEM){
  746. item = new ItemStack(Material.AIR);
  747. continue;
  748. }else if(exec == Executions.STOP){
  749. return item;
  750. }else if(exec == Executions.EXEC){
  751. if(content.contains("addto")){
  752. String[] split = content.split(" ");
  753. Stat stat = logic_api.getStat(split[1]);
  754.  
  755. if(stat == null)
  756. continue;
  757.  
  758. double add = Double.valueOf(logic_api.calculateFromString(item, split[2]));
  759.  
  760. ItemMeta meta = item.getItemMeta();
  761. List<String> lore = item.getItemMeta().getLore();
  762.  
  763. lore = changeStatValue(lore, stat, add);
  764.  
  765. meta.setLore(lore);
  766. item.setItemMeta(meta);
  767. continue;
  768. }else if(content.contains("removefrom")){
  769. String[] split = content.split(" ");
  770. Stat stat = logic_api.getStat(split[1]);
  771.  
  772. if(stat== null)
  773. continue;
  774.  
  775. double add = Double.valueOf(logic_api.calculateFromString(item, split[2])) * -1;
  776.  
  777. ItemMeta meta = item.getItemMeta();
  778. List<String> lore = item.getItemMeta().getLore();
  779. lore = changeStatValue(lore, stat, add);
  780.  
  781. meta.setLore(lore);
  782. item.setItemMeta(meta);
  783. continue;
  784. }else if(content.contains("setstat")){
  785. String[] split = content.split(" ");
  786. Stat stat = logic_api.getStat(split[1]);
  787.  
  788. if(stat== null)
  789. continue;
  790.  
  791. double newval = Double.valueOf(logic_api.calculateFromString(item, split[2]));
  792.  
  793. if(stat.cast_to_int)
  794. newval = (int) newval;
  795.  
  796. ItemMeta meta = item.getItemMeta();
  797. List<String> lore = item.getItemMeta().getLore();
  798. for(int i = 0; i < lore.size(); i++){
  799. String line = lore.get(i);
  800. if(line.contains(stat.name)){
  801. lore.set(i, colorizeText(stat.format.replace("%value%", newval+"")));
  802. }
  803. }
  804.  
  805. meta.setLore(lore);
  806. item.setItemMeta(meta);
  807. continue;
  808. }
  809. }
  810. }else{
  811. continue;
  812. }
  813. }
  814.  
  815.  
  816. return item;
  817. }
  818.  
  819.  
  820. public List<String> changeStatValue(List<String> lore, Stat stat, double change){
  821.  
  822. for(int i = 0; i < lore.size(); i++){
  823. String line = lore.get(i);
  824. if(line.contains(stat.name)){
  825. String[] linesplit = line.split(" ");
  826. String[] formatsplit = stat.format.split(" ");
  827. String stat_string = "";
  828.  
  829. for(int y = 0; y < formatsplit.length; y++){
  830. if(formatsplit[y].contains("%value%")){
  831. stat_string = stripColours(linesplit[y]);
  832. }
  833. }
  834.  
  835. Matcher match1 = single.matcher(stat_string);
  836. Matcher match2 = doubles.matcher(stat_string);
  837. System.out.println("Found stat " + stat_string);
  838. if(match1.find()){
  839. double stat_value = Double.parseDouble(match1.group(1)) + change;
  840. System.out.println("Matched ");
  841. if(stat.cast_to_int)
  842. stat_value = (int) stat_value;
  843.  
  844. System.out.println(stat_value);
  845.  
  846. lore.set(i, colorizeText(stat.format.replace("%value%", stat_value+"")));
  847. return lore;
  848. }else if ( match2.find()){
  849. double stat_value1 = Double.parseDouble(match2.group(2)) + change;
  850. double stat_value2 = Double.parseDouble(match2.group(4)) + change;
  851.  
  852. if(stat.cast_to_int){
  853. stat_value1= (int) stat_value1;
  854. stat_value2 = (int) stat_value2;
  855. }
  856.  
  857. lore.set(i, colorizeText(stat.format.replace("%value%", stat_value1+"-"+stat_value2)));
  858.  
  859. return lore;
  860. }
  861. }
  862.  
  863. }
  864.  
  865. return lore;
  866. }
  867.  
  868. public double getStatValue(ItemStack item, Stat stat){
  869. List<String> lore = item.getItemMeta().getLore();
  870.  
  871.  
  872.  
  873. for(String str : lore){
  874. if(str.contains(stat.name)){
  875.  
  876. String[] linesplit = str.split(" ");
  877. String[] formatsplit = stat.format.split(" ");
  878. String stat_string = "";
  879.  
  880. for(int y = 0; y < formatsplit.length; y++){
  881. if(formatsplit[y].contains("%value%")){
  882. stat_string = stripColours(linesplit[y]);
  883. }
  884. }
  885.  
  886. Matcher match1 = single.matcher(stat_string);
  887. Matcher match2 = doubles.matcher(stat_string);
  888.  
  889. Double stat_value = 0D;
  890.  
  891. if(match1.find()){
  892. stat_value = Double.parseDouble(match1.group(1));
  893. }else if(match2.find()){
  894. stat_value = Double.parseDouble(match2.group(4));
  895. }
  896.  
  897. /*double cur_value = 0;
  898.  
  899. Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(str);
  900. if (m.find()) {
  901. cur_value = Double.parseDouble(m.group(1));
  902. }*/
  903.  
  904. return stat_value;
  905. }
  906. }
  907.  
  908. return 0;
  909. }
  910.  
  911.  
  912. public void sendRawMsg(List<String> lore, Player player, String colour, int newlevel, String name) {
  913.  
  914. String msg = Main.getPlugin().getConfig().getString("lang.success");
  915. msg = msg.replaceAll("%player%", player.getDisplayName());
  916. msg = msg.replaceAll("%upgrade%",
  917. colour + Main.getPlugin().getConfig().getString("upgrade.format.format").replace("%number%", newlevel + ""));
  918.  
  919. JsonBuilder jb;
  920.  
  921. String rawmsg = name + "\n";
  922. int i = 0;
  923. for (String l : lore) {
  924. if (i == (lore.size() - 1))
  925. rawmsg = rawmsg + l;
  926. else
  927. rawmsg = rawmsg + l + "\n";
  928. i++;
  929. }
  930.  
  931. jb = new JsonBuilder().withText(colorizeText(msg)).withHoverEvent(HoverAction.SHOW_TEXT, colorizeText(rawmsg));
  932. jb.toString();
  933. for (Player p : Bukkit.getOnlinePlayers()) {
  934. Main.getApi().sendJson(p);
  935. }
  936. JsonBuilder.extras.clear();
  937. }
  938.  
  939.  
  940. public void runAnim(final Player player){
  941. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
  942. AttributeApi api = Main.getApi();
  943.  
  944. for(final String key : getConfig().getConfigurationSection("upgrade.animation").getKeys(false)){
  945. scheduler.scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
  946. @Override
  947. public void run() {
  948. if(!getConfig().getString("upgrade.animation."+key+".sound").equals(""))
  949. player.playSound(player.getLocation(), Sound.valueOf(getConfig().getString("upgrade.animation."+key+".sound")), 2F, 1F);
  950.  
  951. if(!getConfig().getString("upgrade.animation."+key+".output").equals("")) {
  952. api.sendAction(player, getConfig().getString("upgrade.animation."+key+".output"));
  953. }
  954.  
  955. if(!getConfig().getString("upgrade.animation."+key+".chat").equals(""))
  956. player.sendMessage(colorizeText(getConfig().getString("upgrade.animation."+key+".chat")));
  957. }
  958. }, getConfig().getInt("upgrade.animation."+key+".delay"));
  959. }
  960.  
  961. }
  962.  
  963. @Deprecated
  964. public int getItemLevel(ItemStack item){
  965. if(item == null){
  966. return 0;
  967. }else{
  968.  
  969. List<String> itemlore = item.getItemMeta().getLore();
  970. int level = -1;
  971.  
  972. for(String s: itemlore){
  973. level++;
  974. if (s.contains("Level:")){
  975. String levelraw = itemlore.get(level);
  976. String[] splitlevel = levelraw.split(" ");
  977. level = Integer.parseInt(stripColours(splitlevel[1].replaceAll("ยง2", "")));
  978. break;
  979. }
  980. }
  981. return level;
  982. }
  983.  
  984. }
  985.  
  986.  
  987. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement