From 47ff5d9ad1a6d8e0ed998ba507d6abf61b46ccc2 Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Fri, 20 Apr 2018 15:01:31 +0200 Subject: [PATCH] Fix #2094, Fix #2096 Wrong entry order and Clean Up resource loading --- src/main/java/forestry/book/BookCategory.java | 2 +- src/main/java/forestry/book/BookLoader.java | 55 ++++++++----------- .../book/data/content/StructureContent.java | 46 ++++------------ .../book/gui/GuiForestryBookEntries.java | 14 ++++- .../forestry/core/utils/ResourceUtil.java | 4 -- 5 files changed, 45 insertions(+), 76 deletions(-) diff --git a/src/main/java/forestry/book/BookCategory.java b/src/main/java/forestry/book/BookCategory.java index 35338b88e..2cc8b4b5a 100644 --- a/src/main/java/forestry/book/BookCategory.java +++ b/src/main/java/forestry/book/BookCategory.java @@ -18,7 +18,7 @@ import forestry.core.utils.Translator; @SideOnly(Side.CLIENT) public class BookCategory implements IBookCategory { private final String name; - private final transient Map entries = new LinkedHashMap<>(); + private final Map entries = new LinkedHashMap<>(); private ItemStack stack = ItemStack.EMPTY; public BookCategory(String name) { diff --git a/src/main/java/forestry/book/BookLoader.java b/src/main/java/forestry/book/BookLoader.java index c99ca692d..7de6ad026 100644 --- a/src/main/java/forestry/book/BookLoader.java +++ b/src/main/java/forestry/book/BookLoader.java @@ -1,6 +1,5 @@ package forestry.book; -import com.google.common.collect.MapMaker; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; @@ -14,10 +13,9 @@ import java.io.IOException; import java.lang.reflect.Type; import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.concurrent.ForkJoinPool; import org.apache.commons.io.IOUtils; @@ -67,6 +65,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { public static final BookLoader INSTANCE = new BookLoader(); private static final String BOOK_LOCATION = "forestry:manual/"; private static final String BOOK_LOCATION_LANG = BOOK_LOCATION + "%s/%s"; + private static final String DEFAULT_LANG = "en_US"; private final Map> contentByType = new HashMap<>(); private final Map factoryByName = new HashMap<>(); @Nullable @@ -108,7 +107,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { return book; } book = new ForesterBook(); - BookCategory[] categories = fromJson(new ResourceLocation(BOOK_LOCATION + "categories.json"), BookCategory[].class, new BookCategory[0]); + BookCategory[] categories = fromJson(BOOK_LOCATION + "categories.json", BookCategory[].class, new BookCategory[0]); if (categories != null) { book.addCategories(categories); for (BookCategory category : categories) { @@ -124,31 +123,27 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { } @Nullable - public static ResourceLocation getResourceLocation(String path) { + public static IResource getResource(String path){ + IResource resource; if (!path.contains(":")) { Language currentLanguage = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage(); String lang = currentLanguage.getLanguageCode(); - String defaultLang = "en_US"; ResourceLocation location = new ResourceLocation(String.format(BOOK_LOCATION_LANG, lang, path)); - if (ResourceUtil.resourceExists(location)) { - return location; + resource = ResourceUtil.getResource(location); + if (resource != null) { + return resource; } - location = new ResourceLocation(String.format(BOOK_LOCATION_LANG, defaultLang, path)); - if (ResourceUtil.resourceExists(location)) { - return location; + location = new ResourceLocation(String.format(BOOK_LOCATION_LANG, DEFAULT_LANG, path)); + resource = ResourceUtil.getResource(location); + if (resource != null) { + return resource; } location = new ResourceLocation(BOOK_LOCATION + path); - if (ResourceUtil.resourceExists(location)) { - return location; - } - return null; + return ResourceUtil.getResource(location); } ResourceLocation location = new ResourceLocation(path); - if (ResourceUtil.resourceExists(location)) { - return location; - } - return null; + return ResourceUtil.getResource(location); } @Nullable @@ -158,7 +153,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { private void loadCategory(BookCategory category) { ResourceLocation entriesLocation = new ResourceLocation(BOOK_LOCATION + "entries/" + category.getName() + ".json"); - Set entryNames = new HashSet<>(); + List entryNames = new LinkedList<>(); for (IResource entryResource : ResourceUtil.getResources(entriesLocation)) { try (BufferedReader reader = ResourceUtil.createReader(entryResource)) { Entries entries = GSON.fromJson(reader, Entries.class); @@ -169,9 +164,9 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { IOUtils.closeQuietly(entryResource); } } - Map entries = new MapMaker().concurrencyLevel(ForkJoinPool.commonPool().getPoolSize() + 1).initialCapacity(entryNames.size()).makeMap(); - entryNames.parallelStream().forEach(entry -> loadEntries(entries, entry)); - entryNames.parallelStream().forEach(entry -> createEntry(entries, category, entry)); + Map entries = new HashMap<>(entryNames.size()); + entryNames.forEach(entry -> loadEntries(entries, entry)); + entryNames.forEach(entry -> createEntry(entries, category, entry)); } private void createEntry(Map entries, BookCategory category, String entry){ @@ -196,8 +191,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { } private void loadEntries(Map entries, String entry) { - ResourceLocation location = getResourceLocation(entry + ".json"); - EntryData data = fromJson(location, EntryData.class, null); + EntryData data = fromJson(entry + ".json", EntryData.class, null); if (data != null) { entries.put(entry, data); for (String subEntry : data.subEntries) { @@ -207,11 +201,8 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { } @Nullable - private T fromJson(@Nullable ResourceLocation location, Class classOfT, @Nullable T fallback) { - if (location == null) { - return fallback; - } - IResource resource = ResourceUtil.getResource(location); + private T fromJson( String location, Class classOfT, @Nullable T fallback) { + IResource resource = getResource(location); if (resource == null) { return fallback; } @@ -231,7 +222,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader { } private static class Entries { - private final Set names = new HashSet<>(); + private final List names = new LinkedList<>(); } private static class EntriesDeserializer implements JsonDeserializer { diff --git a/src/main/java/forestry/book/data/content/StructureContent.java b/src/main/java/forestry/book/data/content/StructureContent.java index bdc240f11..c43363a5b 100644 --- a/src/main/java/forestry/book/data/content/StructureContent.java +++ b/src/main/java/forestry/book/data/content/StructureContent.java @@ -21,7 +21,6 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import net.minecraft.client.resources.IResource; -import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -35,7 +34,6 @@ import forestry.book.data.structure.BlockData; import forestry.book.data.structure.StructureData; import forestry.book.gui.elements.MultiblockElement; import forestry.core.utils.Log; -import forestry.core.utils.ResourceUtil; /** * A book content that displays a multiblock structure. @@ -58,20 +56,16 @@ public class StructureContent extends BookContent { return; } - ResourceLocation location = BookLoader.getResourceLocation(structureFile); - - if (location != null && ResourceUtil.resourceExists(location)) { - IResource resource = ResourceUtil.getResource(location); - if (resource == null) { - return; - } - try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) { - structureData = BookLoader.GSON.fromJson(reader, StructureData.class); - } catch (IOException e) { - Log.error("Failed to load structure file {}.{}", location, e); - } finally { - IOUtils.closeQuietly(resource); - } + IResource resource = BookLoader.getResource(structureFile); + if (resource == null) { + return; + } + try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) { + structureData = BookLoader.GSON.fromJson(reader, StructureData.class); + } catch (IOException e) { + Log.error("Failed to load structure file {}.{}", structureFile, e); + } finally { + IOUtils.closeQuietly(resource); } } @@ -89,28 +83,8 @@ public class StructureContent extends BookContent { BlockData[] structure = structureData.structure; if (size.length == 3 && structure.length > 0) { - boolean showButtons = size[1] > 1; - if (showButtons) { - //structureSizeX -= GuiArrow.ArrowType.REFRESH.w; - } MultiblockElement elementStructure = new MultiblockElement(offset, 0, structureSizeX, structureSizeY, size, structure); page.add(elementStructure); - - /*if(showButtons) { - int col = book.appearance.structureButtonColor; - int colHover = book.appearance.structureButtonColorHovered; - int colToggled = book.appearance.structureButtonColorToggled; - - int midY = y + structureSizeY / 2 - (GuiArrow.ArrowType.UP.h + GuiArrow.ArrowType.DOWN.h) / 2; - - int dx = (GuiArrow.ArrowType.REFRESH.w - GuiArrow.ArrowType.UP.w) / 2; - - //list.add(new ElementArrow(ElementStructure.BUTTON_ID_LAYER_UP, elementStructure, structureSizeX + offset + dx, midY, GuiArrow.ArrowType.UP, col, colHover)); - //midY += GuiArrow.ArrowType.UP.h + 2; - //list.add(new ElementArrow(ElementStructure.BUTTON_ID_LAYER_DOWN, elementStructure, structureSizeX + offset + dx, midY, GuiArrow.ArrowType.DOWN, col, colHover)); - - list.add(new ElementAnimationToggle(ElementStructure.BUTTON_ID_ANIMATE, elementStructure, GuiBook.PAGE_WIDTH - GuiArrow.ArrowType.REFRESH.w, 0, GuiArrow.ArrowType.REFRESH, col, colHover, colToggled)); - }*/ } return true; } diff --git a/src/main/java/forestry/book/gui/GuiForestryBookEntries.java b/src/main/java/forestry/book/gui/GuiForestryBookEntries.java index 9fe1cf210..73b2692b4 100644 --- a/src/main/java/forestry/book/gui/GuiForestryBookEntries.java +++ b/src/main/java/forestry/book/gui/GuiForestryBookEntries.java @@ -41,9 +41,17 @@ public class GuiForestryBookEntries extends GuiForesterBook { if(indexStart >= entries.size()){ return; } - for(int i = 0;i + indexStart < entries.size() && i + indexStart < indexStart + 12;i++){ - IBookEntry entry = entries.get(indexStart + i); - addButton(new GuiButtonEntry(i, guiLeft + xStart, guiTop + yStart + i * (fontRenderer.FONT_HEIGHT + 2), entry)); + final int maxIndex; + if(entries.size() > indexStart + 12){ + maxIndex = indexStart + 12; + }else{ + maxIndex = entries.size(); + } + int yOffset = 0; + for(int i = indexStart;i < maxIndex;i++){ + IBookEntry entry = entries.get(i); + addButton(new GuiButtonEntry(buttonList.size(), guiLeft + xStart, guiTop + yStart + yOffset, entry)); + yOffset += fontRenderer.FONT_HEIGHT + 2; } } diff --git a/src/main/java/forestry/core/utils/ResourceUtil.java b/src/main/java/forestry/core/utils/ResourceUtil.java index 26117bef5..c2c504967 100644 --- a/src/main/java/forestry/core/utils/ResourceUtil.java +++ b/src/main/java/forestry/core/utils/ResourceUtil.java @@ -39,8 +39,4 @@ public class ResourceUtil { return Collections.emptyList(); } } - - public static boolean resourceExists(ResourceLocation location) { - return getResource(location) != null; - } }