-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow customizing Tinker's Construct manuals in dreamcraft (#794)
* Apply spotless * Added unit testing * Include mod dependencies in tests * Copied TiCo manuals from assets/tinker/manuals/en_US in TinkersConstruct No changes made to the content yet to help content review * Added BookDataStoreProxy * Added BookDataReader * Added BookLoader interface as a facade for the com.dreammaster.mantle package * Load Tinker's Construct books from dreamcraft * Reduced visibility of MantleBookLoader members
- Loading branch information
Showing
19 changed files
with
2,659 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
test { | ||
useJUnitPlatform() | ||
testLogging { | ||
events "passed", "skipped", "failed" | ||
} | ||
} | ||
configurations { | ||
testImplementation.extendsFrom compileOnly | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/main/java/com/dreammaster/coremod/transformers/ItemFocusWardingTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.dreammaster.mantle; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.xml.sax.SAXException; | ||
|
||
/** | ||
* This class doesn't support loading different XML documents based on Minecraft's currently configured language. Books | ||
* are instead intended to be translated through lang files. | ||
*/ | ||
class BookDataReader { | ||
|
||
private static final DocumentBuilderFactory DB_FACTORY = DocumentBuilderFactory.newInstance(); | ||
|
||
@Nonnull | ||
Document readBook(String xmlDocumentLocation) { | ||
try (InputStream stream = loadBook(xmlDocumentLocation)) { | ||
return readBookDocument(stream); | ||
} catch (ParserConfigurationException | SAXException | IOException e) { | ||
throw new IllegalStateException( | ||
"Failed to load book data from " + xmlDocumentLocation + ":\n" + e.getMessage(), | ||
e); | ||
} | ||
} | ||
|
||
@Nonnull | ||
private InputStream loadBook(String path) throws IOException { | ||
InputStream inputStream = getClass().getResourceAsStream(path); | ||
if (Objects.isNull(inputStream)) { | ||
throw new IOException("File " + path + " does not exist."); | ||
} | ||
return inputStream; | ||
} | ||
|
||
@Nonnull | ||
private Document readBookDocument(InputStream stream) | ||
throws ParserConfigurationException, SAXException, IOException { | ||
Document doc = DB_FACTORY.newDocumentBuilder().parse(stream); | ||
doc.getDocumentElement().normalize(); | ||
return doc; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/dreammaster/mantle/BookDataStoreProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.dreammaster.mantle; | ||
|
||
import java.util.Objects; | ||
|
||
import com.dreammaster.main.MainRegistry; | ||
|
||
import eu.usrv.yamcore.auxiliary.LogHelper; | ||
import mantle.books.BookData; | ||
import mantle.books.BookDataStore; | ||
|
||
/** | ||
* This class helps us avoid issues with books already loaded by another mod. | ||
*/ | ||
class BookDataStoreProxy { | ||
|
||
private static final BookDataStoreProxy INSTANCE = new BookDataStoreProxy(MainRegistry.Logger); | ||
|
||
static BookDataStoreProxy getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private final LogHelper logger; | ||
|
||
BookDataStoreProxy(LogHelper logger) { | ||
Objects.requireNonNull(logger); | ||
this.logger = logger; | ||
|
||
} | ||
|
||
void addBook(BookData bookData) { | ||
Objects.requireNonNull(bookData); | ||
try { | ||
BookDataStore.addBook(bookData); | ||
} catch (IllegalArgumentException e) { | ||
logger.error("Cannot override book " + bookData.unlocalizedName + " which is already defined elsewhere."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.dreammaster.mantle; | ||
|
||
public interface BookLoader { | ||
|
||
static BookLoader of(String unlocalizedName, String modId, String xmlDocumentPath) { | ||
return MantleBookLoader.readBook(unlocalizedName, modId, xmlDocumentPath); | ||
} | ||
|
||
BookLoader setTooltip(String tooltip); | ||
|
||
BookLoader setItemImage(String itemImage); | ||
|
||
BookLoader makeTranslatable(); | ||
|
||
void addToBookDataStore(); | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/dreammaster/mantle/MantleBookLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.dreammaster.mantle; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraft.util.StatCollector; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Strings; | ||
|
||
import gregtech.GT_Mod; | ||
import gregtech.api.interfaces.internal.IGT_Mod; | ||
import mantle.books.BookData; | ||
|
||
final class MantleBookLoader implements BookLoader { | ||
|
||
private static final BookDataReader BOOK_DATA_READER = new BookDataReader(); | ||
private static final BookDataStoreProxy BOOK_DATA_STORE_PROXY = BookDataStoreProxy.getInstance(); | ||
private final BookDataStoreProxy bookDataStoreProxy; | ||
private final BookData data; | ||
private final IGT_Mod sideChecker; | ||
private final BookDataReader bookDataReader; | ||
|
||
static BookLoader readBook(String unlocalizedName, String modId, String xmlDocumentPath) { | ||
return new MantleBookLoader(BOOK_DATA_STORE_PROXY, BOOK_DATA_READER, GT_Mod.gregtechproxy) | ||
.setRequiredData(unlocalizedName, modId, xmlDocumentPath); | ||
} | ||
|
||
@VisibleForTesting | ||
MantleBookLoader(BookDataStoreProxy bookDataStoreProxy, BookDataReader bookDataReader, IGT_Mod sideChecker) { | ||
Stream.of(bookDataStoreProxy, bookDataReader, sideChecker).forEach(Objects::requireNonNull); | ||
this.bookDataStoreProxy = bookDataStoreProxy; | ||
this.bookDataReader = bookDataReader; | ||
this.sideChecker = sideChecker; | ||
this.data = new BookData(); | ||
} | ||
|
||
@VisibleForTesting | ||
BookLoader setRequiredData(String unlocalizedName, String modId, String xmlDocumentPath) { | ||
Objects.requireNonNull(unlocalizedName); | ||
Objects.requireNonNull(modId); | ||
Objects.requireNonNull(xmlDocumentPath); | ||
this.data.unlocalizedName = unlocalizedName; | ||
this.data.modID = modId; | ||
if (sideChecker.isClientSide()) { | ||
data.doc = bookDataReader.readBook(xmlDocumentPath); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public BookLoader setTooltip(String tooltip) { | ||
Objects.requireNonNull(tooltip); | ||
data.toolTip = "§o" + StatCollector.translateToLocal(tooltip); | ||
return this; | ||
} | ||
|
||
@Override | ||
public BookLoader setItemImage(String itemImage) { | ||
Objects.requireNonNull(itemImage); | ||
data.itemImage = new ResourceLocation(data.modID, itemImage); | ||
return this; | ||
} | ||
|
||
@Override | ||
public BookLoader makeTranslatable() { | ||
data.isTranslatable = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void addToBookDataStore() { | ||
if (Strings.isNullOrEmpty(data.unlocalizedName)) { | ||
throw new IllegalStateException("You must call setRequiredData before addToBookDataStore."); | ||
} | ||
bookDataStoreProxy.addBook(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.