Skip to content

Commit

Permalink
Create gaps between book cabinets by year
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels-NTG committed Jun 12, 2024
1 parent 4fad49c commit ca19bfb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
7 changes: 3 additions & 4 deletions LibraryFloor.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ def addBooks(self, books: List[str]):
for building in self.placedTiles.values():
if building.bookCapacity == 0:
continue
if len(books) == 0:
break

booksForTile = books[:building.bookCapacity]
building.addBooks(booksForTile)
del books[:building.bookCapacity]

building.addBooks(books)
43 changes: 28 additions & 15 deletions StructureBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,36 @@ def bookCapacity(self) -> int:
# The minecraft:chiseled_bookshelf block has a capacity of 6 books.
return len(self.bookShelves) * 6

def addBooks(self, booksData: List[str]):
def addBooks(self, books: List[str]):
bookShelfEntries: List[str] = []
bookShelfBlockPositions = self.bookShelves.copy()
for book in booksData:
bookShelfEntries.append(book)
if len(bookShelfEntries) == 6:
# Remove first entry from the list of bookShelvePositions
bookShelfPosition = list(bookShelfBlockPositions.keys())[0]
bookShelfRotation = bookShelfBlockPositions.pop(bookShelfPosition)

bookShelfBlock = bookTools.createBookShelfBlock(
bookShelfRotation,
)
bookShelfBlock = bookTools.fillBookShelf(bookShelfEntries, bookShelfBlock)

nbtTools.setNBTAt(self.nbt, bookShelfPosition, bookShelfBlock)
bookShelfEntries.clear()
if len(books) == 0:
return
lastBookYear = bookTools.yearFromSNBT(books[-1])
while len(bookShelfBlockPositions) > 0 and len(books) > 0:
bookYear = bookTools.yearFromSNBT(books[-1])
if bookYear != lastBookYear:
self.fillBookShelf(bookShelfBlockPositions, bookShelfEntries)
break
bookShelfEntries.append(books.pop())
lastBookYear = bookYear
if len(bookShelfEntries) == 6 or len(books) == 0:
self.fillBookShelf(bookShelfBlockPositions, bookShelfEntries)

def fillBookShelf(self, bookShelfBlockPositions: Dict[ivec3, str], bookShelfEntries: List[str]):
if len(bookShelfEntries) == 0:
return
# Remove first entry from the list of bookShelvePositions
bookShelfPosition = list(bookShelfBlockPositions.keys())[0]
bookShelfRotation = bookShelfBlockPositions.pop(bookShelfPosition)

bookShelfBlock = bookTools.createBookShelfBlock(
bookShelfRotation,
)
bookShelfBlock = bookTools.fillBookShelf(bookShelfEntries, bookShelfBlock)

nbtTools.setBlockInStructure(self.nbt, bookShelfPosition, bookShelfBlock)
bookShelfEntries.clear()

def __eq__(self, other):
return hash(self) == hash(other)
Expand Down
10 changes: 7 additions & 3 deletions bookTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@ def lastVersionYear(data: Dict) -> str:
return re.sub(r'^\w\w\w,\s\d+\s\w\w\w\s|\s.+$', '', data['versions'][-1]['created'])


def primaryAuthor(snbt: str) -> str:
def primaryAuthorFromSNBT(snbt: str) -> str:
return re.sub(r'^.+?title:\'|(?: et al\.)? \(§7.+', '', snbt)


def yearFromSNBT(snbt: str) -> str:
return re.sub(r'^.+?title:\'.+?\(§7|§r\).+', '', snbt)


def truncatedBookTitle(data: Dict) -> str:
title = data['authors_parsed'][0][0]
if len(data['authors_parsed']) > 1:
Expand Down Expand Up @@ -195,7 +199,7 @@ def fillBookShelf(bookSources: List[str], block: Block) -> Block:
if len(bookSources) > 6:
raise Exception('Too many book sources provided at once!')
shelfSNBT = '{Items: ['
for bookIndex in range(6):
for bookIndex in range(len(bookSources)):
shelfSNBT += f'{{Slot: {bookIndex}b, Count: 1b, id: "minecraft:written_book", tag: {bookSources[bookIndex]}}},'
block.states[f'slot_{bookIndex}_occupied'] = '"true"'
shelfSNBT = shelfSNBT[:-1]
Expand Down Expand Up @@ -237,6 +241,6 @@ def gatherBooksOfCategory(category: str = 'cs.') -> List[str]:
# TODO re-run book parser to trim the abstracts, notes and author lists down
if len(line) < 10000:
bookGroup.append(line)
bookGroup.sort(key=lambda x: primaryAuthor(x))
bookGroup.sort(key=lambda x: primaryAuthorFromSNBT(x), reverse=True)
groupedBooks.extend(bookGroup)
return groupedBooks
8 changes: 2 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@
volumeRotation=volumeRotation,
)

bookCapacity = libraryFloor.bookCapacity
cprint(f'Filling floor {floorNumber} with {bookCapacity} books', 'black', 'on_green')
booksForFloor = computerScienceBooks[-bookCapacity:]
libraryFloor.addBooks(booksForFloor)
cprint(f'Filling floor {floorNumber} with {libraryFloor.bookCapacity} books', 'black', 'on_green')
libraryFloor.addBooks(computerScienceBooks)
libraryFloor.placeStructure(floorVolume.offset)

del computerScienceBooks[-bookCapacity:]

volumeY -= VOLUME_Y_SIZE
volumeRotation += 1
floorNumber -= 1
20 changes: 8 additions & 12 deletions nbtTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
from gdpc.src.gdpc.lookup import INVENTORY_BLOCKS, CONTAINER_BLOCK_TO_INVENTORY_SIZE


def SnbttoNbt(snbt: str) -> nbtlib.Compound:
return nbtlib.parse_nbt(snbt)


def extractEntityBlockPos(compound: nbtlib.Compound) -> ivec3:
return ivec3(
np.floor(compound.get('Pos').get(0)),
Expand All @@ -22,30 +18,30 @@ def extractEntityId(compound: nbtlib.Compound) -> str:
return compound.get('id')


def getBlockAt(nbtFile: nbtlib.Compound, pos: ivec3) -> Block:
for nbtBlock in list(nbtFile['blocks']):
def getBlockInStructure(structureFile: nbtlib.Compound, pos: ivec3) -> Block:
for nbtBlock in list(structureFile['blocks']):
if (
nbtBlock['pos'][0] == pos.x and
nbtBlock['pos'][1] == pos.y and
nbtBlock['pos'][2] == pos.z
):
return Block.fromBlockStateTag(
getBlockMaterial(nbtFile, nbtBlock),
getBlockMaterial(structureFile, nbtBlock),
nbtBlock['nbt']
)


def setNBTAt(nbtFile: nbtlib.Compound, pos: ivec3, inputBlock: Block):
def setBlockInStructure(structureFile: nbtlib.Compound, pos: ivec3, inputBlock: Block):
try:
newNBTTag = nbtlib.parse_nbt(inputBlock.data)
except Exception as e:
print(f'Could not parse {inputBlock.data}: {e}')
return
nbtFile['palette'].append(nbtlib.parse_nbt(
structureFile['palette'].append(nbtlib.parse_nbt(
f'{{Properties: {inputBlock.stateString()}, Name: "{inputBlock.id}"}}'
))
paletteId = len(nbtFile['palette']) - 1
for nbtBlock in nbtFile['blocks']:
paletteId = len(structureFile['palette']) - 1
for nbtBlock in structureFile['blocks']:
if (
nbtBlock['pos'][0] == pos.x and
nbtBlock['pos'][1] == pos.y and
Expand All @@ -54,7 +50,7 @@ def setNBTAt(nbtFile: nbtlib.Compound, pos: ivec3, inputBlock: Block):
nbtBlock['nbt'] = newNBTTag
nbtBlock['state'] = nbtlib.Int(paletteId)
return
raise Exception(f'nbtFile has no block at position {pos}')
raise Exception(f'structureFile has no block at position {pos}')


def getBlockMaterial(nbtFile: nbtlib.Compound, block) -> nbtlib.Compound:
Expand Down
2 changes: 1 addition & 1 deletion worldTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
):
self.uuid = uuid
if snbt:
self._nbt = nbtTools.SnbttoNbt(snbt)
self._nbt = nbtlib.parse_nbt(snbt)
self._position = nbtTools.extractEntityBlockPos(self._nbt)
self._id = nbtTools.extractEntityId(self._nbt)

Expand Down

0 comments on commit ca19bfb

Please sign in to comment.