use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class RecallAction method perform.
@Override
public SpellResult perform(CastContext context) {
this.context = context;
enabledTypes.clear();
warps.clear();
commands.clear();
Mage mage = context.getMage();
MageController controller = context.getController();
Player player = mage.getPlayer();
if (player == null) {
return SpellResult.PLAYER_REQUIRED;
}
Set<String> unlockedWarps = new HashSet<>();
ConfigurationSection mageData = mage.getData();
String unlockedString = mageData.getString(unlockKey);
if (unlockedString != null && !unlockedString.isEmpty()) {
unlockedWarps.addAll(Arrays.asList(StringUtils.split(unlockedString, ',')));
}
Set<String> friends = new HashSet<>();
String friendString = mageData.getString(friendKey);
if (friendString != null && !friendString.isEmpty()) {
friends.addAll(Arrays.asList(StringUtils.split(friendString, ',')));
}
ConfigurationSection warpConfig = null;
if (parameters.contains("warps")) {
warpConfig = ConfigurationUtils.getConfigurationSection(parameters, "warps");
}
ConfigurationSection commandConfig = null;
if (parameters.contains("commands")) {
commandConfig = ConfigurationUtils.getConfigurationSection(parameters, "commands");
}
if (parameters.contains("unlock")) {
String unlockWarp = parameters.getString("unlock");
if (unlockWarp == null || unlockWarp.isEmpty() || unlockedWarps.contains(unlockWarp)) {
return SpellResult.NO_ACTION;
}
if (warpConfig == null && commandConfig == null) {
return SpellResult.FAIL;
}
unlockedWarps.add(unlockWarp);
unlockedString = StringUtils.join(unlockedWarps, ",");
mageData.set(unlockKey, unlockedString);
String warpName = unlockWarp;
ConfigurationSection config = warpConfig == null ? null : warpConfig.getConfigurationSection(unlockWarp);
if (config != null) {
warpName = config.getString("name", warpName);
} else {
config = commandConfig == null ? null : commandConfig.getConfigurationSection(unlockWarp);
if (config != null) {
warpName = config.getString("name", warpName);
}
}
String unlockMessage = context.getMessage("unlock_warp").replace("$name", warpName);
context.sendMessage(unlockMessage);
return SpellResult.CAST;
}
if (parameters.contains("lock")) {
String lockWarpString = parameters.getString("lock");
String[] lockWarps = StringUtils.split(lockWarpString, ',');
boolean locked = false;
for (String lockWarp : lockWarps) {
if (unlockedWarps.contains(lockWarp)) {
locked = true;
unlockedWarps.remove(lockWarp);
}
}
if (locked) {
unlockedString = StringUtils.join(unlockedWarps, ",");
mageData.set(unlockKey, unlockedString);
}
return locked ? SpellResult.DEACTIVATE : SpellResult.NO_ACTION;
}
if (parameters.contains("addfriend")) {
String friendName = parameters.getString("addfriend");
if (friendName == null || friendName.isEmpty()) {
return SpellResult.NO_ACTION;
}
Player online = DeprecatedUtils.getPlayer(friendName);
if (online == null) {
return SpellResult.FAIL;
}
String uuid = online.getUniqueId().toString();
if (friends.contains(uuid)) {
return SpellResult.NO_ACTION;
}
friends.add(uuid);
friendString = StringUtils.join(friends, ",");
mageData.set(friendKey, friendString);
String message = context.getMessage("add_friend").replace("$name", online.getDisplayName());
context.sendMessage(message);
return SpellResult.CAST;
}
if (parameters.contains("removefriend")) {
String friendName = parameters.getString("removefriend");
Player online = DeprecatedUtils.getPlayer(friendName);
if (online == null) {
return SpellResult.FAIL;
}
String uuid = online.getUniqueId().toString();
if (!friends.contains(uuid)) {
return SpellResult.NO_ACTION;
}
friends.remove(uuid);
friendString = StringUtils.join(friends, ",");
mageData.set(friendKey, friendString);
String message = context.getMessage("remove_friend").replace("$name", online.getDisplayName());
context.sendMessage(message);
return SpellResult.DEACTIVATE;
}
Location playerLocation = mage.getLocation();
for (RecallType testType : RecallType.values()) {
// Special-case for warps
if (testType == RecallType.WARP) {
if (warpConfig != null) {
Collection<String> warpKeys = warpConfig.getKeys(false);
for (String warpKey : warpKeys) {
ConfigurationSection config = warpConfig.getConfigurationSection(warpKey);
boolean isLocked = config.getBoolean("locked", false);
if (!isLocked || unlockedWarps.contains(warpKey)) {
warps.put(warpKey, config);
}
}
}
} else // Special-case for commands
if (testType == RecallType.COMMAND) {
if (commandConfig != null) {
Collection<String> commandKeys = commandConfig.getKeys(false);
for (String commandKey : commandKeys) {
ConfigurationSection config = commandConfig.getConfigurationSection(commandKey);
boolean isLocked = config.getBoolean("locked", false);
if (!isLocked || unlockedWarps.contains(commandKey)) {
commands.put(commandKey, config);
}
}
}
} else {
if (parameters.getBoolean("allow_" + testType.name().toLowerCase(), true)) {
enabledTypes.add(testType);
}
}
}
if (warps.size() > 0) {
enabledTypes.add(RecallType.WARP);
}
if (commands.size() > 0) {
enabledTypes.add(RecallType.COMMAND);
}
if (parameters.contains("warp")) {
String warpName = parameters.getString("warp");
Waypoint waypoint = getWarp(warpName);
if (tryTeleport(player, waypoint)) {
return SpellResult.CAST;
}
return SpellResult.FAIL;
} else if (parameters.contains("command")) {
String commandName = parameters.getString("command");
Waypoint waypoint = getCommand(context, commandName);
if (tryTeleport(player, waypoint)) {
return SpellResult.CAST;
}
return SpellResult.FAIL;
} else if (parameters.contains("type")) {
String typeString = parameters.getString("type", "");
if (parameters.getBoolean("allow_marker", true)) {
if (typeString.equalsIgnoreCase("remove")) {
if (removeMarker()) {
return SpellResult.TARGET_SELECTED;
}
return SpellResult.FAIL;
}
if (typeString.equalsIgnoreCase("place")) {
Block block = context.getLocation().getBlock();
if (parameters.getBoolean("marker_requires_build", true) && !context.hasBuildPermission(block)) {
return SpellResult.NO_TARGET;
}
if (hasMarker() && parameters.getBoolean("confirm_marker", true)) {
showMarkerConfirm(context);
return SpellResult.CAST;
}
if (placeMarker(block)) {
return SpellResult.TARGET_SELECTED;
}
return SpellResult.FAIL;
}
}
RecallType recallType = RecallType.valueOf(typeString.toUpperCase());
Waypoint location = getWaypoint(player, recallType, 0, parameters, context);
if (tryTeleport(player, location)) {
return SpellResult.CAST;
}
return SpellResult.FAIL;
}
List<Waypoint> allWaypoints = new ArrayList<>();
for (RecallType selectedType : enabledTypes) {
if (selectedType == RecallType.FRIENDS) {
for (String friendId : friends) {
Waypoint targetLocation = getFriend(friendId);
if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
allWaypoints.add(targetLocation);
}
}
} else if (selectedType == RecallType.WARP) {
for (String warpKey : warps.keySet()) {
Waypoint targetLocation = getWarp(warpKey);
if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
allWaypoints.add(targetLocation);
}
}
} else if (selectedType == RecallType.COMMAND) {
for (String commandKey : commands.keySet()) {
Waypoint targetLocation = getCommand(context, commandKey);
if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
allWaypoints.add(targetLocation);
}
}
} else if (selectedType == RecallType.WAND) {
List<LostWand> lostWands = mage.getLostWands();
for (int i = 0; i < lostWands.size(); i++) {
Waypoint targetLocation = getWaypoint(player, selectedType, i, parameters, context);
if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
allWaypoints.add(targetLocation);
}
}
} else if (selectedType == RecallType.FIELDS) {
Map<String, Location> fields = controller.getHomeLocations(player);
if (fields != null) {
for (Map.Entry<String, Location> fieldEntry : fields.entrySet()) {
Location location = fieldEntry.getValue().clone();
location.setX(location.getX() + 0.5);
location.setZ(location.getZ() + 0.5);
allWaypoints.add(new Waypoint(RecallType.FIELDS, location, fieldEntry.getKey(), context.getMessage("cast_field"), context.getMessage("no_target_field"), context.getMessage("description_field", ""), getIcon(context, parameters, "icon_field"), true));
}
}
} else {
Waypoint targetLocation = getWaypoint(player, selectedType, 0, parameters, context);
if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
allWaypoints.add(targetLocation);
}
}
}
if (allWaypoints.size() == 0) {
return SpellResult.NO_TARGET;
}
options.clear();
Collections.sort(allWaypoints);
String inventoryTitle = context.getMessage("title", "Recall");
int invSize = (int) Math.ceil(allWaypoints.size() / 9.0f) * 9;
Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
int index = 0;
for (Waypoint waypoint : allWaypoints) {
ItemStack waypointItem;
if (waypoint.iconURL != null && !waypoint.iconURL.isEmpty()) {
waypointItem = InventoryUtils.getURLSkull(waypoint.iconURL);
} else {
waypointItem = waypoint.icon.getItemStack(1);
}
ItemMeta meta = waypointItem == null ? null : waypointItem.getItemMeta();
if (meta == null) {
waypointItem = new ItemStack(DefaultWaypointMaterial);
meta = waypointItem.getItemMeta();
controller.getLogger().warning("Invalid waypoint icon for " + waypoint.name);
}
meta.setDisplayName(waypoint.name);
if (waypoint.description != null && waypoint.description.length() > 0) {
List<String> lore = new ArrayList<>();
InventoryUtils.wrapText(waypoint.description, lore);
meta.setLore(lore);
}
waypointItem.setItemMeta(meta);
waypointItem = InventoryUtils.makeReal(waypointItem);
InventoryUtils.hideFlags(waypointItem, (byte) 63);
InventoryUtils.setMeta(waypointItem, "waypoint", "true");
CompatibilityUtils.makeUnbreakable(waypointItem);
displayInventory.setItem(index, waypointItem);
options.put(index, waypoint);
index++;
}
mage.activateGUI(this, displayInventory);
return SpellResult.CAST;
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class CustomProjectileAction method reset.
@Override
public void reset(CastContext context) {
super.reset(context);
targeting.reset();
long now = System.currentTimeMillis();
nextUpdate = 0;
distanceTravelled = 0;
lastUpdate = 0;
deadline = now + lifetime;
targetSelfDeadline = targetSelfTimeout > 0 ? now + targetSelfTimeout : 0;
effectLocation = null;
velocity = null;
activeProjectileEffects = null;
entityHitCount = 0;
blockHitCount = 0;
reflectCount = 0;
attachedDeadline = 0;
attachedOffset = null;
missed = false;
returnDistanceAway = null;
// This has to be done here, so that the plan is not shared across parallel instances
if (planConfiguration != null && !planConfiguration.isEmpty()) {
plan = new ArrayDeque<>();
for (ConfigurationSection planStepConfig : planConfiguration) {
plan.add(new PlanStep(planStepConfig));
}
} else {
plan = null;
}
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class ModifyPropertiesAction method prepare.
@Override
public void prepare(CastContext context, ConfigurationSection parameters) {
modifyTarget = parameters.getString("modify_target", "wand");
upgrade = parameters.getBoolean("upgrade", false);
modify = new ArrayList<>();
Object modifyObject = parameters.get("modify");
if (modifyObject instanceof ConfigurationSection) {
ConfigurationSection simple = (ConfigurationSection) modifyObject;
Set<String> keys = simple.getKeys(true);
for (String key : keys) {
ModifyProperty property = new ModifyProperty(key, simple.get(key));
modify.add(property);
}
} else {
Collection<ConfigurationSection> complex = ConfigurationUtils.getNodeList(parameters, "modify");
for (ConfigurationSection section : complex) {
ModifyProperty property = new ModifyProperty(section);
modify.add(property);
}
}
}
use of org.bukkit.configuration.ConfigurationSection 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.ConfigurationSection in project MagicPlugin by elBukkit.
the class ItemShopAction method initialize.
@Override
public void initialize(Spell spell, ConfigurationSection parameters) {
super.initialize(spell, parameters);
items.clear();
if (parameters.contains("items")) {
if (parameters.isConfigurationSection("items")) {
ConfigurationSection itemSection = ConfigurationUtils.getConfigurationSection(parameters, "items");
Collection<String> itemKeys = itemSection.getKeys(false);
for (String itemKey : itemKeys) {
items.add(createShopItem(spell.getController(), itemKey, itemSection.getDouble(itemKey, -1)));
}
} else {
List<?> itemList = parameters.getList("items");
for (Object itemEntry : itemList) {
if (itemEntry instanceof String) {
String itemKey = (String) itemEntry;
items.add(createShopItem(spell.getController(), itemKey, -1));
} else if (itemEntry instanceof ConfigurationSection || itemEntry instanceof Map) {
ConfigurationSection itemConfig = (itemEntry instanceof ConfigurationSection) ? (ConfigurationSection) itemEntry : ConfigUtils.toConfigurationSection((Map<?, ?>) itemEntry);
ShopItem shopItem = null;
if (itemConfig != null) {
double cost = itemConfig.getDouble("cost");
if (itemConfig.isString("item")) {
shopItem = createShopItem(spell.getController(), itemConfig);
if (shopItem != null) {
String name = itemConfig.getString("name");
List<String> lore = ConfigurationUtils.getStringList(itemConfig, "lore");
if (name != null || lore != null) {
ItemStack item = shopItem.getItem();
ItemMeta meta = item.getItemMeta();
if (name != null) {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
}
if (lore != null) {
List<String> translatedLore = new ArrayList<>();
for (String line : lore) {
if (line != null) {
translatedLore.add(ChatColor.translateAlternateColorCodes('&', line));
}
}
meta.setLore(translatedLore);
}
item.setItemMeta(meta);
}
}
} else {
ItemStack itemStack = itemConfig.getItemStack("item");
if (itemStack != null) {
shopItem = new ShopItem(itemStack, cost);
}
}
}
items.add(shopItem);
}
}
}
}
}
Aggregations