422 words
2 minutes
拟ID生成器 | Bukkit插件开发
究其原理,其实很简单,就是字典和数字的混合
package org.encinet.nick.until;
import java.io.File;import java.io.IOException;import java.nio.file.CopyOption;import java.nio.file.Files;import java.util.List;import java.util.Scanner;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.ThreadLocalRandom;import org.encinet.nick.Nick;
/** * ID生成器 */public class Generate { static List<String> worlds = new CopyOnWriteArrayList(); static int worldsLength;
/** * 初始化 词典加载 * 如果插件文件夹有直接获取插件文件夹中的词典 * 如果没有则从插件中提取放到插件文件夹 */ public Generate() { File dataFolder = Nick.plugin.getDataFolder(); File worldsFile = new File(dataFolder, "worlds.txt"); if (!worldsFile.exists()) { dataFolder.mkdir(); try { Files.copy(Nick.plugin.getResourceAsStream("worlds.txt"), dataFolder.toPath(), new CopyOption[0]); } catch (IOException e) { throw new RuntimeException(e); } } try { Scanner scanner = new Scanner(worldsFile); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (!line.startsWith("#") && !"".equals(line.trim())) { worlds.add(line); } } scanner.close(); if (worlds.size() == 0) { throw new RuntimeException(); } worldsLength = worlds.size(); } catch (IOException e2) { throw new RuntimeException(e2); } }
/** * @return 随机生成的ID */ public String getName() { StringBuilder sb = new StringBuilder(); int circulation = ThreadLocalRandom.current().nextInt(1, 4); for (int i = 0; i < circulation; i++) { if (i == 0) { sb.append(randomWord()); } else { sb.append(ThreadLocalRandom.current().nextBoolean() ? randomWord() : Integer.valueOf(randomNum())); } } return sb.toString(); }
/** * @return 随机从字典中获取一个词 */ public String randomWord() { return worlds.get(ThreadLocalRandom.current().nextInt(worldsLength)); }
/** * @return 随机从 `0~9` 选择一个数 */ public int randomNum() { return ThreadLocalRandom.current().nextInt(10); }}
# 一行一个# 建议纯英文# 建议首字母大写ForeverJavaFlyerScriptNightVisionCheatQwQAwASogaBakaCxkIDKThxPlzGloriousSoulmateShineWocBiliVapeWurstLoserHelperTimesFinishPretextDeleteCuteBabyTraumaSubmergeShineStarsReviewActiveSandmTendernessTearsAbsurdDirgeConquerorCokeFairyBritneyPioneerDeepseaRedemptionFalseHermesElaborateLiquorDeepHollowLeaveDaisyNaiveGodPoisonDeceptionPassMonstarOnlookerArchiveAholicDeceiveJamLittleAllureStarryERoslonMoonlightLonelyFreedomDesertedDreamWarmLuvBubbleKoiPrecipitationRunLolitaPearlPamperGutsHanabiBeginnerNefertariCurtainHushnowMidnightNaturePromiseVincentCleanlinessImeranceDestinLemonForeverMemorialAuthorityFerryEmotionalInsaneDespaisAuroraFigureMemorialMissCloudLntanoGentleDarkPureBombastiLiminousAshLcbergUnicornKrismileUnicornPerpetualMorningNoonMoonDinnerSunny
拟ID生成器 | Bukkit插件开发
https://fuwari.vercel.app/posts/id-gen-bukkit/