Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaherz committed May 17, 2018
1 parent 1e835d3 commit 35edb6a
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 24 deletions.
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ repositories {
maven {
url 'http://dogforce-games.com/maven'
}

maven {
url 'https://maven.mcmoddev.com'
}
}

apply plugin: 'net.minecraftforge.gradle.forge'
Expand All @@ -25,10 +29,10 @@ group = "gigaherz.guidebook" // http://maven.apache.org/guides/mini/guide-naming
archivesBaseName = "Guidebook-1.12.2"

minecraft {
version = "1.12.2-14.23.2.2616"
version = "1.12.2-14.23.2.2619"
runDir = "run"

mappings = "snapshot_20180214"
mappings = "snapshot_20180223"

replace "@VERSION@", project.version
replaceIn "GuidebookMod.java"
Expand All @@ -42,6 +46,11 @@ configurations {
dependencies {
deobfCompile "gigaherz.commons:gigaherz.commons-1.12.1:0.6.4"
embed "gigaherz.commons:gigaherz.commons-1.12.1:0.6.4"

deobfCompile "net.darkhax.gamestages:GameStages-1.12.2:1.0.80"
deobfCompile "net.darkhax.bookshelf:Bookshelf-1.12.2:2.3.526"

//compile fileTree(dir: 'libs', include: '*.jar')
}

jar {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/gigaherz/guidebook/GuidebookMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.google.common.collect.Lists;
import gigaherz.guidebook.common.IModProxy;
import gigaherz.guidebook.guidebook.ItemGuidebook;
import gigaherz.guidebook.guidebook.conditions.AdvancementCondition;
import gigaherz.guidebook.guidebook.conditions.BasicConditions;
import gigaherz.guidebook.guidebook.conditions.CompositeCondition;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -71,6 +74,10 @@ public void preInit(FMLPreInitializationEvent event)

giveOnFirstJoin = Lists.newArrayList(give);

BasicConditions.register();
CompositeCondition.register();
//AdvancementCondition.register();

proxy.preInit();
}

Expand Down
104 changes: 87 additions & 17 deletions src/main/java/gigaherz/guidebook/guidebook/BookDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.google.common.primitives.Floats;
import com.google.common.primitives.Ints;
import gigaherz.guidebook.GuidebookMod;
import gigaherz.guidebook.guidebook.conditions.ConditionManager;
import gigaherz.guidebook.guidebook.conditions.IDisplayCondition;
import gigaherz.guidebook.guidebook.drawing.Point;
import gigaherz.guidebook.guidebook.drawing.Rect;
import gigaherz.guidebook.guidebook.drawing.VisualPage;
Expand Down Expand Up @@ -46,13 +48,14 @@ public class BookDocument
private String bookName;
private ResourceLocation bookCover;

final List<ChapterData> chapters = Lists.newArrayList();
final List<SectionData> chapters = Lists.newArrayList();
private Table<Item, Integer, PageRef> stackLinks = HashBasedTable.create();

final Map<String, Integer> chaptersByName = Maps.newHashMap();
final Map<String, PageRef> pagesByName = Maps.newHashMap();

private final Map<String, TemplateDefinition> templates = Maps.newHashMap();
private final Map<String, IDisplayCondition> conditions = Maps.newHashMap();

private int totalPairs = 0;
private IBookGraphics rendering;
Expand All @@ -79,7 +82,7 @@ public ResourceLocation getBookCover()
return bookCover;
}

public ChapterData getChapter(int i)
public SectionData getChapter(int i)
{
return chapters.get(i);
}
Expand Down Expand Up @@ -122,7 +125,7 @@ public void findTextures(Set<ResourceLocation> textures)
textures.add(bookCover);

// TODO: Add <image> texture locations when implemented
for (ChapterData chapter : chapters)
for (SectionData chapter : chapters)
{
for (PageData page : chapter.pages)
{
Expand All @@ -136,7 +139,7 @@ public void findTextures(Set<ResourceLocation> textures)

public void initializeWithLoadError(String error)
{
ChapterData ch = new ChapterData(0);
SectionData ch = new SectionData(0);
chapters.add(ch);

PageData pg = new PageData(0);
Expand Down Expand Up @@ -199,35 +202,39 @@ public boolean parseBook(InputStream stream)
}
}

NodeList chaptersList = root.getChildNodes();
for (int i = 0; i < chaptersList.getLength(); i++)
NodeList firstLevel = root.getChildNodes();
for (int i = 0; i < firstLevel.getLength(); i++)
{
Node chapterItem = chaptersList.item(i);
Node firstLevelNode = firstLevel.item(i);

String nodeName = chapterItem.getNodeName();
String nodeName = firstLevelNode.getNodeName();
if (nodeName.equals("template"))
{
parseTemplateDefinition(chapterItem, templates);
parseTemplateDefinition(firstLevelNode, templates);
}
else if (nodeName.equals("include"))
{
NamedNodeMap attributes = chapterItem.getAttributes();
NamedNodeMap attributes = firstLevelNode.getAttributes();
Node n = attributes.getNamedItem("ref");
TemplateLibrary tpl = TemplateLibrary.get(n.getTextContent());
templates.putAll(tpl.templates);
}
else if (nodeName.equals("chapter"))
{
parseChapter(chapterItem);
parseChapter(firstLevelNode);
}
else if (nodeName.equals("stack-links"))
{
parseStackLinks(chapterItem);
parseStackLinks(firstLevelNode);
}
else if(nodeName.equals("conditions"))
{
parseConditions(firstLevelNode);
}
}

int prevCount = 0;
for (ChapterData chapter : chapters)
for (SectionData chapter : chapters)
{
chapter.startPair = prevCount;
prevCount += chapter.pagePairs;
Expand All @@ -241,6 +248,68 @@ else if (nodeName.equals("stack-links"))
return true;
}

private void parseConditions(Node node)
{
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node condition = children.item(i);
if (condition.getNodeType() != Node.ELEMENT_NODE)
continue;

NamedNodeMap attributes = condition.getAttributes();
if (attributes == null)
continue;

Node conditionName = attributes.getNamedItem("name");
String name = conditionName != null ? conditionName.getTextContent() : null;

if (Strings.isNullOrEmpty(name))
{
throw new BookParsingException("Condition node found without a name attribute");
}

IDisplayCondition displayCondition = parseSingleCondition(this, condition);

conditions.put(name, displayCondition);
}
}

public static List<IDisplayCondition> parseChildConditions(BookDocument context, Node node)
{
List<IDisplayCondition> conditions = Lists.newArrayList();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node condition = children.item(i);
if (condition.getNodeType() != Node.ELEMENT_NODE)
continue;

IDisplayCondition displayCondition = parseSingleCondition(context, condition);

conditions.add(displayCondition);
}
return conditions;
}

private static IDisplayCondition parseSingleCondition(BookDocument context, Node condition)
{
IDisplayCondition displayCondition;
try
{
displayCondition = ConditionManager.parseCondition(context, condition);
if (displayCondition == null)
{
throw new BookParsingException("Condition not found");
}
}
catch(Exception e)
{
throw new BookParsingException("Exception parsing condition", e);
}
return displayCondition;
}

private static void parseTemplateDefinition(Node templateItem, Map<String, TemplateDefinition> templates)
{
if (!templateItem.hasAttributes())
Expand All @@ -263,7 +332,7 @@ private static void parseTemplateDefinition(Node templateItem, Map<String, Templ

private void parseChapter(Node chapterItem)
{
ChapterData chapter = new ChapterData(chapters.size());
SectionData chapter = new SectionData(chapters.size());
chapters.add(chapter);

if (chapterItem.hasAttributes())
Expand Down Expand Up @@ -293,7 +362,7 @@ private void parseChapter(Node chapterItem)
chapter.pagePairs = (chapter.pages.size() + 1) / 2;
}

private void parsePage(ChapterData chapter, Node pageItem)
private void parsePage(SectionData chapter, Node pageItem)
{
PageData page = new PageData(chapter.pages.size());
chapter.pages.add(page);
Expand Down Expand Up @@ -582,18 +651,19 @@ public IBookGraphics getRendering()
return rendering;
}

public class ChapterData
public class SectionData
{
public final int num;
public String id;
public IDisplayCondition condition;

public final List<PageData> pages = Lists.newArrayList();
public final Map<String, Integer> pagesByName = Maps.newHashMap();

public int pagePairs;
public int startPair;

private ChapterData(int num)
private SectionData(int num)
{
this.num = num;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gigaherz.guidebook.guidebook;

public class BookParsingException extends RuntimeException
{
public BookParsingException(String message)
{
super(message);
}

public BookParsingException(String message, Throwable cause)
{
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.util.math.MathHelper;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Rectangle;

import javax.annotation.Nullable;
import java.util.Collections;
Expand Down Expand Up @@ -291,7 +290,7 @@ public boolean mouseClicked(int mouseButton)

if (mouseButton == 0)
{
BookDocument.ChapterData ch = book.getChapter(currentChapter);
BookDocument.SectionData ch = book.getChapter(currentChapter);

final VisualPage pgLeft = getVisualPage(ch, new PageRef(currentChapter, currentPair * 2), true);

Expand All @@ -310,7 +309,7 @@ public boolean mouseClicked(int mouseButton)
return false;
}

private VisualPage getVisualPage(BookDocument.ChapterData ch, PageRef pr, boolean isLeftPage)
private VisualPage getVisualPage(BookDocument.SectionData ch, PageRef pr, boolean isLeftPage)
{
VisualPage pg = visualPages.get(pr);

Expand Down Expand Up @@ -344,7 +343,7 @@ private boolean mouseClickPage(int mX, int mY, VisualPage pg)
@Override
public boolean mouseHover(int mouseX, int mouseY)
{
BookDocument.ChapterData ch = book.getChapter(currentChapter);
BookDocument.SectionData ch = book.getChapter(currentChapter);

final VisualPage pgLeft = getVisualPage(ch, new PageRef(currentChapter, currentPair * 2), true);

Expand Down Expand Up @@ -458,7 +457,7 @@ public void drawCurrentPages()

private void drawPage(int page, boolean isLeftPage)
{
BookDocument.ChapterData ch = book.getChapter(currentChapter);
BookDocument.SectionData ch = book.getChapter(currentChapter);
if (page >= ch.pages.size())
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gigaherz.guidebook.guidebook.conditions;

import com.google.common.base.Strings;
import gigaherz.guidebook.guidebook.BookParsingException;
import org.w3c.dom.Node;

public abstract class AdvancementCondition implements IDisplayCondition
{
public final String stageName;

protected AdvancementCondition(String stageName)
{
this.stageName = stageName;
}

public static void register()
{
ConditionManager.register("advancement-locked", (doc, node) -> new Locked(parseStageName(node)));
ConditionManager.register("advancement-unlocked", (doc, node) -> new Unlocked(parseStageName(node)));
}

public static class Locked extends AdvancementCondition
{

public Locked(String stageName)
{
super(stageName);
}

@Override
public boolean test(ConditionContext conditionContext)
{
return false; // !Advancement.getStageData(conditionContext.getPlayer()).hasUnlockedStage(stageName);
}
}

public static class Unlocked extends AdvancementCondition
{

public Unlocked(String stageName)
{
super(stageName);
}

@Override
public boolean test(ConditionContext conditionContext)
{
return false; // Advancement.getStageData(conditionContext.getPlayer()).hasUnlockedStage(stageName);
}
}

private static String parseStageName(Node xmlNode)
{
Node attr = xmlNode.getAttributes().getNamedItem("advancement");
if (attr == null)
throw new BookParsingException("Missing required XML attribute 'advancement'.");

String stageName = attr.getTextContent();
if (Strings.isNullOrEmpty(stageName))
throw new BookParsingException("Missing required XML attribute 'advancement'.");
return stageName;
}
}
Loading

0 comments on commit 35edb6a

Please sign in to comment.