Fix book exceptions introduced in f1037ce and refined book nullability

This commit is contained in:
Arthur (fuj1n) Uzulin committed 2020-07-25 18:27:21 +10:00
1 parent 115d097603
commit 8eb2932f86
16 files changed
+98 -93

No files matched your search

@@ -2,15 +2,14 @@ package slimeknights.mantle.client.book;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import java.util.Arrays;
import java.util.Objects;
public class BookHelper {
public static String getSavedPage(ItemStack item) {
if (!item.isEmpty() && item.hasTag()) {
CompoundNBT mantleBook = item.getTag().getCompound("mantle").getCompound("book");
CompoundNBT mantleBook = Objects.requireNonNull(item.getTag()).getCompound("mantle").getCompound("book");
if (mantleBook.contains("page", 8)) {
return mantleBook.getString("page");
@@ -5,11 +5,12 @@ import com.google.gson.GsonBuilder;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.resources.IResourceManager;
import net.minecraft.resources.IResourceManagerReloadListener;
import net.minecraft.util.Hand;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.resource.IResourceType;
import net.minecraftforge.resource.ISelectiveResourceReloadListener;
import slimeknights.mantle.client.book.action.StringActionProcessor;
import slimeknights.mantle.client.book.action.protocol.ProtocolGoToPage;
import slimeknights.mantle.client.book.data.BookData;
@@ -33,9 +34,10 @@ import slimeknights.mantle.network.packet.UpdateSavedPagePacket;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.function.Predicate;
@OnlyIn(Dist.CLIENT)
public class BookLoader implements IResourceManagerReloadListener {
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())
@@ -88,6 +90,7 @@ public class BookLoader implements IResourceManagerReloadListener {
* @param name The name of the page type
* @return The class of the page type, ContentError.class if page type not registered
*/
@Nullable
public static Class<? extends PageContent> getPageType(String name) {
return typeToContentMap.get(name);
}
@@ -145,7 +148,7 @@ public class BookLoader implements IResourceManagerReloadListener {
* Reloads all the books, called when the resource manager reloads, such as when the resource pack or the language is changed
*/
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
public void onResourceManagerReload(IResourceManager resourceManager, Predicate<IResourceType> resourcePredicate) {
books.forEach((s, bookData) -> bookData.reset());
}
}
@@ -23,6 +23,7 @@ public abstract class BookTransformer {
return ContentTableTransformer.INSTANCE;
}
@SuppressWarnings("unused") // API
public static BookTransformer contentTableTransformerForSection(String sectionName) {
return new ContentTableTransformer(sectionName);
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.resources.IResource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.api.distmarker.Dist;
@@ -38,7 +39,7 @@ public class BookData implements IDataItem {
protected final transient ArrayList<BookTransformer> transformers = new ArrayList<>();
private ArrayList<BookRepository> repositories;
private final ArrayList<BookRepository> repositories;
public BookData(BookRepository... repositories) {
this.repositories = new ArrayList<>(Arrays.asList(repositories));
@@ -111,20 +112,23 @@ public class BookData implements IDataItem {
if (repo.resourceExists(languageLocation)) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(repo.getResource(languageLocation)
.getInputStream(), StandardCharsets.UTF_8));
IResource resource = repo.getResource(languageLocation);
if (resource != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),
StandardCharsets.UTF_8));
String next = br.readLine();
String next = br.readLine();
while (next != null) {
if (!next.startsWith("//") && next.contains("=")) {
String key = next.substring(0, next.indexOf('='));
String value = next.substring(next.indexOf('=') + 1);
while (next != null) {
if (!next.startsWith("//") && next.contains("=")) {
String key = next.substring(0, next.indexOf('='));
String value = next.substring(next.indexOf('=') + 1);
this.strings.put(key, value);
this.strings.put(key, value);
}
next = br.readLine();
}
next = br.readLine();
}
}
catch (Exception ignored) {
@@ -215,10 +219,6 @@ public class BookData implements IDataItem {
return null;
}
public int getFirstPageNumber(SectionData section) {
return this.getFirstPageNumber(section, null);
}
public int getFirstPageNumber(SectionData section, @Nullable BookScreen.AdvancementCache advancementCache) {
int pages = 0;
for (SectionData sect : this.sections) {
@@ -238,11 +238,6 @@ public class BookData implements IDataItem {
return -1;
}
@Nullable
public PageData findPage(int number) {
return this.findPage(number, null);
}
@Nullable
public PageData findPage(int number, @Nullable BookScreen.AdvancementCache advancementCache) {
if (number < 0) {
@@ -268,14 +263,10 @@ public class BookData implements IDataItem {
return null;
}
@Nullable
public PageData findPage(String location) {
return this.findPage(location, null);
}
@SuppressWarnings("unused") // API
@Nullable
public PageData findPage(String location, @Nullable BookScreen.AdvancementCache advancementCache) {
return this.findPage(this.findPageNumber(location, advancementCache));
return this.findPage(this.findPageNumber(location, advancementCache), advancementCache);
}
public int findPageNumber(String location) {
@@ -319,10 +310,6 @@ public class BookData implements IDataItem {
return -1;
}
public int getPageCount() {
return this.getPageCount(null);
}
public int getPageCount(@Nullable BookScreen.AdvancementCache advancementCache) {
int pages = 0;
for (SectionData section : this.sections) {
@@ -333,10 +320,6 @@ public class BookData implements IDataItem {
return pages;
}
public int getFullPageCount() {
return this.getFullPageCount(null);
}
public int getFullPageCount(@Nullable BookScreen.AdvancementCache advancementCache) {
return (int) Math.ceil((this.getPageCount(advancementCache) - 1) / 2F) + 1;
}
@@ -351,7 +334,7 @@ public class BookData implements IDataItem {
return "";
}
public List<SectionData> getVisibleSections(BookScreen.AdvancementCache advancementCache) {
public List<SectionData> getVisibleSections(@Nullable BookScreen.AdvancementCache advancementCache) {
List<SectionData> visible = new ArrayList<>();
for (SectionData section : this.sections) {
@@ -377,6 +360,7 @@ public class BookData implements IDataItem {
}
}
@SuppressWarnings("unused") // API
public void addRepository(@Nullable BookRepository repository) {
if (repository != null && !this.repositories.contains(repository)) {
this.repositories.add(repository);
@@ -2,10 +2,11 @@ package slimeknights.mantle.client.book.data;
public class BookmarkData {
public String text = "";
public String text;
public int color = 0x464646; // Index bookmark : 0x1ED400
public String page = "";
public String page;
@SuppressWarnings("unused") // used implicitly by GSON
public BookmarkData() {
this("");
}
@@ -4,7 +4,7 @@ import java.util.ArrayList;
public class LinkedSectionData extends SectionData {
private transient ArrayList<SectionData> sections = new ArrayList<>();
private final transient ArrayList<SectionData> sections = new ArrayList<>();
public LinkedSectionData() {
super(true);
@@ -9,6 +9,7 @@ import slimeknights.mantle.client.book.data.content.PageContent;
import slimeknights.mantle.client.book.data.element.IDataElement;
import slimeknights.mantle.client.book.repository.BookRepository;
import javax.annotation.Nullable;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
@@ -25,6 +26,7 @@ public class PageData implements IDataItem {
public transient BookRepository source;
public transient PageContent content;
@SuppressWarnings("unused") // used implicitly by GSON
public PageData() {
this(false);
}
@@ -35,12 +37,12 @@ public class PageData implements IDataItem {
}
}
@SuppressWarnings("unused") // utility
public String translate(String string) {
return this.parent.translate(string);
}
@Override
@SuppressWarnings("unchecked")
public void load() {
if (this.name == null) {
this.name = "page" + this.parent.unnamedPageCounter++;
@@ -55,23 +57,31 @@ public class PageData implements IDataItem {
if (!data.isEmpty()) {
Class<? extends PageContent> ctype = BookLoader.getPageType(this.type);
try {
this.content = BookLoader.GSON.fromJson(data, ctype);
}
catch (Exception e) {
this.content = new ContentError(ctype == null ? "Failed to create a page of type \"" + this.type + "\", perhaps the type is not registered?" : "Failed to create a page of type \"" + this.type + "\", perhaps the page file \"" + this.data + "\" is missing or invalid?", e);
if (ctype != null) {
try {
this.content = BookLoader.GSON.fromJson(data, ctype);
} catch (Exception e) {
this.content = new ContentError("Failed to create a page of type \"" + this.type + "\", perhaps the page file \"" + this.data + "\" is missing or invalid?", e);
}
} else {
this.content = new ContentError("Failed to create a page of type \"" + this.type + "\" as it is not registered.");
}
}
}
}
if (this.content == null) {
try {
this.content = BookLoader.getPageType(this.type).newInstance();
}
catch (InstantiationException | IllegalAccessException | NullPointerException e) {
this.content = new ContentError("Failed to create a page of type \"" + this.type + "\", perhaps the type is not registered?");
}
Class<? extends PageContent> 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.");
}
}
try {
@@ -108,7 +118,7 @@ public class PageData implements IDataItem {
}
}
private void processObject(Object o, final int depth) {
private void processObject(@Nullable Object o, final int depth) {
if(depth > 4 || o == null)
return;
@@ -1,7 +0,0 @@
package slimeknights.mantle.client.book.data;
public class PageDataModule extends PageData {
/** Module required to load this section, see {@link slimeknights.mantle.pulsar.control.PulseManager#isPulseLoaded(String)} for more details */
public String module = "";
}
@@ -14,7 +14,7 @@ import java.util.ArrayList;
@OnlyIn(Dist.CLIENT)
public class ContentError extends PageContent {
private String errorStage;
private final String errorStage;
private Exception exception;
public ContentError(String errorStage) {
@@ -3,7 +3,6 @@ package slimeknights.mantle.client.book.data.content;
import com.google.common.collect.ImmutableList;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.AbstractCookingRecipe;
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.element;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.repository.BookRepository;
@@ -11,6 +12,6 @@ public class DataLocation implements IDataElement {
@Override
public void load(BookRepository source) {
this.location = "$BLOCK_ATLAS".equals(this.file) ? AtlasTexture.LOCATION_BLOCKS_TEXTURE : source.getResourceLocation(this.file, true);
this.location = "$BLOCK_ATLAS".equals(this.file) ? PlayerContainer.LOCATION_BLOCKS_TEXTURE : source.getResourceLocation(this.file, true);
}
}
@@ -52,15 +52,15 @@ public class ItemStackData implements IDataElement {
Item item;
boolean isMissingItem = false;
try {
item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(this.id));//ItemArgument.getItem(null, id).getItem();
item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(this.id));
}
catch (Exception e) {
item = Item.getItemFromBlock(Blocks.BARRIER);
item = Items.BARRIER;
isMissingItem = true;
}
if (item == Items.AIR) {
item = Item.getItemFromBlock(Blocks.BARRIER);
item = Items.BARRIER;
isMissingItem = true;
}
@@ -7,29 +7,40 @@ import java.util.List;
import slimeknights.mantle.client.book.data.SectionData;
import javax.annotation.Nullable;
public abstract class BookRepository {
@SuppressWarnings("StaticInitializerReferencesSubClass") // will only occur in very specific threaded environment
public static final BookRepository DUMMY = new DummyRepository();
public abstract List<SectionData> getSections();
public ResourceLocation getResourceLocation(String path) {
@Nullable
public ResourceLocation getResourceLocation(@Nullable String path) {
return this.getResourceLocation(path, false);
}
public abstract ResourceLocation getResourceLocation(String path, boolean safe);
@Nullable
public abstract ResourceLocation getResourceLocation(@Nullable String path, boolean safe);
public abstract IResource getResource(ResourceLocation loc);
@Nullable
public abstract IResource getResource(@Nullable ResourceLocation loc);
@SuppressWarnings("unused") // API
public boolean resourceExists(@Nullable String location) {
if(location == null) {
return false;
}
public boolean resourceExists(String location) {
return this.resourceExists(new ResourceLocation(location));
}
public abstract boolean resourceExists(ResourceLocation location);
public abstract boolean resourceExists(@Nullable ResourceLocation location);
public String resourceToString(IResource resource) {
public String resourceToString(@Nullable IResource resource) {
return this.resourceToString(resource, true);
}
public abstract String resourceToString(IResource resource, boolean skipCommments);
public abstract String resourceToString(@Nullable IResource resource, boolean skipCommments);
}
@@ -4,6 +4,7 @@ import net.minecraft.resources.IResource;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.data.SectionData;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
@@ -15,22 +16,22 @@ public class DummyRepository extends BookRepository {
}
@Override
public ResourceLocation getResourceLocation(String path, boolean safe) {
throw new UnsupportedOperationException();
public ResourceLocation getResourceLocation(@Nullable String path, boolean safe) {
return null;
}
@Override
public IResource getResource(ResourceLocation loc) {
throw new UnsupportedOperationException();
public IResource getResource(@Nullable ResourceLocation loc) {
return null;
}
@Override
public boolean resourceExists(ResourceLocation location) {
public boolean resourceExists(@Nullable ResourceLocation location) {
return false;
}
@Override
public String resourceToString(IResource resource, boolean skipCommments) {
public String resourceToString(@Nullable IResource resource, boolean skipCommments) {
return "";
}
}
@@ -3,12 +3,13 @@ package slimeknights.mantle.client.book.repository;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.IResource;
import net.minecraft.util.ResourceLocation;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.SectionData;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -28,13 +29,14 @@ public class FileRepository extends BookRepository {
}
@Override
public ResourceLocation getResourceLocation(String path, boolean safe) {
public ResourceLocation getResourceLocation(@Nullable String path, boolean safe) {
if (path == null) {
return safe ? new ResourceLocation("") : null;
}
if (!path.contains(":")) {
String langPath = null;
//noinspection ConstantConditions - this was proven to be null once
if (Minecraft.getInstance().getLanguageManager() != null && Minecraft.getInstance().getLanguageManager().getCurrentLanguage() != null) {
langPath = Minecraft.getInstance().getLanguageManager().getCurrentLanguage().getCode();
}
@@ -43,6 +45,7 @@ public class FileRepository extends BookRepository {
ResourceLocation res;
//noinspection ConstantConditions - see above
if (langPath != null) {
res = new ResourceLocation(this.location + "/" + langPath + "/" + path);
if (this.resourceExists(res)) {
@@ -57,19 +60,19 @@ public class FileRepository extends BookRepository {
if (this.resourceExists(res)) {
return res;
}
return safe ? new ResourceLocation("") : null;
}
else {
ResourceLocation res = new ResourceLocation(path);
if (this.resourceExists(res)) {
return res;
}
return safe ? new ResourceLocation("") : null;
}
return safe ? new ResourceLocation("") : null;
}
@Override
public IResource getResource(ResourceLocation loc) {
public IResource getResource(@Nullable ResourceLocation loc) {
if (loc == null) {
return null;
}
@@ -82,7 +85,7 @@ public class FileRepository extends BookRepository {
}
@Override
public boolean resourceExists(ResourceLocation location) {
public boolean resourceExists(@Nullable ResourceLocation location) {
if (location == null) {
return false;
}
@@ -96,18 +99,18 @@ public class FileRepository extends BookRepository {
}
@Override
public String resourceToString(IResource resource, boolean skipCommments) {
public String resourceToString(@Nullable IResource resource, boolean skipCommments) {
if (resource == null) {
return "";
}
try {
Iterator iterator = IOUtils.readLines(resource.getInputStream(), Charsets.UTF_8).iterator();
Iterator<String> iterator = IOUtils.readLines(resource.getInputStream(), StandardCharsets.UTF_8).iterator();
StringBuilder builder = new StringBuilder();
boolean isLongComment = false;
while (iterator.hasNext()) {
String s = ((String) iterator.next()).trim() + "\n";
String s = iterator.next().trim() + "\n";
// Comment skipper
if (skipCommments) {
@@ -2,7 +2,6 @@ package slimeknights.mantle.network;
import net.minecraftforge.fml.network.NetworkDirection;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.network.packet.FluidUpdatePacket;
import slimeknights.mantle.network.packet.UpdateSavedPagePacket;
public class MantleNetwork {