use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class RecallAction method getWarp.
@Nullable
protected Waypoint getWarp(String warpKey) {
if (warps == null)
return getUnknownWarp(warpKey);
ConfigurationSection config = warps.get(warpKey);
if (config == null)
return getUnknownWarp(warpKey);
MageController controller = context.getController();
String warpName = config.getString("name", warpKey);
String castMessage = context.getMessage("cast_warp").replace("$name", warpName);
String failMessage = context.getMessage("no_target_warp").replace("$name", warpName);
String title = context.getMessage("title_warp").replace("$name", warpName);
String description = config.getString("description");
String iconURL = config.getString("icon_url");
MaterialAndData icon = getIcon(context, config, "icon");
Location warpLocation = controller.getWarp(warpKey);
if (warpLocation == null || warpLocation.getWorld() == null) {
String serverName = config.getString("server", null);
if (serverName != null) {
return new Waypoint(RecallType.WARP, warpKey, serverName, title, castMessage, failMessage, description, icon, iconURL);
}
return null;
}
return new Waypoint(RecallType.WARP, warpLocation, title, castMessage, failMessage, description, icon, iconURL);
}
use of com.elmakers.mine.bukkit.api.magic.MageController 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 com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class DebuggerAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity entity = context.getTargetEntity();
MageController controller = context.getController();
Mage mage = null;
if (entity != null && controller.isMage(entity)) {
mage = controller.getMage(entity);
} else {
Block block = context.getTargetBlock();
if (block.getType() == Material.COMMAND) {
CommandBlock commandBlock = (CommandBlock) block.getState();
// This is a bit of hacky ..
String commandName = commandBlock.getName();
String mageId = "COMMAND";
if (commandName != null && commandName.length() > 0) {
mageId = "COMMAND-" + commandBlock.getName();
}
mage = controller.getRegisteredMage(mageId);
}
}
if (mage == null) {
return SpellResult.NO_TARGET;
}
int currentLevel = mage.getDebugLevel();
if (currentLevel == debugLevel || debugLevel == 0) {
mage.setDebugLevel(0);
mage.setDebugger(null);
return SpellResult.DEACTIVATE;
}
mage.setDebugLevel(debugLevel);
mage.setDebugger(context.getMage().getCommandSender());
if (check) {
mage.debugPermissions(context.getMage().getCommandSender(), null);
}
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class DisablePhysicsAction method perform.
@Override
public SpellResult perform(CastContext context) {
MageController controller = context.getController();
controller.disablePhysics(duration);
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class DisarmAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity target = context.getTargetEntity();
if (target == null || !(target instanceof LivingEntity)) {
return SpellResult.NO_TARGET;
}
LivingEntity entity = (LivingEntity) target;
EntityEquipment equipment = entity.getEquipment();
ItemStack stack = null;
Integer originalSlot = null;
boolean isMainHand = false;
if (displayName == null) {
stack = equipment.getItemInMainHand();
isMainHand = true;
} else {
// This is not compatible
keepInInventory = false;
// Must be a player in this case
if (!(entity instanceof Player)) {
return SpellResult.PLAYER_REQUIRED;
}
PlayerInventory playerInventory = ((Player) entity).getInventory();
for (originalSlot = 0; originalSlot < playerInventory.getSize(); originalSlot++) {
ItemStack item = playerInventory.getItem(originalSlot);
if (InventoryUtils.isEmpty(item))
continue;
ItemMeta meta = item.getItemMeta();
if (meta == null || !meta.hasDisplayName())
continue;
if (meta.getDisplayName().equals(displayName)) {
stack = item;
isMainHand = originalSlot == playerInventory.getHeldItemSlot();
break;
}
}
}
if (InventoryUtils.isEmpty(stack)) {
return SpellResult.NO_TARGET;
}
// Special case for wands
MageController controller = context.getController();
Mage targetMage = controller.getMage(entity);
if (com.elmakers.mine.bukkit.wand.Wand.isWand(stack) && controller.isMage(entity)) {
Mage mage = context.getMage();
// This gets overridden by superpower...
if (!mage.isSuperPowered() && targetMage.isSuperProtected()) {
return SpellResult.NO_TARGET;
}
Wand activeWand = targetMage.getActiveWand();
if (activeWand != null && isMainHand) {
targetMage.getActiveWand().deactivate();
stack = equipment.getItemInMainHand();
}
}
Integer targetSlot = null;
PlayerInventory targetInventory = null;
ItemStack swapItem = null;
Random random = context.getRandom();
if (entity instanceof Player && keepInInventory) {
Player targetPlayer = (Player) entity;
targetInventory = targetPlayer.getInventory();
originalSlot = targetInventory.getHeldItemSlot();
List<Integer> validSlots = new ArrayList<>();
ItemStack[] contents = targetInventory.getContents();
for (int i = minSlot; i <= maxSlot; i++) {
if (contents[i] == null || contents[i].getType() == Material.AIR) {
validSlots.add(i);
}
}
// Randomly choose a slot if no empty one was found
if (validSlots.size() == 0) {
int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
swapItem = targetInventory.getItem(swapSlot);
validSlots.add(swapSlot);
}
int chosen = random.nextInt(validSlots.size());
targetSlot = validSlots.get(chosen);
}
if (displayName != null) {
((Player) entity).getInventory().setItem(originalSlot, null);
} else {
equipment.setItemInMainHand(swapItem);
}
if (targetSlot != null && targetInventory != null) {
targetInventory.setItem(targetSlot, stack);
if (originalSlot != null) {
DisarmUndoAction disarmUndo = new DisarmUndoAction(targetMage, originalSlot, targetSlot);
context.registerForUndo(disarmUndo);
}
} else {
Location location = entity.getLocation();
location.setY(location.getY() + 1);
Item item = entity.getWorld().dropItemNaturally(location, stack);
Vector velocity = item.getVelocity();
velocity.setY(velocity.getY() * 5);
SafetyUtils.setVelocity(item, velocity);
}
return SpellResult.CAST;
}
Aggregations