mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2025-09-02 04:46:55 +00:00
44
src/main/java/cn/citycraft/Yum/Yum.java
Normal file
44
src/main/java/cn/citycraft/Yum/Yum.java
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cn.citycraft.Yum.utils.DownloadUtils;
|
||||
|
||||
/**
|
||||
* MC插件仓库
|
||||
*
|
||||
* @author 蒋天蓓
|
||||
* 2015年8月21日下午5:14:39
|
||||
*/
|
||||
public class Yum extends JavaPlugin {
|
||||
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
break;
|
||||
case 2:
|
||||
switch (args[0]) {
|
||||
case "install":
|
||||
if (DownloadUtils.download(String.format(url, args[1]), getDataFolder().getParent(), args[1])) {
|
||||
sender.sendMessage("OK");
|
||||
} else {
|
||||
sender.sendMessage("Error");
|
||||
}
|
||||
break;
|
||||
case "remove":
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
67
src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java
Normal file
67
src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
/**
|
||||
* @author 蒋天蓓
|
||||
* 2015年8月21日下午6:08:09
|
||||
* TODO
|
||||
*/
|
||||
public class DownloadUtils {
|
||||
public static boolean download(String url, String dir, String filename) {
|
||||
|
||||
BufferedInputStream in = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
URL fileUrl = new URL(url);
|
||||
System.out.println("下载地址: " + url);
|
||||
int fileLength = fileUrl.openConnection().getContentLength();
|
||||
System.out.println("文件长度: " + fileLength);
|
||||
in = new BufferedInputStream(fileUrl.openStream());
|
||||
File file = new File(dir, filename + ".jar");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
fout = new FileOutputStream(file);
|
||||
|
||||
byte[] data = new byte[1024];
|
||||
|
||||
// long downloaded = 0L;
|
||||
int count;
|
||||
while ((count = in.read(data)) != -1) {
|
||||
// downloaded += count;
|
||||
fout.write(data, 0, count);
|
||||
// int percent = (int) (downloaded / fileLength);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
Bukkit.getLogger().log(Level.WARNING, "The auto-updater tried to download a new update, but was unsuccessful.", ex);
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, null, ex);
|
||||
}
|
||||
try {
|
||||
if (fout != null) {
|
||||
fout.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
src/main/resources/plugin.yml
Normal file
8
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,8 @@
|
||||
name: Yum
|
||||
main: cn.citycraft.Yum.Yum
|
||||
website: http://ci.citycraft.cn:8800/jenkins/job/Yum/
|
||||
version: 1.0
|
||||
commands:
|
||||
yum:
|
||||
description: MC插件仓库
|
||||
usage: §6使用§a/yum help§6查看帮助!
|
Reference in New Issue
Block a user