--- title: seed tags: content, MCEA, technology --- # /seed:查詢世界種子碼 ### 原始碼 ```java public static void register(CommandDispatcher<CommandSource> p_241067_0_, boolean p_241067_1_) { p_241067_0_.register(Commands.literal("seed").requires((p_198673_1_) -> { return !p_241067_1_ || p_198673_1_.hasPermissionLevel(2); }).executes((p_198672_0_) -> { long i = p_198672_0_.getSource().getWorld().getSeed(); ITextComponent itextcomponent = TextComponentUtils.wrapWithSquareBrackets( (new StringTextComponent(String.valueOf(i))).modifyStyle((p_211752_2_) -> { return p_211752_2_ .setFormatting(TextFormatting.GREEN) .setClickEvent( new ClickEvent( ClickEvent.Action.COPY_TO_CLIPBOARD, String.valueOf(i) ) ) .setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new TranslationTextComponent("chat.copy.click") ) ) .setInsertion(String.valueOf(i)) ; // object returned }) ); p_198672_0_.getSource().sendFeedback(new TranslationTextComponent("commands.seed.success", itextcomponent), false); })); } ``` ### 對原始碼進行解析 ```java I = world's seed S(I) = make the value of I to string generate a text, its string is S(I), and is wrapped by square brackets, then setting: Formatting : color is GREEN Click Event : copy S(I) to clipboard Hover Event : show translation text : "chat.copy.click" Insetion : S(I) send feedback: "seed.success" and the text we generated. (!) be careful: every time called S(I) will run the function String.valueof(I) ``` ### 效能問題 1. 過多的使用`String.valueof(I)`:累積3次O(N) 2. 呼叫`wrapWithSquareBrackets`函式,會複製整個物件`ITextComponent`