Skip to content

Instantly share code, notes, and snippets.

@QuiltMeow
Created December 21, 2021 21:20
Show Gist options
  • Select an option

  • Save QuiltMeow/8cdd15561ac5da92cbbd7a0aa149521a to your computer and use it in GitHub Desktop.

Select an option

Save QuiltMeow/8cdd15561ac5da92cbbd7a0aa149521a to your computer and use it in GitHub Desktop.
package ew.sr.x1c.quilt.meow.plugin;
import java.io.FileWriter;
import java.io.IOException;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class GetLoadChunkCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!sender.hasPermission("quilt.chunk.log")) {
sender.sendMessage(ChatColor.RED + "權限不足 無法使用該指令");
return false;
}
sender.sendMessage("準備輸出區塊紀錄 ...");
String fileName = "LoadChunk-" + System.currentTimeMillis() + ".log";
StringBuilder sb = new StringBuilder();
for (World world : Bukkit.getWorlds()) {
Chunk[] chunkList = world.getLoadedChunks();
sb.append("世界 : ").append(world.getName()).append(" 載入區塊 : ").append(chunkList.length).append(System.lineSeparator());
for (Chunk chunk : chunkList) {
sb.append("X : ").append(chunk.getX() << 4).append(" Z : ").append(chunk.getZ() << 4).append(" 活動實體數 : ").append(chunk.getEntities().length).append(" 固定實體數 : ").append(chunk.getTileEntities().length).append(System.lineSeparator());
}
sb.append(System.lineSeparator());
}
String output = sb.toString();
writeFileAsync(sender, fileName, output);
return true;
}
public static void writeFileAsync(CommandSender sender, String fileName, String data) {
new Thread(() -> {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(data);
sender.sendMessage(ChatColor.GREEN + "區塊紀錄輸出完成 檔案名稱 : " + fileName);
} catch (IOException ex) {
sender.sendMessage(ChatColor.RED + "輸出區塊紀錄時發生例外狀況 : " + ex.getMessage());
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment