-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/ToroCraft/ToroQuest/issues/102
basic support to load book items from a text file
- Loading branch information
Showing
6 changed files
with
178 additions
and
1 deletion.
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
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,117 @@ | ||
package net.torocraft.toroquest.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import net.minecraft.init.Items; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NBTTagList; | ||
import net.minecraft.nbt.NBTTagString; | ||
import net.minecraft.util.text.ITextComponent; | ||
import net.minecraft.util.text.TextComponentString; | ||
|
||
public class BookCreator { | ||
|
||
public static enum BookTypes { | ||
CIV_LORE, FICTION, GAME_HINTS, LOCATION_HINTS | ||
}; | ||
|
||
public static ItemStack createBook(BookTypes type, String name) { | ||
ItemStack book = null; | ||
try { | ||
book = loadBook(type, name); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
if (book == null) { | ||
book = new ItemStack(Items.BOOK); | ||
} | ||
|
||
return book; | ||
} | ||
|
||
@Nullable | ||
private static ItemStack loadBook(BookTypes type, String name) throws IOException { | ||
String path = "/assets/toroquest/books/" + type.toString().toLowerCase() + "/" + name + ".txt"; | ||
InputStream is = BookCreator.class.getResourceAsStream(path); | ||
|
||
if (is == null) { | ||
System.out.println("Book file not found [" + path + "]"); | ||
return null; | ||
} | ||
|
||
ItemStack book = new ItemStack(Items.WRITTEN_BOOK); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); | ||
String line = null; | ||
int lineNumber = 1; | ||
NBTTagList pages = new NBTTagList(); | ||
String[] words; | ||
StringBuilder page = new StringBuilder(256); | ||
try { | ||
|
||
while ((line = reader.readLine()) != null) { | ||
|
||
line = line.trim(); | ||
|
||
if (lineNumber < 4 && line.length() == 0) { | ||
continue; | ||
} | ||
|
||
if (lineNumber == 1) { | ||
book.setTagInfo("title", new NBTTagString(line)); | ||
} else if (lineNumber == 2) { | ||
book.setTagInfo("author", new NBTTagString(line)); | ||
} else { | ||
|
||
if (line.length() == 0) { | ||
page = writePage(pages, page); | ||
} | ||
|
||
words = line.split("\\s+"); | ||
|
||
for (String word : words) { | ||
if (page.length() + word.length() > 255) { | ||
page = writePage(pages, page); | ||
|
||
} else if (page.length() > 0) { | ||
page.append(" "); | ||
} | ||
|
||
page.append(word); | ||
} | ||
|
||
} | ||
|
||
lineNumber++; | ||
} | ||
|
||
page = writePage(pages, page); | ||
|
||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
|
||
book.setTagInfo("pages", pages); | ||
|
||
return book; | ||
} | ||
|
||
private static StringBuilder writePage(NBTTagList pages, StringBuilder page) { | ||
pages.appendTag(createPage(page.toString())); | ||
page = new StringBuilder(256); | ||
if (pages.tagCount() >= 50) { | ||
throw new IndexOutOfBoundsException("out of book pages"); | ||
} | ||
return page; | ||
} | ||
|
||
private static NBTTagString createPage(String page) { | ||
return new NBTTagString(ITextComponent.Serializer.componentToJson(new TextComponentString(page))); | ||
} | ||
|
||
} |
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,12 @@ | ||
Some Observations about Motion. | ||
Sir Isaac Newton | ||
|
||
Only those bodyes which are absolutely hard are exactly reflected acording to those rules. Now the bodyes here amongst us (being an aggregate of smaller other bodyes) haue a relenting softnesse & springynesse, which makes their contact be for some time & in more points then one. And the touching surfaces during the time of contact doe slide one upon another more or lesse or not at all acording to their roughnesse. And few or none of these bodyes haue a springynesse soe strong, as to force them one from another with the same vigor that they came together. Besides that their motions are continually impeded & slackened by the mediums in which they move. Now hee that would prescribe rules for the reflections of these compound bodies, must consider in how many points the two bodies touch at their meeting, the position & pression of every point, which their planes of contact &c: & how all these are varyed every moment during the time of contact by the more or lesse relenting softnesse or springynesse of those bodies & their various slidings. And also what effect the air or other mediums compressed betwixt the bodies may haue. | ||
|
||
2 These are some cases of Reflections of bodies absolutely hard to which these rules extend not: As when two bodies meet with their angular point, or in more points <83v> then one at once; Or with their superficies. But these cases are rare. | ||
|
||
3 In all reflections of any bodies what ever this rule is true that the common center of two or more bodies changeth not its state of motion or rest by the reflection of those bodies one amongst another. | ||
|
||
4 Motion may be lost by reflection. As Figure if two equall Globes A & α with equall motions from D & δ done in the perpendicular lines DA & δα, hit one another when the center of the body α is in the line DA. Then the body A shall loose all its motion & yet the motion of α is not doubled. For completing the square Bβ, the body α shall move in the Diagonall αC, & arrive at C but at the same time it would haue arrived at β without reflection. see the third section. | ||
|
||
5 Motion may be gained by reflection. For if the body α return with the same motion back again from C to α. The two bodyes A & α after reflection shall regain the same equall motions in the lines AD & αδ (though backwards) which they had at first. |
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,12 @@ | ||
Some Observations about Motion. | ||
Sir Isaac Newton | ||
|
||
Only those bodyes which are absolutely hard are exactly reflected acording to those rules. Now the bodyes here amongst us (being an aggregate of smaller other bodyes) haue a relenting softnesse & springynesse, which makes their contact be for some time & in more points then one. And the touching surfaces during the time of contact doe slide one upon another more or lesse or not at all acording to their roughnesse. And few or none of these bodyes haue a springynesse soe strong, as to force them one from another with the same vigor that they came together. Besides that their motions are continually impeded & slackened by the mediums in which they move. Now hee that would prescribe rules for the reflections of these compound bodies, must consider in how many points the two bodies touch at their meeting, the position & pression of every point, which their planes of contact &c: & how all these are varyed every moment during the time of contact by the more or lesse relenting softnesse or springynesse of those bodies & their various slidings. And also what effect the air or other mediums compressed betwixt the bodies may haue. | ||
|
||
2 These are some cases of Reflections of bodies absolutely hard to which these rules extend not: As when two bodies meet with their angular point, or in more points <83v> then one at once; Or with their superficies. But these cases are rare. | ||
|
||
3 In all reflections of any bodies what ever this rule is true that the common center of two or more bodies changeth not its state of motion or rest by the reflection of those bodies one amongst another. | ||
|
||
4 Motion may be lost by reflection. As Figure if two equall Globes A & α with equall motions from D & δ done in the perpendicular lines DA & δα, hit one another when the center of the body α is in the line DA. Then the body A shall loose all its motion & yet the motion of α is not doubled. For completing the square Bβ, the body α shall move in the Diagonall αC, & arrive at C but at the same time it would haue arrived at β without reflection. see the third section. | ||
|
||
5 Motion may be gained by reflection. For if the body α return with the same motion back again from C to α. The two bodyes A & α after reflection shall regain the same equall motions in the lines AD & αδ (though backwards) which they had at first. |
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,12 @@ | ||
Some Observations about Motion. | ||
Sir Isaac Newton | ||
|
||
Only those bodyes which are absolutely hard are exactly reflected acording to those rules. Now the bodyes here amongst us (being an aggregate of smaller other bodyes) haue a relenting softnesse & springynesse, which makes their contact be for some time & in more points then one. And the touching surfaces during the time of contact doe slide one upon another more or lesse or not at all acording to their roughnesse. And few or none of these bodyes haue a springynesse soe strong, as to force them one from another with the same vigor that they came together. Besides that their motions are continually impeded & slackened by the mediums in which they move. Now hee that would prescribe rules for the reflections of these compound bodies, must consider in how many points the two bodies touch at their meeting, the position & pression of every point, which their planes of contact &c: & how all these are varyed every moment during the time of contact by the more or lesse relenting softnesse or springynesse of those bodies & their various slidings. And also what effect the air or other mediums compressed betwixt the bodies may haue. | ||
|
||
2 These are some cases of Reflections of bodies absolutely hard to which these rules extend not: As when two bodies meet with their angular point, or in more points <83v> then one at once; Or with their superficies. But these cases are rare. | ||
|
||
3 In all reflections of any bodies what ever this rule is true that the common center of two or more bodies changeth not its state of motion or rest by the reflection of those bodies one amongst another. | ||
|
||
4 Motion may be lost by reflection. As Figure if two equall Globes A & α with equall motions from D & δ done in the perpendicular lines DA & δα, hit one another when the center of the body α is in the line DA. Then the body A shall loose all its motion & yet the motion of α is not doubled. For completing the square Bβ, the body α shall move in the Diagonall αC, & arrive at C but at the same time it would haue arrived at β without reflection. see the third section. | ||
|
||
5 Motion may be gained by reflection. For if the body α return with the same motion back again from C to α. The two bodyes A & α after reflection shall regain the same equall motions in the lines AD & αδ (though backwards) which they had at first. |
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,12 @@ | ||
Some Observations about Motion. | ||
Sir Isaac Newton | ||
|
||
Only those bodyes which are absolutely hard are exactly reflected acording to those rules. Now the bodyes here amongst us (being an aggregate of smaller other bodyes) haue a relenting softnesse & springynesse, which makes their contact be for some time & in more points then one. And the touching surfaces during the time of contact doe slide one upon another more or lesse or not at all acording to their roughnesse. And few or none of these bodyes haue a springynesse soe strong, as to force them one from another with the same vigor that they came together. Besides that their motions are continually impeded & slackened by the mediums in which they move. Now hee that would prescribe rules for the reflections of these compound bodies, must consider in how many points the two bodies touch at their meeting, the position & pression of every point, which their planes of contact &c: & how all these are varyed every moment during the time of contact by the more or lesse relenting softnesse or springynesse of those bodies & their various slidings. And also what effect the air or other mediums compressed betwixt the bodies may haue. | ||
|
||
2 These are some cases of Reflections of bodies absolutely hard to which these rules extend not: As when two bodies meet with their angular point, or in more points <83v> then one at once; Or with their superficies. But these cases are rare. | ||
|
||
3 In all reflections of any bodies what ever this rule is true that the common center of two or more bodies changeth not its state of motion or rest by the reflection of those bodies one amongst another. | ||
|
||
4 Motion may be lost by reflection. As Figure if two equall Globes A & α with equall motions from D & δ done in the perpendicular lines DA & δα, hit one another when the center of the body α is in the line DA. Then the body A shall loose all its motion & yet the motion of α is not doubled. For completing the square Bβ, the body α shall move in the Diagonall αC, & arrive at C but at the same time it would haue arrived at β without reflection. see the third section. | ||
|
||
5 Motion may be gained by reflection. For if the body α return with the same motion back again from C to α. The two bodyes A & α after reflection shall regain the same equall motions in the lines AD & αδ (though backwards) which they had at first. |