Skip to content

Commit

Permalink
https://github.com/ToroCraft/ToroQuest/issues/102
Browse files Browse the repository at this point in the history
basic support to load book items from a text file
  • Loading branch information
frodare committed Jan 29, 2017
1 parent 3b801c0 commit f60566d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
14 changes: 13 additions & 1 deletion java/net/torocraft/toroquest/ToroQuestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import net.torocraft.toroquest.generation.MonolithGenerator;
import net.torocraft.toroquest.generation.ThroneRoomGenerator;
import net.torocraft.toroquest.gui.VillageLordGuiHandler;
import net.torocraft.toroquest.util.BookCreator;
import net.torocraft.toroquest.util.BookCreator.BookTypes;

public class ToroQuestCommand extends CommandBase {

Expand Down Expand Up @@ -72,10 +74,19 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
guiCommand(player, args);
} else if ("quest".equals(command)) {
questCommand(player, args);
} else if ("book".equals(command)) {
bookCommand(player, args);
} else {
throw new WrongUsageException("commands.tq.usage", new Object[0]);
}
}

private void bookCommand(EntityPlayer player, String[] args) throws CommandException {
List<ItemStack> items = new ArrayList<ItemStack>();
items.add(BookCreator.createBook(BookTypes.CIV_LORE, "test"));
dropItems(player, items);
}


private void listCommand(EntityPlayer player, String[] args) throws CommandException {
List<Province> provinces = CivilizationsWorldSaveData.get(player.world).getProvinces();
Expand Down Expand Up @@ -273,7 +284,7 @@ private List<ItemStack> pullHotbarItems(EntityPlayer player) {
InventoryPlayer inv = player.inventory;

for (int i = 0; i < inv.getSizeInventory(); i++) {
if (inv.getStackInSlot(i) != null && inv.isHotbar(i)) {
if (inv.getStackInSlot(i) != null && InventoryPlayer.isHotbar(i)) {
ItemStack stack = inv.getStackInSlot(i);
inv.setInventorySlotContents(i, ItemStack.EMPTY);
items.add(stack);
Expand Down Expand Up @@ -306,6 +317,7 @@ public List<String> getTabCompletions(MinecraftServer server, ICommandSender sen
tabOptions.add("gui");
tabOptions.add("quest");
tabOptions.add("list");
tabOptions.add("book");
} else {
if (command.startsWith("r")) {
tabOptions.add("rep");
Expand Down
117 changes: 117 additions & 0 deletions java/net/torocraft/toroquest/util/BookCreator.java
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)));
}

}
12 changes: 12 additions & 0 deletions resources/assets/toroquest/books/civ_lore/test.txt
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.
12 changes: 12 additions & 0 deletions resources/assets/toroquest/books/fiction/test.txt
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.
12 changes: 12 additions & 0 deletions resources/assets/toroquest/books/game_hints/test.txt
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.
12 changes: 12 additions & 0 deletions resources/assets/toroquest/books/location_hints/test.txt
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.

0 comments on commit f60566d

Please sign in to comment.