Fix #2094, Fix #2096 Wrong entry order and Clean Up resource loading

This commit is contained in:
Nedelosk committed 2018-04-20 15:01:31 +02:00
1 parent 6ff1d8b8d7
commit 47ff5d9ad1
5 files changed
+45 -76

No files matched your search

@@ -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<String, IBookEntry> entries = new LinkedHashMap<>();
private final Map<String, IBookEntry> entries = new LinkedHashMap<>();
private ItemStack stack = ItemStack.EMPTY;
public BookCategory(String name) {
+23 -32
View File
@@ -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<String, Class<? extends BookContent>> contentByType = new HashMap<>();
private final Map<String, IBookPageFactory> 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<String> entryNames = new HashSet<>();
List<String> 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<String, EntryData> 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<String, EntryData> entries = new HashMap<>(entryNames.size());
entryNames.forEach(entry -> loadEntries(entries, entry));
entryNames.forEach(entry -> createEntry(entries, category, entry));
}
private void createEntry(Map<String, EntryData> entries, BookCategory category, String entry){
@@ -196,8 +191,7 @@ public class BookLoader implements IResourceManagerReloadListener, IBookLoader {
}
private void loadEntries(Map<String, EntryData> 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> T fromJson(@Nullable ResourceLocation location, Class<T> classOfT, @Nullable T fallback) {
if (location == null) {
return fallback;
}
IResource resource = ResourceUtil.getResource(location);
private <T> T fromJson( String location, Class<T> 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<String> names = new HashSet<>();
private final List<String> names = new LinkedList<>();
}
private static class EntriesDeserializer implements JsonDeserializer<Entries> {
@@ -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;
}
@@ -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;
}
}
@@ -39,8 +39,4 @@ public class ResourceUtil {
return Collections.emptyList();
}
}
public static boolean resourceExists(ResourceLocation location) {
return getResource(location) != null;
}
}