From b8003245f3aae8b962409d5ea94fc9e36dedc3bc Mon Sep 17 00:00:00 2001 From: alexbegt Date: Thu, 8 Apr 2021 22:13:52 -0400 Subject: [PATCH] Format the book code to be more like tinkers construct and add support for text components to be rendered in the book. --- .editorconfig | 256 +++++++++++++ .../mantle/client/book/BookHelper.java | 79 +++- .../mantle/client/book/BookLoader.java | 19 +- .../mantle/client/book/BookTransformer.java | 7 +- .../book/action/StringActionProcessor.java | 1 + .../mantle/client/book/data/BookData.java | 34 +- .../client/book/data/LinkedSectionData.java | 1 - .../mantle/client/book/data/PageData.java | 33 +- .../mantle/client/book/data/SectionData.java | 6 +- .../data/content/ContentBlockInteraction.java | 3 +- .../book/data/content/ContentCrafting.java | 19 +- .../book/data/content/ContentError.java | 2 +- .../book/data/content/ContentImage.java | 6 +- .../book/data/content/ContentImageText.java | 6 +- .../book/data/content/ContentSmelting.java | 66 ++-- .../book/data/content/ContentSmithing.java | 3 +- .../book/data/content/ContentStructure.java | 3 +- .../client/book/data/content/ContentText.java | 3 +- .../book/data/content/ContentTextImage.java | 9 +- .../data/content/ContentTextLeftImage.java | 6 +- .../data/content/ContentTextRightImage.java | 6 +- .../client/book/data/content/PageContent.java | 2 +- .../deserializer/HexStringDeserializer.java | 11 +- .../client/book/data/element/BlockData.java | 2 +- .../book/data/element/IDataElement.java | 3 +- .../client/book/data/element/ImageData.java | 2 +- .../book/data/element/ItemStackData.java | 16 +- .../book/data/element/TextComponentData.java | 30 ++ .../client/book/data/element/TextData.java | 1 + .../book/repository/BookRepository.java | 2 +- .../book/repository/DummyRepository.java | 2 +- .../book/repository/FileRepository.java | 23 +- .../mantle/client/screen/book/BookScreen.java | 57 +-- .../client/screen/book/BookmarkButton.java | 59 --- .../book/TextComponentDataRenderer.java | 354 ++++++++++++++++++ .../client/screen/book/TextDataRenderer.java | 14 +- .../screen/book/element/SelectionElement.java | 13 +- .../screen/book/element/StructureElement.java | 11 +- .../book/element/TextComponentElement.java | 68 ++++ .../network/packet/UpdateSavedPagePacket.java | 2 +- 40 files changed, 938 insertions(+), 302 deletions(-) create mode 100644 .editorconfig create mode 100644 src/main/java/slimeknights/mantle/client/book/data/element/TextComponentData.java delete mode 100644 src/main/java/slimeknights/mantle/client/screen/book/BookmarkButton.java create mode 100644 src/main/java/slimeknights/mantle/client/screen/book/TextComponentDataRenderer.java create mode 100644 src/main/java/slimeknights/mantle/client/screen/book/element/TextComponentElement.java diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1bc0cdf --- /dev/null +++ b/.editorconfig @@ -0,0 +1,256 @@ +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +insert_final_newline = true +max_line_length = 120 +# tab_width = 4 +# ij_continuation_indent_size = 4 + +[*.json] +indent_size = 2 + +[*.java] +indent_size = 2 +# tab_width = 8 +# ij_continuation_indent_size = 4 +# ij_java_align_consecutive_assignments = false +# ij_java_align_consecutive_variable_declarations = false +# ij_java_align_group_field_declarations = false +# ij_java_align_multiline_annotation_parameters = false +# ij_java_align_multiline_array_initializer_expression = true +# ij_java_align_multiline_assignment = true +# ij_java_align_multiline_binary_operation = true +# ij_java_align_multiline_chained_methods = true +# ij_java_align_multiline_extends_list = true +# ij_java_align_multiline_for = true +# ij_java_align_multiline_method_parentheses = false +# ij_java_align_multiline_parameters = true +# ij_java_align_multiline_parameters_in_calls = true +# ij_java_align_multiline_parenthesized_expression = true +# ij_java_align_multiline_resources = true +# ij_java_align_multiline_ternary_operation = true +# ij_java_align_multiline_throws_list = true +# ij_java_align_subsequent_simple_methods = false +# ij_java_align_throws_keyword = false +# ij_java_annotation_parameter_wrap = off +# ij_java_array_initializer_new_line_after_left_brace = false +# ij_java_array_initializer_right_brace_on_new_line = false +# ij_java_array_initializer_wrap = off +# ij_java_assert_statement_colon_on_next_line = false +# ij_java_assert_statement_wrap = off +# ij_java_assignment_wrap = off +# ij_java_binary_operation_sign_on_next_line = true +# ij_java_binary_operation_wrap = off +# ij_java_blank_lines_after_anonymous_class_header = 0 +ij_java_blank_lines_after_class_header = 1 +ij_java_blank_lines_after_imports = 1 +# ij_java_blank_lines_after_package = 1 +# ij_java_blank_lines_around_class = 1 +# ij_java_blank_lines_around_field = 0 +# ij_java_blank_lines_around_field_in_interface = 0 +# ij_java_blank_lines_around_initializer = 1 +# ij_java_blank_lines_around_method = 1 +# ij_java_blank_lines_around_method_in_interface = 1 +# ij_java_blank_lines_before_class_end = 0 +# ij_java_blank_lines_before_imports = 1 +# ij_java_blank_lines_before_method_body = 0 +# ij_java_blank_lines_before_package = 0 +# ij_java_block_brace_style = end_of_line +# ij_java_block_comment_at_first_column = true +# ij_java_call_parameters_new_line_after_left_paren = false +# ij_java_call_parameters_right_paren_on_new_line = false +# ij_java_call_parameters_wrap = off +# ij_java_case_statement_on_separate_line = true +# ij_java_catch_on_new_line = false +# ij_java_class_annotation_wrap = split_into_lines +# ij_java_class_brace_style = end_of_line +ij_java_class_count_to_use_import_on_demand = 99 +# ij_java_class_names_in_javadoc = 1 +# ij_java_do_not_indent_top_level_class_members = false +# ij_java_do_not_wrap_after_single_annotation = false +# ij_java_do_while_brace_force = always +# ij_java_doc_add_blank_line_after_description = true +# ij_java_doc_add_blank_line_after_param_comments = false +# ij_java_doc_add_blank_line_after_return = false +# ij_java_doc_add_p_tag_on_empty_lines = true +# ij_java_doc_align_exception_comments = true +# ij_java_doc_align_param_comments = true +# ij_java_doc_do_not_wrap_if_one_line = false +# ij_java_doc_enable_formatting = true +# ij_java_doc_enable_leading_asterisks = true +# ij_java_doc_indent_on_continuation = false +# ij_java_doc_keep_empty_lines = true +# ij_java_doc_keep_empty_parameter_tag = true +# ij_java_doc_keep_empty_return_tag = true +# ij_java_doc_keep_empty_throws_tag = true +# ij_java_doc_keep_invalid_tags = true +# ij_java_doc_param_description_on_new_line = false +# ij_java_doc_preserve_line_breaks = false +# ij_java_doc_use_throws_not_exception_tag = true +# ij_java_else_on_new_line = true +# ij_java_entity_dd_suffix = EJB +# ij_java_entity_eb_suffix = Bean +# ij_java_entity_hi_suffix = Home +# ij_java_entity_lhi_prefix = Local +# ij_java_entity_lhi_suffix = Home +# ij_java_entity_li_prefix = Local +# ij_java_entity_pk_class = java.lang.String +# ij_java_entity_vo_suffix = VO +# ij_java_enum_constants_wrap = off +# ij_java_extends_keyword_wrap = normal +# ij_java_extends_list_wrap = normal +# ij_java_field_annotation_wrap = split_into_lines +# ij_java_finally_on_new_line = false +# ij_java_for_brace_force = always +# ij_java_for_statement_new_line_after_left_paren = false +# ij_java_for_statement_right_paren_on_new_line = false +# ij_java_for_statement_wrap = normal +# ij_java_generate_final_locals = false +# ij_java_generate_final_parameters = false +# ij_java_if_brace_force = always +# ij_java_imports_layout = com.google.**,|,android.**,|,antenna.**,|,antlr.**,|,ar.**,|,asposewobfuscated.**,|,asquare.**,|,atg.**,|,au.**,|,beaver.**,|,bibtex.**,|,bmsi.**,|,bsh.**,|,ccl.**,|,cern.**,|,ChartDirector.**,|,checkers.**,|,com.**,|,COM.**,|,common.**,|,contribs.**,|,corejava.**,|,cryptix.**,|,cybervillains.**,|,dalvik.**,|,danbikel.**,|,de.**,|,EDU.**,|,eg.**,|,eu.**,|,examples.**,|,fat.**,|,fit.**,|,fitlibrary.**,|,fmpp.**,|,freemarker.**,|,gnu.**,|,groovy.**,|,groovyjarjarantlr.**,|,groovyjarjarasm.**,|,hak.**,|,hep.**,|,ie.**,|,imageinfo.**,|,info.**,|,it.**,|,jal.**,|,Jama.**,|,japa.**,|,japacheckers.**,|,jas.**,|,jasmin.**,|,javancss.**,|,javanet.**,|,javassist.**,|,javazoom.**,|,java_cup.**,|,jcifs.**,|,jetty.**,|,JFlex.**,|,jj2000.**,|,jline.**,|,jp.**,|,JSci.**,|,jsr166y.**,|,junit.**,|,jxl.**,|,jxxload_help.**,|,kawa.**,|,kea.**,|,libcore.**,|,libsvm.**,|,lti.**,|,memetic.**,|,mt.**,|,mx4j.**,|,net.**,|,netscape.**,|,nl.**,|,nu.**,|,oauth.**,|,ognl.**,|,opennlp.**,|,oracle.**,|,org.**,|,penn2dg.**,|,pennconverter.**,|,pl.**,|,prefuse.**,|,proguard.**,|,repackage.**,|,scm.**,|,se.**,|,serp.**,|,simple.**,|,soot.**,|,sqlj.**,|,src.**,|,ssa.**,|,sun.**,|,sunlabs.**,|,tcl.**,|,testdata.**,|,testshell.**,|,testsuite.**,|,twitter4j.**,|,uk.**,|,ViolinStrings.**,|,weka.**,|,wet.**,|,winstone.**,|,woolfel.**,|,wowza.**,|,java.**,|,javax.**,|,*,|,$* +# ij_java_indent_case_from_switch = true +# ij_java_insert_inner_class_imports = false +ij_java_insert_override_annotation = true +ij_java_keep_blank_lines_before_right_brace = 2 +ij_java_keep_blank_lines_between_package_declaration_and_header = 2 +ij_java_keep_blank_lines_in_code = 2 +ij_java_keep_blank_lines_in_declarations = 2 +# ij_java_keep_control_statement_in_one_line = false +# ij_java_keep_first_column_comment = true +# ij_java_keep_indents_on_empty_lines = false +# ij_java_keep_line_breaks = true +# ij_java_keep_multiple_expressions_in_one_line = false +ij_java_keep_simple_blocks_in_one_line = true +ij_java_keep_simple_classes_in_one_line = true +ij_java_keep_simple_lambdas_in_one_line = true +ij_java_keep_simple_methods_in_one_line = true +# ij_java_lambda_brace_style = end_of_line +# ij_java_layout_static_imports_separately = true +# ij_java_line_comment_add_space = false +# ij_java_line_comment_at_first_column = true +# ij_java_message_dd_suffix = EJB +# ij_java_message_eb_suffix = Bean +# ij_java_method_annotation_wrap = split_into_lines +# ij_java_method_brace_style = end_of_line +# ij_java_method_call_chain_wrap = off +# ij_java_method_parameters_new_line_after_left_paren = false +# ij_java_method_parameters_right_paren_on_new_line = false +# ij_java_method_parameters_wrap = off +# ij_java_modifier_list_wrap = false +ij_java_names_count_to_use_import_on_demand = 99 +# ij_java_packages_to_use_import_on_demand = java.awt.*,javax.swing.* +# ij_java_parameter_annotation_wrap = off +# ij_java_parentheses_expression_new_line_after_left_paren = false +# ij_java_parentheses_expression_right_paren_on_new_line = false +# ij_java_place_assignment_sign_on_next_line = false +# ij_java_prefer_longer_names = true +# ij_java_prefer_parameters_wrap = false +# ij_java_repeat_synchronized = true +# ij_java_replace_instanceof_and_cast = false +# ij_java_replace_null_check = true +# ij_java_replace_sum_lambda_with_method_ref = true +# ij_java_resource_list_new_line_after_left_paren = false +# ij_java_resource_list_right_paren_on_new_line = false +# ij_java_resource_list_wrap = off +# ij_java_session_dd_suffix = EJB +# ij_java_session_eb_suffix = Bean +# ij_java_session_hi_suffix = Home +# ij_java_session_lhi_prefix = Local +# ij_java_session_lhi_suffix = Home +# ij_java_session_li_prefix = Local +# ij_java_session_si_suffix = Service +# ij_java_space_after_closing_angle_bracket_in_type_argument = false +# ij_java_space_after_colon = true +# ij_java_space_after_comma = true +# ij_java_space_after_comma_in_type_arguments = true +# ij_java_space_after_for_semicolon = true +# ij_java_space_after_quest = true +# ij_java_space_after_type_cast = true +# ij_java_space_before_annotation_array_initializer_left_brace = false +# ij_java_space_before_annotation_parameter_list = false +# ij_java_space_before_array_initializer_left_brace = false +# ij_java_space_before_catch_keyword = true +# ij_java_space_before_catch_left_brace = true +# ij_java_space_before_catch_parentheses = false +# ij_java_space_before_class_left_brace = true +# ij_java_space_before_colon = true +# ij_java_space_before_colon_in_foreach = true +# ij_java_space_before_comma = false +# ij_java_space_before_do_left_brace = true +# ij_java_space_before_else_keyword = true +# ij_java_space_before_else_left_brace = true +# ij_java_space_before_finally_keyword = true +# ij_java_space_before_finally_left_brace = true +# ij_java_space_before_for_left_brace = true +# ij_java_space_before_for_parentheses = false +# ij_java_space_before_for_semicolon = false +# ij_java_space_before_if_left_brace = true +# ij_java_space_before_if_parentheses = false +# ij_java_space_before_method_call_parentheses = false +# ij_java_space_before_method_left_brace = true +# ij_java_space_before_method_parentheses = false +# ij_java_space_before_opening_angle_bracket_in_type_parameter = false +# ij_java_space_before_quest = true +# ij_java_space_before_switch_left_brace = true +# ij_java_space_before_switch_parentheses = false +# ij_java_space_before_synchronized_left_brace = true +# ij_java_space_before_synchronized_parentheses = false +# ij_java_space_before_try_left_brace = true +# ij_java_space_before_try_parentheses = false +# ij_java_space_before_type_parameter_list = false +# ij_java_space_before_while_keyword = true +# ij_java_space_before_while_left_brace = true +# ij_java_space_before_while_parentheses = false +# ij_java_space_inside_one_line_enum_braces = false +# ij_java_space_within_empty_array_initializer_braces = false +# ij_java_space_within_empty_method_call_parentheses = false +# ij_java_space_within_empty_method_parentheses = false +# ij_java_spaces_around_additive_operators = true +# ij_java_spaces_around_assignment_operators = true +# ij_java_spaces_around_bitwise_operators = true +# ij_java_spaces_around_equality_operators = true +# ij_java_spaces_around_lambda_arrow = true +# ij_java_spaces_around_logical_operators = true +# ij_java_spaces_around_method_ref_dbl_colon = false +# ij_java_spaces_around_multiplicative_operators = true +# ij_java_spaces_around_relational_operators = true +# ij_java_spaces_around_shift_operators = true +# ij_java_spaces_around_type_bounds_in_type_parameters = true +# ij_java_spaces_around_unary_operator = false +# ij_java_spaces_within_angle_brackets = false +# ij_java_spaces_within_annotation_parentheses = false +# ij_java_spaces_within_array_initializer_braces = false +# ij_java_spaces_within_braces = false +# ij_java_spaces_within_brackets = false +# ij_java_spaces_within_cast_parentheses = false +# ij_java_spaces_within_catch_parentheses = false +# ij_java_spaces_within_for_parentheses = false +# ij_java_spaces_within_if_parentheses = false +# ij_java_spaces_within_method_call_parentheses = false +# ij_java_spaces_within_method_parentheses = false +# ij_java_spaces_within_parentheses = false +# ij_java_spaces_within_switch_parentheses = false +# ij_java_spaces_within_synchronized_parentheses = false +# ij_java_spaces_within_try_parentheses = false +# ij_java_spaces_within_while_parentheses = false +# ij_java_special_else_if_treatment = true +# ij_java_subclass_name_suffix = Impl +# ij_java_ternary_operation_signs_on_next_line = true +# ij_java_ternary_operation_wrap = off +# ij_java_test_name_suffix = Test +# ij_java_throws_keyword_wrap = normal +# ij_java_throws_list_wrap = normal +# ij_java_use_external_annotations = false +# ij_java_use_fq_class_names = false +# ij_java_use_single_class_imports = true +# ij_java_variable_annotation_wrap = off +# ij_java_visibility = public +# ij_java_while_brace_force = always +# ij_java_while_on_new_line = false +# ij_java_wrap_comments = false +# ij_java_wrap_first_method_in_call_chain = false +# ij_java_wrap_long_lines = false diff --git a/src/main/java/slimeknights/mantle/client/book/BookHelper.java b/src/main/java/slimeknights/mantle/client/book/BookHelper.java index c3ca646..d11f4ce 100644 --- a/src/main/java/slimeknights/mantle/client/book/BookHelper.java +++ b/src/main/java/slimeknights/mantle/client/book/BookHelper.java @@ -3,37 +3,78 @@ package slimeknights.mantle.client.book; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; +import javax.annotation.Nullable; import java.util.Objects; public class BookHelper { - public static String getSavedPage(ItemStack item) { - if (!item.isEmpty() && item.hasTag()) { - CompoundNBT mantleBook = Objects.requireNonNull(item.getTag()).getCompound("mantle").getCompound("book"); + public static final String BOOK_COMPOUND = "mantle"; + public static final String BOOK_DATA_COMPOUND = "book"; - if (mantleBook.contains("page", 8)) { - return mantleBook.getString("page"); + public static final String NBT_CURRENT_PAGE = "current_page"; + + /** + * Returns the current saved page on the book + * Returns an empty string is one is not found + * + * @param item The book to check for a saved page on + * @return The current saved page + * @deprecated Remove after book rework + */ + @Deprecated + public static String getSavedPage(ItemStack item) { + return getCurrentSavedPage(item); + } + + /** + * Returns the current saved page on the book + * Returns an empty string is one is not found + * + * @param item The book to check for a saved page on + * @return The current saved page + */ + public static String getCurrentSavedPage(@Nullable ItemStack item) { + if (item != null) { + if (!item.isEmpty() && item.hasTag()) { + CompoundNBT bookNBT = item.getOrCreateTag().getCompound(BOOK_COMPOUND).getCompound(BOOK_DATA_COMPOUND); + + if (bookNBT.contains(NBT_CURRENT_PAGE, 8)) { + return bookNBT.getString(NBT_CURRENT_PAGE); + } } } return ""; } + /** + * Saves the current open page to the given book ItemStack. + * + * @param item the current book + * @param page the current open page + * @deprecated Remove after book rework + */ + @Deprecated public static void writeSavedPage(ItemStack item, String page) { - CompoundNBT compound = item.getTag(); - - if (compound == null) { - compound = new CompoundNBT(); - } - - CompoundNBT mantle = compound.getCompound("mantle"); - CompoundNBT book = mantle.getCompound("book"); - - book.putString("page", page); - - mantle.put("book", book); - compound.put("mantle", mantle); - item.setTag(compound); + writeSavedPageToBook(item, page); } + /** + * Saves the current open page to the given book ItemStack. + * + * @param stack the current book stack + * @param currentPage the current open page + */ + public static void writeSavedPageToBook(ItemStack stack, String currentPage) { + CompoundNBT compoundNBT = stack.getOrCreateTag(); + + CompoundNBT mantleCompound = compoundNBT.getCompound(BOOK_COMPOUND); + CompoundNBT bookCompound = compoundNBT.getCompound(BOOK_DATA_COMPOUND); + + bookCompound.putString(NBT_CURRENT_PAGE, currentPage); + + mantleCompound.put(BOOK_DATA_COMPOUND, bookCompound); + compoundNBT.put(BOOK_COMPOUND, mantleCompound); + stack.setTag(compoundNBT); + } } diff --git a/src/main/java/slimeknights/mantle/client/book/BookLoader.java b/src/main/java/slimeknights/mantle/client/book/BookLoader.java index 821a265..b575b8a 100644 --- a/src/main/java/slimeknights/mantle/client/book/BookLoader.java +++ b/src/main/java/slimeknights/mantle/client/book/BookLoader.java @@ -39,14 +39,19 @@ import java.util.function.Predicate; @OnlyIn(Dist.CLIENT) public class BookLoader implements ISelectiveResourceReloadListener { - /** GSON object to be used for book loading purposes */ - public static final Gson GSON = new GsonBuilder().registerTypeAdapter(int.class, new HexStringDeserializer()) - .create(); + /** + * GSON object to be used for book loading purposes + */ + public static final Gson GSON = new GsonBuilder().registerTypeAdapter(int.class, new HexStringDeserializer()).create(); - /** Maps page content presets to names */ + /** + * Maps page content presets to names + */ private static final HashMap> typeToContentMap = new HashMap<>(); - /** Internal registry of all books for the purposes of the reloader, maps books to name */ + /** + * Internal registry of all books for the purposes of the reloader, maps books to name + */ private static final HashMap books = new HashMap<>(); public BookLoader() { @@ -140,7 +145,7 @@ public class BookLoader implements ISelectiveResourceReloadListener { return; } - BookHelper.writeSavedPage(item, page); + BookHelper.writeSavedPageToBook(item, page); MantleNetwork.INSTANCE.network.sendToServer(new UpdateSavedPagePacket(page)); } @@ -151,4 +156,4 @@ public class BookLoader implements ISelectiveResourceReloadListener { public void onResourceManagerReload(IResourceManager resourceManager, Predicate resourcePredicate) { books.forEach((s, bookData) -> bookData.reset()); } -} \ No newline at end of file +} diff --git a/src/main/java/slimeknights/mantle/client/book/BookTransformer.java b/src/main/java/slimeknights/mantle/client/book/BookTransformer.java index 2ef56b0..df10ece 100644 --- a/src/main/java/slimeknights/mantle/client/book/BookTransformer.java +++ b/src/main/java/slimeknights/mantle/client/book/BookTransformer.java @@ -115,11 +115,10 @@ public abstract class BookTransformer { for (int i = 0; i < pages.length; i++) { pages[i] = new PageData(true); pages[i].name = "tableofcontents" + i; - TextData[] text = new TextData[i > pages.length - 1 ? ENTRIES_PER_PAGE : section - .getPageCount() - (genPages - 1) * ENTRIES_PER_PAGE]; + TextData[] text = new TextData[i > pages.length - 1 ? ENTRIES_PER_PAGE : section.getPageCount() - (genPages - 1) * ENTRIES_PER_PAGE]; + for (int j = 0; j < text.length; j++) { - text[j] = new TextData((i * ENTRIES_PER_PAGE + j + 1) + ". " + section.pages.get(i * ENTRIES_PER_PAGE + j) - .getTitle()); + text[j] = new TextData((i * ENTRIES_PER_PAGE + j + 1) + ". " + section.pages.get(i * ENTRIES_PER_PAGE + j).getTitle()); text[j].action = "go-to-page-rtn:" + section.name + "." + section.pages.get(i * ENTRIES_PER_PAGE + j).name; } diff --git a/src/main/java/slimeknights/mantle/client/book/action/StringActionProcessor.java b/src/main/java/slimeknights/mantle/client/book/action/StringActionProcessor.java index 0f9d801..57b7c9e 100644 --- a/src/main/java/slimeknights/mantle/client/book/action/StringActionProcessor.java +++ b/src/main/java/slimeknights/mantle/client/book/action/StringActionProcessor.java @@ -16,6 +16,7 @@ public class StringActionProcessor { if (protocol == null || protocol.protocol == null || protocol.protocol.isEmpty()) { throw new IllegalArgumentException("Protocol must be defined and must not have an empty protocol identifier."); } + if (protocols.containsKey(protocol.protocol)) { throw new IllegalArgumentException("Protocol " + protocol.protocol + " already registered."); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/BookData.java b/src/main/java/slimeknights/mantle/client/book/data/BookData.java index 614346b..c0ba2ad 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/BookData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/BookData.java @@ -54,7 +54,9 @@ public class BookData implements IDataItem { if (this.initialized) { return; } + Mantle.logger.info("Started loading book..."); + try { this.initialized = true; this.sections.clear(); @@ -69,8 +71,7 @@ public class BookData implements IDataItem { for (SectionData section : repoContents) { section.source = repo; } - } - catch (Exception e) { + } catch (Exception e) { SectionData error = new SectionData(); error.name = "errorenous"; PageData page = new PageData(true); @@ -86,10 +87,8 @@ public class BookData implements IDataItem { if (repo.resourceExists(appearanceLocation)) { try { - this.appearance = BookLoader.GSON - .fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class); - } - catch (Exception e) { + this.appearance = BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class); + } catch (Exception e) { e.printStackTrace(); } } @@ -100,10 +99,8 @@ public class BookData implements IDataItem { if (repo.resourceExists(itemLinkLocation)) { try { - this.itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON - .fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class))); - } - catch (Exception e) { + this.itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class))); + } catch (Exception e) { e.printStackTrace(); } } @@ -114,9 +111,7 @@ public class BookData implements IDataItem { try { IResource resource = repo.getResource(languageLocation); if (resource != null) { - BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), - StandardCharsets.UTF_8)); - + BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); String next = br.readLine(); while (next != null) { @@ -130,19 +125,20 @@ public class BookData implements IDataItem { next = br.readLine(); } } - } - catch (Exception ignored) { + } catch (Exception ignored) { } } } for (int i = 0; i < this.sections.size(); i++) { SectionData section = this.sections.get(i); + if (section.name == null) { continue; } List matchingSections = this.sections.stream().filter(sect -> section.name.equalsIgnoreCase(sect.name)).collect(Collectors.toList()); + if (matchingSections.size() < 2) { continue; } @@ -184,8 +180,7 @@ public class BookData implements IDataItem { section.load(); } } - } - catch (Exception e) { + } catch (Exception e) { this.sections.clear(); SectionData section = new SectionData(true); section.name = "errorenous"; @@ -221,6 +216,7 @@ public class BookData implements IDataItem { public int getFirstPageNumber(SectionData section, @Nullable BookScreen.AdvancementCache advancementCache) { int pages = 0; + for (SectionData sect : this.sections) { sect.update(advancementCache); @@ -245,6 +241,7 @@ public class BookData implements IDataItem { } int pages = 0; + for (SectionData section : this.sections) { section.update(advancementCache); @@ -254,8 +251,7 @@ public class BookData implements IDataItem { if (pages + section.getPageCount() > number) { return section.pages.get(number - pages); - } - else { + } else { pages += section.getPageCount(); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/LinkedSectionData.java b/src/main/java/slimeknights/mantle/client/book/data/LinkedSectionData.java index e266d03..baf472f 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/LinkedSectionData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/LinkedSectionData.java @@ -37,7 +37,6 @@ public class LinkedSectionData extends SectionData { this.name = data.name; this.icon = data.icon; this.hideWhenLocked = data.hideWhenLocked; - } this.requirements.addAll(data.requirements); diff --git a/src/main/java/slimeknights/mantle/client/book/data/PageData.java b/src/main/java/slimeknights/mantle/client/book/data/PageData.java index 9b3145f..bf5879d 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/PageData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/PageData.java @@ -71,24 +71,23 @@ public class PageData implements IDataItem { } if (this.content == null) { - Class ctype = BookLoader.getPageType(this.type); + Class ctype = BookLoader.getPageType(this.type); - if(ctype != null) { - try { - this.content = ctype.newInstance(); - } catch (InstantiationException | IllegalAccessException | NullPointerException e) { - this.content = new ContentError("Failed to create a page of type \"" + this.type + "\".", e); - } - } else { - this.content = new ContentError("Failed to create a page of type \"" + this.type + "\" as it is not registered."); + if (ctype != null) { + try { + this.content = ctype.newInstance(); + } catch (InstantiationException | IllegalAccessException | NullPointerException e) { + this.content = new ContentError("Failed to create a page of type \"" + this.type + "\".", e); } + } else { + this.content = new ContentError("Failed to create a page of type \"" + this.type + "\" as it is not registered."); + } } try { this.content.parent = this; this.content.load(); - } - catch (Exception e) { + } catch (Exception e) { this.content = new ContentError("Failed to load page " + this.parent.name + "." + this.name + ".", e); e.printStackTrace(); } @@ -104,7 +103,7 @@ public class PageData implements IDataItem { f.setAccessible(true); if (Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier - .isFinal(f.getModifiers())) { + .isFinal(f.getModifiers())) { return; } @@ -119,20 +118,20 @@ public class PageData implements IDataItem { } private void processObject(@Nullable Object o, final int depth) { - if(depth > 4 || o == null) + if (depth > 4 || o == null) return; Class c = o.getClass(); boolean isArray = c.isArray(); - if(!isArray) { - if(IDataElement.class.isAssignableFrom(c)) { - ((IDataElement)o).load(this.source); + if (!isArray) { + if (IDataElement.class.isAssignableFrom(c)) { + ((IDataElement) o).load(this.source); } return; } - for(int i = 0; i < Array.getLength(o); i++){ + for (int i = 0; i < Array.getLength(o); i++) { this.processObject(Array.get(o, i), depth + 1); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/SectionData.java b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java index 57bdbf0..72a87bf 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/SectionData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java @@ -59,8 +59,7 @@ public class SectionData implements IDataItem { if (!data.isEmpty()) { try { this.pages = this.getPages(data); - } - catch (Exception e) { + } catch (Exception e) { this.pages = new ArrayList<>(); PageData pdError = new PageData(true); pdError.name = "errorrenous"; @@ -84,7 +83,8 @@ public class SectionData implements IDataItem { /** * Gets a list of pages from the given data - * @param data JSON data + * + * @param data JSON data * @return ArrayList of pages for the book */ protected ArrayList getPages(String data) { diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java index d5ed69a..b8a9dde 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java @@ -39,8 +39,7 @@ public class ContentBlockInteraction extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java index 71bbe9a..eab998f 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java @@ -64,8 +64,7 @@ public class ContentCrafting extends PageContent { list.add(new ImageElement(x, y, IMG_CRAFTING_SMALL.width, IMG_CRAFTING_SMALL.height, IMG_CRAFTING_SMALL, book.appearance.slotColor)); resultX = x + X_RESULT_SMALL; resultY = y + Y_RESULT_SMALL; - } - else if (this.grid_size.equalsIgnoreCase("large")) { + } else if (this.grid_size.equalsIgnoreCase("large")) { x = BookScreen.PAGE_WIDTH / 2 - IMG_CRAFTING_LARGE.width / 2; height = y + IMG_CRAFTING_LARGE.height; list.add(new ImageElement(x, y, IMG_CRAFTING_LARGE.width, IMG_CRAFTING_LARGE.height, IMG_CRAFTING_LARGE, book.appearance.slotColor)); @@ -97,9 +96,9 @@ public class ContentCrafting extends PageContent { public void load() { super.load(); - if(!StringUtils.isEmpty(recipe) && ResourceLocation.isResouceNameValid(recipe)) { + if (!StringUtils.isEmpty(recipe) && ResourceLocation.isResouceNameValid(recipe)) { int w = 0, h = 0; - switch(grid_size.toLowerCase()) { + switch (grid_size.toLowerCase()) { case "large": w = h = 3; break; @@ -109,8 +108,8 @@ public class ContentCrafting extends PageContent { } IRecipe recipe = Minecraft.getInstance().world.getRecipeManager().getRecipe(new ResourceLocation(this.recipe)).orElse(null); - if(recipe instanceof ICraftingRecipe) { - if(!recipe.canFit(w, h)) { + if (recipe instanceof ICraftingRecipe) { + if (!recipe.canFit(w, h)) { throw new BookLoadException("Recipe " + this.recipe + " cannot fit in a " + w + "x" + h + " crafting grid"); } @@ -118,13 +117,13 @@ public class ContentCrafting extends PageContent { NonNullList ingredients = recipe.getIngredients(); - if(recipe instanceof IShapedRecipe) { + if (recipe instanceof IShapedRecipe) { IShapedRecipe shaped = (IShapedRecipe) recipe; grid = new ItemStackData[shaped.getRecipeHeight()][shaped.getRecipeWidth()]; - for(int y = 0; y < grid.length; y++) { - for(int x = 0; x < grid[y].length; x++){ + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[y].length; x++) { grid[y][x] = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, ingredients.get(x + y * grid[y].length).getMatchingStacks())); } } @@ -133,7 +132,7 @@ public class ContentCrafting extends PageContent { } grid = new ItemStackData[h][w]; - for(int i = 0; i < ingredients.size(); i++){ + for (int i = 0; i < ingredients.size(); i++) { grid[i / h][i % w] = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, ingredients.get(i).getMatchingStacks())); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java index de30d8e..f593d1e 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java @@ -30,7 +30,7 @@ public class ContentError extends PageContent { public void build(BookData book, ArrayList list, boolean rightSide) { this.addTitle(list, "Error"); - if(exception instanceof BookLoadException) { + if (exception instanceof BookLoadException) { buildSimple(list); return; } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java index 0229039..9e706c7 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImage.java @@ -22,15 +22,13 @@ public class ContentImage extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } if (this.image != null && this.image.location != null) { list.add(new ImageElement(0, y, BookScreen.PAGE_WIDTH, BookScreen.PAGE_HEIGHT - y, this.image)); - } - else { + } else { list.add(new ImageElement(ImageData.MISSING)); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java index 5ada1a8..f9fd8bf 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentImageText.java @@ -25,15 +25,13 @@ public class ContentImageText extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } if (this.image != null && this.image.location != null) { list.add(new ImageElement(0, y, BookScreen.PAGE_WIDTH, 100, this.image)); - } - else { + } else { list.add(new ImageElement(0, y, 32, 32, ImageData.MISSING)); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java index 24ce32f..a0429bd 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import static slimeknights.mantle.client.screen.book.Textures.TEX_SMELTING; public class ContentSmelting extends PageContent { + private static final NonNullList FUELS; public static final transient int TEX_SIZE = 128; @@ -77,8 +78,7 @@ public class ContentSmelting extends PageContent { public NonNullList getFuelsList() { //TODO ask JEI for fuel list if it is present - - if(this.fuel != null) { + if (this.fuel != null) { return this.fuel.getItems(); } @@ -89,12 +89,12 @@ public class ContentSmelting extends PageContent { public void load() { super.load(); - if(!StringUtils.isEmpty(this.recipe) && ResourceLocation.isResouceNameValid(this.recipe)) { + if (!StringUtils.isEmpty(this.recipe) && ResourceLocation.isResouceNameValid(this.recipe)) { IRecipe recipe = Minecraft.getInstance().world.getRecipeManager().getRecipe(new ResourceLocation(this.recipe)).orElse(null); - if(recipe instanceof AbstractCookingRecipe) { + if (recipe instanceof AbstractCookingRecipe) { this.input = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, recipe.getIngredients().get(0).getMatchingStacks())); - this.cookTime = ((AbstractCookingRecipe)recipe).getCookTime(); + this.cookTime = ((AbstractCookingRecipe) recipe).getCookTime(); this.result = ItemStackData.getItemStackData(recipe.getRecipeOutput()); } } @@ -102,33 +102,33 @@ public class ContentSmelting extends PageContent { static { FUELS = NonNullList.from(ItemStack.EMPTY, - new ItemStack(Blocks.OAK_SLAB), - new ItemStack(Blocks.SPRUCE_SLAB), - new ItemStack(Blocks.BIRCH_SLAB), - new ItemStack(Blocks.JUNGLE_SLAB), - new ItemStack(Blocks.ACACIA_SLAB), - new ItemStack(Blocks.DARK_OAK_SLAB), - new ItemStack(Blocks.OAK_PLANKS), - new ItemStack(Blocks.SPRUCE_PLANKS), - new ItemStack(Blocks.BIRCH_PLANKS), - new ItemStack(Blocks.JUNGLE_PLANKS), - new ItemStack(Blocks.ACACIA_PLANKS), - new ItemStack(Blocks.DARK_OAK_PLANKS), - new ItemStack(Blocks.COAL_BLOCK), - new ItemStack(Items.WOODEN_PICKAXE), - new ItemStack(Items.WOODEN_SWORD), - new ItemStack(Items.WOODEN_HOE), - new ItemStack(Items.STICK), - new ItemStack(Items.COAL), - new ItemStack(Items.LAVA_BUCKET), - new ItemStack(Blocks.OAK_SAPLING), - new ItemStack(Blocks.SPRUCE_SAPLING), - new ItemStack(Blocks.BIRCH_SAPLING), - new ItemStack(Blocks.JUNGLE_SAPLING), - new ItemStack(Blocks.ACACIA_SAPLING), - new ItemStack(Blocks.DARK_OAK_SAPLING), - new ItemStack(Items.BLAZE_ROD), - new ItemStack(Items.WOODEN_SHOVEL), - new ItemStack(Items.WOODEN_AXE)); + new ItemStack(Blocks.OAK_SLAB), + new ItemStack(Blocks.SPRUCE_SLAB), + new ItemStack(Blocks.BIRCH_SLAB), + new ItemStack(Blocks.JUNGLE_SLAB), + new ItemStack(Blocks.ACACIA_SLAB), + new ItemStack(Blocks.DARK_OAK_SLAB), + new ItemStack(Blocks.OAK_PLANKS), + new ItemStack(Blocks.SPRUCE_PLANKS), + new ItemStack(Blocks.BIRCH_PLANKS), + new ItemStack(Blocks.JUNGLE_PLANKS), + new ItemStack(Blocks.ACACIA_PLANKS), + new ItemStack(Blocks.DARK_OAK_PLANKS), + new ItemStack(Blocks.COAL_BLOCK), + new ItemStack(Items.WOODEN_PICKAXE), + new ItemStack(Items.WOODEN_SWORD), + new ItemStack(Items.WOODEN_HOE), + new ItemStack(Items.STICK), + new ItemStack(Items.COAL), + new ItemStack(Items.LAVA_BUCKET), + new ItemStack(Blocks.OAK_SAPLING), + new ItemStack(Blocks.SPRUCE_SAPLING), + new ItemStack(Blocks.BIRCH_SAPLING), + new ItemStack(Blocks.JUNGLE_SAPLING), + new ItemStack(Blocks.ACACIA_SAPLING), + new ItemStack(Blocks.DARK_OAK_SAPLING), + new ItemStack(Items.BLAZE_ROD), + new ItemStack(Items.WOODEN_SHOVEL), + new ItemStack(Items.WOODEN_AXE)); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmithing.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmithing.java index e8fd59b..f597ebe 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmithing.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmithing.java @@ -41,8 +41,7 @@ public class ContentSmithing extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java index 423b05c..8c7670c 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java @@ -49,8 +49,7 @@ public class ContentStructure extends PageContent { int y = TITLE_HEIGHT; if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java index aae8709..c006072 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentText.java @@ -23,8 +23,7 @@ public class ContentText extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java index 05a6e63..a8b8532 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextImage.java @@ -25,15 +25,13 @@ public class ContentTextImage extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } @@ -43,8 +41,7 @@ public class ContentTextImage extends PageContent { if (this.image != null && this.image.location != null) { list.add(new ImageElement(0, y + BookScreen.PAGE_HEIGHT - 100, BookScreen.PAGE_WIDTH, 100 - y, this.image)); - } - else { + } else { list.add(new ImageElement(0, y + BookScreen.PAGE_HEIGHT - 100, BookScreen.PAGE_WIDTH, 100 - y, ImageData.MISSING)); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java index a4b6bbe..b4955b4 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextLeftImage.java @@ -26,15 +26,13 @@ public class ContentTextLeftImage extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } if (this.image != null && this.image.location != null) { list.add(new ImageElement(0, y, 50, 50, this.image)); - } - else { + } else { list.add(new ImageElement(0, y, 50, 50, ImageData.MISSING)); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java index 7d720ab..abfe22b 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTextRightImage.java @@ -26,8 +26,7 @@ public class ContentTextRightImage extends PageContent { if (this.title == null || this.title.isEmpty()) { y = 0; - } - else { + } else { this.addTitle(list, this.title); } @@ -37,8 +36,7 @@ public class ContentTextRightImage extends PageContent { if (this.image != null && this.image.location != null) { list.add(new ImageElement(BookScreen.PAGE_WIDTH - 50, y, 50, 50, this.image)); - } - else { + } else { list.add(new ImageElement(BookScreen.PAGE_WIDTH - 50, y, 50, 50, ImageData.MISSING)); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java index fb5972b..3f7be1e 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java @@ -28,7 +28,7 @@ public abstract class PageContent { public void addTitle(ArrayList list, String title) { TextData tdTitle = new TextData(title); tdTitle.underlined = true; - this.addTitle(list, new TextData[] { tdTitle }); + this.addTitle(list, new TextData[]{tdTitle}); } public void addTitle(ArrayList list, TextData[] title) { diff --git a/src/main/java/slimeknights/mantle/client/book/data/deserializer/HexStringDeserializer.java b/src/main/java/slimeknights/mantle/client/book/data/deserializer/HexStringDeserializer.java index cc86343..136f14b 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/deserializer/HexStringDeserializer.java +++ b/src/main/java/slimeknights/mantle/client/book/data/deserializer/HexStringDeserializer.java @@ -11,22 +11,19 @@ public class HexStringDeserializer implements JsonDeserializer { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { + throws JsonParseException { if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) { return json.getAsInt(); - } - else if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) { + } else if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) { try { String s = json.getAsString(); if (s.toLowerCase().startsWith("0b")) { return Integer.parseInt(s.substring(2), 2); - } - else if (s.toLowerCase().startsWith("0x")) { + } else if (s.toLowerCase().startsWith("0x")) { return Integer.parseInt(s.substring(2), 16); } - } - catch (NumberFormatException ignored) { + } catch (NumberFormatException ignored) { } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java b/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java index 908b50b..94bb7e5 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java @@ -15,7 +15,7 @@ public class BlockData implements IDataElement { @Override public void load(BookRepository source) { - if(this.endPos == null) { + if (this.endPos == null) { this.endPos = this.pos; } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/IDataElement.java b/src/main/java/slimeknights/mantle/client/book/data/element/IDataElement.java index 116e2c7..55a6a2c 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/IDataElement.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/IDataElement.java @@ -3,5 +3,6 @@ package slimeknights.mantle.client.book.data.element; import slimeknights.mantle.client.book.repository.BookRepository; public interface IDataElement { - void load(BookRepository source); + + void load(BookRepository source); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java index 935d20c..3681c56 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java @@ -63,7 +63,7 @@ public class ImageData extends DataLocation { public void load(BookRepository source) { super.load(source); - if(this.item != null) { + if (this.item != null) { this.item.load(source); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java b/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java index 5db8218..757f122 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java @@ -37,7 +37,7 @@ public class ItemStackData implements IDataElement { private transient int tagGeneration; public NonNullList getItems() { - if(this.isTag && this.tagGeneration != 0) { //ItemTags.getGeneration()) { TODO FIX + if (this.isTag && this.tagGeneration != 0) { //ItemTags.getGeneration()) { TODO FIX this.loadTag(); } @@ -53,8 +53,7 @@ public class ItemStackData implements IDataElement { boolean isMissingItem = false; try { item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(this.id)); - } - catch (Exception e) { + } catch (Exception e) { item = Items.BARRIER; isMissingItem = true; } @@ -69,8 +68,7 @@ public class ItemStackData implements IDataElement { if (this.nbt != null) { try { itemStack.setTag(JsonToNBT.getTagFromJson(filterJsonQuotes(this.nbt.toString()))); - } - catch (CommandSyntaxException ignored) { + } catch (CommandSyntaxException ignored) { } } @@ -122,8 +120,7 @@ public class ItemStackData implements IDataElement { ITag values = ItemTags.getCollection().get(new ResourceLocation(this.tag)); if (values != null) { this.items = values.getAllElements().stream().map(ItemStack::new).collect(Collectors.toCollection(NonNullList::create)); - } - else { + } else { this.items = NonNullList.create(); } } @@ -148,7 +145,7 @@ public class ItemStackData implements IDataElement { if (source.resourceExists(location)) { try { ItemsList itemsList = BookLoader.GSON - .fromJson(source.resourceToString(source.getResource(location)), ItemsList.class); + .fromJson(source.resourceToString(source.getResource(location)), ItemsList.class); this.items = NonNullList.withSize(itemsList.items.length, ItemStack.EMPTY); for (int i = 0; i < itemsList.items.length; i++) { @@ -156,8 +153,7 @@ public class ItemStackData implements IDataElement { } this.action = itemsList.action; - } - catch (Exception ignored) { + } catch (Exception ignored) { } } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/TextComponentData.java b/src/main/java/slimeknights/mantle/client/book/data/element/TextComponentData.java new file mode 100644 index 0000000..4cdb698 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/TextComponentData.java @@ -0,0 +1,30 @@ +package slimeknights.mantle.client.book.data.element; + +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; + +@OnlyIn(Dist.CLIENT) +public class TextComponentData { + + public static final TextComponentData LINEBREAK = new TextComponentData("\n"); + + public ITextComponent text; + + public boolean isParagraph = false; + public boolean dropShadow = false; + public float scale = 1.F; + public String action = ""; + public ITextComponent[] tooltips = null; + + public TextComponentData(String text) { + this(new StringTextComponent(text)); + } + + public TextComponentData(ITextComponent text) { + this.text = text; + } + + +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java b/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java index 29c08b9..eecbc39 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/TextData.java @@ -11,6 +11,7 @@ public class TextData { public String text; public String color = "black"; + public int rgbColor = 0; public boolean useOldColor = true; public boolean bold = false; diff --git a/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java index fb6d07f..4300cc0 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java @@ -42,5 +42,5 @@ public abstract class BookRepository { return this.resourceToString(resource, true); } - public abstract String resourceToString(@Nullable IResource resource, boolean skipCommments); + public abstract String resourceToString(@Nullable IResource resource, boolean skipComments); } diff --git a/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java index e91c8d0..d83292d 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java @@ -31,7 +31,7 @@ public class DummyRepository extends BookRepository { } @Override - public String resourceToString(@Nullable IResource resource, boolean skipCommments) { + public String resourceToString(@Nullable IResource resource, boolean skipComments) { return ""; } } diff --git a/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java index 5cda4a3..2a909bc 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java @@ -33,6 +33,7 @@ public class FileRepository extends BookRepository { if (path == null) { return safe ? new ResourceLocation("") : null; } + if (!path.contains(":")) { String langPath = null; @@ -60,8 +61,7 @@ public class FileRepository extends BookRepository { if (this.resourceExists(res)) { return res; } - } - else { + } else { ResourceLocation res = new ResourceLocation(path); if (this.resourceExists(res)) { return res; @@ -76,10 +76,10 @@ public class FileRepository extends BookRepository { if (loc == null) { return null; } + try { return Minecraft.getInstance().getResourceManager().getResource(loc); - } - catch (IOException e) { + } catch (IOException e) { return null; } } @@ -89,20 +89,21 @@ public class FileRepository extends BookRepository { if (location == null) { return false; } + try { Minecraft.getInstance().getResourceManager().getResource(location); return true; - } - catch (IOException e) { + } catch (IOException e) { return false; } } @Override - public String resourceToString(@Nullable IResource resource, boolean skipCommments) { + public String resourceToString(@Nullable IResource resource, boolean skipComments) { if (resource == null) { return ""; } + try { Iterator iterator = IOUtils.readLines(resource.getInputStream(), StandardCharsets.UTF_8).iterator(); StringBuilder builder = new StringBuilder(); @@ -113,14 +114,13 @@ public class FileRepository extends BookRepository { String s = iterator.next().trim() + "\n"; // Comment skipper - if (skipCommments) { + if (skipComments) { if (isLongComment) { if (s.endsWith("*/")) { isLongComment = false; } continue; - } - else { + } else { if (s.startsWith("/*")) { isLongComment = true; continue; @@ -135,8 +135,7 @@ public class FileRepository extends BookRepository { } return builder.toString().trim(); - } - catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/slimeknights/mantle/client/screen/book/BookScreen.java b/src/main/java/slimeknights/mantle/client/screen/book/BookScreen.java index 3b67797..82203bf 100644 --- a/src/main/java/slimeknights/mantle/client/screen/book/BookScreen.java +++ b/src/main/java/slimeknights/mantle/client/screen/book/BookScreen.java @@ -90,7 +90,7 @@ public class BookScreen extends Screen { this.advancementCache = new AdvancementCache(); this.minecraft.player.connection.getAdvancementManager().setListener(this.advancementCache); - this.openPage(book.findPageNumber(BookHelper.getSavedPage(item), this.advancementCache)); + this.openPage(book.findPageNumber(BookHelper.getCurrentSavedPage(item), this.advancementCache)); } @Override @@ -146,8 +146,7 @@ public class BookScreen extends Screen { fontRenderer.drawStringWithShadow(matrixStack, this.book.appearance.subtitle, (this.width / 2) / 1.5F + 7 - fontRenderer.getStringWidth(this.book.appearance.subtitle) / 2, (this.height / 2 + 100 - fontRenderer.FONT_HEIGHT * 2) / 1.5F, 0xAE8000); RenderSystem.popMatrix(); } - } - else { + } else { render.bindTexture(TEX_BOOK); RenderHelper.disableStandardItemLighting(); @@ -307,8 +306,7 @@ public class BookScreen extends Screen { if (this.page == -1) { this.nextArrow.x = this.width / 2 + 80; this.indexArrow.visible = false; - } - else { + } else { this.previousArrow.x = this.width / 2 - 184; this.nextArrow.x = this.width / 2 + 165; @@ -319,16 +317,6 @@ public class BookScreen extends Screen { this.nextArrow.y = this.height / 2 + 75; } - /*@Override - TODO: REMOVE - public void actionPerformed(GuiButton button) { - if(button instanceof GuiBookmark) { - openPage(book.findPageNumber(((GuiBookmark) button).data.page, advancementCache)); - - return; - } - }*/ - @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { super.keyPressed(keyCode, scanCode, modifiers); @@ -374,8 +362,7 @@ public class BookScreen extends Screen { this.buildPages(); return true; - } - else if (scrollDelta > 0.0D) { + } else if (scrollDelta > 0.0D) { this.page--; if (this.page < -1) { this.page = -1; @@ -469,8 +456,7 @@ public class BookScreen extends Screen { if (this.page == -1) { BookLoader.updateSavedPage(this.minecraft.player, this.item, ""); - } - else if (page != null && page.parent != null) { + } else if (page != null && page.parent != null) { BookLoader.updateSavedPage(this.minecraft.player, this.item, page.parent.name + "." + page.name); } } @@ -483,8 +469,7 @@ public class BookScreen extends Screen { public void drawerTransform(boolean rightSide) { if (rightSide) { RenderSystem.translatef(this.width / 2 + PAGE_PADDING_RIGHT + PAGE_MARGIN, this.height / 2 - PAGE_HEIGHT_UNSCALED / 2 + PAGE_PADDING_TOP + PAGE_MARGIN, 0); - } - else { + } else { RenderSystem.translatef(this.width / 2 - PAGE_WIDTH_UNSCALED + PAGE_PADDING_LEFT + PAGE_MARGIN, this.height / 2 - PAGE_HEIGHT_UNSCALED / 2 + PAGE_PADDING_TOP + PAGE_MARGIN, 0); } } @@ -494,8 +479,7 @@ public class BookScreen extends Screen { if (rightSide) { // from center: go padding + margin to the right return this.width / 2 + PAGE_PADDING_RIGHT + PAGE_MARGIN; - } - else { + } else { // from center: go page width left, then right with padding and margin return this.width / 2 - PAGE_WIDTH_UNSCALED + PAGE_PADDING_LEFT + PAGE_MARGIN; } @@ -525,11 +509,9 @@ public class BookScreen extends Screen { int bookPage; if (page == 1) { bookPage = 0; - } - else if (page % 2 == 0) { + } else if (page % 2 == 0) { bookPage = (page - 1) / 2 + 1; - } - else { + } else { bookPage = (page - 2) / 2 + 1; } @@ -552,17 +534,13 @@ public class BookScreen extends Screen { public int getPage(int side) { if (this.page == 0 && side == 0) { return -1; - } - else if (this.page == 0 && side == 1) { + } else if (this.page == 0 && side == 1) { return 0; - } - else if (side == 0) { + } else if (side == 0) { return (this.page - 1) * 2 + 1; - } - else if (side == 1) { + } else if (side == 1) { return (this.page - 2) * 2 + 2; - } - else { + } else { return -1; } } @@ -601,8 +579,7 @@ public class BookScreen extends Screen { if (page != null) { page.content.build(this.book, this.rightElements, false); } - } - else { + } else { PageData leftPage = this.book.findPage((this.page - 1) * 2 + 1, this.advancementCache); PageData rightPage = this.book.findPage((this.page - 1) * 2 + 2, this.advancementCache); @@ -622,10 +599,10 @@ public class BookScreen extends Screen { } } - public class AdvancementCache implements ClientAdvancementManager.IListener { + public static class AdvancementCache implements ClientAdvancementManager.IListener { - private HashMap progress = new HashMap<>(); - private HashMap nameCache = new HashMap<>(); + private final HashMap progress = new HashMap<>(); + private final HashMap nameCache = new HashMap<>(); @Nullable public AdvancementProgress getProgress(String id) { diff --git a/src/main/java/slimeknights/mantle/client/screen/book/BookmarkButton.java b/src/main/java/slimeknights/mantle/client/screen/book/BookmarkButton.java deleted file mode 100644 index 4c37efe..0000000 --- a/src/main/java/slimeknights/mantle/client/screen/book/BookmarkButton.java +++ /dev/null @@ -1,59 +0,0 @@ -package slimeknights.mantle.client.screen.book; - -import com.mojang.blaze3d.matrix.MatrixStack; -import com.mojang.blaze3d.systems.RenderSystem; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.widget.button.Button; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.text.StringTextComponent; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; -import slimeknights.mantle.client.book.data.BookmarkData; - -@OnlyIn(Dist.CLIENT) -public class BookmarkButton extends Button { - - private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png"); - - public static final int WIDTH = 31; - public static final int HEIGHT = 9; - public static final int TEX_X = 0, TEX_Y = 400; - public static final int ADD_W = 5, ADD_H = 5, ADD_X = 32, ADD_Y = 402; - - public static final float TEXT_SCALE = 0.5F; - - public int type = 0; - - public final BookmarkData data; - - public BookmarkButton(BookmarkData data, IPressable iPressable) { - super(-500, -500, WIDTH, HEIGHT, StringTextComponent.EMPTY, iPressable); - - this.data = data; - } - - @Override - public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) { - Minecraft minecraft = Minecraft.getInstance(); - int tex_y = TEX_Y + HEIGHT * this.type; - - minecraft.textureManager.bindTexture(TEX_BOOK); - - float r = ((this.data.color >> 16) & 0xff) / 255.F; - float g = ((this.data.color >> 8) & 0xff) / 255.F; - float b = (this.data.color & 0xff) / 255.F; - - RenderSystem.color3f(r, g, b); - blit(matrixStack, this.x, this.y, this.width, this.y, TEX_X, tex_y, WIDTH, HEIGHT, 512, 512); - - if (this.data.text != null && !this.data.text.isEmpty()) { - TextDataRenderer.drawScaledString(matrixStack, minecraft.fontRenderer, this.data.text, this.x + 1, this.y + this.y / 2 - minecraft.fontRenderer.FONT_HEIGHT * TEXT_SCALE / 2 + 1, 0xFFFFFFFF, true, TEXT_SCALE); - } - - RenderSystem.color3f(1F, 1F, 1F); - - if (this.data.page.equals("ADD")) { - blit(matrixStack, this.x + this.width / 2 - ADD_W / 2, this.y + this.y / 2 - ADD_H / 2, ADD_X, ADD_Y, ADD_W, ADD_H, 512, 512); - } - } -} diff --git a/src/main/java/slimeknights/mantle/client/screen/book/TextComponentDataRenderer.java b/src/main/java/slimeknights/mantle/client/screen/book/TextComponentDataRenderer.java new file mode 100644 index 0000000..e2bfd6f --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/screen/book/TextComponentDataRenderer.java @@ -0,0 +1,354 @@ +package slimeknights.mantle.client.screen.book; + +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.systems.RenderSystem; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.IReorderingProcessor; +import net.minecraft.util.text.IFormattableTextComponent; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import org.apache.commons.lang3.StringUtils; +import slimeknights.mantle.Mantle; +import slimeknights.mantle.client.book.data.element.TextComponentData; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@OnlyIn(Dist.CLIENT) +public class TextComponentDataRenderer { + + /** + * Renders the given Text Components on the screen and returns the action if any of them have one. + * + * @param matrixStack the matrix stack to render with + * @param x the x position to render at + * @param y the y position to render at + * @param boxWidth the width of the given render box + * @param boxHeight the height of the given render box + * @param data the list of text component data to draw + * @param mouseX the mouseY + * @param mouseY the mouseX + * @param fr the font renderer + * @param tooltip the list of tooltips + * @return the action if there's any + */ + public static String drawText(MatrixStack matrixStack, int x, int y, int boxWidth, int boxHeight, TextComponentData[] data, int mouseX, int mouseY, FontRenderer fr, List tooltip) { + String action = ""; + + int atX = x; + int atY = y; + + float prevScale = 1.F; + + for (TextComponentData item : data) { + int box1X, box1Y, box1W = 9999, box1H = y + fr.FONT_HEIGHT; + int box2X, box2Y = 9999, box2W, box2H; + int box3X = 9999, box3Y = 9999, box3W, box3H; + + if (item == null || item.text == null) { + continue; + } + + if (item.text.getString().equals("\n")) { + atX = x; + atY += fr.FONT_HEIGHT; + continue; + } + + if (item.isParagraph) { + atX = x; + atY += fr.FONT_HEIGHT * 2 * prevScale; + } + + prevScale = item.scale; + + ITextComponent text = translateTextComponent(item.text); + + ITextComponent[] split = cropTextComponentsBySize(text, boxWidth, boxHeight - (atY - y), boxWidth - (atX - x), fr, item.scale); + + box1X = atX; + box1Y = atY; + box2X = x; + box2W = x + boxWidth; + + for (int i = 0; i < split.length; i++) { + if (i == split.length - 1) { + box3X = atX; + box3Y = atY; + } + + ITextComponent textComponent = split[i]; + drawScaledTextComponent(matrixStack, fr, textComponent, atX, atY, item.dropShadow, item.scale); + + if (i < split.length - 1) { + atY += fr.FONT_HEIGHT; + atX = x; + } + + if (i == 0) { + box2Y = atY; + + if (atX == x) { + box1W = x + boxWidth; + } else { + box1W = atX; + } + } + } + + box2H = atY; + + atX += fr.func_243245_a(split[split.length - 1].func_241878_f()) * item.scale; + if (atX - x >= boxWidth) { + atX = x; + atY += fr.FONT_HEIGHT * item.scale; + } + + box3W = atX; + box3H = (int) (atY + fr.FONT_HEIGHT * item.scale); + + boolean mouseCheck = (mouseX >= box1X && mouseX <= box1W && mouseY >= box1Y && mouseY <= box1H && box1X != box1W && box1Y != box1H) || (mouseX >= box2X && mouseX <= box2W && mouseY >= box2Y && mouseY <= box2H && box2X != box2W && box2Y != box2H) || (mouseX >= box3X && mouseX <= box3W && mouseY >= box3Y && mouseY <= box3H && box3X != box3W && box1Y != box3H); + + if (item.tooltips != null && item.tooltips.length > 0) { + // Uncomment to render bounding boxes for event handling + if (BookScreen.debug) { + drawGradientRect(box1X, box1Y, box1W, box1H, 0xFF00FF00, 0xFF00FF00); + drawGradientRect(box2X, box2Y, box2W, box2H, 0xFFFF0000, 0xFFFF0000); + drawGradientRect(box3X, box3Y, box3W, box3H, 0xFF0000FF, 0xFF0000FF); + drawGradientRect(mouseX, mouseY, mouseX + 5, mouseY + 5, 0xFFFF00FF, 0xFFFFFF00); + } + + if (mouseCheck) { + tooltip.addAll(Arrays.asList(item.tooltips)); + } + } + + if (item.action != null && !item.action.isEmpty()) { + if (mouseCheck) { + action = item.action; + } + } + + if (atY >= y + boxHeight) { + if (item.dropShadow) { + fr.drawStringWithShadow(matrixStack, "...", atX, atY, 0); + } else { + fr.drawString(matrixStack, "...", atX, atY, 0); + } + break; + } + + y = atY; + } + + if (BookScreen.debug && !action.isEmpty()) { + tooltip.add(StringTextComponent.EMPTY); + tooltip.add(new StringTextComponent("Action: " + action).mergeStyle(TextFormatting.GRAY)); + } + + return action; + } + + /** + * Translates the text inside of the given text component + * Works with the siblings of the text component also. + * + * @param text the given text component to translate + * @return the translated text component + * @deprecated Remove since the text components passed to this method should be using TranslationTextComponent + */ + @Deprecated + private static ITextComponent translateTextComponent(ITextComponent text) { + IFormattableTextComponent iformattabletextcomponent = text.copyRaw().setStyle(text.getStyle()); + + String s = iformattabletextcomponent.getString(); + + s = s.replace("$$(", "$\0(").replace(")$$", ")\0$"); + + while (s.contains("$(") && s.contains(")$") && s.indexOf("$(") < s.indexOf(")$")) { + String loc = s.substring(s.indexOf("$(") + 2, s.indexOf(")$")); + s = s.replace("$(" + loc + ")$", I18n.format(loc)); + } + + if (s.indexOf("$(") > s.indexOf(")$") || s.contains(")$")) { + Mantle.logger.error("[Books] [TextDataRenderer] Detected unbalanced localization symbols \"$(\" and \")$\" in string: \"" + s + "\"."); + } + + iformattabletextcomponent = new StringTextComponent(s).setStyle(text.getStyle()); + + for (ITextComponent itextcomponent : text.getSiblings()) { + iformattabletextcomponent.append(translateTextComponent(itextcomponent)); + } + + return iformattabletextcomponent; + } + + /** + * Crops text components to the given size for rendering + * + * @param textComponent the text component to crop + * @param width the width to use + * @param height the height to use + * @param fr the font renderer + * @param scale the scale of text to use + * @return a list of text components that are cropped. + */ + // TODO this does not actually crop the text components, fix? + public static ITextComponent[] cropTextComponentsBySize(ITextComponent textComponent, int width, int height, FontRenderer fr, float scale) { + return cropTextComponentsBySize(textComponent, width, height, width, fr, scale); + } + + /** + * Crops text components to the given size for rendering + * + * @param textComponent the text component to crop + * @param width the width to use + * @param height the height to use + * @param firstWidth the actual width to use + * @param fontRenderer the font renderer + * @param scale the scale of text to use + * @return a list of text components which is what got clipped due to sizes + */ + // TODO this does not actually crop the text components, fix? + public static ITextComponent[] cropTextComponentsBySize(ITextComponent textComponent, int width, int height, int firstWidth, FontRenderer fontRenderer, float scale) { + IFormattableTextComponent iformattabletextcomponent = cropTextComponentBySize(textComponent, width, height, firstWidth, fontRenderer, scale); + + for (ITextComponent itextcomponent : textComponent.getSiblings()) { + iformattabletextcomponent.append(cropTextComponentBySize(itextcomponent, width, height, firstWidth, fontRenderer, scale)); + } + + List textComponents = new ArrayList<>(); + + textComponents.add(iformattabletextcomponent); + + return textComponents.toArray(new ITextComponent[0]); + } + + /** + * Crops text components to the given size for rendering + * + * @param textComponent the text component to crop + * @param width the width to use + * @param height the height to use + * @param firstWidth the actual width to use + * @param fontRenderer the font renderer + * @param scale the scale of text to use + * @return a text components which is what got clipped due to sizes + */ + // TODO this does not actually crop the text components, fix? + public static IFormattableTextComponent cropTextComponentBySize(ITextComponent textComponent, int width, int height, int firstWidth, FontRenderer fontRenderer, float scale) { + int curWidth = 0; + int curHeight = (int) (fontRenderer.FONT_HEIGHT * scale); + IFormattableTextComponent iformattabletextcomponent = textComponent.copyRaw().setStyle(textComponent.getStyle()); + String text = iformattabletextcomponent.getString(); + + for (int i = 0; i < text.length(); i++) { + curWidth += fontRenderer.getStringWidth(String.valueOf(text.charAt(i))) * scale; + + if (text.charAt(i) == '\n' || (curHeight == (int) (fontRenderer.FONT_HEIGHT * scale) && curWidth > firstWidth) || (curHeight != (int) (fontRenderer.FONT_HEIGHT * scale) && curWidth > width)) { + int oldI = i; + if (text.charAt(i) != '\n') { + while (i >= 0 && text.charAt(i) != ' ') { + i--; + } + if (i <= 0) { + i = oldI; + } + } else { + oldI++; + } + + text = text.substring(0, i) + "\r" + StringUtils.stripStart(text.substring(i + (i == oldI ? 0 : 1)), " "); + + i++; + curWidth = 0; + curHeight += fontRenderer.FONT_HEIGHT * scale; + } + } + + IFormattableTextComponent splitTextComponent = null; + + for (String s : text.split("\r")) { + if (splitTextComponent == null) + splitTextComponent = new StringTextComponent(s).setStyle(textComponent.getStyle()); + else + splitTextComponent.append(new StringTextComponent(s).setStyle(textComponent.getStyle())); + } + + if (splitTextComponent != null) + return splitTextComponent; + else + return iformattabletextcomponent; + } + + /** + * Draws a text component with the given scale at the given position + * + * @param matrixStack the given matrix stack used for rendering. + * @param font the font renderer to render with + * @param textComponent the text component to render + * @param x the x position to render at + * @param y the y position to render at + * @param dropShadow if there should be a shadow on the text + * @param scale the scale to render as + */ + public static void drawScaledTextComponent(MatrixStack matrixStack, FontRenderer font, ITextComponent textComponent, float x, float y, boolean dropShadow, float scale) { + RenderSystem.pushMatrix(); + RenderSystem.translatef(x, y, 0); + RenderSystem.scalef(scale, scale, 1F); + + if (dropShadow) { + IReorderingProcessor ireorderingprocessor = textComponent.func_241878_f(); + font.func_238407_a_(matrixStack, ireorderingprocessor, 0, 0, 0); + } else { + font.func_243246_a(matrixStack, textComponent, 0, 0, 0); + } + + RenderSystem.popMatrix(); + } + + /** + * Draws a gradient box with the from the given points + * Only used in debug + * + * @param left the left position + * @param top the top position + * @param right the right position + * @param bottom the bottom position + * @param startColor the start color to use + * @param endColor the end color to use + */ + private static void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) { + float f = (float) (startColor >> 24 & 255) / 255.0F; + float f1 = (float) (startColor >> 16 & 255) / 255.0F; + float f2 = (float) (startColor >> 8 & 255) / 255.0F; + float f3 = (float) (startColor & 255) / 255.0F; + float f4 = (float) (endColor >> 24 & 255) / 255.0F; + float f5 = (float) (endColor >> 16 & 255) / 255.0F; + float f6 = (float) (endColor >> 8 & 255) / 255.0F; + float f7 = (float) (endColor & 255) / 255.0F; + RenderSystem.disableTexture(); + RenderSystem.disableAlphaTest(); + RenderSystem.blendFuncSeparate(770, 771, 1, 0); + RenderSystem.shadeModel(7425); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder vertexBuffer = tessellator.getBuffer(); + vertexBuffer.begin(7, DefaultVertexFormats.POSITION_COLOR); + vertexBuffer.pos((double) right, (double) top, 0D).color(f1, f2, f3, f).endVertex(); + vertexBuffer.pos((double) left, (double) top, 0D).color(f1, f2, f3, f).endVertex(); + vertexBuffer.pos((double) left, (double) bottom, 0D).color(f5, f6, f7, f4).endVertex(); + vertexBuffer.pos((double) right, (double) bottom, 0D).color(f5, f6, f7, f4).endVertex(); + tessellator.draw(); + RenderSystem.shadeModel(7424); + RenderSystem.enableAlphaTest(); + RenderSystem.enableTexture(); + } +} diff --git a/src/main/java/slimeknights/mantle/client/screen/book/TextDataRenderer.java b/src/main/java/slimeknights/mantle/client/screen/book/TextDataRenderer.java index 29d03bf..b0cdd67 100644 --- a/src/main/java/slimeknights/mantle/client/screen/book/TextDataRenderer.java +++ b/src/main/java/slimeknights/mantle/client/screen/book/TextDataRenderer.java @@ -42,7 +42,6 @@ public class TextDataRenderer { return action; } - @Nullable public static String drawText(MatrixStack matrixStack, int x, int y, int boxWidth, int boxHeight, TextData[] data, int mouseX, int mouseY, FontRenderer fr, List tooltip) { String action = ""; @@ -74,7 +73,7 @@ public class TextDataRenderer { String modifiers = ""; - if(item.useOldColor) { + if (item.useOldColor) { modifiers += TextFormatting.getValueByName(item.color); } @@ -122,8 +121,7 @@ public class TextDataRenderer { if (atX == x) { box1W = x + boxWidth; - } - else { + } else { box1W = atX; } } @@ -161,8 +159,7 @@ public class TextDataRenderer { if (atY >= y + boxHeight) { if (item.dropshadow) { fr.drawStringWithShadow(matrixStack, "...", atX, atY, 0); - } - else { + } else { fr.drawString(matrixStack, "...", atX, atY, 0); } break; @@ -206,7 +203,7 @@ public class TextDataRenderer { if (s.charAt(i) == '\n' || (curHeight == (int) (fr.FONT_HEIGHT * scale) && curWidth > firstWidth) || (curHeight != (int) (fr.FONT_HEIGHT * scale) && curWidth > width)) { int oldI = i; - if(s.charAt(i) != '\n') { + if (s.charAt(i) != '\n') { while (i >= 0 && s.charAt(i) != ' ') { i--; } @@ -244,8 +241,7 @@ public class TextDataRenderer { RenderSystem.scalef(scale, scale, 1F); if (dropShadow) { font.drawStringWithShadow(matrixStack, text, 0, 0, color); - } - else { + } else { font.drawString(matrixStack, text, 0, 0, color); } RenderSystem.popMatrix(); diff --git a/src/main/java/slimeknights/mantle/client/screen/book/element/SelectionElement.java b/src/main/java/slimeknights/mantle/client/screen/book/element/SelectionElement.java index dcdb03e..7cd049e 100644 --- a/src/main/java/slimeknights/mantle/client/screen/book/element/SelectionElement.java +++ b/src/main/java/slimeknights/mantle/client/screen/book/element/SelectionElement.java @@ -47,8 +47,7 @@ public class SelectionElement extends SizedBookElement { } if (unlocked) { RenderSystem.color4f(1F, 1F, 1F, hover ? 1F : 0.5F); - } - else { + } else { float r = ((this.parent.book.appearance.lockedSectionColor >> 16) & 0xff) / 255.F; float g = ((this.parent.book.appearance.lockedSectionColor >> 8) & 0xff) / 255.F; float b = (this.parent.book.appearance.lockedSectionColor & 0xff) / 255.F; @@ -60,17 +59,17 @@ public class SelectionElement extends SizedBookElement { if (this.section.parent.appearance.drawSectionListText) { String title = this.section.getTitle().replace("\\n", "\n"); String[] splitTitle = TextDataRenderer.cropStringBySize(title, "", WIDTH + 2, - fontRenderer.FONT_HEIGHT * 2 + 1, fontRenderer, 1F); + fontRenderer.FONT_HEIGHT * 2 + 1, fontRenderer, 1F); - for(int i = 0; i < splitTitle.length; i++) { + for (int i = 0; i < splitTitle.length; i++) { int textW = fontRenderer.getStringWidth(splitTitle[i]); int textX = this.x + WIDTH / 2 - textW / 2; int textY = this.y + HEIGHT - fontRenderer.FONT_HEIGHT / 2 + fontRenderer.FONT_HEIGHT * i; fontRenderer.drawString(matrixStack, splitTitle[i], - textX, - textY, - hover ? 0xFF000000 : 0x7F000000); + textX, + textY, + hover ? 0xFF000000 : 0x7F000000); } } } diff --git a/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java b/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java index fc8ce4a..35358e0 100644 --- a/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java +++ b/src/main/java/slimeknights/mantle/client/screen/book/element/StructureElement.java @@ -154,8 +154,7 @@ public class StructureElement extends SizedBookElement { this.rotY += changeX; this.rotX += changeY; - } - else { + } else { this.lastClick = null; } } @@ -167,8 +166,7 @@ public class StructureElement extends SizedBookElement { this.fullStructureSteps = 0; } } - } - else { + } else { this.structureData.reset(); this.structureData.setShowLayer(9); } @@ -207,8 +205,7 @@ public class StructureElement extends SizedBookElement { if (Minecraft.isAmbientOcclusionEnabled()) { RenderSystem.shadeModel(GL11.GL_SMOOTH); - } - else { + } else { RenderSystem.shadeModel(GL11.GL_FLAT); } @@ -271,7 +268,7 @@ public class StructureElement extends SizedBookElement { //lastX = mouseX; //lastY = mouseY; - this.lastClick = new int[] { (int) mouseX, (int) mouseY }; + this.lastClick = new int[]{(int) mouseX, (int) mouseY}; } @Override diff --git a/src/main/java/slimeknights/mantle/client/screen/book/element/TextComponentElement.java b/src/main/java/slimeknights/mantle/client/screen/book/element/TextComponentElement.java new file mode 100644 index 0000000..4462278 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/screen/book/element/TextComponentElement.java @@ -0,0 +1,68 @@ +package slimeknights.mantle.client.screen.book.element; + +import com.mojang.blaze3d.matrix.MatrixStack; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import slimeknights.mantle.client.book.action.StringActionProcessor; +import slimeknights.mantle.client.book.data.element.TextComponentData; +import slimeknights.mantle.client.screen.book.TextComponentDataRenderer; +import slimeknights.mantle.client.screen.book.TextDataRenderer; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@OnlyIn(Dist.CLIENT) +public class TextComponentElement extends SizedBookElement { + + public TextComponentData[] text; + private final List tooltip = new ArrayList(); + + private boolean doAction = false; + + public TextComponentElement(int x, int y, int width, int height, String text) { + this(x, y, width, height, new StringTextComponent(text)); + } + + public TextComponentElement(int x, int y, int width, int height, ITextComponent text) { + this(x, y, width, height, new TextComponentData(text)); + } + + public TextComponentElement(int x, int y, int width, int height, Collection text) { + this(x, y, width, height, text.toArray(new TextComponentData[0])); + } + + public TextComponentElement(int x, int y, int width, int height, TextComponentData... text) { + super(x, y, width, height); + + this.text = text; + } + + @Override + public void draw(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) { + String action = TextComponentDataRenderer.drawText(matrixStack, this.x, this.y, this.width, this.height, this.text, mouseX, mouseY, fontRenderer, this.tooltip); + + if (this.doAction) { + this.doAction = false; + StringActionProcessor.process(action, this.parent); + } + } + + @Override + public void drawOverlay(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks, FontRenderer fontRenderer) { + if (this.tooltip.size() > 0) { + TextDataRenderer.drawTooltip(matrixStack, this.tooltip, mouseX, mouseY, fontRenderer); + this.tooltip.clear(); + } + } + + @Override + public void mouseClicked(double mouseX, double mouseY, int mouseButton) { + if (mouseButton == 0) { + this.doAction = true; + } + } +} diff --git a/src/main/java/slimeknights/mantle/network/packet/UpdateSavedPagePacket.java b/src/main/java/slimeknights/mantle/network/packet/UpdateSavedPagePacket.java index 682e320..b9241dc 100644 --- a/src/main/java/slimeknights/mantle/network/packet/UpdateSavedPagePacket.java +++ b/src/main/java/slimeknights/mantle/network/packet/UpdateSavedPagePacket.java @@ -30,7 +30,7 @@ public class UpdateSavedPagePacket implements IThreadsafePacket { if (player != null && this.pageName != null) { ItemStack is = player.getHeldItem(Hand.MAIN_HAND); if (!is.isEmpty()) { - BookHelper.writeSavedPage(is, this.pageName); + BookHelper.writeSavedPageToBook(is, this.pageName); } } }