Element edition fixed

This commit is contained in:
Indyuce 2020-08-10 22:49:16 +02:00
parent 937ce9c884
commit 95f4fa076d
9 changed files with 105 additions and 75 deletions

View File

@ -33,7 +33,7 @@ public enum Element {
target.setFireTicks((int) (attack * 2));
result.addDamage(absolute);
}
}),
}, 19, 25),
ICE(VersionMaterial.SNOWBALL.toMaterial(), "Ice", ChatColor.AQUA, new ElementParticle(Particle.BLOCK_CRACK, .07f, 16, Material.ICE),
new ElementHandler() {
@ -57,7 +57,7 @@ public enum Element {
target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, (int) (attack * 1.5), 5));
result.addDamage(absolute);
}
}),
}, 20, 24),
WIND(Material.FEATHER, "Wind", ChatColor.GRAY, new ElementParticle(Particle.EXPLOSION_NORMAL, .06f, 8), new ElementHandler() {
public void elementAttack(CachedStats stats, ItemAttackResult result, LivingEntity target, double attack, double absolute) {
@ -74,7 +74,7 @@ public enum Element {
target.getWorld().spawnParticle(Particle.CLOUD, target.getLocation().add(0, target.getHeight() / 2, 0), 0, Math.cos(k), .01,
Math.sin(k), .15);
}
}),
}, 28, 34),
EARTH(VersionMaterial.OAK_SAPLING.toMaterial(), "Earth", ChatColor.GREEN, new ElementParticle(Particle.BLOCK_CRACK, .05f, 24, Material.DIRT),
new ElementHandler() {
@ -89,7 +89,7 @@ public enum Element {
if (MMOUtils.canDamage(stats.getPlayer(), entity))
entity.setVelocity(new Vector(0, 1, 0));
}
}),
}, 29, 33),
THUNDER(VersionMaterial.GUNPOWDER.toMaterial(), "Thunder", ChatColor.YELLOW, new ElementParticle(Particle.FIREWORKS_SPARK, .05f, 8),
new ElementHandler() {
@ -105,7 +105,7 @@ public enum Element {
target.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, target.getLocation().add(0, target.getHeight() / 2, 0), 0,
Math.cos(k), .01, Math.sin(k), .18);
}
}),
}, 30, 32),
WATER(VersionMaterial.LILY_PAD.toMaterial(), "Water", ChatColor.BLUE, new ElementParticle(Particle.BLOCK_CRACK, .07f, 32, Material.WATER),
new ElementHandler() {
@ -125,20 +125,24 @@ public enum Element {
}
}.runTaskTimer(MMOItems.plugin, 0, 1);
}
});
}, 37, 43);
private final ItemStack item;
private final String name;
private final ChatColor color;
private final ElementParticle particle;
private final ElementHandler handler;
private final int damageGuiSlot, defenseGuiSlot;
private Element(Material material, String name, ChatColor color, ElementParticle particle, ElementHandler handler) {
private Element(Material material, String name, ChatColor color, ElementParticle particle, ElementHandler handler, int damageGuiSlot,
int defenseGuiSlot) {
this.item = new ItemStack(material);
this.name = name;
this.color = color;
this.particle = particle;
this.handler = handler;
this.damageGuiSlot = damageGuiSlot;
this.defenseGuiSlot = defenseGuiSlot;
}
public ItemStack getItem() {
@ -161,6 +165,14 @@ public enum Element {
return handler;
}
public int getDamageGuiSlot() {
return damageGuiSlot;
}
public int getDefenseGuiSlot() {
return defenseGuiSlot;
}
public static interface ElementHandler {
public void elementAttack(CachedStats stats, ItemAttackResult result, LivingEntity target, double damage, double absolute);
}

View File

@ -37,9 +37,9 @@ public class NumericStatFormula implements RandomStatData {
if (object instanceof String) {
String[] split = object.toString().split("\\ ");
base = Double.parseDouble(split[0]);
scale = Double.parseDouble(split[1]);
spread = Double.parseDouble(split[2]);
maxSpread = Double.parseDouble(split[3]);
scale = split.length > 1 ? Double.parseDouble(split[1]) : 0;
spread = split.length > 2 ? Double.parseDouble(split[2]) : 0;
maxSpread = split.length > 3 ? Double.parseDouble(split[3]) : 0;
return;
}

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmoitems.gui.edition;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
@ -19,13 +18,13 @@ import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.ability.Ability;
import net.Indyuce.mmoitems.api.ability.Ability.CastingMode;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.ItemTag;
import net.mmogroup.mmolib.api.util.AltChar;
import net.mmogroup.mmolib.version.VersionMaterial;
public class AbilityListEdition extends EditionInventory {
private static final DecimalFormat modifierFormat = new DecimalFormat("0.###");
private static final int[] slots = { 19, 20, 21, 22, 23, 24, 25 };
public AbilityListEdition(Player player, MMOItemTemplate template) {
@ -59,11 +58,16 @@ public class AbilityListEdition extends EditionInventory {
boolean check = false;
if (ability != null)
for (String modifier : getEditedSection().getConfigurationSection("ability." + key).getKeys(false))
if (!modifier.equals("type") && !modifier.equals("mode") && ability.getModifiers().contains(modifier)) {
abilityItemLore.add(ChatColor.GRAY + "* " + MMOUtils.caseOnWords(modifier.toLowerCase().replace("-", " ")) + ": "
+ ChatColor.GOLD + modifierFormat.format(getEditedSection().getDouble("ability." + key + "." + modifier)));
check = true;
}
if (!modifier.equals("type") && !modifier.equals("mode") && ability.getModifiers().contains(modifier))
try {
abilityItemLore.add(
ChatColor.GRAY + "* " + MMOUtils.caseOnWords(modifier.toLowerCase().replace("-", " ")) + ": " + ChatColor.GOLD
+ new NumericStatFormula(getEditedSection().get("ability." + key + "." + modifier)).toString());
check = true;
} catch (IllegalArgumentException exception) {
abilityItemLore.add(ChatColor.GRAY + "* " + MMOUtils.caseOnWords(modifier.toLowerCase().replace("-", " ")) + ": "
+ ChatColor.GOLD + "Unreadable");
}
if (check)
abilityItemLore.add("");

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmoitems.gui.edition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -72,12 +73,22 @@ public abstract class EditionInventory extends PluginInventory {
}
/**
* @return The item data map used to display what the player is currently
* editing. If he is editing a stat modifier, use the modifier item
* data map. Otherwise, use the base item data map
* Used in edition GUIs to display the current stat data of the edited
* template.
*
* @param stat
* The stat which data we are looking for
* @return Optional which contains the corresponding random stat data
*/
public Map<ItemStat, RandomStatData> getCurrentDataMap() {
return editedModifier != null ? editedModifier.getItemData() : template.getBaseItemData();
public Optional<RandomStatData> getEventualStatData(ItemStat stat) {
/**
* The item data map used to display what the player is currently
* editing. If he is editing a stat modifier, use the modifier item data
* map. Otherwise, use the base item data map
*/
Map<ItemStat, RandomStatData> map = editedModifier != null ? editedModifier.getItemData() : template.getBaseItemData();
return map.containsKey(stat) ? Optional.of(map.get(stat)) : Optional.empty();
}
public void registerTemplateEdition() {

View File

@ -1,9 +1,8 @@
package net.Indyuce.mmoitems.gui.edition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -19,28 +18,14 @@ import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Element;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.stat.data.random.RandomElementListData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.api.util.AltChar;
public class ElementsEdition extends EditionInventory {
public static Map<Integer, String> correspondingSlot = new HashMap<>();
private static final int[] slots = { 19, 25, 20, 24, 28, 34, 29, 33, 30, 32, 37, 43, 38, 42, 39, 41 };
static {
correspondingSlot.put(19, "fire.damage");
correspondingSlot.put(25, "fire.defense");
correspondingSlot.put(20, "ice.damage");
correspondingSlot.put(24, "ice.defense");
correspondingSlot.put(28, "wind.damage");
correspondingSlot.put(34, "wind.defense");
correspondingSlot.put(29, "earth.damage");
correspondingSlot.put(33, "earth.defense");
correspondingSlot.put(30, "thunder.damage");
correspondingSlot.put(32, "thunder.defense");
correspondingSlot.put(37, "water.damage");
correspondingSlot.put(43, "water.defense");
}
public ElementsEdition(Player player, MMOItemTemplate template) {
super(player, template);
}
@ -54,9 +39,12 @@ public class ElementsEdition extends EditionInventory {
ItemStack attack = element.getItem().clone();
ItemMeta attackMeta = attack.getItemMeta();
attackMeta.setDisplayName(ChatColor.GREEN + element.getName() + " Damage");
List<String> attackLore = new ArrayList<String>();
List<String> attackLore = new ArrayList<>();
Optional<RandomStatData> optional = getEventualStatData(ItemStat.ELEMENTS);
attackLore.add(ChatColor.GRAY + "Current Value: " + ChatColor.GREEN
+ getEditedSection().getDouble("element." + element.getName().toLowerCase() + ".damage"));
+ (optional.isPresent() && ((RandomElementListData) optional.get()).hasDamage(element)
? ((RandomElementListData) optional.get()).getDamage(element) + " (%)"
: "---"));
attackLore.add("");
attackLore.add(ChatColor.YELLOW + AltChar.listDash + " Click to change this value.");
attackLore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove this value.");
@ -66,9 +54,11 @@ public class ElementsEdition extends EditionInventory {
ItemStack defense = element.getItem().clone();
ItemMeta defenseMeta = defense.getItemMeta();
defenseMeta.setDisplayName(ChatColor.GREEN + element.getName() + " Defense");
List<String> defenseLore = new ArrayList<String>();
List<String> defenseLore = new ArrayList<>();
defenseLore.add(ChatColor.GRAY + "Current Value: " + ChatColor.GREEN
+ getEditedSection().getDouble("element." + element.getName().toLowerCase() + ".defense"));
+ (optional.isPresent() && ((RandomElementListData) optional.get()).hasDefense(element)
? ((RandomElementListData) optional.get()).getDefense(element) + " (%)"
: "---"));
defenseLore.add("");
defenseLore.add(ChatColor.YELLOW + AltChar.listDash + " Click to change this value.");
defenseLore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove this value.");
@ -93,28 +83,38 @@ public class ElementsEdition extends EditionInventory {
if (event.getInventory() != event.getClickedInventory() || !MMOUtils.isMetaItem(item, false))
return;
if (correspondingSlot.containsKey(event.getSlot())) {
if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(this, ItemStat.ELEMENTS, event.getSlot()).enable("Write in the value you want.");
String elementPath = getElementPath(event.getSlot());
if (elementPath == null)
return;
if (event.getAction() == InventoryAction.PICKUP_HALF) {
String elementPath = correspondingSlot.get(event.getSlot());
getEditedSection().set("element." + elementPath, null);
if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(this, ItemStat.ELEMENTS, elementPath).enable("Write in the value you want.");
// clear element config section
String elementName = elementPath.split("\\.")[0];
if (getEditedSection().contains("element." + elementName)
&& getEditedSection().getConfigurationSection("element." + elementName).getKeys(false).isEmpty()) {
getEditedSection().set("element." + elementName, null);
if (getEditedSection().getConfigurationSection("element").getKeys(false).isEmpty())
getEditedSection().set("element", null);
}
else if (event.getAction() == InventoryAction.PICKUP_HALF) {
getEditedSection().set("element." + elementPath, null);
registerTemplateEdition();
new ElementsEdition(player, template).open(getPreviousPage());
player.sendMessage(MMOItems.plugin.getPrefix() + ChatColor.RED + MMOUtils.caseOnWords(elementPath.replace(".", " ")) + ChatColor.GRAY
+ " successfully removed.");
// clear element config section
String elementName = elementPath.split("\\.")[0];
if (getEditedSection().contains("element." + elementName)
&& getEditedSection().getConfigurationSection("element." + elementName).getKeys(false).isEmpty()) {
getEditedSection().set("element." + elementName, null);
if (getEditedSection().getConfigurationSection("element").getKeys(false).isEmpty())
getEditedSection().set("element", null);
}
registerTemplateEdition();
new ElementsEdition(player, template).open(getPreviousPage());
player.sendMessage(MMOItems.plugin.getPrefix() + ChatColor.RED + MMOUtils.caseOnWords(elementPath.replace(".", " ")) + ChatColor.GRAY
+ " successfully removed.");
}
}
public String getElementPath(int guiSlot) {
for (Element element : Element.values())
if (element.getDamageGuiSlot() == guiSlot)
return element.name().toLowerCase() + ".damage";
else if (element.getDefenseGuiSlot() == guiSlot)
return element.name().toLowerCase() + ".defense";
return null;
}
}

View File

@ -2,8 +2,6 @@ package net.Indyuce.mmoitems.gui.edition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
@ -19,7 +17,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.type.InternalStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.MMOLib;
@ -59,8 +56,7 @@ public class ItemEdition extends EditionInventory {
lore.add(ChatColor.GRAY + MMOLib.plugin.parseColors(s1));
lore.add("");
Map<ItemStat, RandomStatData> itemData = getCurrentDataMap();
stat.whenDisplayed(lore, itemData.containsKey(stat) ? Optional.of(itemData.get(stat)) : Optional.empty());
stat.whenDisplayed(lore, getEventualStatData(stat));
meta.setLore(lore);
item.setItemMeta(meta);

View File

@ -16,6 +16,7 @@ import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Element;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.gui.edition.ElementsEdition;
@ -54,12 +55,10 @@ public class Elements extends ItemStat {
@Override
public void whenInput(EditionInventory inv, String message, Object... info) {
String elementPath = ElementsEdition.correspondingSlot.get(info[0]);
double value = Double.parseDouble(message);
String elementPath = info[0].toString();
inv.getEditedSection().set("element." + elementPath, value);
if (value == 0)
inv.getEditedSection().set("element." + elementPath, null);
NumericStatFormula formula = new NumericStatFormula(message);
formula.fillConfigurationSection(inv.getEditedSection(), "element." + elementPath);
// clear element config section
String elementName = elementPath.split("\\.")[0];
@ -73,7 +72,7 @@ public class Elements extends ItemStat {
inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + ChatColor.RED + MMOUtils.caseOnWords(elementPath.replace(".", " ")) + ChatColor.GRAY
+ " successfully changed to " + value + ".");
+ " successfully changed to " + ChatColor.GOLD + formula.toString() + ChatColor.GRAY + ".");
}
@Override

View File

@ -53,8 +53,8 @@ public class ElementListData implements StatData, Mergeable {
ElementListData extra = (ElementListData) data;
for (Element element : extra.damage.keySet())
damage.put(element, extra.damage.get(element) + (damage.containsKey(element) ? damage.get(element) : 0));
damage.put(element, extra.damage.get(element) + damage.getOrDefault(element, 0d));
for (Element element : extra.defense.keySet())
defense.put(element, extra.defense.get(element) + (defense.containsKey(element) ? defense.get(element) : 0));
defense.put(element, extra.defense.get(element) + defense.getOrDefault(element, 0d));
}
}

View File

@ -28,6 +28,14 @@ public class RandomElementListData implements StatData, RandomStatData {
}
}
public boolean hasDamage(Element element) {
return damage.containsKey(element);
}
public boolean hasDefense(Element element) {
return defense.containsKey(element);
}
public NumericStatFormula getDefense(Element element) {
return defense.getOrDefault(element, NumericStatFormula.ZERO);
}