mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-21 01:38:51 +00:00
feat: 添加几个工具类
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
c15a26b29b
commit
92e9fae869
5
pom.xml
5
pom.xml
@ -135,5 +135,10 @@
|
||||
<artifactId>ProtocolLib</artifactId>
|
||||
<version>4.2.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sk89q.worldedit</groupId>
|
||||
<artifactId>worldedit-bukkit</artifactId>
|
||||
<version>6.1.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -26,7 +26,7 @@ public abstract class AbstractInjectConfig {
|
||||
private static String INJECT_TYPE_ERROR = "配置节点 %s 数据类型不匹配 应该为: %s 但实际为: %s!";
|
||||
private static String INJECT_ERROR = "自动注入配置失败 可能造成插件运行错误 %s: %s!";
|
||||
private static String PATH_NOT_FOUND = "配置节点 %s 丢失 将使用默认值!";
|
||||
private ConfigurationSection config;
|
||||
protected ConfigurationSection config;
|
||||
|
||||
/**
|
||||
* 添加默认值
|
||||
@ -243,4 +243,8 @@ public abstract class AbstractInjectConfig {
|
||||
Log.d(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ConfigurationSection getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package pw.yumc.YumCore.config.inject;
|
||||
|
||||
import pw.yumc.YumCore.config.FileConfig;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import pw.yumc.YumCore.config.FileConfig;
|
||||
|
||||
/**
|
||||
* 配置自动载入类
|
||||
*
|
||||
@ -11,8 +11,6 @@ import java.io.File;
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public abstract class InjectConfig extends AbstractInjectConfig {
|
||||
protected FileConfig config;
|
||||
|
||||
public InjectConfig() {
|
||||
this(new FileConfig());
|
||||
}
|
||||
@ -22,7 +20,6 @@ public abstract class InjectConfig extends AbstractInjectConfig {
|
||||
}
|
||||
|
||||
public InjectConfig(FileConfig config) {
|
||||
this.config = config;
|
||||
inject(config);
|
||||
}
|
||||
|
||||
@ -36,14 +33,14 @@ public abstract class InjectConfig extends AbstractInjectConfig {
|
||||
* @return 配置文件
|
||||
*/
|
||||
public FileConfig getConfig() {
|
||||
return config;
|
||||
return (FileConfig) config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载配置文件
|
||||
*/
|
||||
public void reload() {
|
||||
config.reload();
|
||||
getConfig().reload();
|
||||
inject(config);
|
||||
}
|
||||
|
||||
@ -52,6 +49,6 @@ public abstract class InjectConfig extends AbstractInjectConfig {
|
||||
*/
|
||||
public void save() {
|
||||
save(config);
|
||||
config.save();
|
||||
getConfig().save();
|
||||
}
|
||||
}
|
||||
|
25
src/main/java/pw/yumc/YumCore/mc/MinecraftTools.java
Normal file
25
src/main/java/pw/yumc/YumCore/mc/MinecraftTools.java
Normal file
@ -0,0 +1,25 @@
|
||||
package pw.yumc.YumCore.mc;
|
||||
|
||||
/**
|
||||
* Minecraft工具类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2017/1/26 0026
|
||||
*/
|
||||
public class MinecraftTools {
|
||||
/**
|
||||
* 获得服务器信息
|
||||
*
|
||||
* @param address
|
||||
* 服务器地址
|
||||
* @return {@link ServerInfo} 服务器信息
|
||||
*/
|
||||
public static ServerInfo getServerInfo(String address) {
|
||||
if (address.contains(":")) {
|
||||
String[] args = address.split(":");
|
||||
return new ServerInfo(args[0], Integer.parseInt(args[1]));
|
||||
} else {
|
||||
return new ServerInfo(address);
|
||||
}
|
||||
}
|
||||
}
|
165
src/main/java/pw/yumc/YumCore/mc/ServerInfo.java
Normal file
165
src/main/java/pw/yumc/YumCore/mc/ServerInfo.java
Normal file
@ -0,0 +1,165 @@
|
||||
package pw.yumc.YumCore.mc;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Minecraft服务器数据获取类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2017/1/26 0026
|
||||
*/
|
||||
public class ServerInfo {
|
||||
private String address = "localhost";
|
||||
private int port = 25565;
|
||||
private int timeout = 1500;
|
||||
private int pingVersion = -1;
|
||||
private int protocolVersion = -1;
|
||||
private String gameVersion = "初始化中...";
|
||||
private String motd = "初始化中...";
|
||||
private int playersOnline = -1;
|
||||
private int maxPlayers = -1;
|
||||
|
||||
/**
|
||||
* Minecraft服务器数据获取类
|
||||
*
|
||||
* @param address
|
||||
* 服务器地址 默认端口25565
|
||||
*/
|
||||
public ServerInfo(String address) {
|
||||
this(address, 25565);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minecraft服务器数据获取类
|
||||
*
|
||||
* @param address
|
||||
* 服务器地址
|
||||
* @param port
|
||||
* 服务器端口
|
||||
*/
|
||||
public ServerInfo(String address, int port) {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器数据
|
||||
*
|
||||
* @return 是否获取成功
|
||||
*/
|
||||
public boolean fetchData() {
|
||||
try {
|
||||
Socket socket = new Socket();
|
||||
OutputStream outputStream;
|
||||
DataOutputStream dataOutputStream;
|
||||
InputStream inputStream;
|
||||
InputStreamReader inputStreamReader;
|
||||
|
||||
socket.setSoTimeout(this.timeout);
|
||||
|
||||
socket.connect(new InetSocketAddress(this.getAddress(), this.getPort()), this.getTimeout());
|
||||
|
||||
outputStream = socket.getOutputStream();
|
||||
dataOutputStream = new DataOutputStream(outputStream);
|
||||
|
||||
inputStream = socket.getInputStream();
|
||||
inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-16BE"));
|
||||
|
||||
dataOutputStream.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
|
||||
|
||||
int packetId = inputStream.read();
|
||||
|
||||
if (packetId == -1) { throw new IOException("Premature end of stream."); }
|
||||
|
||||
if (packetId != 0xFF) { throw new IOException("Invalid packet ID (" + packetId + ")."); }
|
||||
|
||||
int length = inputStreamReader.read();
|
||||
|
||||
if (length == -1) { throw new IOException("Premature end of stream."); }
|
||||
|
||||
if (length == 0) { throw new IOException("Invalid string length."); }
|
||||
|
||||
char[] chars = new char[length];
|
||||
|
||||
if (inputStreamReader.read(chars, 0, length) != length) { throw new IOException("Premature end of stream."); }
|
||||
|
||||
String string = new String(chars);
|
||||
|
||||
if (string.startsWith("§")) {
|
||||
String[] data = string.split("\0");
|
||||
this.pingVersion = Integer.parseInt(data[0].substring(1));
|
||||
this.protocolVersion = Integer.parseInt(data[1]);
|
||||
this.gameVersion = data[2];
|
||||
this.motd = data[3];
|
||||
this.playersOnline = Integer.parseInt(data[4]);
|
||||
this.maxPlayers = Integer.parseInt(data[5]);
|
||||
} else {
|
||||
String[] data = string.split("§");
|
||||
this.motd = data[0];
|
||||
this.playersOnline = Integer.parseInt(data[1]);
|
||||
this.maxPlayers = Integer.parseInt(data[2]);
|
||||
}
|
||||
|
||||
dataOutputStream.close();
|
||||
outputStream.close();
|
||||
|
||||
inputStreamReader.close();
|
||||
inputStream.close();
|
||||
|
||||
socket.close();
|
||||
} catch (IOException exception) {
|
||||
gameVersion = "获取失败!";
|
||||
motd = "获取失败!";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
public int getPingVersion() {
|
||||
return this.pingVersion;
|
||||
}
|
||||
|
||||
public int getProtocolVersion() {
|
||||
return this.protocolVersion;
|
||||
}
|
||||
|
||||
public String getGameVersion() {
|
||||
return this.gameVersion;
|
||||
}
|
||||
|
||||
public String getMotd() {
|
||||
return this.motd;
|
||||
}
|
||||
|
||||
public int getPlayersOnline() {
|
||||
return this.playersOnline;
|
||||
}
|
||||
|
||||
public int getMaxPlayers() {
|
||||
return this.maxPlayers;
|
||||
}
|
||||
|
||||
}
|
133
src/main/java/pw/yumc/YumCore/text/Scroller.java
Normal file
133
src/main/java/pw/yumc/YumCore/text/Scroller.java
Normal file
@ -0,0 +1,133 @@
|
||||
package pw.yumc.YumCore.text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* 字符串滚动工具
|
||||
*
|
||||
* @since 2016年6月7日 下午2:22:12
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class Scroller {
|
||||
private static final char COLOUR_CHAR = '\u00A7';
|
||||
private String content;
|
||||
private int position;
|
||||
private final List<String> list = new ArrayList<>();
|
||||
private ChatColor colour;
|
||||
|
||||
/**
|
||||
* @param text
|
||||
* 需要显示的字符串
|
||||
*/
|
||||
public Scroller(final String text) {
|
||||
this(text, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text
|
||||
* 需要显示的字符串
|
||||
* @param size
|
||||
* 每次显示长度
|
||||
*/
|
||||
public Scroller(final String text, final int size) {
|
||||
this(text, size, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text
|
||||
* 需要显示的字符串
|
||||
* @param size
|
||||
* 每次显示长度
|
||||
* @param skip
|
||||
* 每次滚动长度
|
||||
*/
|
||||
public Scroller(final String text, final int size, final int skip) {
|
||||
this(text, size, skip, '&');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text
|
||||
* 需要显示的字符串
|
||||
* @param size
|
||||
* 每次显示长度
|
||||
* @param skip
|
||||
* 每次滚动长度
|
||||
* @param colorChar
|
||||
* 颜色字符串
|
||||
*/
|
||||
public Scroller(String text, int size, int skip, final char colorChar) {
|
||||
this.colour = ChatColor.RESET;
|
||||
StringBuilder result;
|
||||
if (text.length() < size) {
|
||||
result = new StringBuilder(text);
|
||||
while (result.length() < size) {
|
||||
result.append(" ");
|
||||
}
|
||||
text = result.toString();
|
||||
}
|
||||
size -= 2;
|
||||
if (size < 1) {
|
||||
size = 1;
|
||||
}
|
||||
if (skip < 0) {
|
||||
skip = 0;
|
||||
}
|
||||
if (colorChar != COLOUR_CHAR) {
|
||||
text = ChatColor.translateAlternateColorCodes(colorChar, text);
|
||||
}
|
||||
for (int i = 0; i < text.length() - size; ++i) {
|
||||
this.list.add(text.substring(i, i + size));
|
||||
}
|
||||
result = new StringBuilder();
|
||||
for (int i = 0; i < skip; ++i) {
|
||||
System.out.println(i);
|
||||
list.add(text.substring(text.length() - size + (i > size ? size : i), text.length()) + result);
|
||||
if (result.length() < size) {
|
||||
result.append(" ");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < size - skip; ++i) {
|
||||
System.out.println(i);
|
||||
list.add(text.substring(text.length() - size + skip + i, text.length()) + result + text.substring(0, i));
|
||||
}
|
||||
for (int i = 0; i < skip && i <= result.length(); ++i) {
|
||||
System.out.println(i);
|
||||
list.add(result.substring(0, result.length() - i) + text.substring(0, size - (skip > size ? size : skip) + i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得下一条显示的字符串
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public String next() {
|
||||
StringBuilder result = this.getNext();
|
||||
if (result.charAt(result.length() - 1) == COLOUR_CHAR) {
|
||||
result.setCharAt(result.length() - 1, ' ');
|
||||
}
|
||||
if (result.charAt(0) == COLOUR_CHAR) {
|
||||
final ChatColor var2 = ChatColor.getByChar(result.charAt(1));
|
||||
if (var2 != null) {
|
||||
colour = var2;
|
||||
result = getNext();
|
||||
if (result.charAt(0) != 32) {
|
||||
result.setCharAt(0, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
return colour + result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从List获得下一条字符串
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
private StringBuilder getNext() {
|
||||
return new StringBuilder(this.list.get(this.position++ % this.list.size()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user