use of org.bukkit.configuration.MemoryConfiguration in project MagicPlugin by elBukkit.
the class HeroesManager method createSkillSpell.
@Nullable
public SpellTemplate createSkillSpell(MagicController controller, String skillName) {
if (skills == null)
return null;
Skill skill = skills.getSkill(skillName);
if (skill == null)
return null;
MageSpell newSpell = new HeroesSkillSpell();
newSpell.initialize(controller);
ConfigurationSection config = new MemoryConfiguration();
String iconURL = SkillConfigManager.getRaw(skill, "icon-url", SkillConfigManager.getRaw(skill, "icon_url", null));
if (iconURL == null || iconURL.isEmpty()) {
String icon = SkillConfigManager.getRaw(skill, "icon", null);
if (icon == null || icon.isEmpty()) {
config.set("icon", controller.getDefaultSkillIcon());
} else if (icon.startsWith("http://")) {
config.set("icon_url", icon);
} else {
config.set("icon", icon);
}
} else {
config.set("icon_url", iconURL);
}
String iconDisabledURL = SkillConfigManager.getRaw(skill, "icon-disabled-url", SkillConfigManager.getRaw(skill, "icon_disabled_url", null));
if (iconDisabledURL == null || iconDisabledURL.isEmpty()) {
String icon = SkillConfigManager.getRaw(skill, "icon-disabled", SkillConfigManager.getRaw(skill, "icon_disabled", null));
if (icon != null && !icon.isEmpty()) {
if (icon.startsWith("http://")) {
config.set("icon_disabled_url", icon);
} else {
config.set("icon_disabled", icon);
}
}
} else {
config.set("icon_disabled_url", iconDisabledURL);
}
String nameTemplate = controller.getMessages().get("skills.item_name", "$skill");
String skillDisplayName = SkillConfigManager.getRaw(skill, "name", skill.getName());
config.set("name", nameTemplate.replace("$skill", skillDisplayName));
config.set("category", "skills");
String descriptionTemplate = controller.getMessages().get("skills.item_description", "$description");
descriptionTemplate = descriptionTemplate.replace("$description", SkillConfigManager.getRaw(skill, "description", ""));
config.set("description", descriptionTemplate);
newSpell.loadTemplate("heroes*" + skillName, config);
return newSpell;
}
use of org.bukkit.configuration.MemoryConfiguration in project MagicPlugin by elBukkit.
the class CasterProperties method setAttribute.
@Override
public void setAttribute(String attributeKey, Double attributeValue) {
ConfigurationSection attributes = getConfigurationSection("attributes");
if (attributes == null) {
if (attributeValue == null)
return;
attributes = new MemoryConfiguration();
}
attributes.set(attributeKey, attributeValue);
setProperty("attributes", attributes);
updated();
}
use of org.bukkit.configuration.MemoryConfiguration in project MagicPlugin by elBukkit.
the class ModifyPropertiesAction method perform.
@Override
public SpellResult perform(CastContext context) {
if (modify == null) {
return SpellResult.FAIL;
}
Entity entity = context.getTargetEntity();
Mage mage = entity == null ? null : context.getController().getRegisteredMage(entity);
if (mage == null) {
return SpellResult.NO_TARGET;
}
CasterProperties properties = null;
if (modifyTarget.equals("wand")) {
properties = mage.getActiveWand();
} else if (modifyTarget.equals("player")) {
properties = mage.getProperties();
} else {
properties = mage.getClass(modifyTarget);
}
// I am now wishing I hadn't made a base class called "mage" :(
if (properties == null && modifyTarget.equals("mage")) {
properties = mage.getProperties();
}
if (properties == null) {
return SpellResult.NO_TARGET;
}
ConfigurationSection original = new MemoryConfiguration();
ConfigurationSection changed = new MemoryConfiguration();
for (ModifyProperty property : modify) {
Object originalValue = properties.getProperty(property.path);
Object newValue = property.value;
if ((originalValue == null || originalValue instanceof Number) && property.value instanceof String) {
EquationTransform transform = EquationStore.getInstance().getTransform((String) property.value);
originalValue = originalValue == null ? null : NumberConversions.toDouble(originalValue);
double defaultValue = property.defaultValue == null ? 0 : property.defaultValue;
if (transform.isValid()) {
if (originalValue == null) {
originalValue = defaultValue;
} else if (property.max != null && (Double) originalValue >= property.max) {
continue;
} else if (property.min != null && (Double) originalValue <= property.min) {
continue;
}
transform.setVariable("x", (Double) originalValue);
double transformedValue = transform.get();
if (!Double.isNaN(transformedValue)) {
if (property.max != null) {
transformedValue = Math.min(transformedValue, property.max);
}
if (property.min != null) {
transformedValue = Math.max(transformedValue, property.min);
}
newValue = transformedValue;
}
}
}
changed.set(property.path, newValue);
original.set(property.path, originalValue);
}
if (changed.getKeys(false).isEmpty())
return SpellResult.NO_TARGET;
if (upgrade)
properties.upgrade(changed);
else
properties.configure(changed);
context.registerForUndo(new ModifyPropertyUndoAction(original, properties));
return SpellResult.CAST;
}
use of org.bukkit.configuration.MemoryConfiguration in project MagicPlugin by elBukkit.
the class ConfigurationMageDataStore method save.
public static void save(MageController controller, MageData mage, ConfigurationSection saveFile) {
saveFile.set("id", mage.getId());
saveFile.set("name", mage.getName());
saveFile.set("last_cast", mage.getLastCast());
saveFile.set("cooldown_expiration", mage.getCooldownExpiration());
saveFile.set("last_death_location", ConfigurationUtils.fromLocation(mage.getLastDeathLocation()));
Location location = mage.getLocation();
if (location != null) {
saveFile.set("location", ConfigurationUtils.fromLocation(location));
}
saveFile.set("destination_warp", mage.getDestinationWarp());
saveFile.set("fall_protection_count", mage.getFallProtectionCount());
saveFile.set("fall_protection", mage.getFallProtectionDuration());
BrushData brush = mage.getBrushData();
if (brush != null) {
ConfigurationSection brushNode = saveFile.createSection("brush");
try {
Location cloneSource = brush.getCloneLocation();
if (cloneSource != null) {
brushNode.set("clone_location", ConfigurationUtils.fromLocation(cloneSource));
}
Location cloneTarget = brush.getCloneTarget();
if (cloneTarget != null) {
brushNode.set("clone_target", ConfigurationUtils.fromLocation(cloneTarget));
}
Location materialTarget = brush.getMaterialTarget();
if (materialTarget != null) {
brushNode.set("material_target", ConfigurationUtils.fromLocation(materialTarget));
}
brushNode.set("map_id", brush.getMapId());
brushNode.set("material", ConfigurationUtils.fromMaterial(brush.getMaterial()));
brushNode.set("data", brush.getMaterialData());
brushNode.set("schematic", brush.getSchematicName());
brushNode.set("scale", brush.getScale());
brushNode.set("erase", brush.isFillWithAir());
} catch (Exception ex) {
controller.getLogger().warning("Failed to save brush data: " + ex.getMessage());
ex.printStackTrace();
}
}
UndoData undoData = mage.getUndoData();
if (undoData != null) {
List<Map<String, Object>> nodeList = new ArrayList<>();
List<UndoList> undoList = undoData.getBlockList();
for (UndoList list : undoList) {
MemoryConfiguration listNode = new MemoryConfiguration();
list.save(listNode);
nodeList.add(listNode.getValues(true));
}
saveFile.set("undo", nodeList);
}
ConfigurationSection spellNode = saveFile.createSection("spells");
Collection<SpellData> spellData = mage.getSpellData();
if (spellData != null) {
for (SpellData spell : spellData) {
ConfigurationSection node = spellNode.createSection(spell.getKey().getKey());
node.set("cast_count", spell.getCastCount());
node.set("last_cast", spell.getLastCast());
node.set("last_earn", spell.getLastEarn());
node.set("cooldown_expiration", spell.getCooldownExpiration());
node.set("active", spell.isActive() ? true : null);
ConfigurationSection extra = spell.getExtraData();
if (extra != null) {
ConfigurationUtils.addConfigurations(node, extra);
}
}
}
Map<String, ItemStack> boundWands = mage.getBoundWands();
if (boundWands != null && boundWands.size() > 0) {
ConfigurationSection wandSection = saveFile.createSection("wands");
for (Map.Entry<String, ItemStack> wandEntry : boundWands.entrySet()) {
String key = wandEntry.getKey();
if (key == null || key.isEmpty())
continue;
controller.serialize(wandSection, key, wandEntry.getValue());
}
}
Map<Integer, ItemStack> respawnArmor = mage.getRespawnArmor();
if (respawnArmor != null) {
ConfigurationSection armorSection = saveFile.createSection("respawn_armor");
for (Map.Entry<Integer, ItemStack> entry : respawnArmor.entrySet()) {
controller.serialize(armorSection, Integer.toString(entry.getKey()), entry.getValue());
}
}
Map<Integer, ItemStack> respawnInventory = mage.getRespawnInventory();
if (respawnInventory != null) {
ConfigurationSection inventorySection = saveFile.createSection("respawn_inventory");
for (Map.Entry<Integer, ItemStack> entry : respawnInventory.entrySet()) {
controller.serialize(inventorySection, Integer.toString(entry.getKey()), entry.getValue());
}
}
List<ItemStack> storedInventory = mage.getStoredInventory();
if (storedInventory != null) {
saveFile.set("inventory", storedInventory);
}
saveFile.set("experience", mage.getStoredExperience());
saveFile.set("level", mage.getStoredLevel());
saveFile.set("open_wand", mage.isOpenWand());
saveFile.set("gave_welcome_wand", mage.getGaveWelcomeWand());
ConfigurationSection extraData = mage.getExtraData();
if (extraData != null) {
ConfigurationSection dataSection = saveFile.createSection("data");
ConfigurationUtils.addConfigurations(dataSection, extraData);
}
ConfigurationSection properties = mage.getProperties();
if (properties != null) {
ConfigurationSection propertiesSection = saveFile.createSection("properties");
ConfigurationUtils.addConfigurations(propertiesSection, properties);
}
Map<String, ConfigurationSection> classProperties = mage.getClassProperties();
if (classProperties != null) {
ConfigurationSection classesSection = saveFile.createSection("classes");
for (Map.Entry<String, ConfigurationSection> entry : classProperties.entrySet()) {
ConfigurationSection classSection = classesSection.createSection(entry.getKey());
ConfigurationUtils.addConfigurations(classSection, entry.getValue());
}
}
saveFile.set("active_class", mage.getActiveClass());
}
use of org.bukkit.configuration.MemoryConfiguration in project MagicPlugin by elBukkit.
the class WandLevel method randomizeWand.
public boolean randomizeWand(Mage mage, Wand wand, boolean additive, boolean hasUpgrade, boolean addSpells) {
// Add random spells to the wand
Mage activeMage = wand.getActiveMage();
if (mage == null) {
mage = activeMage;
}
wand.setActiveMage(mage);
boolean addedSpells = false;
Deque<WeightedPair<String>> remainingSpells = getRemainingSpells(wand);
if (addSpells) {
if (remainingSpells.size() > 0) {
Integer spellCount = RandomUtils.weightedRandom(spellCountProbability);
for (int i = 0; spellCount != null && i < spellCount; i++) {
String spellKey = RandomUtils.weightedRandom(remainingSpells);
boolean added = wand.addSpell(spellKey);
mage.sendDebugMessage("Trying to add spell: " + spellKey + " ? " + added);
if (added) {
addedSpells = true;
}
}
}
}
// Look through all spells for the max mana casting cost
// Also look for any material-using spells
boolean needsMaterials = false;
int maxManaCost = 0;
Set<String> spells = wand.getSpells();
for (String spellName : spells) {
SpellTemplate spell = wand.getController().getSpellTemplate(spellName);
if (spell != null) {
needsMaterials = needsMaterials || spell.usesBrush();
Collection<CastingCost> costs = spell.getCosts();
if (costs != null) {
for (CastingCost cost : costs) {
maxManaCost = Math.max(maxManaCost, cost.getMana());
}
}
}
}
// Add random materials
boolean addedMaterials = false;
Deque<WeightedPair<String>> remainingMaterials = getRemainingMaterials(wand);
if (needsMaterials && remainingMaterials.size() > 0) {
int currentMaterialCount = wand.getBrushes().size();
Integer materialCount = RandomUtils.weightedRandom(materialCountProbability);
// Make sure the wand has at least one material.
if (materialCount == null) {
materialCount = 0;
}
if (currentMaterialCount == 0) {
materialCount = Math.max(1, materialCount);
}
int retries = 100;
for (int i = 0; i < materialCount; i++) {
String materialKey = RandomUtils.weightedRandom(remainingMaterials);
materialKey = materialKey.replace("|", ":");
if (!wand.addBrush(materialKey)) {
// Try again up to a certain number if we picked one the wand already had.
if (retries-- > 0)
i--;
} else {
addedMaterials = true;
}
}
}
// Let them upgrade if they aren't getting any new spells or brushes
if (hasUpgrade && addSpells && !(addedMaterials && needsMaterials) && !addedSpells && ((getSpellCount() > 0 && spellProbability.size() > 0) || (getMaterialCount() > 0 && materialProbability.size() > 0))) {
if (mage != null && mage.getDebugLevel() > 0) {
mage.sendDebugMessage("Has upgrade: " + hasUpgrade);
mage.sendDebugMessage("Added spells: " + addedSpells + ", should: " + addSpells);
mage.sendDebugMessage("Spells per enchant: " + getSpellCount());
mage.sendDebugMessage("Spells in list: " + spellProbability.size());
mage.sendDebugMessage("Added brushes: " + addedMaterials + ", needed: " + needsMaterials);
}
wand.setActiveMage(activeMage);
return false;
}
// Add random wand properties
boolean addedProperties = false;
Integer propertyCount = propertyCountProbability.size() == 0 ? Integer.valueOf(0) : RandomUtils.weightedRandom(propertyCountProbability);
ConfigurationSection wandProperties = new MemoryConfiguration();
List<String> propertyKeys = new ArrayList<>(propertiesProbability.keySet());
List<String> propertiesAvailable = new ArrayList<>();
for (String propertyKey : propertyKeys) {
double currentValue = wand.getDouble(propertyKey);
double maxValue = path.getMaxProperty(propertyKey);
if (currentValue < maxValue) {
propertiesAvailable.add(propertyKey);
}
}
// Make sure we give them *something* if something is available
if (propertiesAvailable.size() > 0 && !addedMaterials && !addedSpells && propertyCount == 0) {
propertyCount = 1;
}
while (propertyCount != null && propertyCount-- > 0 && propertiesAvailable.size() > 0) {
int randomPropertyIndex = (int) (Math.random() * propertiesAvailable.size());
String randomProperty = propertiesAvailable.get(randomPropertyIndex);
Deque<WeightedPair<Float>> probabilities = propertiesProbability.get(randomProperty);
double current = wand.getDouble(randomProperty);
double maxValue = path.getMaxProperty(randomProperty);
if (probabilities.size() > 0 && current < maxValue) {
addedProperties = true;
current = Math.min(maxValue, current + RandomUtils.weightedRandom(probabilities));
wandProperties.set(randomProperty, current);
}
}
if (wand.isCostFree()) {
// Cost-Free wands don't need mana.
wandProperties.set("mana_regeneration", 0);
wandProperties.set("mana_max", 0);
wandProperties.set("mana", 0);
} else {
int manaRegeneration = wand.getManaRegeneration();
if (manaRegenerationProbability.size() > 0 && manaRegeneration < path.getMaxManaRegeneration()) {
addedProperties = true;
manaRegeneration = Math.min(path.getMaxManaRegeneration(), manaRegeneration + RandomUtils.weightedRandom(manaRegenerationProbability));
wandProperties.set("mana_regeneration", manaRegeneration);
}
int manaMax = wand.getManaMax();
if (manaMaxProbability.size() > 0 && manaMax < path.getMaxMaxMana()) {
manaMax = Math.min(path.getMaxMaxMana(), manaMax + RandomUtils.weightedRandom(manaMaxProbability));
if (path.getMatchSpellMana()) {
// Make sure the wand has at least enough mana to cast the highest costing spell it has.
manaMax = Math.max(maxManaCost, manaMax);
}
wandProperties.set("mana_max", manaMax);
addedProperties = true;
}
// Refill the wand's mana, why not
wandProperties.set("mana", manaMax);
}
// Add or set uses to the wand
if (additive) {
// Only add uses to a wand if it already has some.
int wandUses = wand.getRemainingUses();
if (wandUses > 0 && wandUses < path.getMaxUses() && addUseProbability.size() > 0) {
wandProperties.set("uses", Math.min(path.getMaxUses(), wandUses + RandomUtils.weightedRandom(addUseProbability)));
addedProperties = true;
}
} else if (useProbability.size() > 0) {
wandProperties.set("uses", Math.min(path.getMaxUses(), RandomUtils.weightedRandom(useProbability)));
}
// Set properties.
wand.upgrade(wandProperties);
wand.setActiveMage(activeMage);
return addedMaterials || addedSpells || addedProperties;
}
Aggregations