1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-25 20:16:13 +01:00

Lets add option to limit boosts by levels

This commit is contained in:
Zrips 2019-01-31 16:47:52 +02:00
parent f310ad0677
commit 84681df41a
10 changed files with 168 additions and 76 deletions

View File

@ -21,7 +21,7 @@ import com.gamingmesh.jobs.container.JobItems;
public class ItemBoostManager { public class ItemBoostManager {
static HashMap<String, JobItems> items = new HashMap<String, JobItems>(); private static HashMap<String, JobItems> items = new HashMap<String, JobItems>();
public ItemBoostManager() { public ItemBoostManager() {
@ -82,24 +82,59 @@ public class ItemBoostManager {
Set<String> keys = cfg.getC().getKeys(false); Set<String> keys = cfg.getC().getKeys(false);
cfg.addComment("exampleBoost", "Name which will be used to identify this particular item boost",
"This is EXAMPLE boost and will be ignored");
cfg.addComment("exampleBoost.id", "Item Id which can be any material name as of 1.13 update",
"This is only used when performing command like /jobs give, but boost itself is not dependent on item type",
"You can use ingame command /jobs edititemboost to give particular boost to any item you are holding");
cfg.get("exampleBoost.id", "Golden_shovel");
cfg.addComment("exampleBoost.name", "(Optional) Item custom name", "Custom colors like &2 &5 can be used");
cfg.get("exampleBoost.name", "&2Custom item name");
cfg.addComment("exampleBoost.lore", "(Optional) Item custom lore", "Same as name, supports color codes");
cfg.get("exampleBoost.lore", Arrays.asList("&2Some random", "&5Lore with some", "&7Colors"));
cfg.addComment("exampleBoost.enchants", "(Optional) Item custom enchants",
"All enchantment names can be found https://hub.spigotmc.org/javadocs/spigot/org/bukkit/enchantments/Enchantment.html");
cfg.get("exampleBoost.enchants", Arrays.asList("FIRE_ASPECT=1", "DAMAGE_ALL=1"));
cfg.addComment("exampleBoost.moneyBoost", "[Required] Money boost: 1.1 is equals 10% more income when 0.9 is equals 10% less from base income");
for (CurrencyType oneC : CurrencyType.values()) {
cfg.get("exampleBoost." + oneC.toString().toLowerCase() + "Boost", 1D);
}
cfg.addComment("exampleBoost.jobs", "[Required] Jobs which should receive this boost",
"Can be specific jobs or use 'all' to give this boost for every job");
cfg.get("exampleBoost.jobs", Arrays.asList("Miner", "Woodcutter", "All"));
cfg.addComment("exampleBoost.levelFrom", "(Optional) Defines level of job from which this boost should be applied",
"Keep in mind that if boost have multiple jobs, then level will be checked by job which is requesting boost value");
cfg.get("exampleBoost.levelFrom", 0);
cfg.addComment("exampleBoost.levelUntil", "(Optional) Defines level of job until which this boost should be applied");
cfg.get("exampleBoost.levelUntil", 50);
for (String one : keys) { for (String one : keys) {
if (!cfg.getC().isConfigurationSection(one)) if (!cfg.getC().isConfigurationSection(one))
continue; continue;
CMIMaterial mat = CMIMaterial.get(cfg.get(one + ".id", "Stone")); // Ignoring example job
if (mat == null) { if (one.equalsIgnoreCase("exampleBoost"))
Jobs.getPluginLogger().warning("Cant load " + one + " boosted item!");
continue; continue;
CMIMaterial mat = null;
if (cfg.getC().isString(one + ".id")) {
mat = CMIMaterial.get(cfg.get(one + ".id", "Stone"));
} }
String name = null; String name = null;
if (cfg.getC().isString(one + ".name"))
if (cfg.getC().isString(one + ".name")) {
name = cfg.get(one + ".name", ""); name = cfg.get(one + ".name", "");
}
List<String> lore = new ArrayList<>(); List<String> lore = new ArrayList<>();
if (cfg.getC().getStringList(one + ".lore") != null && !cfg.getC().getStringList(one + ".lore").isEmpty()) if (cfg.getC().getStringList(one + ".lore") != null && !cfg.getC().getStringList(one + ".lore").isEmpty()) {
for (String eachLine : cfg.get(one + ".lore", Arrays.asList(""))) { for (String eachLine : cfg.get(one + ".lore", Arrays.asList(""))) {
lore.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine)); lore.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine));
} }
}
HashMap<Enchantment, Integer> enchants = new HashMap<>(); HashMap<Enchantment, Integer> enchants = new HashMap<>();
if (cfg.getC().getStringList(one + ".enchants") != null && !cfg.getC().getStringList(one + ".enchants").isEmpty()) if (cfg.getC().getStringList(one + ".enchants") != null && !cfg.getC().getStringList(one + ".enchants").isEmpty())
@ -132,7 +167,10 @@ public class ItemBoostManager {
Jobs.getPluginLogger().warning("Cant determine job by " + oneJ + " name for " + one + " boosted item!"); Jobs.getPluginLogger().warning("Cant determine job by " + oneJ + " name for " + one + " boosted item!");
continue; continue;
} }
jobs.add(job); if (oneJ.equalsIgnoreCase("all")) {
jobs.addAll(Jobs.getJobs());
} else if (job != null)
jobs.add(job);
} }
if (jobs.isEmpty()) { if (jobs.isEmpty()) {
@ -140,7 +178,18 @@ public class ItemBoostManager {
continue; continue;
} }
JobItems item = new JobItems(one.toLowerCase(), mat, 1, name, lore, enchants, b, jobs); JobItems item = new JobItems(one.toLowerCase(), mat, 1, name, lore, enchants, b, jobs);
if (cfg.getC().isInt(one + ".levelFrom")) {
item.setFromLevel(cfg.get(one + ".levelFrom", 0));
}
if (cfg.getC().isInt(one + ".levelUntil")) {
item.setUntilLevel(cfg.get(one + ".levelUntil", 1000));
}
Jobs.consoleMsg(one.toLowerCase() + " " + (item == null) + "");
for (Job oneJ : jobs) { for (Job oneJ : jobs) {
if (oneJ == null)
continue;
oneJ.getItemBonus().put(one.toLowerCase(), item); oneJ.getItemBonus().put(one.toLowerCase(), item);
} }
@ -172,4 +221,12 @@ public class ItemBoostManager {
public static JobItems getItemByKey(String key) { public static JobItems getItemByKey(String key) {
return items.get(key.toLowerCase()); return items.get(key.toLowerCase());
} }
public static HashMap<String, JobItems> getItems() {
return items;
}
public static void setItems(HashMap<String, JobItems> items) {
ItemBoostManager.items = items;
}
} }

View File

@ -58,6 +58,7 @@ import com.gamingmesh.jobs.dao.JobsDAO;
import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.dao.JobsDAOData;
import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.economy.PaymentData;
import com.gamingmesh.jobs.economy.PointsData; import com.gamingmesh.jobs.economy.PointsData;
import com.gamingmesh.jobs.stuff.Debug;
import com.gamingmesh.jobs.stuff.PerformCommands; import com.gamingmesh.jobs.stuff.PerformCommands;
public class PlayerManager { public class PlayerManager {
@ -758,7 +759,7 @@ public class PlayerManager {
ItemStack iih = Jobs.getNms().getItemInMainHand(player); ItemStack iih = Jobs.getNms().getItemInMainHand(player);
JobItems jitem = getJobsItemByNbt(iih); JobItems jitem = getJobsItemByNbt(iih);
if (jitem != null && jitem.getJobs().contains(prog)) if (jitem != null && jitem.getJobs().contains(prog))
data.add(jitem.getBoost()); data.add(jitem.getBoost(this.getJobsPlayer(player).getJobProgression(prog)));
for (ItemStack OneArmor : player.getInventory().getArmorContents()) { for (ItemStack OneArmor : player.getInventory().getArmorContents()) {
if (OneArmor == null || OneArmor.getType() == Material.AIR) if (OneArmor == null || OneArmor.getType() == Material.AIR)
@ -768,7 +769,7 @@ public class PlayerManager {
if (armorboost == null || !armorboost.getJobs().contains(prog)) if (armorboost == null || !armorboost.getJobs().contains(prog))
return data; return data;
data.add(armorboost.getBoost()); data.add(armorboost.getBoost(this.getJobsPlayer(player).getJobProgression(prog)));
} }
return data; return data;
@ -777,40 +778,33 @@ public class PlayerManager {
public boolean containsItemBoostByNBT(ItemStack item) { public boolean containsItemBoostByNBT(ItemStack item) {
if (item == null) if (item == null)
return false; return false;
return Jobs.getReflections().hasNbtString(item, "JobsItemBoost") || Jobs.getReflections().hasNbt(item, "JobsItemBoost"); return Jobs.getReflections().hasNbtString(item, "JobsItemBoost");
} }
// public BoostMultiplier getItemBoostByNBT(Job prog, ItemStack item) {
// BoostMultiplier bonus = new BoostMultiplier();
//// if (prog.getItemBonus().isEmpty())
//// return bonus;
// if (item == null)
// return bonus;
//
// if (!Jobs.getReflections().hasNbtString(item, "JobsItemBoost"))
// return bonus;
//
//// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", prog.getName());
// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost");
//
// if (itemName == null)
// return bonus;
// JobItems b = ItemBoostManager.getItemByKey((String) itemName);
// if (b == null)
// return bonus;
//
// return b.getBoost();
// }
public JobItems getJobsItemByNbt(ItemStack item) { public JobItems getJobsItemByNbt(ItemStack item) {
if (item == null) if (item == null)
return null; return null;
// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", prog.getName());
Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost"); Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost");
if (itemName == null) { if (itemName == null || ((String) itemName).isEmpty()) {
return null;
// Checking old boost items and converting to new format if needed
if (Jobs.getReflections().hasNbt(item, "JobsItemBoost")) {
for (Job one : Jobs.getJobs()) {
itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", one.getName());
if (itemName != null) {
JobItems b = ItemBoostManager.getItemByKey((String) itemName);
if (b != null) {
ItemStack ic = Jobs.getReflections().setNbt(item, "JobsItemBoost", b.getNode());
item.setItemMeta(ic.getItemMeta());
}
break;
}
}
}
if (itemName == null)
return null;
} }
JobItems b = ItemBoostManager.getItemByKey((String) itemName); JobItems b = ItemBoostManager.getItemByKey((String) itemName);
if (b == null) if (b == null)
@ -819,12 +813,12 @@ public class PlayerManager {
return b; return b;
} }
public BoostMultiplier getJobsBoostByNbt(ItemStack item) { // public BoostMultiplier getJobsBoostByNbt(ItemStack item) {
JobItems b = getJobsItemByNbt(item); // JobItems b = getJobsItemByNbt(item);
if (b == null) // if (b == null)
return null; // return null;
return b.getBoost(); // return b.getBoost();
} // }
public enum BoostOf { public enum BoostOf {
McMMO, PetPay, NearSpawner, Permission, Global, Dynamic, Item, Area McMMO, PetPay, NearSpawner, Permission, Global, Dynamic, Item, Area

View File

@ -215,27 +215,27 @@ public class Reflections {
} }
} }
// public Object getNbt(ItemStack item, String base, String path) { public Object getNbt(ItemStack item, String base, String path) {
// if (item == null) if (item == null)
// return null; return null;
// try { try {
// Object nbt = getNbt(item); Object nbt = getNbt(item);
// if (nbt == null) if (nbt == null)
// return null; return null;
//
// Method compoundMeth = nbt.getClass().getMethod("getCompound", String.class); Method compoundMeth = nbt.getClass().getMethod("getCompound", String.class);
// Object compoundRes = compoundMeth.invoke(nbt, base); Object compoundRes = compoundMeth.invoke(nbt, base);
//
// if (compoundRes == null) if (compoundRes == null)
// return null; return null;
//
// Method meth = compoundRes.getClass().getMethod("getString", String.class); Method meth = compoundRes.getClass().getMethod("getString", String.class);
// Object res = meth.invoke(compoundRes, path); Object res = meth.invoke(compoundRes, path);
// return res; return res;
// } catch (Throwable e) { } catch (Throwable e) {
// return null; return null;
// } }
// } }
// //
// public ItemStack setNbt(ItemStack item, String base, String path, String value) { // public ItemStack setNbt(ItemStack item, String base, String path, String value) {
// if (item == null) // if (item == null)

View File

@ -103,6 +103,9 @@ public class edititembonus implements Cmd {
JobItems item = ItemBoostManager.getItemByKey((String) key); JobItems item = ItemBoostManager.getItemByKey((String) key);
if (item == null)
return true;
BoostMultiplier boost = item.getBoost(); BoostMultiplier boost = item.getBoost();
for (Job one : item.getJobs()) { for (Job one : item.getJobs()) {

View File

@ -63,8 +63,11 @@ public class give implements Cmd {
sender.sendMessage(Jobs.getLanguage().getMessage("command.give.output.notonline", "%playername%", args[0])); sender.sendMessage(Jobs.getLanguage().getMessage("command.give.output.notonline", "%playername%", args[0]));
return true; return true;
} }
if (name == null)
name = actions.items;
if (name == null || itemName == null) { if (itemName == null) {
Jobs.getCommandManager().sendUsage(sender, "give"); Jobs.getCommandManager().sendUsage(sender, "give");
return true; return true;
} }

View File

@ -59,7 +59,7 @@ public class itembonus implements Cmd {
if (jitem == null) if (jitem == null)
continue; continue;
for (Job one : jitem.getJobs()) { for (Job one : jitem.getJobs()) {
BoostMultiplier boost = jitem.getBoost(); BoostMultiplier boost = jitem.getBoost(jPlayer.getJobProgression(one));
boolean any = false; boolean any = false;
for (CurrencyType oneC : CurrencyType.values()) { for (CurrencyType oneC : CurrencyType.values()) {
if (boost.get(oneC) != 0D) if (boost.get(oneC) != 0D)

View File

@ -209,7 +209,7 @@ public class LanguageManager {
c.get("command.edititembonus.help.info", "Edit item boost bonus"); c.get("command.edititembonus.help.info", "Edit item boost bonus");
c.get("command.edititembonus.help.args", "list/add/remove [jobname] [itemBoostName]"); c.get("command.edititembonus.help.args", "list/add/remove [jobname] [itemBoostName]");
Jobs.getGCManager().commandArgs.put("edititembonus", Arrays.asList("list%%add%%remove", "[jobname]", "[jobitemname]")); Jobs.getGCManager().commandArgs.put("edititembonus", Arrays.asList("list%%add%%remove", "[boosteditems]"));
c.get("command.bonus.help.info", "Show job bonuses"); c.get("command.bonus.help.info", "Show job bonuses");
c.get("command.bonus.help.args", "[jobname]"); c.get("command.bonus.help.args", "[jobname]");

View File

@ -369,6 +369,8 @@ public class Job {
*/ */
@Deprecated @Deprecated
public HashMap<String, JobItems> getItemBonus() { public HashMap<String, JobItems> getItemBonus() {
if (jobItems == null)
jobItems = new HashMap<String, JobItems>();
return jobItems; return jobItems;
} }

View File

@ -38,9 +38,11 @@ public class JobItems {
ItemStack item; ItemStack item;
private BoostMultiplier boostMultiplier = new BoostMultiplier(); private BoostMultiplier boostMultiplier = new BoostMultiplier();
private List<Job> jobs = new ArrayList<Job>(); private List<Job> jobs = new ArrayList<Job>();
private int fromLevel = 0;
private int untilLevel = Integer.MAX_VALUE;
public JobItems(String node, CMIMaterial mat, int amount, String name, List<String> lore, HashMap<Enchantment, Integer> enchants, BoostMultiplier boostMultiplier, List<Job> jobs) { public JobItems(String node, CMIMaterial mat, int amount, String name, List<String> lore, HashMap<Enchantment, Integer> enchants, BoostMultiplier boostMultiplier, List<Job> jobs) {
mat = mat == null ? CMIMaterial.STONE : mat;
try { try {
item = mat.newItemStack(); item = mat.newItemStack();
item.setAmount(amount); item.setAmount(amount);
@ -103,6 +105,14 @@ public class JobItems {
return boostMultiplier.clone(); return boostMultiplier.clone();
} }
public BoostMultiplier getBoost(JobProgression job) {
if (job == null || !this.jobs.contains(job.getJob()))
return new BoostMultiplier();
if (job.getLevel() < getFromLevel() || job.getLevel() > getUntilLevel())
return new BoostMultiplier();
return boostMultiplier.clone();
}
public List<Job> getJobs() { public List<Job> getJobs() {
return jobs; return jobs;
} }
@ -110,4 +120,20 @@ public class JobItems {
public void setJobs(List<Job> jobs) { public void setJobs(List<Job> jobs) {
this.jobs = jobs; this.jobs = jobs;
} }
public int getFromLevel() {
return fromLevel;
}
public void setFromLevel(int fromLevel) {
this.fromLevel = fromLevel;
}
public int getUntilLevel() {
return untilLevel;
}
public void setUntilLevel(int untilLevel) {
this.untilLevel = untilLevel;
}
} }

View File

@ -76,17 +76,24 @@ public class TabComplete implements TabCompleter {
} }
break; break;
case "[jobitemname]": case "[jobitemname]":
Job oneJob = Jobs.getJob(args[i - 1]); if (args[3].equals("items")) {
if (oneJob != null) for (Entry<String, JobItems> one : ItemBoostManager.getItems().entrySet()) {
if (args[3].equals("items")) { temp.add(one.getValue().getNode());
for (Entry<String, JobItems> item : ItemBoostManager.getItemsMapByJob(oneJob).entrySet()) {
temp.add(item.getValue().getNode());
}
} else if (args[3].equals("limiteditems")) {
for (Entry<String, JobLimitedItems> limitedItem : oneJob.getLimitedItems().entrySet()) {
temp.add(limitedItem.getValue().getNode());
}
} }
} else if (args[3].equals("limiteditems")) {
Job oneJob = Jobs.getJob(args[i - 1]);
if (oneJob != null)
if (args[3].equals("limiteditems")) {
for (Entry<String, JobLimitedItems> limitedItem : oneJob.getLimitedItems().entrySet()) {
temp.add(limitedItem.getValue().getNode());
}
}
}
break;
case "[boosteditems]":
for (Entry<String, JobItems> one : ItemBoostManager.getItems().entrySet()) {
temp.add(one.getValue().getNode());
}
break; break;
case "[oldjob]": case "[oldjob]":
JobsPlayer onePlayerJob = Jobs.getPlayerManager().getJobsPlayer(args[i - 1]); JobsPlayer onePlayerJob = Jobs.getPlayerManager().getJobsPlayer(args[i - 1]);