-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.projectkorra.spirits.command; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.CommandSender; | ||
|
||
import com.projectkorra.spirits.ProjectKorraSpirits; | ||
|
||
public class HelpCommand extends PKSCommand { | ||
|
||
public HelpCommand() { | ||
super("help", "/b spirits help", "Help command for Spirits", new String[] { "help", "h" }); | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, List<String> args) { | ||
if (!hasPermission(sender) || !correctLength(sender, args.size(), 0, 1)) { | ||
return; | ||
} else if (args.size() == 0) { | ||
List<String> strings = new ArrayList<>(); | ||
for (PKSCommand command : ProjectKorraSpirits.plugin.getCommandManager().getInstances().values()) { | ||
if (!command.getName().equalsIgnoreCase("help") && sender.hasPermission("spirits.command." + command.getName())) { | ||
strings.add(command.getProperUse()); | ||
} | ||
} | ||
Collections.sort(strings); | ||
Collections.reverse(strings); | ||
strings.add(ProjectKorraSpirits.plugin.getCommandManager().getInstances().get("help").getProperUse()); | ||
Collections.reverse(strings); | ||
for (String message : getPage(strings, ChatColor.translateAlternateColorCodes('&', "&9Commands: &3<Required> [Optional]"), 1, false)) { | ||
sender.sendMessage(message); | ||
} | ||
return; | ||
} | ||
|
||
String arg = args.get(0).toLowerCase(); | ||
if (isNumeric(arg)) { | ||
List<String> strings = new ArrayList<>(); | ||
for (PKSCommand command : ProjectKorraSpirits.plugin.getCommandManager().getInstances().values()) { | ||
strings.add(command.getProperUse()); | ||
} | ||
for (String message : getPage(strings, ChatColor.translateAlternateColorCodes('&', "&9Commands: &3<Required> [Optional]"), Integer.valueOf(arg), true)) { | ||
sender.sendMessage(message); | ||
} | ||
} else if (ProjectKorraSpirits.plugin.getCommandManager().getInstances().keySet().contains(arg.toLowerCase())) { | ||
ProjectKorraSpirits.plugin.getCommandManager().getInstances().get(arg).help(sender, true); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package com.projectkorra.spirits.command; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.entity.Player; | ||
|
||
import com.projectkorra.projectkorra.GeneralMethods; | ||
import com.projectkorra.projectkorra.command.SubCommand; | ||
import com.projectkorra.spirits.ProjectKorraSpirits; | ||
|
||
public abstract class PKSCommand implements SubCommand { | ||
|
||
private final String name; | ||
private final String properUse; | ||
private final String description; | ||
private final String[] aliases; | ||
|
||
protected String insufficientPermission; | ||
protected String insufficientArgs; | ||
protected String invalidArgs; | ||
protected String mustBePlayer; | ||
|
||
public PKSCommand(String name, String properUse, String description, String[] aliases) { | ||
this.name = name; | ||
this.properUse = properUse; | ||
this.description = description; | ||
this.aliases = aliases; | ||
|
||
FileConfiguration lang = ProjectKorraSpirits.plugin.getConfigManager().getLanguageConfig().get(); | ||
this.insufficientPermission = ChatColor.translateAlternateColorCodes('&', lang.getString("Commands.InsufficientPermission")); | ||
this.insufficientArgs = ChatColor.translateAlternateColorCodes('&', lang.getString("Commands.InsufficientArgs")); | ||
this.invalidArgs = ChatColor.translateAlternateColorCodes('&', lang.getString("Commands.InvalidArgs")); | ||
this.mustBePlayer = ChatColor.translateAlternateColorCodes('&', lang.getString("Commands.MustBePlayer")); | ||
|
||
ProjectKorraSpirits.plugin.getCommandManager().register(this); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String getProperUse() { | ||
return properUse; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public String[] getAliases() { | ||
return aliases; | ||
} | ||
|
||
public void help(CommandSender sender, boolean description) { | ||
GeneralMethods.sendBrandingMessage(sender, ChatColor.BLUE + "Usage: " + ChatColor.DARK_AQUA + properUse); | ||
if (description) { | ||
sender.sendMessage(ChatColor.AQUA + this.description); | ||
} | ||
} | ||
|
||
protected boolean hasPermission(CommandSender sender) { | ||
if (sender.hasPermission("spirits.command." + name)) { | ||
return true; | ||
} else { | ||
GeneralMethods.sendBrandingMessage(sender, insufficientPermission); | ||
return false; | ||
} | ||
} | ||
|
||
protected boolean hasPermission(CommandSender sender, String extra) { | ||
if (sender.hasPermission("spirits.command." + name + "." + extra)) { | ||
return true; | ||
} else { | ||
GeneralMethods.sendBrandingMessage(sender, insufficientPermission); | ||
return false; | ||
} | ||
} | ||
|
||
protected boolean correctLength(CommandSender sender, int size, int min, int max) { | ||
if (size < min || size > max) { | ||
help(sender, false); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
protected boolean isPlayer(CommandSender sender) { | ||
if (sender instanceof Player) { | ||
return true; | ||
} else { | ||
GeneralMethods.sendBrandingMessage(sender, mustBePlayer); | ||
return false; | ||
} | ||
} | ||
|
||
protected boolean isNumeric(String id) { | ||
try { | ||
Integer.parseInt(id); | ||
return true; | ||
} | ||
catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
|
||
protected List<String> getPage(List<String> entries, String title, int page, boolean sort) { | ||
List<String> strings = new ArrayList<String>(); | ||
if (sort) { | ||
Collections.sort(entries); | ||
} | ||
|
||
if (page < 1) { | ||
page = 1; | ||
} | ||
if ((page * 8) - 8 >= entries.size()) { | ||
page = Math.round(entries.size() / 8) + 1; | ||
if (page < 1) { | ||
page = 1; | ||
} | ||
} | ||
strings.add(ChatColor.translateAlternateColorCodes('&', "&9ProjectKorra &3&lSpirits &8- [&7" + page + "&8/&7" + (int) Math.ceil((entries.size() + .0) / (8 + .0)) + "&8]")); | ||
strings.add(title); | ||
if (entries.size() > ((page * 8) - 8)) { | ||
for (int i = ((page * 8) - 8); i < entries.size(); i++) { | ||
if (entries.get(i) != null) { | ||
strings.add(ChatColor.DARK_AQUA + entries.get(i).toString()); | ||
} | ||
if (i >= (page * 8) - 1) { | ||
break; | ||
} | ||
} | ||
} | ||
return strings; | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
src/com/projectkorra/spirits/command/SpiritWorldCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.projectkorra.spirits.command; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.List; | ||
|
||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import com.projectkorra.spirits.ProjectKorraSpirits; | ||
|
||
public class SpiritWorldCommand extends PKSCommand { | ||
|
||
public SpiritWorldCommand() { | ||
super("spiritworld", "/bending spirits spiritworld <teleport/download>", "Base command for SpiritWorld", new String[] { "spiritworld", "sw" }); | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, List<String> args) { | ||
if (!hasPermission(sender) || !correctLength(sender, args.size(), 1, 1)) { | ||
return; | ||
} | ||
|
||
if (args.get(0).equalsIgnoreCase("teleport")) { | ||
if (!isPlayer(sender)) { | ||
return; | ||
} | ||
((Player) sender).teleport(ProjectKorraSpirits.plugin.getMethods().getSpiritWorld().getSpawnLocation()); | ||
// TODO: Message player of teleportation | ||
} else if (args.get(0).equalsIgnoreCase("download")) { | ||
navigate("http://www.mediafire.com/download/aqtmhwvb8yvqclu/SmartSharePC.jar"); | ||
} | ||
// /bending spirits spiritworld teleport | ||
} | ||
|
||
private void navigate(String url) { | ||
String downloadLink; | ||
try { | ||
downloadLink = fetchDownloadLink(getUrlSource(url)); | ||
saveUrl(downloadLink); | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void saveUrl(final String urlString) throws MalformedURLException, IOException { | ||
System.out.println("Downloading..."); | ||
String filename = urlString.substring(urlString.lastIndexOf("/") + 1, urlString.lastIndexOf(".")) + urlString.substring(urlString.lastIndexOf("."), urlString.length()); | ||
BufferedInputStream in = null; | ||
FileOutputStream fout = null; | ||
try { | ||
in = new BufferedInputStream(new URL(urlString).openStream()); | ||
fout = new FileOutputStream(filename); | ||
|
||
final byte data[] = new byte[1024]; | ||
int count; | ||
while ((count = in.read(data, 0, 1024)) != -1) { | ||
fout.write(data, 0, count); | ||
} | ||
} | ||
finally { | ||
if (in != null) { | ||
in.close(); | ||
} | ||
if (fout != null) { | ||
fout.close(); | ||
} | ||
} | ||
System.out.println("Success!"); | ||
} | ||
|
||
private String getUrlSource(String urlString) throws UnsupportedEncodingException, IOException { | ||
System.out.println("Connecting..."); | ||
URL url = new URL(urlString); | ||
URLConnection urlConnection = url.openConnection(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); | ||
String inputLine; | ||
String total = ""; | ||
while ((inputLine = in.readLine()) != null) { | ||
total += inputLine; | ||
} | ||
in.close(); | ||
return total; | ||
} | ||
|
||
private String fetchDownloadLink(String string) { | ||
System.out.println("Fetching download link"); | ||
String regex = "(?=\\<)|(?<=\\>)"; | ||
String[] data = string.split(regex); | ||
String found = "NOTFOUND"; | ||
for (String dat : data) { | ||
if (dat.contains("DLP_mOnDownload(this)")) { | ||
found = dat; | ||
break; | ||
} | ||
} | ||
String wentthru = found.substring(found.indexOf("href=\"") + 6); | ||
wentthru = wentthru.substring(0, wentthru.indexOf("\"")); | ||
return wentthru; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.projectkorra.spirits.command; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.CommandSender; | ||
|
||
import com.projectkorra.projectkorra.command.PKCommand; | ||
import com.projectkorra.spirits.ProjectKorraSpirits; | ||
|
||
public class SpiritsCommand extends PKCommand { | ||
|
||
private List<String> generalHelp; | ||
|
||
public SpiritsCommand() { | ||
super("spirits", "/bending spirits", "Main command for spirits", new String[] { "spirits", "s" }); | ||
|
||
this.generalHelp = ProjectKorraSpirits.plugin.getConfigManager().getLanguageConfig().get().getStringList("Commands.GeneralHelp"); | ||
} | ||
|
||
@Override | ||
public void execute(CommandSender sender, List<String> args) { | ||
// User has typed '/b spirits' | ||
if (args.size() == 0) { | ||
for (String line : generalHelp) { | ||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', line)); | ||
} | ||
return; | ||
} | ||
|
||
List<String> sendingArgs = args.subList(1, args.size()); | ||
for (PKSCommand cmd : ProjectKorraSpirits.plugin.getCommandManager().getInstances().values()) { | ||
if (Arrays.asList(cmd.getAliases()).contains(args.get(0).toLowerCase())) { | ||
cmd.execute(sender, sendingArgs); | ||
return; | ||
} | ||
} | ||
|
||
for (String line : generalHelp) { | ||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', line)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
} |
Oops, something went wrong.